From 392c0e9d698e968d166cdb8cd4147d2e5c860166 Mon Sep 17 00:00:00 2001 From: Cameron Jarnot <48696097+cameronjarnot1@users.noreply.github.com> Date: Tue, 13 Jul 2021 13:14:56 -0400 Subject: [PATCH] Add create billing agreement from token (#204) * Add create billing agreement from token --- billing_agreements.go | 28 ++++++++++++++++++++++++++ unit_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/billing_agreements.go b/billing_agreements.go index 981550e2..21be8f7f 100644 --- a/billing_agreements.go +++ b/billing_agreements.go @@ -40,3 +40,31 @@ func (c *Client) CreateBillingAgreementToken( 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 +} diff --git a/unit_test.go b/unit_test.go index 81b7acab..d8e15938 100644 --- a/unit_test.go +++ b/unit_test.go @@ -397,6 +397,11 @@ func (ts *webprofileTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request 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) { @@ -435,6 +440,34 @@ func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) { 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) { var data map[string]interface{} @@ -769,4 +802,18 @@ func TestCreateBillingAgreementToken(t *testing.T) { t.Fatal(err) } +} + +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) + } } \ No newline at end of file