💳 Payment Gateway API Specification

Centralized microservice integration for Billing, Communities, and ERP systems

Admin Portal →

1. Architecture Overview

To avoid duplicating 3rd party payment gateway SDKs (PayPal, NewebPay) and managing manual bank transfers across multiple repositories, client applications delegate checkout sessions to this microservice.

  1. Authenticate: Request machine-to-machine OAuth Bearer token via POST /oauth/token.
  2. Initiate: Call POST /api/v1/checkout/sessions with order details, currency, and allowed methods.
  3. Redirect: Redirect user to the hosted checkout_url.
  4. Settle: User completes payment via PayPal, NewebPay, or submits Bank Wire remittance slip.
  5. Webhook: Payment Gateway dispatches HMAC-SHA256 signed event to the client application's webhook listener.
  6. Return: User is redirected back to the initiator app's success_url.

2. OAuth 2.0 Machine-to-Machine Handshake

POST /oauth/token

Obtain a 24-hour machine-to-machine token using Client Credentials Grant (RFC 6749, Section 4.4):

Headers:

Content-Type: application/x-www-form-urlencoded

Request Body (Form URL-Encoded):

grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&scope=payments

Response (200 OK):

{ "access_token": "pg_at_5a8f2c...", "token_type": "Bearer", "expires_in": 86400, "scope": "payments" }

Supported Scopes: payments (default), payments:checkout, payments:refund, payments:platforms:read, currencies:read.
Note: OAuth scopes are immutable code constants (OAuthController::SUPPORTED_SCOPES) representing fixed platform capabilities, not dynamic environment variables.

3. List Enabled Payment Providers for Client

GET /api/v1/providers or /api/v1/payment-methods

Query the staff-authorized payment providers and supported submethods available for your registered client application:

Headers:

Authorization: Bearer <access_token>

Response (200 OK):

{ "status": "success", "data": { "client_id": "erp_app", "client_name": "ERP Platform", "providers": [ { "id": "paypal", "name": "PayPal / International Cards", "description": "Pay securely with Visa, MasterCard, American Express or PayPal balance.", "icon": "🅿️", "supports_submethods": false, "submethods": [] }, { "id": "newebpay", "name": "NewebPay (藍新金流)", "description": "Taiwan credit cards, ATM virtual accounts, and CVS convenience store codes.", "icon": "🇹🇼", "supports_submethods": true, "submethods": [ { "code": "CREDIT", "name": "Credit Card (信用卡)" }, { "code": "ATM", "name": "ATM Virtual Account (虛擬帳號)" }, { "code": "CVS", "name": "CVS Barcode (超商代碼)" } ] }, { "id": "bank_transfer", "name": "Bank Wire / Remittance (銀行匯款)", "description": "Corporate domestic or international wire transfer with deposit slip upload.", "icon": "🏦", "supports_submethods": false, "submethods": [] } ], "allowed_methods": ["paypal", "newebpay", "bank_transfer"] } }

4. Create Checkout Session

POST /api/v1/checkout/sessions or /api/v1/payments/create-session

Headers:

Authorization: Bearer <access_token> Content-Type: application/json X-Client-App: billing

Request Body:

{ "client_reference_id": "ORG-1042-SUB-2026-001", "organization_id": "1042", "organization_name": "Acme Global Logistics S.L.", "customer_email": "admin@acme.com", "customer_name": "Juan Perez", "currency": "EUR", "amount": 29.00, "billing_cycle": "monthly", "plan_code": "plan_professional", "plan_name": "Professional Tier - Multi-Tenant Billing", "allowed_methods": ["paypal", "newebpay", "bank_transfer"], "preselected_method": "newebpay", "preselected_submethod": "CREDIT", "bank_transfer_notes": "Por favor transfiera el monto exacto de EUR 29.00 indicando su ID de Remesa en el concepto de la transferencia bancaria.", "language": "es", "metadata": { "rfq_id": "RFQ-88219", "po_id": "PO-2026-1042", "transaction_type": "procurement_order" }, "success_url": "https://billing.ehehdada.com/dashboard/organization/subscription?status=success&session_id={CHECKOUT_SESSION_ID}", "cancel_url": "https://billing.ehehdada.com/dashboard/organization/subscription?status=cancelled&session_id={CHECKOUT_SESSION_ID}", "webhook_url": "https://billing.ehehdada.com/api/webhooks/payment?session_id={CHECKOUT_SESSION_ID}" }
⚙️ Staff-Managed Allowed Payment Methods & Preselection:
Payment methods available to end-users during checkout are centrally configured and governed by our financial and operations staff through the Payment Gateway Admin Portal GUI.
  • allowed_methods (Optional): If omitted, all staff-authorized methods for your registered application are enabled. If provided, the intersection of requested and authorized methods will be available.
  • preselected_method (Optional): Pass "paypal", "newebpay", or "bank_transfer" to automatically pre-select that payment provider on the hosted checkout UI, avoiding extra user clicks.
  • preselected_submethod (Optional): For NewebPay, pass "CREDIT", "ATM", or "CVS" to pre-select the submethod.
  • metadata (Optional): Custom key-value map (e.g. rfq_id, po_id, category). Persisted with the session and mirrored back in all signed webhook callbacks.
