Comment on page
Accept your first subscription payment
Example of how to create a recurring subscription using Copperx APIs
In this example, we will learn to start a subscription payment for 10 USDC/mo.
Before we start, few things to keep in mind
- We are using our
dev
environment which supports on Polygon Mumbai Testnet and Goerli Testnet. - We will be using a
curl
to request Copperx APIs. - Make sure you generate your
API_KEY
from our Dashboard. If you don't have one, then generate it from here. - You will need test ERC20 Faucets to make the payment, you can request them from https://dashboard.copperx.dev/faucet. If you need more faucet, then feel free to contact our team via Telegram.
- As you are using test environment, there is no real money involved. So feel free to play around.
To create a subscription, you first need to create a plan in Copperx Dashboard.
Let's create a plan to accept 10 USDC/mo.
- 1.
- 2.Login with your Copperx account.
- 3.Click Billing in the top menu and click on Plans.
- 4.Press Create Plan button on top-right to create a new plan.
- 5.Do fill plan title, description, recurring interval.
- 6.Do set price as 10 USDC.
- 7.Click Create Plan button and plan will be created.
Congratulations 🎉, you have successfully created your first plan on Copperx.
Now, let's move ahead and create a Checkout Session to create a subscription.
To create a Checkout Session, you will need
priceId
of the newly created plan. You will be able to find priceId
of the plan from plan details page.- 1.
- 2.Find the plan for which you want to create a Checkout Session.
- 3.Click
•••
> Details to find more details. - 4.On details page, you will find a field called Plan Id.
- 5.Press Copy icon to copy
Price Id
Now let's go ahead and create subscription of our plan.
To create a checkout session, run below command in your terminal:
Make sure you update
YOUR_API_KEY
and PRICE_ID
in the code.curl --request POST \
--url 'https://api.copperx.io/api/v1/checkout/sessions' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--header 'Content-Type: application/json'
--data-raw '{
"successUrl": "https://copperx.io?cid={CHECKOUT_SESSION_ID}",
"mode": "subscription",
"lineItems": [{
"priceId": "{PRICE_ID}"
}]
}'
You will receive the responsive as below:
{
"id": "6c88e598-335d-468b-aea5-fa7a3aa074bf",
"createdAt": "2023-04-03T17:57:16.943Z",
"updatedAt": "2023-04-03T17:57:16.982Z",
"mode": "subscription",
"paymentMethodTypes": [
"superfluid"
],
"paymentSetting": {
"allowedChains": [
{
"chainId": 137
}
],
"paymentMethodTypes": null,
"preferredChainId": 137,
"allowSwap": false
},
"expiresAt": "2023-04-03T18:57:16.940Z",
"customerCreation": "if_required",
"customerUpdate": null,
"submitType": "pay",
"afterCompletion": null,
"organizationId": "758cbac4-58b8-4e0a-b1a2-27b60d4b148d",
"amountTotal": "10000000",
"currency": "usdc",
"paymentLinkId": null,
"subscriptionId": null,
"customerId": "820b4fa9-8f4d-4866-9333-527752e32775",
"clientReferenceId": null,
"successUrl": "https://copperx.io?cid=6c88e598-335d-468b-aea5-fa7a3aa074bf",
"cancelUrl": null,
"status": "open",
"paymentStatus": "unpaid",
"metadata": null,
"emailCollection": false,
"phoneNumberCollection": false,
"shippingAddressCollection": false,
"billingAddressCollection": false,
"afterCompletionConfirmMsg": null,
"shippingDetails": {},
"billingDetails": {},
"url": "https://buy.copperx.io/payment/checkout-session/6c88e598-335d-468b-aea5-fa7a3aa074bf",
"customer": {
"name": null,
"email": null,
"phone": null,
"id": "820b4fa9-8f4d-4866-9333-527752e32775",
"createdAt": "2023-04-03T17:57:16.921Z",
"updatedAt": "2023-04-03T17:57:16.921Z",
"customerReferenceId": null,
"visibility": 10,
"metadata": {}
},
"customerDetails": {
"name": null,
"email": null,
"phone": null,
"address": {}
},
"lineItems": {
"object": "list",
"data": [
{
"amountTotal": "10000000",
"currency": "usdc",
"description": null,
"quantity": 1,
"price": {
"id": "ae575f1d-57b0-47cd-b40b-f1c2a057c067",
"createdAt": "2023-04-03T17:57:16.909Z",
"updatedAt": "2023-04-03T17:57:16.909Z",
"billingScheme": "per_unit",
"currency": "usdc",
"productId": "a90004dc-48be-4d21-9f45-05cc3acacc52",
"interval": "month",
"usageType": null,
"type": "recurring",
"unitAmount": "10000000",
"unitAmountDecimal": null,
"customUnitMax": null,
"customUnitMin": null,
"customPreset": null,
"customUnitAmountSuggestions": null,
"product": {
"id": "a90004dc-48be-4d21-9f45-05cc3acacc52",
"createdAt": "2023-04-03T17:57:16.903Z",
"updatedAt": "2023-04-03T17:57:16.918Z",
"name": "Basic",
"description": "For early stage projects who are getting started",
"isActive": true,
"images": [],
"unitLabel": null,
"url": null,
"metadata": null,
"defaultPriceId": "ae575f1d-57b0-47cd-b40b-f1c2a057c067",
"visibility": 20
},
"metadata": null,
"isActive": true,
"visibility": 20
}
}
]
},
"addresses": [
{
"id": "c0e4bd0d-e7a4-4202-ac4d-f7bc60e6c474",
"createdAt": "2023-04-03T17:57:16.982Z",
"updatedAt": "2023-04-03T17:57:16.982Z",
"checkoutSessionId": "6c88e598-335d-468b-aea5-fa7a3aa074bf",
"asset": {
"id": "13056880-798b-4bd4-a555-c1c71de017fa",
"name": "USDC",
"address": "0x2791bca1f2de4661ed88a30c99a7a9449aa84174",
"chainId": 137,
"currency": "usdc",
"symbol": "USDC",
"decimals": 6,
"coingeckoId": "usd-coin",
"isPublic": true,
"superToken": {
"address": "0xcaa7349cea390f89641fe306d93591f87595dc1f",
"symbol": "USDCx",
"decimals": 18
},
"chain": {
"id": 137,
"name": "Polygon",
"icon": "icon",
"currency": "matic",
"blockExplorerName": "Polygonscan",
"blockExplorerTransactionUrl": "https://polygonscan.com/tx/{hash}"
}
},
"paymentAddress": "0xd55069e3b3a088dfe3690decac89740b897bd1e7"
}
],
"paymentIntent": {
"id": "a6da4225-1d26-4ede-b4fa-259ed5a0a3ab",
"createdAt": "2023-04-03T17:57:16.937Z",
"updatedAt": "2023-04-03T17:57:16.937Z",
"organizationId": "758cbac4-58b8-4e0a-b1a2-27b60d4b148d",
"amount": "10000000",
"amountReceived": null,
"currency": "usdc",
"status": "requires_payment_method",
"paymentMethodId": null,
"paymentMethodTypes": [
"superfluid"
],
"description": null,
"transferAccountAddress": null,
"transactions": []
}
}
Here,
id
= Unique Identifier for Checkout SessionamountTotal
= Amount to be collected from user (in 8 decimal)currency
= Payment Currencyurl
= Hosted Checkout page for the Session. You need to redirect user to this URL.
Once you create a Checkout Session, you need to redirect user to the give URL in
url
parameter of the response.Once user makes the payment, user will be redirected to the URL provided in
successUrl
.If user cancels the payment, then user will be redirected to the URL provided in
cancelUrl
.
Last modified 7mo ago