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

# Accept a payment

> Collect a card in the browser with Myela Elements and charge it from your server with @myela/payments.

The shopper types their card into a hosted field in your page. Your page gets a single-use token, sends it to your server, and your server charges it. Card numbers never touch your code.

1. Browser · card field
2. Token `mtok_…`
3. Your server · SDK
4. `POST /v1/payments`

## Before you start

You need a test key. Test keys are issued inside your Myela account: sign in, open API keys, and confirm with the one-time code we email you. A signed-in session alone isn't enough to mint a key that can charge cards.

> **This step needs an account** [Request access](https://docs.merchantservicedepot.com/sign-in/#request)

Sandbox keys and test cards are tied to your account and never published. Request access to get one.

Each key arrives as a pair, and the two halves are not interchangeable:

| Key | Prefix | Lives |
| --- | --- | --- |
| Secret | `myela_sk_test_` | Your server only. Shown once; Myela keeps only a hash, so store it now. |
| Publishable | `myela_pk_test_` | The browser. It can open a checkout session and nothing else. |

Add your checkout page's origin to the key's allow-list. An empty list refuses every origin.

## Install the SDK

`@myela/payments` runs on your server (Node 20 or later). Point it at the sandbox host; the SDK has no default base URL, so it never guesses an environment.

```bash title="terminal"
npm install @myela/payments
```

```bash title=".env"
MYELA_API_KEY=myela_sk_test_…
MYELA_API_BASE_URL=https://payments-api-sandbox.merchantservicedepot.com
MYELA_SIGNING_SECRET=myela_sign_…
```

## Collect the card

Myela Elements mounts one hosted card field in your page and hands back an opaque token. It takes the publishable key and refuses a secret one.

> **Preview:** 

Elements is pre-1.0. Confirm the hosted field is enabled for your account before you build on it.

```html title="checkout.html"
<script type="module">
  import { Myela } from 'https://js.merchantservicedepot.com/v1/elements.js';

  const myela = Myela({
    publishableKey: 'myela_pk_test_...',
    apiBase: 'https://payments-api-sandbox.merchantservicedepot.com',
  });
  const card = myela.elements().create('card');

  await card.mount('#card-field');

  card.on('change', (e) => (payButton.disabled = !e.complete));

  payButton.addEventListener('click', async () => {
    const { token, error } = await card.tokenize();
    if (error) return showError(error.message);

    // token is opaque: "mtok_...". Send it to YOUR server.
    await fetch('/checkout', { method: 'POST', body: JSON.stringify({ token }) });
  });
</script>
```

## Create the payment

On your server, charge the token. Amounts are integers in minor units, so `4999` is $49.99; decimals are rejected. Send an `Idempotency-Key` so a retried request can't charge twice.

**Node**

```ts title="server.ts"
import { Myela } from '@myela/payments';

const myela = new Myela({
  apiKey: process.env.MYELA_API_KEY!,
  baseUrl: process.env.MYELA_API_BASE_URL!,
  // Always pass it. Required by keys that enforce signing, ignored by keys that
  // do not — so the same code works either way. See "Request signing" below.
  signingSecret: process.env.MYELA_SIGNING_SECRET,
});

const payment = await myela.gateway.payments.create({
  amount: 4999, // minor units — $49.99
  currency: 'USD',
  paymentToken: token, // from Myela Elements, in the browser
});
```

**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": 4999, "currency": "USD", "paymentToken": "…" }'
```

Capture, void, and refund are calls on the same payment. Every field and response is in the [API reference](https://docs.merchantservicedepot.com/api-reference/).

## Listen for webhooks

Myela signs every delivery with the endpoint's own secret in a `Myela-Webhook-Signature` header. Verify it against the raw body, answer fast, and dedupe on the event id: deliveries are at least once, and a slow or failing endpoint is retried for up to six hours.

```ts title="webhooks.ts"
import { constructEvent } from '@myela/payments';

// The RAW body is required — see below.
app.post('/webhooks/myela', express.raw({ type: 'application/json' }), async (req, res) => {
  let event;
  try {
    event = await constructEvent({
      payload: req.body,
      signature: req.header('Myela-Webhook-Signature')!,
      secret: process.env.MYELA_WEBHOOK_SECRET!,
      toleranceSeconds: 300, // default
    });
  } catch {
    return res.sendStatus(400); // discard — do not act on it
  }

  res.sendStatus(200); // acknowledge FIRST, then work — slow handlers get retried

  // Dedupe on event.id before acting — you WILL receive the same event twice.
  switch (event.type) {
    case 'invoice.paid':
      await markPaid(event.data);
      break;
  }
});
```

| Event | When |
| --- | --- |
| `invoice.paid` | An invoice was paid. |
| `invoice.payment_failed` | An invoice payment attempt failed. |
| `invoice.overdue` | Collection stopped without the invoice being paid. |
| `payment.settled` | A payment settled. Not on every account yet. |
| `payment.refunded` | A payment was refunded. Not on every account yet. |

## Go live

Live keys are issued by Myela once your account is approved, and they come with the live host. A live key must also sign every request: an HMAC in a `Myela-Signature` header, with a timestamp inside five minutes and a nonce used once. Pass `signingSecret` to the SDK from day one and it signs for you, so the code you tested is the code you ship.

> **Live keys come from Myela** [Talk to us](https://docs.merchantservicedepot.com/sign-in/#request)

There is no self-serve live key. Your Myela contact issues it with the key's permissions set.

Source: https://docs.merchantservicedepot.com/docs/payments/accept-a-payment/