⚠️ Disclaimer regarding session_id={CHECKOUT_SESSION_ID} in URLs:
The query parameter session_id={CHECKOUT_SESSION_ID} shown in the success_url, cancel_url, and webhook_url examples above is not an ID required by this payment gateway microservice itself. It is a convenience parameter intended for the integrating client application (e.g. Billing, ERP, Communities) for its own internal state tracking and session correlation upon redirection. When the gateway redirects the customer or invokes the webhook, it will interpolate {CHECKOUT_SESSION_ID} with the real session ID (e.g. cs_live_...), allowing your application to immediately identify and resolve the payment session without ambiguity.

Response (201 Created):

{ "status": "success", "data": { "session_id": "cs_live_9b7c8e9f2a1b4c3d", "checkout_url": "https://paymentgateway.lucentinian.com/checkout/cs_live_9b7c8e9f2a1b4c3d", "expires_at": "2026-09-24T16:30:00Z", "amount": 29.00, "currency": "EUR", "client_reference_id": "ORG-1042-SUB-2026-001", "preselected_method": "newebpay", "metadata": { "rfq_id": "RFQ-88219", "po_id": "PO-2026-1042", "transaction_type": "procurement_order" } } }

5. Webhook Notification & Signature Verification

When a payment state transitions, the Payment Gateway dispatches a cryptographically signed HTTP POST request to your webhook_url.

Webhook Request Headers:

POST /api/webhooks/payment Content-Type: application/json X-Gateway-Signature: 8f3c7e4d89a2b5e1c0d4f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7 X-Gateway-Timestamp: 1790242200

Payload Schema:

{ "event_id": "evt_01J8F9W2A7B4C5D6E7", "event_type": "payment.succeeded", "created_at": "2026-09-24T14:35:12Z", "data": { "session_id": "cs_live_9b7c8e9f2a1b4c3d", "initiator_request_id": "ORG-1042-SUB-2026-001", "client_reference_id": "ORG-1042-SUB-2026-001", "client_app": "billing", "organization_id": "1042", "payment_method": "newebpay", "payment_reference": "NEWEB_2026092414300099", "amount": 29.00, "currency": "EUR", "status": "paid", "billing_cycle": "monthly", "metadata": { "rfq_id": "RFQ-88219", "po_id": "PO-2026-1042" } } }

PHP Verification Snippet for Client Applications:

<?php $rawBody = file_get_contents('php://input'); $signature = $_SERVER['HTTP_X_GATEWAY_SIGNATURE'] ?? ''; $timestamp = (int)($_SERVER['HTTP_X_GATEWAY_TIMESTAMP'] ?? 0); $webhookSecret = 'YOUR_SHARED_WEBHOOK_SECRET'; // 1. Replay attack guard (5 minutes window) if (abs(time() - $timestamp) > 300) { http_response_code(401); exit('Timestamp expired'); } // 2. Compute expected HMAC $expectedSignature = hash_hmac('sha256', $timestamp . '.' . $rawBody, $webhookSecret); // 3. Constant-time comparison if (!hash_equals($expectedSignature, $signature)) { http_response_code(403); exit('Invalid webhook signature'); } // Signature is verified! Process event: $event = json_decode($rawBody, true); if ($event['event_type'] === 'payment.succeeded') { // Unlock organization and advance due date }

