# Vendure

> Accept cryptocurrency payments in Vendure 3.8+ through Cryptonly hosted checkout.

The **Cryptonly plugin for Vendure** lets your Vendure store accept cryptocurrency payments through Cryptonly's hosted checkout.

Customers choose Cryptonly at checkout, pay on the Cryptonly payment page and return to your store. The order is placed once Cryptonly confirms the payment through a signed webhook. There is no manual reconciliation.

Setup takes four steps:

1. [Install the plugin](https://docs.cryptonly.net/integration/plugins/vendure.md#installation)
2. [Create a Cryptonly payment method](https://docs.cryptonly.net/integration/plugins/vendure.md#payment-method) in the Dashboard
3. [Redirect to the payment page](https://docs.cryptonly.net/integration/plugins/vendure.md#storefront) from your storefront
4. Add a return page that waits for the payment

## How the payment flow works

```mermaid
sequenceDiagram
    participant Customer
    participant Storefront
    participant Vendure
    participant Cryptonly
    Customer->>Storefront: Place order with Cryptonly
    Storefront->>Vendure: addPaymentToOrder
    Vendure->>Cryptonly: Create invoice
    Vendure-->>Storefront: Payment "Created" with paymentPageUrl
    Storefront-->>Customer: Redirect to hosted checkout
    Customer->>Cryptonly: Pay with crypto
    Cryptonly->>Vendure: invoice.statusChanged webhook
    Vendure->>Cryptonly: Confirm invoice status
    Vendure-->>Customer: Order placed (PaymentSettled)
```

In detail:

1. The customer reaches `ArrangingPayment`, and the storefront calls the standard `addPaymentToOrder` mutation with the Cryptonly method.
2. Vendure creates a Cryptonly invoice server-side, so your API key never reaches the browser. It records a payment in the `Created` state. The cart is locked while the invoice is open.
3. The storefront redirects the customer to the hosted payment page (`paymentPageUrl`).
4. Cryptonly sends signed webhooks to Vendure. The plugin verifies each one, confirms the invoice status with the Cryptonly API, and settles the payment once the invoice is `paid` or `overpaid`.

For details on invoices and statuses, see [Invoices](https://docs.cryptonly.net/guides/invoices.md).

## Requirements

| Requirement          | Details                                                                         |
| -------------------- | ------------------------------------------------------------------------------- |
| Vendure              | 3.8+                                                                            |
| Node                 | 22.12+                                                                          |
| Cryptonly account    | [merchant.cryptonly.net](https://merchant.cryptonly.net/)                       |
| Public HTTPS server  | Cryptonly must reach `{host}/payments/cryptonly/{channelToken}/{paymentMethodId}` |

## Installation

Install the plugin:

```bash
npm install @cryptonly/vendure-plugin-cryptonly
```

Add it to `vendure-config.ts`:

```ts

export const config: VendureConfig = {
  // ...
  systemOptions: {
    // Needed only with encryptSecrets: true
    encryptionStrategy: new DefaultEncryptionStrategy({ secret: process.env.VENDURE_ENCRYPTION_KEY }),
  },
  plugins: [
    CryptonlyPlugin.init({
      host: process.env.VENDURE_PUBLIC_URL, // e.g. https://api.my-shop.com
      encryptSecrets: true,
    }),
  ],
};
```

| Option           | Required | Description                                                                                                                  |
| ---------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `host`           | Yes      | Public HTTPS origin of the Vendure server. Webhooks are sent to `{host}/payments/cryptonly/...`. Localhost and IP addresses are rejected. |
| `encryptSecrets` | No       | Store the API key and signing key encrypted, and hide them from admins without the `ReadSecret` permission. Requires an `EncryptionStrategy` with a key. Default: `false`. |

The plugin adds no database columns, so no migration is needed.

:::tip
For local development, expose the server through a tunnel such as ngrok and use the tunnel URL as `host`.
:::

## Payment method

In the Dashboard, open **Settings → Payment methods**, create a payment method with the code `cryptonly` and choose the **Cryptonly crypto payments** handler:

| Field                                 | Description                                                                                 |
| ------------------------------------- | ------------------------------------------------------------------------------------------- |
| API key                               | API key from the account **Security** settings. The key selects the account.                |
| Webhook signing key                   | Signing key from the same account. Used to verify webhooks.                                 |
| Sandbox mode                          | Turn on for sandbox keys.                                                                   |
| Invoice lifetime (minutes)            | Optional, 15 to 43200. Empty uses the account default.                                      |
| Return URL                            | Your storefront's return page, opened by the **Return to** button on the hosted page whatever the outcome. `{orderCode}` is replaced with the order code. |

Create one payment method per channel that should accept crypto. Each channel can use its own Cryptonly account.

### API key scopes

| Scope             | Used for                                                     |
| ----------------- | ------------------------------------------------------------ |
| `invoices.create` | Create the invoice when the customer places the order         |
| `invoices.list`   | Confirm the invoice status for webhooks and manual settlement |
| `invoices.cancel` | Cancel the invoice when the customer leaves checkout          |

The plugin sets the webhook URL on every invoice, so you do **not** need to configure a default webhook in the Merchant Dashboard.

## Storefront

### Redirect to the payment page

The order must be in `ArrangingPayment`. Add the payment and send the customer to its `paymentPageUrl`:

```ts
const { addPaymentToOrder } = await shopApi(`
  mutation {
    addPaymentToOrder(input: { method: "cryptonly", metadata: {} }) {
      __typename
      ... on Order { code payments { method state metadata } }
      ... on ErrorResult { errorCode message }
      ... on PaymentFailedError { paymentErrorMessage }
    }
  }
`);

const payment = addPaymentToOrder.payments.find(
  (p) => p.method === 'cryptonly' && p.state === 'Created'
);
window.location.assign(payment.metadata.public.paymentPageUrl);
```

If the customer places the order again while the invoice is still open, redirect to the same `paymentPageUrl` from `activeOrder.payments`. A second `addPaymentToOrder` returns `PaymentFailedError` until the order goes back to `AddingItems`.

### Return page

Set the payment method's Return URL to a page such as `/checkout/cryptonly/{orderCode}`. Customers land there whether they paid, cancelled or let the invoice expire, so the page polls the Shop API every few seconds:

| What you see                                                    | Meaning                                              |
| --------------------------------------------------------------- | ---------------------------------------------------- |
| `activeOrder` has this code, state `ArrangingPayment`            | Waiting for payment: show a spinner                  |
| `activeOrder` has this code, state `AddingItems`                 | Invoice cancelled or expired: send back to checkout  |
| `orderByCode(code)` returns `PaymentSettled`                    | Paid: show the order confirmation                    |

Ready-made helpers for both steps are in the plugin's [assets/storefront](https://github.com/cryptonly-net/cryptonly-vendure/tree/main/assets/storefront) folder.

## Status mapping

| Cryptonly status                 | Vendure                                                                           |
| -------------------------------- | --------------------------------------------------------------------------------- |
| `paid`, `overpaid`               | Payment **Settled**, order moves to `PaymentSettled`                              |
| `created`, `processing`          | Payment stays `Created`                                                           |
| `cancelled`, `expired`, `failed` | Payment **Declined**, order goes back to `AddingItems`                            |
| `partially_paid`, `suspended`    | Payment stays `Created` and is flagged `needsReview`. Resolve it in the Merchant Dashboard |

Keep in mind:

* When an order leaves `ArrangingPayment` unpaid (the customer returns to the cart, or the order is cancelled), the open invoice is cancelled in Cryptonly.
* **Settle** in the Dashboard only succeeds when Cryptonly reports the invoice as paid. **Cancel** is refused once the invoice has received funds.
* Refunds made in Vendure are not sent to Cryptonly. Refund the customer from the Merchant Dashboard.

For webhook signing details, see [Webhooks](https://docs.cryptonly.net/api-reference/webhooks.md). For sandbox testing, see [Testing your integration](https://docs.cryptonly.net/guides/testing-your-integration.md).

## Related pages

  <ExternalLinkCard title="Plugin on GitHub" href="https://github.com/cryptonly-net/cryptonly-vendure" icon="github" />
  <ExternalLinkCard title="npm package" href="https://www.npmjs.com/package/@cryptonly/vendure-plugin-cryptonly" icon="npm" />
- [Invoices](https://docs.cryptonly.net/guides/invoices.md): How checkout payments work

## Support

For help with your Cryptonly merchant account, credentials, or payment questions, write to **support@cryptonly.net**.
