> Index of every page: https://docs.merchantservicedepot.com/llms.txt

# 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

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](https://docs.merchantservicedepot.com/docs/payments/accept-a-payment/).

Myela Elements is in preview. Confirm the hosted card field is enabled for your account before you build on it. Until it is, your Myela contact provides the field you capture with.

## 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](https://docs.merchantservicedepot.com/docs/payments/customers/).

**Node**

```ts title="server.ts"
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
});
```

**cURL**

```bash title="terminal"
# 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

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.

**Node**

```ts title="server.ts"
await myela.gateway.payments.create({
  amount: 2500,
  currency: 'USD',
  paymentMethodId: card.id,
});
```

**cURL**

```bash title="terminal"
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](https://docs.merchantservicedepot.com/docs/payments/capture-void-refund/).

## 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.

**Node**

```ts title="server.ts"
// 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 });
```

**cURL**

```bash title="terminal"
# 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`.

> **Removing a card needs Myela** [Talk to us](https://docs.merchantservicedepot.com/sign-in/#request)

Deleting a saved card answers 403 until Myela enables it for your key. Even then, a card that a subscription uses can't be removed.

## 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](https://docs.merchantservicedepot.com/docs/platform/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. |

Source: https://docs.merchantservicedepot.com/docs/payments/save-a-card/