6. Query Session Status (By Session ID or Client Reference)

Allows client applications to query real-time session status using either the microservice session_id or your internal client_reference_id:

GET /api/v1/checkout/sessions/{id_or_reference}

GET /api/v1/checkout/sessions/by-reference/{reference}

Headers:

Authorization: Bearer <access_token>

Response (200 OK):

{ "status": "success", "data": { "session_id": "cs_live_9b7c8e9f2a1b4c3d", "client_reference_id": "ORG-1042-SUB-2026-001", "organization_id": "1042", "payment_method": "newebpay", "payment_submethod": "CREDIT", "preselected_method": "newebpay", "metadata": { "rfq_id": "RFQ-88219" }, "amount": 29.00, "currency": "EUR", "gateway_transaction_id": "NEWEB_2026092414300099", "status": "paid", "paid_at": "2026-09-24T14:35:10Z", "created_at": "2026-09-24T14:30:00Z" } }

7. Client API: Report Completed Bank Wire Transfer

When a customer notifies your customer support or mobile app that they have completed a bank wire transfer, your application can directly report the payment to the gateway.

POST /api/v1/checkout/sessions/{id}/bank-transfer

Headers:

Authorization: Bearer <access_token> Content-Type: application/json

Request Body:

{ "remittance_reference": "WT-260924-88412", "transfer_date": "2026-09-24", "bank_name": "Bank of Taiwan, Taipei Branch", "account_last5": "99887", "wire_reference_id": "DBS-TXN-776192", "notes": "Customer confirmed wire transfer via customer support ticket #4812" }

Response (200 OK):

{ "status": "success", "message": "Bank transfer remittance reported successfully. Transaction placed in financial verification queue.", "data": { "session_id": "cs_live_9b7c8e9f2a1b4c3d", "client_reference_id": "ORG-1042-SUB-2026-001", "remittance_reference": "WT-260924-88412", "status": "pending_verification", "amount": 29.00, "currency": "EUR" } }

* Note: Calling this API moves the session to pending_verification and dispatches a signed payment.pending_verification webhook so your application can immediately grant a grace period to the user while our financial team verifies the bank statement.

8. Client API: Update Taiwanese Uniform Invoice (統一發票)

To attach or update the Taiwanese government invoice / GUI number (統一發票號碼) issued for a payment session.

POST /api/v1/checkout/sessions/{id}/invoice

Headers:

Authorization: Bearer <access_token> Content-Type: application/json

Request Body:

{ "government_invoice_number": "AB-12345678", "invoice_title": "Acme Global Logistics S.L.", "invoice_address": "123 Business Boulevard, Taipei 110, Taiwan" }

Response (200 OK):

{ "status": "success", "message": "Taiwanese government invoice updated successfully.", "data": { "session_id": "cs_live_9b7c8e9f2a1b4c3d", "client_reference_id": "ORG-1042-SUB-2026-001", "government_invoice_number": "AB-12345678", "invoice_title": "Acme Global Logistics S.L.", "invoice_address": "123 Business Boulevard, Taipei 110, Taiwan", "status": "paid" } }

* Note: Updates trigger an invoice.updated signed webhook and record the invoice number into the central financial audit ledger.

9. Client API: Cancel Checkout Session

Allows client applications to explicitly cancel an unpaid or expired checkout session initialized from the client side.

POST /api/v1/checkout/sessions/{id}/cancel

Headers:

Authorization: Bearer <access_token> Content-Type: application/json

Request Body (Optional):

{ "reason": "Customer cancelled order or changed subscription tier" }

Response (200 OK):

{ "status": "success", "message": "Checkout session cancelled successfully.", "data": { "session_id": "cs_live_9b7c8e9f2a1b4c3d", "client_reference_id": "ORG-1042-SUB-2026-001", "status": "cancelled", "reason": "Customer cancelled order or changed subscription tier" } }

* Note: Dispatches a signed payment.cancelled webhook to inform your system. Sessions that are already paid cannot be cancelled.

10. Client API: Programmatic Refund

Allows client applications (e.g. ERP procurement, Billing admin) to issue a programmatic full or partial refund on a settled, paid checkout session:

POST /api/v1/checkout/sessions/{id}/refund

Headers:

Authorization: Bearer <access_token> Content-Type: application/json

Request Body (Optional):

