Documentation menu

Adding a payment gateway

The kit ships with Stripe for online payments and cash on completion. Bookings, refunds and provider payouts only talk to the PaymentGateway interface, so you can add another provider without touching the booking code. Razorpay, PayPal, Paystack, Tap, Midtrans and similar providers all fit.

A developer who knows the provider's API should get a working test-mode gateway in about an hour.

How payments flow

App                       API (Laravel)                          Gateway
───                       ─────────────                          ───────
Book (deposit/full) ──▶   booking: awaiting_payment
"Pay" ──────────────▶     POST bookings/{code}/payment-intent
                            PaymentService::checkout ─────────▶  checkout(): create payment
                    ◀──   { gateway, amount, …your data… }
Pay in the provider's UI ─────────────────────────────────────▶  (customer pays)
                          POST webhooks/{gateway}  ◀───────────  webhook
                            parseWebhook() → GatewayEvent[]
                            payment succeeded → provider sees the request
"Done" ─────────────▶     POST bookings/{code}/payment/sync
                            fetch() → GatewayResult (if the webhook is late)
Cancel / dispute          RefundService → refund()
Job confirmed             payouts:release → transfer() (if supported)

1. Write the class

Create backend/app/Services/Payments/Gateways/AcmeGateway.php:

<?php

namespace App\Services\Payments\Gateways;

use App\Models\Booking;
use App\Models\Payment;
use App\Models\Payout;
use App\Models\User;
use App\Services\Settings;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class AcmeGateway implements PaymentGateway
{
    public function __construct(private readonly Settings $settings) {}

    public function key(): string
    {
        return 'acme';
    }

    public function available(): bool
    {
        return $this->settings->filled('integrations.acme_secret_key');
    }

    public function checkout(Payment $payment, Booking $booking, User $customer): array
    {
        // Reuse the provider's payment if we already created one for this row.
        if (! $payment->external_id) {
            $res = Http::withToken($this->settings->string('integrations.acme_secret_key'))
                ->withHeaders(['Idempotency-Key' => "payment-{$payment->id}"])
                ->post('https://api.acme.test/v1/payments', [
                    'amount' => $payment->amount,          // minor units
                    'currency' => $payment->currency,
                    'reference' => $booking->code,
                    'callback_url' => url('/api/v1/webhooks/acme'),
                ])->throw()->json();
            $payment->external_id = $res['id'];        // saved by PaymentService
        }

        // Whatever your app screen needs: a hosted-page URL, an SDK token, …
        return ['redirect_url' => "https://pay.acme.test/{$payment->external_id}"];
    }

    public function fetch(Payment $payment): GatewayResult
    {
        $p = Http::withToken($this->settings->string('integrations.acme_secret_key'))
            ->get("https://api.acme.test/v1/payments/{$payment->external_id}")->throw()->json();

        return match ($p['status']) {
            'captured' => GatewayResult::paid($p['id'], $p['card_token'] ?? null),
            'failed' => GatewayResult::failed($p['error'] ?? 'Payment failed', $p['id']),
            default => new GatewayResult(GatewayResult::PENDING, $p['id']),
        };
    }

    public function chargeSaved(Payment $payment, Payment $source, Booking $booking): GatewayResult
    {
        // No saved cards? Return failed: the customer then pays the balance in the app.
        return GatewayResult::failed('Not supported');
    }

    public function refund(Payment $payment, int $amount, string $idempotencyKey): ?string
    {
        $r = Http::withToken($this->settings->string('integrations.acme_secret_key'))
            ->withHeaders(['Idempotency-Key' => $idempotencyKey])
            ->post("https://api.acme.test/v1/payments/{$payment->external_id}/refunds", ['amount' => $amount])
            ->throw()->json();

        return $r['id'];
    }

    public function parseWebhook(Request $request): ?array
    {
        $expected = hash_hmac('sha256', $request->getContent(), (string) $this->settings->string('integrations.acme_webhook_secret'));
        if (! hash_equals($expected, (string) $request->header('X-Acme-Signature'))) {
            return null; // → 400
        }

        $e = $request->json()->all();

        return match ($e['event']) {
            'payment.captured' => [new GatewayEvent($e['id'], GatewayEvent::PAYMENT_SUCCEEDED, $e['payment_id'])],
            'payment.failed' => [new GatewayEvent($e['id'], GatewayEvent::PAYMENT_FAILED, $e['payment_id'], message: $e['reason'] ?? null)],
            default => [],
        };
    }

    public function supportsTransfers(): bool
    {
        return false; // pay providers manually, or implement transfer() below
    }

    public function transfer(Payout $payout, Booking $booking, string $idempotencyKey): ?string
    {
        return null;
    }

    public function reverseTransfer(Payout $payout, int $amount, string $idempotencyKey): void {}
}

2. Register it

In backend/config/payments.php:

'gateways' => [
    'stripe' => StripeGateway::class,
    'cash' => CashGateway::class,
    'acme' => AcmeGateway::class,
],

3. Settings

  1. Add the keys to backend/config/settings.php. Put secrets in the encrypted list.
  2. Add their validation to app/Services/SettingsSchema.php under integrations.
  3. Add the fields to admin/src/features/settings/integrations-tab.tsx, and add the gateway to the Online payments select.
  4. Choose it in Admin → Settings → Integrations → Online payments. GET /api/v1/config then returns payments.gateway: "acme", and card bookings use it.

4. The app

POST bookings/{code}/payment-intent returns gateway plus whatever your checkout() returned. In mobile/lib/features/payments/:

  1. Add a PaymentSheetGateway implementation (see StripePaymentSheet) that takes the customer through your provider, e.g. by opening redirect_url in an in-app browser tab or calling the provider's SDK.
  2. Pick the implementation by gateway in the DI setup.
  3. After the customer returns, the app calls payment/sync, so fetch() must report the real state.

Checklist

  • checkout() reuses $payment->external_id, so opening checkout twice never creates two charges.
  • Every money call sends the idempotency key you were given (or payment-{id}).
  • parseWebhook() returns null for a bad signature, and event ids are unique. The platform handles each id once.
  • fetch() maps the provider's statuses to paid, processing, pending or failed.
  • refund() supports partial amounts.
  • Amounts are minor units. Check zero-decimal currencies such as JPY.
  • Test mode keys only in development; secrets in the encrypted settings.
  • Add a feature test, faking the provider's HTTP API with Http::fake(). tests/Feature/Api/Booking/CashAndGatewaysTest.php registers a FakeGateway this way and pays, confirms and refunds a booking through it.

Cash on completion

Cash is its own gateway (CashGateway): no checkout, webhooks or transfers.

  1. The customer picks Pay cash on completion. It is offered when Settings → Business → Cash payments is on.
  2. The booking goes to the provider right away.
  3. After the job, the provider taps Cash received. The platform's commission on the job is then recorded as owed by the provider (provider_balances). It is also recorded automatically when the job is confirmed, if the provider never tapped it.
  4. Providers see the amount owed on Earnings. Admins see it on the provider page and in Payments → Owed commission, where Mark settled records how it was paid.