swellstores / swell-js

JS library for building storefronts and checkouts with Swell ecommerce.
https://swell.is
MIT License
139 stars 29 forks source link

How to cancel a swell subscription plan with backend API? #141

Closed augustynglowacki closed 10 months ago

augustynglowacki commented 10 months ago

Hello!

I was wondering how to cancel a subscription plan using the Swell's backend api. With the frontend-api, there seems to be an easy method, documented here: https://developers.swell.is/frontend-api/subscriptions#cancel-a-subscription

However, for the backend, I can't find a way to cancel the subscription plan in any other way than to just use the admin panel manually.

You can change non-critical fields of the subscription plan using swell.put method, just like the one below:

        const parentSubscription = await swell.put('/subscriptions/{id}', {
            id: subscription.id,
            active: false,
            status: 'cancelled',
        });

However, using this code results in the API throwing errors about read-only fields.

        // active: { message: 'Read-only field: active', code: 'READONLY' },
        // status: { message: 'Read-only field: status', code: 'READONLY' }

Additionally, Swell docs mention a more "Raw" methods for updating the subscription plans, without the swell-node wrapper. https://developers.swell.is/backend-api/subscription-plans/update-a-subscription I sadly couldn't get any of the requests without the swell-node wrapper to return 200 for me - it is always either 401 or 404. I guess logically, swell.put('/subscriptions/{id}') should be an equivalent of the raw request method.

It would be wonderful to get a feedback on the possibilities of the backend subscription plan status manipulation. Thanks!

augustynglowacki commented 10 months ago

Turns out it was just a typo bug, I used "cancelled" instead of "canceled", for anyone in the future, the method for cancelation is:

        await swell.put('/subscriptions/{id}', {
            id: subscription.id,
            canceled: true,
        });