- Docs
- Payments
Save a card, then charge it
Turn a one-time card token into a saved payment method on a customer, then charge it later.
Saving a card means the browser step happens once per card, not once per charge. After that, your server charges the card by its id, with no token and no cardholder at the page.
How saving works
Section titled “How saving works”Your page mounts the Myela card field with a publishable key, and the cardholder types the card. You get back a single-use paymentToken, mtok_…. A card number never touches your server or any Myela endpoint. Your server then creates a customer and attaches the token to that customer. The response is a saved card, pm_…, and every later charge uses that id.
Attaching the token with POST /v1/payment_methods is the only thing that saves a card. Opening a checkout session with intent: "store" tells the card field what it’s capturing for, but it doesn’t save the card on its own. Collecting the card in the browser is covered in Accept a payment.
Save the card
Section titled “Save the card”Create the customer with an email, then send its id and the token to POST /v1/payment_methods. Both fields are required, and the customer must exist. An unknown or deleted customer is a 404. More on customers in Customers and addresses.
const customer = await myela.gateway.customers.create({ email: 'ada@example.com' });
const card = await myela.gateway.paymentMethods.create({
customerId: customer.id,
paymentToken, // from Elements — never a card number
});# 1. Create the customer
curl -X POST https://payments-api-sandbox.merchantservicedepot.com/v1/customers \
-H "Authorization: Bearer myela_sk_test_…" \
-H "Content-Type: application/json" \
-d '{ "email": "ada@example.com" }'
# 2. Attach the token. This saves the card.
curl -X POST https://payments-api-sandbox.merchantservicedepot.com/v1/payment_methods \
-H "Authorization: Bearer myela_sk_test_…" \
-H "Content-Type: application/json" \
-d '{ "customerId": "cus_…", "paymentToken": "mtok_…" }'The response is the saved card:
| Field | Means |
|---|---|
id |
The saved card’s id, pm_…. Charge with this. |
customerId |
The customer who owns the card. |
brand |
The card brand, for example visa. |
last4 |
The last four digits. The full number is never returned. |
expMonth, expYear |
The card’s expiry. |
isDefault |
Whether this is the customer’s default card. |
createdAt |
When the card was saved, as an ISO timestamp. |
The first active card on a customer becomes their default. Change it with the default call in the next section.
Charge the saved card
Section titled “Charge the saved card”Charge with POST /v1/payments and the card’s paymentMethodId. Send exactly one funding source: a saved card or a token. Sending both, or neither, is a 422. Send an Idempotency-Key, one per order rather than one per retry, so a retried request can’t charge twice.
await myela.gateway.payments.create({
amount: 2500,
currency: 'USD',
paymentMethodId: card.id,
});curl -X POST https://payments-api-sandbox.merchantservicedepot.com/v1/payments \
-H "Authorization: Bearer myela_sk_test_…" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order_1001" \
-d '{ "amount": 2500, "currency": "USD", "paymentMethodId": "pm_…" }'| Field | Required | Means |
|---|---|---|
amount |
Yes | Integer in minor units, above zero. 4999 is $49.99. |
currency |
Yes | Three-letter code, for example USD. |
paymentMethodId |
One of | The saved card, pm_…. |
paymentToken |
One of | A one-off card token instead of a saved card. |
capture |
No | Defaults to true. Send false to authorize only. |
You don’t need to send customerId. With a saved card, the payment always belongs to the card’s owner. If you do send customerId, it must be that owner: a different customer is a 422 with STORED_CARD_NOT_FOR_SHOPPER, and nothing is charged. A saved-card charge is merchant-initiated: the cardholder isn’t present.
A decline doesn’t throw. The request succeeds and the payment comes back with status: "declined", a failureCode, and a failureMessage. To authorize now and capture later, send capture: false and follow Capture, void, and refund.
List cards and set the default
Section titled “List cards and set the default”The default card is the one charged when a request names no card, including when an invoice is collected on a schedule. Make any saved card the default with POST /v1/payment_methods/{id}/default. It takes no body.
// Makes this the card used when no card is named — by
// `invoices.schedule()` and by a recurring invoice template.
await myela.gateway.paymentMethods.setDefault(card.id);
// `customerId` is required on list.
const cards = await myela.gateway.paymentMethods.list({ customerId: customer.id });# Make a card the default (no body)
curl -X POST https://payments-api-sandbox.merchantservicedepot.com/v1/payment_methods/{id}/default \
-H "Authorization: Bearer myela_sk_test_…"
# List a customer's cards, default first
curl "https://payments-api-sandbox.merchantservicedepot.com/v1/payment_methods?customerId=cus_…" \
-H "Authorization: Bearer myela_sk_test_…"To read cards, list them by customer. customerId is required, the default comes first, and the list pages with limit and startingAfter. To read one card, GET /v1/payment_methods/{id}. A deleted card, or an id that isn’t yours, is a 404 with STORED_CARD_NOT_FOUND.
When saving fails
Section titled “When saving fails”A token is single-use and short-lived. You can’t save a token and then charge that same token, and there’s no call that charges and saves at once. Get a token right before you save, and get a fresh one after any failed attempt.
Errors come back with a details.code you can branch on. Don’t branch on the message. More in Errors.
| details.code | Status | When |
|---|---|---|
PAYMENT_TOKEN_UNUSABLE |
422 | The token is expired, already used, or not yours. The API doesn’t say which. Get a new token. |
DUPLICATE_CARD |
409 | This customer already has this card saved (same last four and expiry). List their cards and use that one. |
CAPTURE_UNAVAILABLE |
422 | Opening a checkout session failed because browser card capture isn’t set up for your account. |