# OpenCart plugin

> Install TapPay for OpenCart, set the keys and the webhook, test in the sandbox, and refund orders.

Source: https://docs.tappay.mn/no-code/plugins/opencart/

The TapPay extension for OpenCart opens a hosted [checkout session](https://docs.tappay.mn/glossary.md#checkout-session) for each order and marks the order as paid when TapPay confirms the [payment](https://docs.tappay.mn/glossary.md#payment). If you prefer to connect your shop with code, follow the [checkout guide](https://docs.tappay.mn/guides/checkout.md) instead.

## Requirements {#requirements}

| Plugin version | Minimum platform | Minimum runtime |
| - | - | - |
| `0.1.0` | OpenCart 4.1.0.0 | PHP 8.1 |

- An OpenCart 4.1.0.x shop with PHP 8.1 or newer, and the currency set to MNT. The extension offers TapPay only for orders in MNT.
- A merchant account in TapPay, with at least one [terminal](https://docs.tappay.mn/glossary.md#terminal). With [one-click connect](#connect), the extension gets its key and webhook for you. For [manual setup](#manual-setup), you need a [sandbox](https://docs.tappay.mn/glossary.md#sandbox) [secret key](https://docs.tappay.mn/glossary.md#secret-key). The key needs the `checkout:write`, `payments:write` and `refunds:write` [scopes](https://docs.tappay.mn/glossary.md#scope). The `webhooks:manage` scope is optional: with it, the extension can also check your [webhook endpoint](https://docs.tappay.mn/glossary.md#webhook-endpoint).
- A public HTTPS address for your shop, so that TapPay can send webhooks to it.

## Install {#install}

The extension is not in the OpenCart marketplace yet. Download it from the merchant portal.

1. In the merchant portal, open **Connections › TapPay systems › Online store** and choose **Download plugin (.zip)** for OpenCart. You get the file `tappay.ocmod.zip`.
2. In the OpenCart admin, open **Extensions > Installer**, choose **Upload** and select the file, then choose **Install** for TapPay.
3. Open **Extensions > Extensions**, choose the type **Payments**, find **TapPay**, and choose **Install**.
4. Choose **Edit** to open the settings.

## Configure {#configure}

### Connect in one click {#connect}

1. In the TapPay settings, choose **Connect to TapPay** for the `Sandbox` mode.
2. The merchant portal opens. Sign in if it asks. The `Sandbox` mode opens the sandbox portal, and the `Production` mode opens the [production](https://docs.tappay.mn/glossary.md#production) portal.
3. Check the site address on the page. The connection belongs to one terminal: if your business has several, choose one.
4. Choose **Approve**. In production, the portal may ask for two-step verification first. After it, choose **Approve** again.
5. The portal sends you back to the extension. The extension saves the keys and shows **Connected**.

The connection creates a secret key with the `checkout:write`, `payments:write`, `refunds:write` and `webhooks:manage` scopes. It also creates a webhook endpoint with the [events](https://docs.tappay.mn/glossary.md#event) that the extension needs. To approve, you need the permissions to manage API keys and webhooks.

To go live, choose **Connect to TapPay** for the `Production` mode, then switch **Mode** to `Production`. The production connection is separate from the sandbox one.

To disconnect, choose **Disconnect** in the extension, or open the system under **Developers › API keys & webhooks** in the merchant portal and choose **Disable**. Both revoke the key and disable the webhook endpoint.

### Manual setup (fallback) {#manual-setup}

Use these steps when you cannot use one-click connect. In the TapPay settings, the fields are under **Manual setup**.

1. Sign in to the merchant portal in the sandbox and create a secret key. Your sandbox key looks like `sk_test_YOUR_SECRET_KEY`, and it starts with `sk_test_`.
2. In the TapPay settings, copy the **Webhook URL**. It has the form `https://your-shop.example/index.php?route=extension/tappay/payment/tappay.webhook`.
3. In the merchant portal, open **Developers › API keys & webhooks** and create a webhook endpoint. Paste the URL exactly as the settings show it. Do not change the scheme, host, path or query. Choose these [events](https://docs.tappay.mn/glossary.md#event): `payment.succeeded`, `payment.overpaid`, `payment.failed`, `payment.processing`, `invoice.expired`, `invoice.partially_paid`, `refund.succeeded` and `refund.failed`.
4. Copy the [webhook secret](https://docs.tappay.mn/glossary.md#webhook-secret) of the endpoint. It starts with `whsec_` and the portal shows it once.
5. In the settings, keep **Mode** on `Sandbox`, enter the sandbox secret key and the sandbox webhook secret, and save. The extension refuses a key with the wrong prefix, such as a `sk_live_` key in the sandbox field.
6. Choose **Check connection**. The extension reads your account with the key. If the key has the `webhooks:manage` scope, it also looks for an active webhook endpoint with exactly the URL of the extension.

Optional fields: the **[Terminal](https://docs.tappay.mn/glossary.md#terminal) ID** is needed when your key does not belong to one terminal. The **Request signing secret** is needed when TapPay signs requests for your account. You can also choose the order statuses for paid, pending, failed, cancelled and refunded orders.

After you save, the extension shows a secret only as its last characters. Leave a secret field empty to keep the saved value.

### Go live {#go-live}

When the sandbox works, switch the extension to [production](https://docs.tappay.mn/glossary.md#production). The [go-live](https://docs.tappay.mn/go-live.md) checklist applies:

- Create a production secret key, and enter it in the production field.
- Create a **separate** production webhook endpoint with the same URL and the same events. Enter its own webhook secret. The sandbox secret does not work in production.
- Add the outgoing IP address of your server to the system's IP restriction under **Developers › API keys & webhooks** in the merchant portal.
- If your account uses request signing, enter the request signing secret.
- Switch **Mode** to `Production`, save, and choose **Check connection**.

The extension does not take the page that the [customer](https://docs.tappay.mn/glossary.md#customer) sees after paying as proof of payment. It sends an `Idempotency-Key` with every request, so you do nothing more for those two points.

## Test in sandbox {#test-in-sandbox}

1. Add a product to the cart and choose **TapPay** at checkout.
2. On the hosted page, pay with the [test card](https://docs.tappay.mn/glossary.md#test-card) `4242 4242 4242 4242`. The [testing](https://docs.tappay.mn/testing.md) page lists more cards, and the [simulator](https://docs.tappay.mn/glossary.md#simulator) lets you create other outcomes.
3. After the redirect to your shop, the order gets the paid status. The payment id appears in the TapPay section of the order.
4. If the order stays in the pending status, the webhook has not arrived. See Troubleshooting below.

## Refunds {#refunds}

A [refund](https://docs.tappay.mn/glossary.md#refund) starts in the **TapPay** section of the order page in **Sales > Orders**, because OpenCart has no core refund API. The section shows the amount paid, the amount refunded (including pending refunds) and the amount you can still refund. Enter the amount, and choose **Refund via TapPay**. It is asynchronous: TapPay confirms the refund with the `refund.succeeded` webhook. If TapPay sends `refund.failed`, the extension removes the refund from the total and adds a note to the order history. When the full amount is refunded, the order gets the refunded status. See the [refunds guide](https://docs.tappay.mn/guides/refunds.md) for the rules of the API.

## Troubleshooting {#troubleshooting}

- **The order stays in the pending status.** The webhook URL in the portal does not equal the URL in the settings. Copy it again, exactly, including the `?route=extension/tappay/payment/tappay.webhook` query. Also check that the endpoint has the events listed above, and that the webhook secret is the one of this endpoint and this mode. See the [webhooks guide](https://docs.tappay.mn/guides/webhooks.md).
- **The webhook returns `401`.** The [signature](https://docs.tappay.mn/glossary.md#signature) does not match. The webhook secret is wrong, or a proxy, a redirect or a changed URL alters the request. Enter the secret of this endpoint again.
- **"Invalid key" or error `EVT-206` or `EVT-207`.** The key belongs to the other mode. Use a `sk_test_` key and a sandbox endpoint in `Sandbox` mode, and a `sk_live_` key in `Production` mode.
- **TapPay is not offered at checkout.** The extension works only with orders in MNT, and only when the selected mode has a valid secret key and webhook secret. Set the shop currency to MNT and complete the settings.
- **The refund shows an error with `503`, a timeout, `429` or `403`.** A refund is asynchronous. After a temporary error, the extension remembers the refund. Repeat it with exactly the same amount. The extension rejects a different amount, so that the customer is not refunded twice.
- **"An earlier refund attempt was not confirmed by TapPay".** The extension repeats a remembered refund only within 23 hours, because TapPay keeps an [idempotency key](https://docs.tappay.mn/glossary.md#idempotency-key) for 24 hours. After that it stops, so that the customer is not refunded twice. Check the payment in the TapPay dashboard. If the refund is not there, choose **Clear unconfirmed attempt** in the TapPay section of the order, and refund again.
- **"Development override".** The `TAPPAY_API_BASE_OVERRIDE` constant is for TapPay platform developers only. Remove it from `config.php`.

## Changelog {#changelog}

The latest version is shown in the table in Requirements. The full history is in the `CHANGELOG.md` file of the plugin repository.
