Skip to content

Commit

Permalink
Add create billing agreement from token (#204)
Browse files Browse the repository at this point in the history
* Add create billing agreement from token
  • Loading branch information
cameronjarnot1 authored Jul 13, 2021
1 parent a3977a8 commit 392c0e9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
28 changes: 28 additions & 0 deletions billing_agreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
47 changes: 47 additions & 0 deletions unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{}

Expand Down Expand Up @@ -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)
}
}

0 comments on commit 392c0e9

Please sign in to comment.