# Magento 2 (beta)

> **Beta.** The `OnCharge_Payments` module has passed its library tests (request signing, webhook verification, the compliance protocol test vectors, paging, carriers, amounts) but has not yet run on a live store. Its [README](https://oncharge.io/docs/files/magento/README.md) is the full reference. Contact OnCharge to join the beta.

The module adds OnCharge as a payment method to Magento Open Source and Adobe Commerce **2.4.4 or later** (PHP 8.1+). It creates the checkout session for each order with its shipping address, shows the OnCharge checkout by redirect or embedded in a store page, updates orders from signed webhooks, reports shipment tracking so delivered orders are paid out, refunds online from credit memos, and serves the [RUO compliance feed](https://oncharge.io/docs/compliance-feed).

## Install

The module is a Composer package (`oncharge/module-payments`). Add the package source OnCharge gives you as a Composer repository, then:

```bash
composer require oncharge/module-payments:^1.0
bin/magento module:enable OnCharge_Payments
bin/magento setup:upgrade
bin/magento setup:di:compile                  # production mode only
bin/magento setup:static-content:deploy -f    # production mode only
bin/magento cache:flush
```

Magento's cron must run: the module uses it to process webhooks and resend pending tracking numbers.

## Configure

**Stores → Configuration → Sales → Payment Methods → OnCharge**

| Setting | |
|---|---|
| Enabled, Title, Description | What shoppers see at checkout. |
| API Key / API Secret | From the OnCharge portal. The secret is stored encrypted and never reaches the browser. |
| Environment | Production (`https://app.oncharge.io`). Use a custom base URL only if OnCharge support asks. |
| Payment Action | **Sale**: the order is invoiced when OnCharge confirms the payment. **Authorize**: the payment is recorded and you create the invoice (Capture Online checks with OnCharge that the payment was captured). |
| Checkout Display | **Redirect** to the OnCharge checkout page, or **Embedded** on a payment page in your store using the [hosted script](https://oncharge.io/docs/checkout#script-tag). |
| Callback Signing Secret | Register `https://<your store>/oncharge/webhook` as the store's webhook URL in the OnCharge portal and paste the `oc_cbsec_…` secret it shows once. |
| Countries, order total limits, sort order, debug logging | Standard payment method options. |

Register your store's domain under **Security → Domains** in the OnCharge portal: OnCharge accepts return URLs and framing only on registered domains.

## How it works

| In Magento | With OnCharge |
|---|---|
| The customer places the order | The order is created as **Pending Payment**. |
| The browser goes to `/oncharge/checkout/start` | [`POST /api/v1/checkout/sessions`](https://oncharge.io/docs/api#createcheckoutsession) with the amount, the increment ID as `order_id`, the shipping address and line items, and an idempotency key; then the redirect or the embedded page. |
| The shopper returns to `/oncharge/checkout/return` | The session status picks the page to show (success, or the cart restored). It never changes the order. |
| OnCharge calls `/oncharge/webhook` | `OnCharge-Signature` is verified (legacy header only when it is absent) and event IDs are deduplicated. `payment.captured` invoices the order (Sale) or records the authorization (Authorize) after checking amount and currency; `payment.refused` and `payment.cancelled` cancel it; `refund.succeeded` confirms refunds; `chargeback.opened` puts the order on hold; `delivery.confirmed` adds a comment. |
| A shipment tracking number is saved | [`POST /api/v1/shipments`](https://oncharge.io/docs/shipments); Magento carrier codes map to USPS, UPS, FedEx or DHL Express. The result is added as an order comment, and pending numbers are resent by cron every 15 minutes. |
| An online credit memo | [`POST /api/v1/refunds`](https://oncharge.io/docs/api#createrefund) with an idempotency key. |

## RUO stores: compliance review

The same settings page has a **Compliance Review** group:

1. In the OnCharge portal, add your store host, choose **Magento** and set the base path to `/oncharge/compliance`. Paste the one-time connection code (`occs_…`) into **Connection Code** and save.
2. Select **Verify connection** in the portal.
3. Set up reviewer access: a separate customer account with the same access a verified buyer has. Enter its email, sign in as it once to check that it shows every product with its price, then set **I Signed In as This Account** to **Yes**.

The feed serves active CMS pages and every enabled product with its own page (including products hidden from search), up to 5,000 items, at:

```text
GET /oncharge/compliance/proof?challenge=<hex64>&ts=<unix>
GET /oncharge/compliance/content?cursor=<n>&limit=<n>      (signed)
GET /oncharge/compliance/reviewer-access                    (signed)
```

## Building your own Magento integration

If you prefer to integrate yourself, follow [Custom & headless](https://oncharge.io/docs/headless): create the session from your payment method's server-side code, render the checkout with the [script tag](https://oncharge.io/docs/checkout#script-tag), handle webhooks in a CSRF-exempt controller and report tracking from a `sales_order_shipment_track_save_after` observer. The [PHP signing example](https://oncharge.io/docs/authentication#php) works as-is.
