Adding functions for requesting billing agreement token (#203)

* created billing agreements file

* CreateBillingAgreementToken created with new types

* updated formatting

* error to return nil

* removed new line

* added test
This commit is contained in:
Cameron Jarnot 2021-07-12 05:25:19 -04:00 committed by GitHub
parent 021cc68201
commit a3977a8e74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 0 deletions

42
billing_agreements.go Normal file
View File

@ -0,0 +1,42 @@
package paypal
import (
"context"
"fmt"
)
// CreateBillingAgreementToken - Use this call to create a billing agreement
// Endpoint: POST /v1/billing-agreements/agreement-tokens
func (c *Client) CreateBillingAgreementToken(
ctx context.Context,
name string,
description string,
startDate string,
payer *Payer,
plan *BillingPlan,
) (*BillingAgreementToken, error) {
type createBARequest struct {
Name string `json:"name"`
Description string `json:"description"`
StartDate string `json:"start_date"`
Payer *Payer `json:"payer"`
Plan *BillingPlan `json:"plan"`
}
billingAgreementToken := &BillingAgreementToken{}
req, err := c.NewRequest(
ctx,
"POST",
fmt.Sprintf("%s%s", c.APIBase, "/v1/billing-agreements/agreement-tokens"),
createBARequest{Name: name, Description: description, StartDate: startDate, Payer: payer, Plan: plan})
if err != nil {
return nil, err
}
if err = c.SendWithAuth(req, billingAgreementToken); err != nil {
return billingAgreementToken, err
}
return billingAgreementToken, nil
}

View File

@ -328,6 +328,36 @@ type (
OverrideMerchantPreferences *MerchantPreferences `json:"override_merchant_preferences,omitempty"`
}
// BillingAgreementToken response struct
BillingAgreementToken struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
StartDate string `json:"start_date,omitempty"`
AgreementDetails *AgreementDetails `json:"agreement_details,omitempty"`
Payer *Payer `json:"payer,omitempty"`
ShippingAddress *ShippingAddress `json:"shipping_address,omitempty"`
OverrideMerchantPreferences *MerchantPreferences `json:"override_merchant_preferences,omitempty"`
OverrideChargeModels *OverrideChargeModel `json:"override_charge_models,omitempty"`
Plan *Plan `json:"plan,omitempty"`
}
//OverrideChargeModel struct
OverrideChargeModel struct {
ChargeID string `json:"charge_id"`
Amount *Amount `json:"amount"`
}
// Plan struct
Plan struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CreateTime string `json:"create_time,omitempty"`
UpdateTime string `json:"update_time,omitempty"`
PaymentDefinitions []PaymentDefinition `json:"payment_definitions,omitempty"`
}
// BillingInfo struct
BillingInfo struct {
OutstandingBalance AmountPayout `json:"outstanding_balance,omitempty"`

View File

@ -392,6 +392,11 @@ func (ts *webprofileTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request
ts.deleteinvalid(w, r)
}
}
if r.RequestURI == "/v1/billing-agreements/agreement-tokens" {
if r.Method == "POST" {
ts.create(w, r)
}
}
}
func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) {
@ -744,3 +749,24 @@ func TestDeleteWebProfile_invalid(t *testing.T) {
}
}
func TestCreateBillingAgreementToken(t *testing.T) {
ts := httptest.NewServer(&webprofileTestServer{t: t})
defer ts.Close()
c, _ := NewClient("foo", "bar", ts.URL)
_, err := c.CreateBillingAgreementToken(
context.Background(),
"name A",
"description A",
"start date A",
&Payer{PaymentMethod: "paypal"},
&BillingPlan{ID: "id B", Name: "name B", Description: "description B", Type: "type B"})
if err != nil {
t.Fatal(err)
}
}