github.com/plutov/paypal/v4@v4.7.1/subscription_plan.go (about) 1 package paypal 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "time" 8 ) 9 10 type ( 11 // SubscriptionDetailResp struct 12 SubscriptionPlan struct { 13 ID string `json:"id,omitempty"` 14 ProductId string `json:"product_id"` 15 Name string `json:"name"` 16 Status SubscriptionPlanStatus `json:"status"` 17 Description string `json:"description,omitempty"` 18 BillingCycles []BillingCycle `json:"billing_cycles"` 19 PaymentPreferences *PaymentPreferences `json:"payment_preferences"` 20 Taxes *Taxes `json:"taxes"` 21 QuantitySupported bool `json:"quantity_supported"` //Indicates whether you can subscribe to this plan by providing a quantity for the goods or service. 22 } 23 24 // Doc https://developer.paypal.com/docs/api/subscriptions/v1/#definition-billing_cycle 25 BillingCycle struct { 26 PricingScheme PricingScheme `json:"pricing_scheme"` // The active pricing scheme for this billing cycle. A free trial billing cycle does not require a pricing scheme. 27 Frequency Frequency `json:"frequency"` // The frequency details for this billing cycle. 28 TenureType TenureType `json:"tenure_type"` // The tenure type of the billing cycle. In case of a plan having trial cycle, only 2 trial cycles are allowed per plan. The possible values are: 29 Sequence int `json:"sequence"` // The order in which this cycle is to run among other billing cycles. For example, a trial billing cycle has a sequence of 1 while a regular billing cycle has a sequence of 2, so that trial cycle runs before the regular cycle. 30 TotalCycles int `json:"total_cycles"` // The number of times this billing cycle gets executed. Trial billing cycles can only be executed a finite number of times (value between 1 and 999 for total_cycles). Regular billing cycles can be executed infinite times (value of 0 for total_cycles) or a finite number of times (value between 1 and 999 for total_cycles). 31 } 32 33 // Doc: https://developer.paypal.com/docs/api/subscriptions/v1/#definition-payment_preferences 34 PaymentPreferences struct { 35 AutoBillOutstanding bool `json:"auto_bill_outstanding"` 36 SetupFee *Money `json:"setup_fee"` 37 SetupFeeFailureAction SetupFeeFailureAction `json:"setup_fee_failure_action"` 38 PaymentFailureThreshold int `json:"payment_failure_threshold"` 39 } 40 41 PricingScheme struct { 42 Version int `json:"version"` 43 FixedPrice Money `json:"fixed_price"` 44 CreateTime time.Time `json:"create_time"` 45 UpdateTime time.Time `json:"update_time"` 46 } 47 48 PricingSchemeUpdateRequest struct { 49 Schemes []PricingSchemeUpdate `json:"pricing_schemes"` 50 } 51 52 PricingSchemeUpdate struct { 53 BillingCycleSequence int `json:"billing_cycle_sequence"` 54 PricingScheme PricingScheme `json:"pricing_scheme"` 55 } 56 57 //doc: https://developer.paypal.com/docs/api/subscriptions/v1/#definition-frequency 58 Frequency struct { 59 IntervalUnit IntervalUnit `json:"interval_unit"` 60 IntervalCount int `json:"interval_count"` //different per unit. check documentation 61 } 62 63 Taxes struct { 64 Percentage string `json:"percentage"` 65 Inclusive bool `json:"inclusive"` 66 } 67 68 CreateSubscriptionPlanResponse struct { 69 SubscriptionPlan 70 SharedResponse 71 } 72 73 SubscriptionPlanListParameters struct { 74 ProductId string `json:"product_id"` 75 PlanIds string `json:"plan_ids"` // Filters the response by list of plan IDs. Filter supports upto 10 plan IDs. 76 ListParams 77 } 78 79 ListSubscriptionPlansResponse struct { 80 Plans []SubscriptionPlan `json:"plans"` 81 SharedListResponse 82 } 83 ) 84 85 func (self *SubscriptionPlan) GetUpdatePatch() []Patch { 86 result := []Patch{ 87 { 88 Operation: "replace", 89 Path: "/description", 90 Value: self.Description, 91 }, 92 } 93 94 if self.Taxes != nil { 95 result = append(result, Patch{ 96 Operation: "replace", 97 Path: "/taxes/percentage", 98 Value: self.Taxes.Percentage, 99 }) 100 } 101 102 if self.PaymentPreferences != nil { 103 if self.PaymentPreferences.SetupFee != nil { 104 result = append(result, Patch{ 105 Operation: "replace", 106 Path: "/payment_preferences/setup_fee", 107 Value: self.PaymentPreferences.SetupFee, 108 }, 109 ) 110 } 111 112 result = append(result, []Patch{{ 113 Operation: "replace", 114 Path: "/payment_preferences/auto_bill_outstanding", 115 Value: self.PaymentPreferences.AutoBillOutstanding, 116 }, 117 { 118 Operation: "replace", 119 Path: "/payment_preferences/payment_failure_threshold", 120 Value: self.PaymentPreferences.PaymentFailureThreshold, 121 }, 122 { 123 Operation: "replace", 124 Path: "/payment_preferences/setup_fee_failure_action", 125 Value: self.PaymentPreferences.SetupFeeFailureAction, 126 }}...) 127 } 128 129 return result 130 } 131 132 // CreateSubscriptionPlan creates a subscriptionPlan 133 // Doc: https://developer.paypal.com/docs/api/subscriptions/v1/#plans_create 134 // Endpoint: POST /v1/billing/plans 135 func (c *Client) CreateSubscriptionPlan(ctx context.Context, newPlan SubscriptionPlan) (*CreateSubscriptionPlanResponse, error) { 136 req, err := c.NewRequest(ctx, http.MethodPost, fmt.Sprintf("%s%s", c.APIBase, "/v1/billing/plans"), newPlan) 137 response := &CreateSubscriptionPlanResponse{} 138 if err != nil { 139 return response, err 140 } 141 err = c.SendWithAuth(req, response) 142 return response, err 143 } 144 145 // UpdateSubscriptionPlan. updates a plan 146 // Doc: https://developer.paypal.com/docs/api/subscriptions/v1/#plans_patch 147 // Endpoint: PATCH /v1/billing/plans/:plan_id 148 func (c *Client) UpdateSubscriptionPlan(ctx context.Context, updatedPlan SubscriptionPlan) error { 149 req, err := c.NewRequest(ctx, http.MethodPatch, fmt.Sprintf("%s%s%s", c.APIBase, "/v1/billing/plans/", updatedPlan.ID), updatedPlan.GetUpdatePatch()) 150 if err != nil { 151 return err 152 } 153 err = c.SendWithAuth(req, nil) 154 return err 155 } 156 157 // UpdateSubscriptionPlan. updates a plan 158 // Doc: https://developer.paypal.com/docs/api/subscriptions/v1/#plans_get 159 // Endpoint: GET /v1/billing/plans/:plan_id 160 func (c *Client) GetSubscriptionPlan(ctx context.Context, planId string) (*SubscriptionPlan, error) { 161 req, err := c.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s%s%s", c.APIBase, "/v1/billing/plans/", planId), nil) 162 response := &SubscriptionPlan{} 163 if err != nil { 164 return response, err 165 } 166 err = c.SendWithAuth(req, response) 167 return response, err 168 } 169 170 // List all plans 171 // Doc: https://developer.paypal.com/docs/api/subscriptions/v1/#plans_list 172 // Endpoint: GET /v1/billing/plans 173 func (c *Client) ListSubscriptionPlans(ctx context.Context, params *SubscriptionPlanListParameters) (*ListSubscriptionPlansResponse, error) { 174 req, err := c.NewRequest(ctx, http.MethodGet, fmt.Sprintf("%s%s", c.APIBase, "/v1/billing/plans"), nil) 175 response := &ListSubscriptionPlansResponse{} 176 if err != nil { 177 return response, err 178 } 179 180 if params != nil { 181 q := req.URL.Query() 182 q.Add("page", params.Page) 183 q.Add("page_size", params.PageSize) 184 q.Add("total_required", params.TotalRequired) 185 q.Add("product_id", params.ProductId) 186 q.Add("plan_ids", params.PlanIds) 187 req.URL.RawQuery = q.Encode() 188 } 189 190 err = c.SendWithAuth(req, response) 191 return response, err 192 } 193 194 // Activates a plan 195 // Doc: https://developer.paypal.com/docs/api/subscriptions/v1/#plans_activate 196 // Endpoint: POST /v1/billing/plans/{id}/activate 197 func (c *Client) ActivateSubscriptionPlan(ctx context.Context, planId string) error { 198 req, err := c.NewRequest(ctx, http.MethodPost, fmt.Sprintf("%s/v1/billing/plans/%s/activate", c.APIBase, planId), nil) 199 if err != nil { 200 return err 201 } 202 203 err = c.SendWithAuth(req, nil) 204 return err 205 } 206 207 // Deactivates a plan 208 // Doc: https://developer.paypal.com/docs/api/subscriptions/v1/#plans_deactivate 209 // Endpoint: POST /v1/billing/plans/{id}/deactivate 210 func (c *Client) DeactivateSubscriptionPlans(ctx context.Context, planId string) error { 211 req, err := c.NewRequest(ctx, http.MethodPost, fmt.Sprintf("%s/v1/billing/plans/%s/deactivate", c.APIBase, planId), nil) 212 if err != nil { 213 return err 214 } 215 216 err = c.SendWithAuth(req, nil) 217 return err 218 } 219 220 // Updates pricing for a plan. For example, you can update a regular billing cycle from $5 per month to $7 per month. 221 // Doc: https://developer.paypal.com/docs/api/subscriptions/v1/#plans_update-pricing-schemes 222 // Endpoint: POST /v1/billing/plans/{id}/update-pricing-schemes 223 func (c *Client) UpdateSubscriptionPlanPricing(ctx context.Context, planId string, pricingSchemes []PricingSchemeUpdate) error { 224 req, err := c.NewRequest(ctx, http.MethodPost, fmt.Sprintf("%s/v1/billing/plans/%s/update-pricing-schemes", c.APIBase, planId), PricingSchemeUpdateRequest{ 225 Schemes: pricingSchemes, 226 }) 227 if err != nil { 228 return err 229 } 230 231 err = c.SendWithAuth(req, nil) 232 return err 233 }