Tokenized Cards
Welcome to our guide on using tokens for recurring payments with Moyasar! Tokens are a powerful tool that allows you to securely handle recurring payments without needing to store sensitive payment information on your servers.
Before Starting
Tokenization must be enabled on your account before using the tokenization feature. Please reach out to your account manager or the sales team to enable it.
To get started with recurring payments, you will first need to learn how to get the token.
To generate a tokenized card, you have 2 options:
- Save the card after a successful payment transaction.
- Through our Create Token API.
Create A Payment
To initiate a payment using the stored token, send a POST request to https://api.moyasar.com/v1/payments; here is an example using cURL
Sample Request
curl -X POST https://api.moyasar.com/v1/payments \
-u pk_test_MrtwozLJAuFmLKWWSaRaoaLX: \
-d amount= 100 \
-d currency= SAR \
-d source['type']= 'token' \
-d source['token']= 'token_qbmmXzo97AESrZLS6KpWvof6uK2hAKcQGfEcKg' \
-d source['3ds']= true \
-d source['manual']= false \
Sample Response
{
"id": "ad515696-246f-4ae6-88cd-88fe64c13c51",
"status": "paid",
"amount": 100,
"fee": 0,
"currency": "SAR",
"refunded": 0,
"refunded_at": null,
"captured": 0,
"captured_at": null,
"voided_at": null,
"description": null,
"amount_format": "1.00 SAR",
"fee_format": "0.00 SAR",
"refunded_format": "0.00 SAR",
"captured_format": "0.00 SAR",
"invoice_id": null,
"ip": "some.ip.address",
"callback_url": "https://www.moyasar.com/thankyou",
"created_at": "2023-03-19T08:44:02.429Z",
"updated_at": "2023-03-19T08:44:02.440Z",
"metadata": null,
"source": {
"type": "creditcard",
"company": "visa",
"name": "Warren Buffett",
"number": "XXXX-XXXX-XXXX-1111",
"gateway_id": "moyasar_cc_ce1iUidxhrh74257S891wvW",
"reference_number": null,
"token": null,
"message": "Succeeded! (Test Environment)",
"transaction_url": null
}
}
Fetching Card Information
You can use a token to retrieve saved card information to provide your users with good UX. Here is an example of the information you can obtain from the token:
Sample Request
curl https://api.moyasar.com/v1/tokens/token_qbmmXzo97AESrZLS6KpWvof6uK2hAKcQGfEcKg \
-u sk_test_MrtwozLJAuFmLKWWSaRaoaLX:
Sample Response
{
"id": "token_qbmmXzo97AESrZLS6KpWvof6uK2hAKcQGfEcKg",
"status": "active",
"brand": "visa",
"funding": "credit",
"country": "SA",
"month": "07",
"year": "2027",
"name": "Warren Buffett",
"last_four": "1111",
"verification_url": "",
"metadata": null,
"message": "Card stored successfully",
"verification_url": null,
"created_at": "2023-02-11T17:04:17.000Z",
"updated_at": "2023-02-12T17:04:19.633Z"
}
This will help your users identify which card is being used for the recurring payment.
Remove Card Information
Sometimes your users may want to remove old or expired cards from their accounts, Tokens will be invalidated when the card reaches its expiration date or by issuing a DELETE request, here is an example:
Sample Request
curl -X DELETE https://api.moyasar.com/v1/tokens/token_qbmmXzo97AESrZLS6KpWvof6uK2hAKcQGfEcKg \
-u sk_test_MrtwozLJAuFmLKWWSaRaoaLX:
Sample Response
You will get an empty response with a 204 HTTP status code.