# Paymenter Plugin

> Accept stablecoins and XAUT in Paymenter v1.x with One-Click Connect Paymos, Laravel app.key encryption, 10-minute cron sync, and Filament refunds.

## 1. Requirements

- Paymenter 1.x (Laravel / Filament) with a public **HTTPS** `APP_URL` (plain HTTP is rejected by Connect Paymos)
- PHP 8.2+ 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-paymenter.zip`) from the Paymos dashboard or [GitHub](https://github.com/Paymos-labs/paymenter) once published

Official packages are identical for every merchant and carry zero API keys, API secrets, project IDs, webhook secrets, OAuth tokens, or device codes. `paymos/php-sdk` travels bundled under `Paymos/vendor/paymos/php-sdk/`, so no Composer command is needed on the Paymenter host. 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-paymenter.zip` from your Paymos project panel or [GitHub](https://github.com/Paymos-labs/paymenter).
2. Extract the archive into `extensions/Gateways/` so the extension sits at `extensions/Gateways/Paymos/` (`Paymos.php`, `routes.php`, `src/`, `resources/views/`, `lang/`, and `vendor/paymos/php-sdk/`).
3. Clear Laravel caches if route, view, or config caching is active:

   ```bash
   php artisan route:clear
   php artisan view:clear
   php artisan config:clear
   ```

4. In **Paymenter Admin → Extensions → Gateways**, enable **Paymos**.

## 3. Connect

1. Open the target project inside the Paymos dashboard. Connect Paymos uses that current project automatically with no second selector.
2. Signed in as an administrator with `admin.gateways.update` permission, open **Extensions → Gateways → Paymos** in Paymenter and click **Connect Paymos**.
3. Verify the displayed panel URL, user code, and active project in Paymos, then approve the connection.
4. Return to Paymenter and wait for the **Connected** status.

One approval via RFC 8628 OAuth Device Flow provisions both **Sandbox** and **Live** credentials (`pk_*`, `sk_*`, `prj_*`, `whsec_*`), enables `invoice_refunds_v1`, and registers Paymenter's webhook endpoint without overwriting conflicting webhooks:

```text
https://your-panel.example/extensions/gateways/paymos/webhook
```

Connect and refund admin routes enforce both CSRF middleware and Filament role permissions (`admin.gateways.update` and `admin.invoices.update`), rejecting regular customer accounts with HTTP `403`. The short-lived device token is discarded after setup; 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 from Laravel's `config('app.key')` and stored in `paymos_settings`. No manual API key, API secret, project ID, or webhook secret inputs exist in the Filament gateway settings, and stored secrets are never rendered back into HTML or logs.

## 5. Test and go live

Keep **Mode** on **Sandbox**, open a test invoice, run a simulated payment, and confirm Paymenter records it via `ExtensionHelper::addPayment()` and triggers service provisioning. Switch **Mode** to **Live** on the same extension when ready; reconnect with **Reconnect Paymos** only after changing `APP_URL`, revoking a Payment key, or rotating a webhook secret.

During checkout, the extension persists a crash-safe `STATUS_CREATING` intent, bills `$invoice->remaining` so partially credited invoices charge the exact balance due, forwards up to 50 valid items from `$invoice->items` in `products[]` (falling back to amount-only when unavailable), and runs fail-closed `InvoiceReplacement` if totals or mode change.

Incoming webhooks on `/extensions/gateways/paymos/webhook` are verified across Sandbox and Live by `MultiEnvironmentWebhookVerifier` (dual-`v1=` rotation grace, 300-second window, atomic `200`/`409`/`401` dedup), reverse-verified against `GET /v1/invoices/{id}`, validated by `AmountGuard`, and protected against status rollback. `invoice.paid_late` is recorded as a note without auto-crediting. `Paymos::boot()` registers a 10-minute Laravel `Schedule` job (`InvoiceReconciler`, throttled to 600 seconds) that sweeps up to 50 unpaid invoices from the last 24 hours and reconciles 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.

Administrators with `admin.invoices.update` permission manage both modes from the Filament refund panel on each paid invoice or trigger a `full` refund via `Gateway::refund()`. Because Paymenter's `ExtensionHelper::refund()` books a local refund immediately whenever `Gateway::refund()` returns `true`, `Paymos::refund()` never returns `true` while a Paymos refund is `pending`, `processing`, or `unknown`. Once `GET /v1/refunds/{id}` and `GET /v1/invoices/{id}` verify `completed` (via `withdrawal.completed` or scheduled sync), Paymenter records the refund once via `ExtensionHelper::refundLocally()` for `full` refunds, or adds a note while keeping the invoice paid for `overpayment` refunds. Refunds started in the Paymos dashboard are discovered and reconciled automatically by `RefundDiscovery`.

[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 **Paymenter** to review the official package details and webhook callback path.

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

2. In Paymenter Admin, open **Extensions → Gateways → Paymos** and click **Connect Paymos**.

   ![Paymenter admin gateway settings showing Paymos and the Connect Paymos button](/img/guides/cms/admin-paymenter.webp)

3. Review the panel 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 customer pays a Paymenter invoice, Paymos opens the hosted checkout where they select a supported stablecoin or XAUT and network.

   ![Hosted Paymos checkout page showing asset and network selection for a Paymenter 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 Paymenter invoice paid.

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