# Blesta Plugin

> Accept stablecoins and XAUT in Blesta 5 and 6 with One-Click Connect Paymos, encrypted keys, 10-minute cron reconciliation, and async-safe refunds.

## 1. Requirements

- Blesta 5.x or 6.x with a public **HTTPS** installation URL (plain HTTP is rejected by Connect Paymos)
- PHP 8.1+ with the `curl`, `hash`, `json`, `openssl`, and `pdo` extensions
- A Paymos account with the target project selected in the Paymos dashboard
- The official release archive (`paymos-blesta.zip`) from the Paymos dashboard or [GitHub](https://github.com/Paymos-labs/blesta) once published

Official packages are identical for every merchant and contain no API keys, API secrets, project IDs, webhook secrets, OAuth tokens, or device codes. The Paymos PHP SDK (`paymos/php-sdk`) travels bundled inside the archive, so no Composer step is needed on your server. Clients can pay in four stablecoins (**USDT**, **USDC**, **USD1**, **DAI**) and gold-backed **XAUT** across 13 mainnet networks.

## 2. Install

1. Once the public release is available, download `paymos-blesta.zip` from your Paymos project panel or [GitHub](https://github.com/Paymos-labs/blesta).
2. Extract the archive into your Blesta web root. It installs both `components/gateways/nonmerchant/paymos/` (the non-merchant payment gateway with bundled `vendor/paymos/php-sdk/`) and `plugins/paymos/` (the companion plugin for cron reconciliation and staff admin routes).
3. In Blesta admin, install both components:
   - **Settings → Company → Payment Gateways → Available → Paymos → Install**
   - **Settings → Company → Plugins → Available → Paymos Companion → Install**

## 3. Connect

1. Open the project you want to bill into inside the Paymos dashboard. The plugin binds that active project automatically and never asks for a second project selector.
2. In Blesta admin, open **Settings → Company → Payment Gateways → Paymos → Manage** and click **Connect Paymos**.
3. Confirm the displayed Blesta URL, verification code, and active project in the Paymos tab, then approve the connection.
4. Return to Blesta and wait for the **Connected** status.

One approval via RFC 8628 OAuth Device Flow provisions both **Sandbox** and **Live** Payment API credentials (`pk_*`, `sk_*`, `prj_*`, `whsec_*`), enables the `invoice_refunds_v1` capability, and registers the dedicated Invoice webhook without overwriting any conflicting endpoint:

```text
https://your-billing.example/callback/gw/1/paymos/
```

One-Click Connect binds Blesta's primary company path (`company_id = 1`); multi-company installations operating under `company_id != 1` are not provisioned by One-Click Connect. The short-lived device token is used once and discarded; runtime Merchant API requests stay HMAC-SHA256 signed.

## 4. Secret storage

Provisioned Sandbox and Live credentials and temporary device flow state are sealed in an authenticated AES-256-GCM envelope (`AesGcmEnvelope`) keyed via HKDF from `Configure::get('Blesta.system_key')` and stored in `paymos_settings`. No manual API key, API secret, project ID, or webhook secret inputs exist in the gateway UI, and saved secrets are never rendered back into HTML or logs.

## 5. Test and go live

Keep **Mode** on **Sandbox**, open a test client invoice, complete a simulated checkout, and confirm Blesta marks the invoice paid and runs your service provisioning rules. Switch **Mode** to **Live** on the same installation when ready; reconnect with **Reconnect Paymos** only after changing your domain URL, revoking a Payment key, or rotating a webhook secret.

During checkout, the gateway persists a crash-safe `STATUS_CREATING` intent before calling `POST /v1/invoices`, forwards up to 50 valid Blesta invoice lines in `products[]` (falling back to amount-only when lines are unavailable), and runs fail-closed `InvoiceReplacement` before issuing a replacement if totals or settings change.

Incoming callbacks are verified across Sandbox and Live by `MultiEnvironmentWebhookVerifier` (dual-`v1=` rotation grace, 300-second window, atomic `200`/`409`/`401` deduplication), reverse-verified against `GET /v1/invoices/{id}`, checked by `AmountGuard`, and protected against status rollback. `invoice.paid_late` is recorded as a manual-review note without altering payment status. The companion plugin's 10-minute cron task (`paymos_reconcile`) runs `InvoiceReconciler` to sweep up to 50 unpaid invoices from the last 24 hours and reconcile pending refunds.

## Refunds and overpayments

Paymos supports server-calculated **full** refunds and **overpayment** (`paid_over`) refunds sent as outbound blockchain transfers strictly to the verified `first_sender` address. Arbitrary partial amounts and manual recipient fields are rejected before any Paymos API call.

Staff with admin permissions manage both modes under **Paymos Refunds** (`/admin/plugin/paymos/admin_refunds/`) or trigger a `full` refund through Blesta's native `Paymos::refund()` hook. Because on-chain refunds settle asynchronously, `Paymos::refund()` never records a premature accounting debit while a refund is `pending`, `processing`, or `unknown`. Only after `GET /v1/refunds/{id}` and `GET /v1/invoices/{id}` verify `completed` (via the `withdrawal.completed` webhook or cron) does Blesta project the refund once via `Transactions::edit` for `full` refunds, or log an overpayment note while leaving the invoice paid for `overpayment` refunds. Refunds started directly in the Paymos dashboard are picked up automatically by `RefundDiscovery` without creating duplicates.

[Refund rules and limitations](/docs/support/refunds.md).

## Quick visual guide {#quick-visual-guide}

1. Open your project's **CMS** section in the Paymos dashboard and select **Blesta** to review the official package details and webhook callback path.

   ![Paymos dashboard CMS view with Blesta selected and its webhook callback path](/img/guides/cms/panel-blesta.webp)

2. In Blesta admin, open **Settings → Company → Payment Gateways → Paymos → Manage** and click **Connect Paymos**.

   ![Blesta Payment Gateways settings showing Paymos and the Connect Paymos button](/img/guides/cms/admin-blesta.webp)

3. Review the Blesta URL, verification code, and selected Paymos project, then approve the One-Click connection so Paymos delivers Sandbox and Live credentials and registers the invoice webhook automatically.

   ![Paymos One-Click connection approval screen with verification code, URL, and active project](/img/guides/cms/02-connect-approval.webp)

4. When a client pays a Blesta invoice, Paymos opens the hosted checkout where they pick a supported stablecoin or XAUT and network.

   ![Hosted Paymos checkout page showing asset and network selection for a Blesta invoice](/img/guides/cms/03-store-checkout.webp)

5. Once the on-chain transfer confirms, the paid invoice appears under **Payments** in Paymos and the verified webhook marks the Blesta invoice paid.

   ![Confirmed invoice details in Paymos Payments after on-chain settlement](/img/guides/cms/04-order-paid-dashboard.webp)