{ "amount": 29.00, "reason": "Tenant requested cancellation within money-back guarantee window" }

Response (200 OK):

{ "status": "success", "message": "Payment refunded successfully.", "data": { "session_id": "cs_live_9b7c8e9f2a1b4c3d", "client_reference_id": "ORG-1042-SUB-2026-001", "status": "refunded", "refund_amount": 29.00, "currency": "EUR", "refund_transaction_id": "REF_9A8B7C6D5E", "reason": "Tenant requested cancellation within money-back guarantee window" } }

* Note: Automatically calls upstream PayPal / NewebPay refund APIs and dispatches a signed payment.refunded webhook to your application.

11. Client API: List Supported Currencies

Retrieves the comprehensive catalog of supported fiat and cryptocurrency codes with descriptive names. Automatically synchronized from upstream with a 1-hour cache and resilient fallback if upstream is temporarily unreachable:

GET /api/v1/currencies

Headers:

Authorization: Bearer <access_token>

Response (200 OK):

{ "status": "success", "data": { "count": 185, "currencies": { "USD": "United States Dollar", "EUR": "Euro", "TWD": "New Taiwan Dollar", "JPY": "Japanese Yen", "GBP": "British Pound Sterling", "CAD": "Canadian Dollar", "AUD": "Australian Dollar", "SGD": "Singapore Dollar" } } }

12. Client API: USD Exchange Rates

Retrieves latest exchange rates based on USD (1 USD = X target currency). Sibling applications can query all rates or specify a target query parameter:

GET /api/v1/currencies/rates

GET /api/v1/currencies/rates?target=TWD

Headers:

Authorization: Bearer <access_token>

Response (200 OK - All Rates):

{ "status": "success", "data": { "base": "USD", "count": 187, "rates": { "USD": 1.0, "TWD": 31.9245, "EUR": 0.8887, "JPY": 158.63, "GBP": 0.7461 } } }

Response (200 OK - Filtered by target):

{ "status": "success", "data": { "base": "USD", "target": "TWD", "rate": 31.924501 } }

13. Client API: Currency Conversion Calculator

Calculates real-time currency conversions for a list of values (such as pricing plans, tier add-ons, or procurement milestones) from a base currency (defaulting to USD) to the user's or organization's preferred currency. Automatically applies appropriate rounding and zero-decimal rules (e.g. TWD, JPY, KRW are rounded to whole numbers without decimal places):

POST /api/v1/currencies/convert

Headers:

Authorization: Bearer <access_token> Content-Type: application/json

Request Body:

{ "from": "USD", "to": "TWD", "values": [29, 99, 149.50] }

Response (200 OK):

{ "status": "success", "data": { "from": "USD", "to": "TWD", "rate": 31.924501, "is_zero_decimal": true, "conversions": [ { "original": 29, "converted": 926, "formatted": "926" }, { "original": 99, "converted": 3161, "formatted": "3161" }, { "original": 149.5, "converted": 4773, "formatted": "4773" } ] } }

* Note: Sibling projects can display plan pricing in USD by default while providing instant localized price conversions to customers in their preferred billing currency.

14. Client API: 3rd-Party Payment Gateways Integration Status

Query the real-time operational integration status of upstream third-party payment gateways (PayPal, NewebPay, Bank Wire Transfer) configured in the central payment microservice. Sibling applications and Super Admin dashboards can poll this endpoint to verify health, connectivity, and configuration readiness:

GET /api/v1/platforms/status or /api/v1/gateways/status

Headers:

Authorization: Bearer <access_token>

Required OAuth Scope: payments:platforms:read or default payments

Response (200 OK):

{ "status": "success", "data": { "platforms": [ { "platform": "paypal", "name": "PayPal", "status": "connected", "description": "PayPal international payment gateway is configured and ready." }, { "platform": "newebpay", "name": "NewebPay", "status": "connected", "description": "NewebPay (藍新金流) payment gateway is configured and ready." }, { "platform": "bank_transfer", "name": "Bank Wire Transfer", "status": "connected", "description": "Bank wire remittance payment gateway is configured and ready." } ] } }

* Status values: connected (configured, authenticated & ready), not_configured (missing credentials), disconnected (deactivated for this client application), expired (requires reauthorization), error (upstream connectivity failure or invalid credentials).