mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 10:21:03 +01:00
Add create billing agreement from token (#204)
* Add create billing agreement from token
This commit is contained in:
parent
a3977a8e74
commit
392c0e9d69
|
@ -40,3 +40,31 @@ func (c *Client) CreateBillingAgreementToken(
|
||||||
|
|
||||||
return billingAgreementToken, nil
|
return billingAgreementToken, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateBillingAgreementFromToken - Use this call to create a billing agreement
|
||||||
|
// Endpoint: POST /v1/billing-agreements/agreements
|
||||||
|
func (c *Client) CreateBillingAgreementFromToken(
|
||||||
|
ctx context.Context,
|
||||||
|
tokenID string,
|
||||||
|
) (*BillingAgreement, error) {
|
||||||
|
type createBARequest struct {
|
||||||
|
TokenID string `json:"token_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
billingAgreement := &BillingAgreement{}
|
||||||
|
|
||||||
|
req, err := c.NewRequest(
|
||||||
|
ctx,
|
||||||
|
"POST",
|
||||||
|
fmt.Sprintf("%s%s", c.APIBase, "/v1/billing-agreements/agreements"),
|
||||||
|
createBARequest{TokenID: tokenID})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = c.SendWithAuth(req, billingAgreement); err != nil {
|
||||||
|
return billingAgreement, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return billingAgreement, nil
|
||||||
|
}
|
||||||
|
|
47
unit_test.go
47
unit_test.go
|
@ -397,6 +397,11 @@ func (ts *webprofileTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request
|
||||||
ts.create(w, r)
|
ts.create(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if r.RequestURI == "/v1/billing-agreements/agreements" {
|
||||||
|
if r.Method == "POST" {
|
||||||
|
ts.createWithoutName(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) {
|
func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -435,6 +440,34 @@ func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write(res)
|
w.Write(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ts *webprofileTestServer) createWithoutName(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var data map[string]interface{}
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(body, &data)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
var raw map[string]string
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
raw = map[string]string{
|
||||||
|
"id": "B-12345678901234567",
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
|
||||||
|
res, _ := json.Marshal(raw)
|
||||||
|
w.Write(res)
|
||||||
|
}
|
||||||
|
|
||||||
func (ts *webprofileTestServer) updatevalid(w http.ResponseWriter, r *http.Request) {
|
func (ts *webprofileTestServer) updatevalid(w http.ResponseWriter, r *http.Request) {
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
|
|
||||||
|
@ -770,3 +803,17 @@ func TestCreateBillingAgreementToken(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateBillingAgreementFromToken(t *testing.T) {
|
||||||
|
|
||||||
|
ts := httptest.NewServer(&webprofileTestServer{t: t})
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
c, _ := NewClient("foo", "bar", ts.URL)
|
||||||
|
|
||||||
|
_, err := c.CreateBillingAgreementFromToken(context.Background(),"BillingAgreementToken")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user