# WooCommerce plugin

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

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

The TapPay plugin for WooCommerce 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` | WordPress 6.9, WooCommerce 10.0 | PHP 8.1 |

- A WooCommerce shop with the currency set to MNT. The plugin 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 plugin 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 plugin 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 plugin is not in the WordPress.org plugin directory yet. Download it from the merchant portal.

1. In the merchant portal, open **Connections › TapPay systems › Online store** and choose **Download plugin (.zip)** for WooCommerce. You get the file `tappay-for-woocommerce.zip`.
2. In WordPress, open **Plugins > Add New Plugin > Upload Plugin**, choose the zip file, then **Install Now** and **Activate**.
3. Open **WooCommerce > Settings > Payments > TapPay**.

The plugin works with the classic checkout and with the WooCommerce Checkout block, and with High-Performance Order Storage.

## 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 plugin. The plugin 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 plugin 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 plugin, 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 plugin 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 plugin settings, copy the **Webhook URL**. It has the form `https://your-shop.example/?wc-api=tappay`, and it is the same whatever your permalink settings are.
3. In the merchant portal, open **Developers › API keys & webhooks** and create a webhook endpoint. Paste the URL exactly as the plugin shows 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 plugin settings, keep **Mode** on `sandbox`, enter the sandbox secret key and the sandbox webhook secret, and save. The plugin refuses a key with the wrong prefix, such as a `sk_live_` key in the sandbox field.
6. Choose **Check connection**. The plugin 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 plugin.

Optional fields: the **[Terminal](https://docs.tappay.mn/glossary.md#terminal) ID** (needed when your key does not belong to one terminal) and the **Request signing secret** (needed when TapPay signs requests for your account).

After you save, the plugin shows a secret only as `••••` and its last four characters. Leave a secret field empty to keep the saved value.

You can keep the secrets out of the database by defining constants in `wp-config.php`. A field with a constant is read-only in the settings.

```php
define( 'TAPPAY_SANDBOX_SECRET_KEY', 'sk_test_YOUR_SECRET_KEY' );
define( 'TAPPAY_SANDBOX_WEBHOOK_SECRET', 'whsec_YOUR_WEBHOOK_SECRET' );
define( 'TAPPAY_PRODUCTION_SECRET_KEY', 'sk_live_YOUR_SECRET_KEY' );
define( 'TAPPAY_PRODUCTION_WEBHOOK_SECRET', 'whsec_YOUR_PRODUCTION_WEBHOOK_SECRET' );
```

The constants `TAPPAY_SANDBOX_SIGNING_SECRET`, `TAPPAY_PRODUCTION_SIGNING_SECRET` and `TAPPAY_TERMINAL_ID` work in the same way.

### Go live {#go-live}

When the sandbox works, switch the plugin 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 plugin 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 moves to **Processing**. The payment id is the transaction id of the order.
4. If the order stays in **Pending payment**, the webhook has not arrived. See Troubleshooting below.

## Refunds {#refunds}

A [refund](https://docs.tappay.mn/glossary.md#refund) starts on the order: open it, choose **Refund**, enter the amount, and choose **Refund via TapPay**. It is asynchronous: WooCommerce shows the refund at once, and TapPay confirms it with the `refund.succeeded` webhook. If TapPay sends `refund.failed`, the plugin removes the refund from the order and adds an order note. When the full amount is refunded, the order becomes **Refunded**. See the [refunds guide](https://docs.tappay.mn/guides/refunds.md) for the rules of the API.

## Troubleshooting {#troubleshooting}

- **The order stays in "Pending payment".** The webhook URL in the portal does not equal the URL in the plugin settings. Copy it again, exactly, including the `?wc-api=tappay` 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).
- **"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 plugin works only with orders in MNT. Set the shop currency to MNT.
- **The webhook returns `401`.** The webhook secret is wrong, or a proxy changes the body of the request. Enter the secret of this endpoint again.
- **"Repeat the same refund".** A refund is asynchronous. After a temporary error (a timeout, or a `5xx`, `429` or `403` answer), the plugin remembers the refund. Repeat it with exactly the same amount. The plugin rejects a different amount, so that the customer is not refunded twice.
- **"An earlier TapPay refund … was never confirmed".** The plugin 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, open the order, choose the order action **TapPay: clear unconfirmed refund attempt**, and refund again.
- **"The development override is active".** The `TAPPAY_API_BASE_OVERRIDE` constant is for TapPay platform developers only. Remove it from `wp-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, and on the **Changelog** tab of the plugin page.
