Add create billing agreement from token (#204)

* Add create billing agreement from token
This commit is contained in:
Cameron Jarnot 2021-07-13 13:14:56 -04:00 committed by GitHub
parent a3977a8e74
commit 392c0e9d69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

View File

@ -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
}

View File

@ -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{}
@ -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)
}
}