Skip to content

Commit

Permalink
Merge pull request #10 from r3ap3r2004/fix-subscription-update
Browse files Browse the repository at this point in the history
Fix for updating subscriptions
  • Loading branch information
AchoArnold authored Aug 20, 2023
2 parents d081c09 + 91e8b03 commit f4a4c42
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 39 deletions.
60 changes: 34 additions & 26 deletions internal/stubs/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,40 @@ func SubscriptionGetResponse() []byte {
func SubscriptionUpdateResponse() []byte {
return []byte(`
{
"type": "subscriptions",
"id": "1",
"attributes": {
"store_id": 1,
"order_id": 1,
"order_item_id": 1,
"product_id": 9,
"variant_id": 11,
"product_name": "New Plan Product",
"variant_name": "New Plan Variant",
"user_name": "Darlene Daugherty",
"user_email": "[email protected]",
"status": "active",
"status_formatted": "Active",
"pause": null,
"cancelled": false,
"trial_ends_at": null,
"billing_anchor": 29,
"urls": {
"update_payment_method": "https://app.lemonsqueezy.com/my-orders/2ba92a4e-a00a-45d2-a128-16856ffa8cdf/subscription/8/update-payment-method?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413"
},
"renews_at": "2022-11-12T00:00:00.000000Z",
"ends_at": null,
"created_at": "2021-08-11T13:47:27.000000Z",
"updated_at": "2021-08-11T13:54:19.000000Z",
"test_mode": false
"jsonapi":{
"version":"1.0"
},
"links":{
"self":"https://api.lemonsqueezy.com/v1/subscriptions/1"
},
"data": {
"type": "subscriptions",
"id": "1",
"attributes": {
"store_id": 1,
"order_id": 1,
"order_item_id": 1,
"product_id": 9,
"variant_id": 11,
"product_name": "New Plan Product",
"variant_name": "New Plan Variant",
"user_name": "Darlene Daugherty",
"user_email": "[email protected]",
"status": "active",
"status_formatted": "Active",
"pause": null,
"cancelled": false,
"trial_ends_at": null,
"billing_anchor": 29,
"urls": {
"update_payment_method": "https://app.lemonsqueezy.com/my-orders/2ba92a4e-a00a-45d2-a128-16856ffa8cdf/subscription/8/update-payment-method?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413"
},
"renews_at": "2022-11-12T00:00:00.000000Z",
"ends_at": null,
"created_at": "2021-08-11T13:47:27.000000Z",
"updated_at": "2021-08-11T13:54:19.000000Z",
"test_mode": false
}
}
}
`)
Expand Down
19 changes: 10 additions & 9 deletions subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ type SubscriptionPause struct {

// SubscriptionUpdateParams are parameters for updating a subscription
type SubscriptionUpdateParams struct {
Type string `json:"type"`
ID string `json:"id"`
Pause *SubscriptionPause `json:"pause,omitempty"`
Cancelled bool `json:"cancelled,omitempty"`
InvoiceImmediately bool `json:"invoice_immediately,omitempty"`
Attributes SubscriptionUpdateParamsAttributes `json:"attributes"`
Type string `json:"type"`
ID string `json:"id"`
Attributes SubscriptionUpdateParamsAttributes `json:"attributes"`
}

// SubscriptionUpdateParamsAttributes are subscription update attributes
type SubscriptionUpdateParamsAttributes struct {
ProductID int `json:"product_id"`
VariantID int `json:"variant_id"`
BillingAnchor int `json:"billing_anchor"`
ProductID int `json:"product_id,omitempty"`
VariantID int `json:"variant_id,omitempty"`
BillingAnchor int `json:"billing_anchor,omitempty"`
Cancelled bool `json:"cancelled"`
Pause *SubscriptionPause `json:"pause,omitempty"`
InvoiceImmediately bool `json:"invoice_immediately"`
DisableProrations bool `json:"disable_prorations"`
}

// ApiResponseRelationshipsSubscription relationships of a subscription object
Expand Down
18 changes: 15 additions & 3 deletions subscriptions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ type SubscriptionsService service
// Update an existing subscription to specific parameter
//
// https://docs.lemonsqueezy.com/api/subscriptions#update-a-subscription
func (service *SubscriptionsService) Update(ctx context.Context, params *SubscriptionUpdateParams) (*Resource[Subscription], *Response, error) {
response, err := service.client.do(ctx, http.MethodPatch, "/v1/subscriptions/"+params.ID, params)
func (service *SubscriptionsService) Update(ctx context.Context, params *SubscriptionUpdateParams) (*ApiResponseSubscription, *Response, error) {
typeParam := "subscriptions"
if len(params.Type) > 0 {
typeParam = params.Type
}
payload := map[string]any{
"data": map[string]any{
"id": params.ID,
"type": typeParam,
"attributes": params.Attributes,
},
}

response, err := service.client.do(ctx, http.MethodPatch, "/v1/subscriptions/"+params.ID, payload)
if err != nil {
return nil, response, err
}

subscription := new(Resource[Subscription])
subscription := new(ApiResponseSubscription)
if err = json.Unmarshal(*response.Body, subscription); err != nil {
return nil, response, err
}
Expand Down
2 changes: 1 addition & 1 deletion subscriptions_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func TestSubscriptionsService_Update(t *testing.T) {

assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
assert.Equal(t, stubs.SubscriptionUpdateResponse(), *response.Body)
assert.Equal(t, "1", subscription.ID)
assert.Equal(t, "1", subscription.Data.ID)

// Teardown
server.Close()
Expand Down

0 comments on commit f4a4c42

Please sign in to comment.