# FOSSBilling Plugin

> Accept stablecoins and XAUT in FOSSBilling 0.7+ and 2.0 with One-Click Connect Paymos, a fixed Guest API webhook, cron sync, and async-safe refunds.

## 1. Requirements

- FOSSBilling `0.7.x` (RedBean) or `0.8+ / 2.0` (Doctrine) with a public **HTTPS** URL in `config.php` (plain HTTP is rejected by Connect Paymos)
- PHP 8.1+ with the `curl`, `hash`, `json`, `openssl`, and `pdo` extensions
- A non-empty `salt` configured in FOSSBilling's `config.php`
- A Paymos account with the target project selected in the Paymos dashboard
- The official release archive (`paymos-fossbilling.zip`) from the Paymos dashboard or [GitHub](https://github.com/Paymos-labs/fossbilling) 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` is bundled under `modules/Paymos/vendor/`, so Composer is not required on your server. Clients can pay invoices 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-fossbilling.zip` from your Paymos project panel or [GitHub](https://github.com/Paymos-labs/fossbilling).
2. Extract the archive over your FOSSBilling root directory. It places the module in `modules/Paymos/` and the payment adapter in `library/Payment/Adapter/Paymos/`.
3. Open **Extensions → Overview** in FOSSBilling admin and activate the **Paymos** module.
4. Open **System → Payment Gateways → New Payment Gateway**, install **Paymos**, and enable it.

## 3. Connect

1. Open the target project inside the Paymos dashboard. Connect Paymos uses that current project automatically with no second selector.
2. In FOSSBilling admin, open **Extensions → Paymos** and click **Connect Paymos**.
3. Check the displayed FOSSBilling URL, verification code, and active project in Paymos, then approve the binding.
4. Return to FOSSBilling 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 FOSSBilling's fixed Guest API webhook route without overwriting any conflicting endpoint:

```text
https://your-billing.example/index.php?_url=/api/guest/paymos/webhook
```

Because this route sits on `modules/Paymos/` instead of `ipn.php`, it works without pretty URLs and never depends on an internal numeric `gateway_id`. The device authorization token is discarded after setup, and runtime Merchant API calls stay HMAC-SHA256 signed.

## 4. Secret storage

Provisioned Sandbox and Live credentials and temporary device flow state are sealed in an AES-256-GCM envelope (`AesGcmEnvelope`) keyed via HKDF-SHA256 from FOSSBilling's `config.php` `salt`. No manual credential or webhook secret inputs exist in the admin UI, and stored secrets are never rendered back into Twig views, API responses, or logs.

## 5. Test and go live

Keep **Mode** on **Sandbox**, create a test client invoice, complete a simulated payment, and confirm FOSSBilling credits it through `addFunds` and marks the invoice paid. Switch **Mode** to **Live** on the same connected installation when ready; reconnect with **Reconnect Paymos** only after changing your installation URL, revoking a Payment key, or rotating a webhook secret.

Before creating an invoice, the adapter stores a crash-safe `STATUS_CREATING` intent, sends up to 50 valid positive invoice lines in `products[]` (falling back to amount-only when unavailable), and runs fail-closed `InvoiceReplacement` if the total, currency, mode, or project changes.

Incoming webhooks hit `/index.php?_url=/api/guest/paymos/webhook`, read raw `php://input`, emit real HTTP status codes (`200`/`401`/`409`/`400`/`500`), verify signatures across Sandbox and Live via `MultiEnvironmentWebhookVerifier` (dual-`v1=` rotation, 300-second window), reverse-verify terminal states via `GET /v1/invoices/{id}`, enforce `AmountGuard`, and guard paid invoices from rollback. `invoice.paid_late` is logged strictly as a manual-review note. FOSSBilling's cron listener (`onAfterAdminCronRun`) runs `InvoiceReconciler` (throttled to every 10 minutes) to sweep up to 50 unpaid invoices over 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 addresses or networks are rejected before any Paymos API call.

Administrators with refund permissions manage both modes under **Invoices → Paymos Refunds** (`/admin/paymos/refunds`) or via FOSSBilling 2.0's native `refundPayment()` and `listRefundsForTransaction()` hooks. Pending, processing, or unknown refunds never post a premature debit. Only after `GET /v1/refunds/{id}` and `GET /v1/invoices/{id}` confirm `completed` (via `withdrawal.completed` or cron) does FOSSBilling project a `full` refund once via FOSSBilling 2.0 `external_reversal` (with a safe 0.7 fallback), while an `overpayment` refund logs a completion note and leaves the invoice paid. Refunds initiated directly in the Paymos dashboard are detected by `RefundDiscovery` and reconciled without issuing 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 **FOSSBilling** to review the official package details and fixed webhook path.

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

2. In FOSSBilling admin, open **Extensions → Paymos** and click **Connect Paymos**.

   ![FOSSBilling admin Paymos extension page showing the Connect Paymos button](/img/guides/cms/admin-fossbilling.webp)

3. Review the FOSSBilling URL, verification code, and selected Paymos project, then approve the One-Click connection so Paymos delivers Sandbox and Live credentials and registers the 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 FOSSBilling 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 FOSSBilling 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 credits and closes the FOSSBilling invoice.

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