From e748748f5c9890bf4b7858483951ab0cc0186f16 Mon Sep 17 00:00:00 2001 From: imikod Date: Mon, 26 Mar 2018 12:13:58 +0300 Subject: [PATCH] add ListBillingPlans() --- billing.go | 58 ++++++++++++++++++++++++++++++++++--------------- billing_test.go | 2 ++ types.go | 4 ++++ 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/billing.go b/billing.go index 2ac4da1..f78dc24 100644 --- a/billing.go +++ b/billing.go @@ -9,17 +9,6 @@ import ( ) type ( - // CreateBillingResp struct - CreateBillingResp struct { - ID string `json:"id,omitempty"` - State string `json:"state,omitempty"` - PaymentDefinitions []PaymentDefinition `json:"payment_definitions,omitempty"` - MerchantPreferences MerchantPreferences `json:"merchant_preferences,omitempty"` - CreateTime time.Time `json:"create_time,omitempty"` - UpdateTime time.Time `json:"update_time,omitempty"` - Links []Link `json:"links,omitempty"` - } - // CreateAgreementResp struct CreateAgreementResp struct { ID string `json:"id,omitempty"` @@ -29,13 +18,21 @@ type ( Links []Link `json:"links,omitempty"` StartTime time.Time `json:"start_time,omitempty"` } + + // ListBillingPlansResp struct + ListBillingPlansResp struct { + TotalItems string `json:"total_items,omitempty"` + TotalPages string `json:"total_pages,omitempty"` + Plans []BillingPlan `json:"plans,omitempty"` + Links []Link `json:"links,omitempty"` + } ) -// CreateBillingPlan creates a billing plan in Paypal +// CreateBillingPlan creates a billing plan in Paypal. // Endpoint: POST /v1/payments/billing-plans -func (c *Client) CreateBillingPlan(plan BillingPlan) (*CreateBillingResp, error) { +func (c *Client) CreateBillingPlan(plan BillingPlan) (*BillingPlan, error) { req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans"), plan) - response := &CreateBillingResp{} + response := &BillingPlan{} if err != nil { return response, err } @@ -43,8 +40,8 @@ func (c *Client) CreateBillingPlan(plan BillingPlan) (*CreateBillingResp, error) return response, err } -// ActivatePlan activates a billing plan -// By default, a new plan is not activated +// ActivatePlan activates a billing plan. +// By default, a new plan is not activated. // Endpoint: PATCH /v1/payments/billing-plans/ func (c *Client) ActivatePlan(planID string) error { buf := bytes.NewBuffer([]byte("[{\"op\":\"replace\",\"path\":\"/\",\"value\":{\"state\":\"ACTIVE\"}}]")) @@ -57,7 +54,7 @@ func (c *Client) ActivatePlan(planID string) error { return c.SendWithAuth(req, nil) } -// CreateBillingAgreement creates an agreement for specified plan +// CreateBillingAgreement creates an agreement for specified plan. // Endpoint: POST /v1/payments/billing-agreements func (c *Client) CreateBillingAgreement(a BillingAgreement) (*CreateAgreementResp, error) { // PayPal needs only ID, so we will remove all fields except Plan ID @@ -74,7 +71,7 @@ func (c *Client) CreateBillingAgreement(a BillingAgreement) (*CreateAgreementRes return response, err } -// DeletePlan deletes a billing plan +// DeletePlan deletes a billing plan. // Endpoint: PATCH /v1/payments/billing-plans/ func (c *Client) DeletePlan(planID string) error { buf := bytes.NewBuffer([]byte("[{\"op\":\"replace\",\"path\":\"/\",\"value\":{\"state\":\"DELETED\"}}]")) @@ -110,3 +107,28 @@ func (c *Client) ExecuteApprovedAgreement(token string) (*ExecuteAgreementRespon return &e, err } + +// ListBillingPlans - Lists billing plans. +// Valid values for status: "CREATED", "ACTIVE", "INACTIVE". +// Endpoint: GET /v1/payments/billing-plans/ +func (c *Client) ListBillingPlans(status interface{}, page interface{}) (*ListBillingPlansResp, error) { + if status == nil { + status = "CREATED" + } + if page == nil { + page = "0" + } + req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans?total_required=yes&status="+status.(string)+"&page="+page.(string)), nil) + if err != nil { + return &ListBillingPlansResp{}, err + } + + req.SetBasicAuth(c.ClientID, c.Secret) + req.Header.Set("Authorization", "Bearer "+c.Token.Token) + + l := ListBillingPlansResp{} + + err = c.SendWithAuth(req, &l) + + return &l, err +} \ No newline at end of file diff --git a/billing_test.go b/billing_test.go index b5fcb98..2b1e4f1 100644 --- a/billing_test.go +++ b/billing_test.go @@ -94,6 +94,8 @@ func BillingExample() { } err = c.ActivatePlan(planResp.ID) fmt.Println(err) + plans, err := c.ListBillingPlans("ACTIVE", nil) + fmt.Println(err, plans) agreement := pp.BillingAgreement{ Name: "Fast Speed Agreement", Description: "Agreement for Fast Speed Plan", diff --git a/types.go b/types.go index 05fc344..b9dc8c2 100644 --- a/types.go +++ b/types.go @@ -137,6 +137,10 @@ type ( Type string `json:"type,omitempty"` PaymentDefinitions []PaymentDefinition `json:"payment_definitions,omitempty"` MerchantPreferences *MerchantPreferences `json:"merchant_preferences,omitempty"` + State string `json:"state,omitempty"` + CreateTime string `json:"create_time,omitempty"` + UpdateTime string `json:"update_time,omitempty"` + Links []Link `json:"links,omitempty"` } // Capture struct