BillingAgreement definition

This commit is contained in:
envy124 2017-07-23 08:47:17 +03:00
parent 77c97c8824
commit 1f84980013
2 changed files with 47 additions and 13 deletions

View File

@ -18,24 +18,27 @@ type (
UpdateTime time.Time `json:"update_time,omitempty"` UpdateTime time.Time `json:"update_time,omitempty"`
Links []Link `json:"links,omitempty"` Links []Link `json:"links,omitempty"`
} }
// CreateAgreementResp struct
CreateAgreementResp struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Plan BillingPlan `json:"plan,omitempty"`
Links []Link `json:"links,omitempty"`
StartTime time.Time `json:"start_time,omitempty"`
}
) )
// CreateBillingPlan creates a billing plan in Paypal // CreateBillingPlan creates a billing plan in Paypal
// Endpoint: POST /v1/payments/billing-plans // Endpoint: POST /v1/payments/billing-plans
func (c *Client) CreateBillingPlan(plan BillingPlan) (*CreateBillingResp, error) { func (c *Client) CreateBillingPlan(plan BillingPlan) (*CreateBillingResp, error) {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans"), plan) req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans"), plan)
if err != nil {
return &CreateBillingResp{}, err
}
response := &CreateBillingResp{} response := &CreateBillingResp{}
err = c.SendWithAuth(req, response)
if err != nil { if err != nil {
return response, err return response, err
} }
err = c.SendWithAuth(req, response)
return response, nil return response, err
} }
// Activates a billing plan // Activates a billing plan
@ -51,3 +54,15 @@ func (c *Client) ActivatePlan(planID string) error {
req.Header.Set("Authorization", "Bearer "+c.Token.Token) req.Header.Set("Authorization", "Bearer "+c.Token.Token)
return c.SendWithAuth(req, nil) return c.SendWithAuth(req, nil)
} }
// Creates an agreement for specified plan
// Endpoint: POST /v1/payments/billing-agreements
func (c *Client) CreateBillingAgreement(a BillingAgreement) (*CreateAgreementResp, error) {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-agreements"), a)
response := &CreateAgreementResp{}
if err != nil {
return response, err
}
err = c.SendWithAuth(req, response)
return response, err
}

View File

@ -44,6 +44,9 @@ const (
) )
type ( type (
// JsonTime overrides MarshalJson method to format in ISO8601
JsonTime time.Time
// Address struct // Address struct
Address struct { Address struct {
Line1 string `json:"line1"` Line1 string `json:"line1"`
@ -93,13 +96,24 @@ type (
SenderBatchHeader *SenderBatchHeader `json:"sender_batch_header,omitempty"` SenderBatchHeader *SenderBatchHeader `json:"sender_batch_header,omitempty"`
} }
// BillingAgreement struct
BillingAgreement struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
StartDate JsonTime `json:"start_date,omitempty"`
Plan BillingPlan `json:"plan,omitempty"`
Payer Payer `json:"payer,omitempty"`
ShippingAddress *ShippingAddress `json:"shipping_address,omitempty"`
}
// BillingPlan struct // BillingPlan struct
BillingPlan struct { BillingPlan struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
PaymentDefinitions []PaymentDefinition `json:"payment_definitions,omitempty"` PaymentDefinitions []PaymentDefinition `json:"payment_definitions,omitempty"`
MerchantPreferences MerchantPreferences `json:"merchant_preferences,omitempty"` MerchantPreferences *MerchantPreferences `json:"merchant_preferences,omitempty"`
} }
// Capture struct // Capture struct
@ -486,3 +500,8 @@ type (
func (r *ErrorResponse) Error() string { func (r *ErrorResponse) Error() string {
return fmt.Sprintf("%v %v: %d %s", r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, r.Message) return fmt.Sprintf("%v %v: %d %s", r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, r.Message)
} }
func (t JsonTime) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf(`"%s"`, time.Time(t).UTC().Format(time.RFC3339))
return []byte(stamp), nil
}