Get subscription information
You can get information about activity with subscriptions in the following ways:
- request information using the API calls (suitable for serverless integration)
- get information when the subscription status changes automatically using webhooks (suitable for server-side integration)
Set up via API calls
On the client side of your application, implement getting information about user subscriptions using an HTTP GET request.
Xsolla API uses HTTP access authentication. The request must contain a header Authorization: Bearer <client_user_jwt>
, where <client_user_jwt>
is a unique token encoded according to the Base64 standard. To get it:
- When authorizing by login and password use the Register new user and Auth by username and password API calls.
- When authorizing through social networks use the Auth via social network API calls.
Specify project ID as the path parameter projectId
. You can find this parameter in your Publisher Account next to the name of the project.
Specify as query parameters:
limit
— limit for the number of elements on the page (15 elements are displayed by default).offset
— number of the element from which the list is generated (the count starts from 0).locale
— interface language (English by default). Accepts values according to the ISO 639-1 standard.
- curl
curl -X 'GET' \
'https://subscriptions.xsolla.com/api/user/v1/projects/{projectId}/subscriptions?locale=ru&limit=5&offset=5 ' \
-H 'accept: application/json' \
-H 'Authorization: Bearer client_user_jwt'
- javascript
{
"items": [
{
"id": 11111111,
"plan_id": 11111,
"plan_external_id": "TestChangePlanBase",
"plan_name": "package_recurrent_name_66053",
"plan_description": "package_recurrent_description_66053",
"product_id": null,
"product_external_id": null,
"product_name": null,
"product_description": null,
"status": "active",
"date_create": "2021-03-11T13:50:11+03:00",
"date_next_charge": "2031-04-11T13:51:02+03:00",
"date_last_charge": "2021-03-11T13:51:02+03:00",
"charge": {
"amount": "0.2500",
"currency": "USD"
},
"period": {
"value": 1,
"unit": "day"
}
}
],
"has_more": true
}
Set up webhooks
The Subscriptions solution supports webhooks for the following events:
- Paying for a subscription:
- Renewing a subscription:
- Canceling a subscription: Canceled Subscription (subscription was canceled in Publisher Account or all payments were canceled for all valid periods)
- Refund:
- Refund (payment was canceled in Publisher Account)
- Updated Subscription (Payment was canceled, but subscription is still active. This is possible if the user accidentally paid for the subscription twice and only canceled the last payment.)
- Canceled Subscription (payment was canceled in Publisher Account or all payments were canceled for all valid periods)
- In Publisher Account, go to Project settings > Webhooks.
- Set the toggle in the Webhooks block to On.
- Specify your webhook URL. Click Save settings.
- Test webhooks in Publisher Account.
- Test subscription purchase.
- Test subscription renewal.
- Test subscription cancellation.
Test webhooks in Publisher Account
- In Publisher Account > Project settings > Webhooks, go to Subscriptions tab.
- To test webhooks, specify values from your project (User ID, Invoice ID) to be sent in a request to the webhook URL you provided. In the absence of real values, you can enter arbitrary values. Complete the following form fields:
- User ID
- Xsolla Invoice ID
- Amount
- Currency
- Plan ID
- Subscription product (optional)
- Invoice ID — internal order number (optional)
- Trial period (To test subscription purchase in Sandbox mode or test subscription renewal, set a
0
value.)
- Click Test.
- Verify that you received a test success message. If your test is successful, your server will return a 204 HTTP status code. If it is unsuccessful, your server will return either a 400 or 500 HTTP status code along with a description of the error.
Test subscription purchase
When testing a payment, User Validation, Payment, and Created Subscription webhooks will be sent to the URL you specified at the Set up webhooks step. To test the payment process, you can:- Use the Sandbox mode (for subscriptions with a trial period of 0 days)
- Make a real payment and then initiate a refund via Publisher Account (for subscriptions with a trial period of more than 0 days)
Sandbox mode
- Get a token for Sandbox mode. Pass the
0
value to the Trial period (trial_days
) parameter. - Choose the Credit/debit cards group of payment methods.
- Enter test bank card details. Enter any values in the remaining fields. You can also specify incorrect details (card number, expiration date, or CVV) in order to generate an error.
- Receive webhooks for User Validation, Payment, and Updated Subscription at the URL you specified at the Set up webhooks step.
Real payment
To test paying for a subscription with a trial period, you’ll need to use a real payment:- Complete Testing webhooks in Publisher Account and activate the Subscriptions module.
- Sign a License agreement.
- Get a token for real payments.
“mode”:“sandbox”
parameter from the token request before you start to receive real payments.- Open the payment UI with the following link:
https://secure.xsolla.com/paystation4/?token=ACCESS_TOKEN
, whereACCESS_TOKEN
is the token obtained in the previous step. - Choose the Credit/debit cards group of payment methods.
- Enter valid bank card details.
- Receive User Validation, Payment, and Updated Subscription webhooks at the webhook URL you provided in the Set up webhooks step.
- Refund your payment. To do this, go to Publisher Account > Support > Transaction search.
- Choose the test transaction and click the More (•••) menu button. Click Refund in the pop-up menu (transaction should have Complete status). This will send Canceled Subscription and Refund webhooks.
Test subscription renewal
- Get a token for Sandbox mode. Pass the
0
value to the Trial period (trial_days
) parameter. - Pay for your subscription using any payment method.
- Reopen the payment interface in Sandbox mode.
- Pay again to renew your subscription using any payment method.
- Get User Validation, Payment, and Updated Subscription webhooks at the webhook URL that you specified at the Set up webhooks step.
Test subscription cancellation
To test subscription cancellation, use:Search by subscription
- In Publisher Account go to Support > Subscribers.
- Find the subscription you want to cancel.
- Open the subscription status menu and select:
- Cancel and refund to receive Canceled Subscription and Refund webhooks.
- Cancel without refund to receive the Canceled Subscription webhook.
Search by recent transactions
- In Publisher Account, go to Support > Transaction search.
- Find the last transactions to be canceled.
- Click the More (•••) icon. Click Refund in the pop-up menu to receive the Canceled Subscription and Refund webhooks.
Use API calls
Use the Update Subscription API call to change the subscription status to Canceled and receive the Canceled Subscription webhook.Open payment UI in Sandbox mode
To open the Sandbox payment UI, get a token with activesandbox
mode. The following are ways to get a link with the required token:Via Publisher Account
- Test webhooks in Publisher Account. Specify the
0
value in the Trial period field to test a subscription that does not have a trial period. After you receive a confirmation message for a successful test, a link with the required token will appear under the Sandbox URL heading.
- Follow the Sandbox URL link.
Via API call
- Pass the
"settings":{"mode":"sandbox"}
parameters to Create token method. Pass the"purchase": {"subscription":{"trial_days": 0}}
parameters to test a subscription that does not have a trial period. - Use the following link:
https://sandbox-secure.xsolla.com/paystation4/?token=ACCESS_TOKEN
, whereACCESS_TOKEN
is the token obtained in the previous step.
Found a typo or other text error? Select the text and press Ctrl+Enter.