Create subscription using the API

I am working on an app where users can subscribe to gain acces to content.

I would like to make subscription plans and variations using the dashboard, as it is more user friendly for my colleagues. I have been playing around with the options in Sandbox and believe have a good grasp of the api but unfortunately I get an INTERNAL_SERVER_ERROR in the last step of creating a subscription.

The current flow is as follows. First tokenise a card with it verification code (verifyBuyer) using the Web SDK. These codes are send to my backend server which will check if the logged in user already is in the Square Directory or not. If it is, it will retrieve the customer_id, if not it will add the customer to the Square Directory and with the corresponding customer_id assign the card to the customer. So far so good.

Now I make a subscription using the sandbox dashboard NVJWSYCOTPRPIJI4WKRYQCYD with a plan variation S2PWD7LBU5QMRRVTSSJVLZQQ. When creating the subscription, the API throws error that I have to include the phases, so OK, I include the phases as obtained when I retrieve the subscription:

"phases": [ { "uid": "LPXPKJUOTW2AGQ7IOMO7JBMC", "cadence": "MONTHLY", "ordinal": 0, "pricing": { "type": "RELATIVE" } } ]

But now I get the error:

errors: [ { code: 'INTERNAL_SERVER_ERROR', detail: 'An internal error has occurred, and the API was unable to service your request.', category: 'API_ERROR' } ]

Example of the data body when creating the subscription:

{ idempotency_key: '9601acf8f4452c84b0dd46e183587079cab986ed', location_id: 'LVVPXS39HD364', plan_variation_id: 'S2PWD7LBU5QMRRVTSSJVLZQQ', customer_id: 'Z8WXR2JVJX61Z28HE8FKWWF21R', card_id: 'ccof:CA4SEDFhF_AvNyipFJnN4DK9SO4oAg', phases: [ { ordinal: 0, order_template_id: 'LPXPKJUOTW2AGQ7IOMO7JBMC' } ], source: { name: 'NxtDay' } }

When I try with a subscription plan as in the Coffee subscription Square example using only the API, it works. I guess it has something to do the STATIC/RELATIVE stuff, as this examples uses STATIC. But you get a ‘legacy’ plan and more over you cannot make such item in the dashboard.

SQUARE_UP_LOCATION=LVVPXS39HD364

Would be cool is someone had similar problem or could help me look at what I am doing wrong.

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

https://developer.squareup.com/docs/app-marketplace/requirements/subscriptions-api

Additional Documentation

Guidelines and Limitations
Handling Errors
Handling Errors

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

The error you’re encountering seems to be related to the “phases” field in your request. When creating a subscription, you don’t need to include the “phases” field in your request. The “phases” field is automatically populated based on the subscription plan you’re using.

In your request, you should only need to provide the “idempotency_key”, “location_id”, “plan_id”, “customer_id”, and “card_id”. The “plan_id” should be the ID of the subscription plan you created in the dashboard.

Here’s an example of what your request should look like:

{
  "idempotency_key": "9601acf8f4452c84b0dd46e183587079cab986ed",
  "location_id": "LVVPXS39HD364",
  "plan_id": "S2PWD7LBU5QMRRVTSSJVLZQQ",
  "customer_id": "Z8WXR2JVJX61Z28HE8FKWWF21R",
  "card_id": "ccof:CA4SEDFhF_AvNyipFJnN4DK9SO4oAg"
}

If you’re still encountering issues after making these changes, it might be a good idea to double-check that your “plan_id”, “customer_id”, and “card_id” are correct.

This answer was reviewed by @Bryan-Square.

Hi, thank you for looking into my problem. Plan_id should be plan_variation_id right?

Anyway, I have tried both ways. Using Plan id:

{ idempotency_key: 'f6da36227e728ce0229f991b3456a31ef1dbec0e', location_id: 'LVVPXS39HD364', plan_variation_id: 'NVJWSYCOTPRPIJI4WKRYQCYD', customer_id: 'Z8WXR2JVJX61Z28HE8FKWWF21R', card_id: 'ccof:CA4SEDFhF_AvNyipFJnN4DK9SO4oAg' }

Response is:

{ errors: [ { code: 'BAD_REQUEST', detail: "Object 'NVJWSYCOTPRPIJI4WKRYQCYD' does not have type SUBSCRIPTION_PLAN_VARIATION", category: 'INVALID_REQUEST_ERROR' } ] }

Using plan variation id:

{ idempotency_key: 'c1be20b25051e19da71781584ab7aa86bb8df1da', location_id: 'LVVPXS39HD364', plan_variation_id: 'S2PWD7LBU5QMRRVTSSJVLZQQ', customer_id: 'Z8WXR2JVJX61Z28HE8FKWWF21R', card_id: 'ccof:CA4SEDFhF_AvNyipFJnN4DK9SO4oAg' }

Response is:

{ errors: [ { code: 'CONFLICTING_PARAMETERS', detail: 'Phases with RELATIVE pricing type must have phases on the subscription', category: 'INVALID_REQUEST_ERROR' } ] }

Also changing the key plan_variation_id to plan_id as follows:

{ idempotency_key: 'adbe6bdafbc4aa6e13c4bfcc46aaac75312edf48', location_id: 'LVVPXS39HD364', plan_id: 'NVJWSYCOTPRPIJI4WKRYQCYD', customer_id: 'Z8WXR2JVJX61Z28HE8FKWWF21R', card_id: 'ccof:CA4SEDFhF_AvNyipFJnN4DK9SO4oAg' }

Results in:

{ errors: [ { code: 'BAD_REQUEST', detail: 'plan_variation_id cannot be empty.', category: 'INVALID_REQUEST_ERROR' } ] }