Search by

simonet85 / laravel-saspay

Simonet85

Laravel package for the SasPay API: mobile money and card payments, payouts, wallet and signed webhooks.

Package info

github.com/simonet85/saspay-money-package

pkg:composer/simonet85/laravel-saspay

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-12-03 14:29 UTC

This package is auto-updated.

Last update: 2026-12-03 14:51:43 UTC


README

Tests

Laravel package for the SasPay API: mobile money and card payments, payouts, checkout sessions, wallet transfers and signed webhooks.

  • One method per SasPay endpoint, on a thin SasPayService behind a SasPay facade.
  • Idempotency-Key sent automatically on every money-moving request.
  • Signed webhook endpoint with raw-body HMAC verification, timestamp tolerance and delivery deduplication.
  • Optional SasPayTransaction model that follows the API status through guarded transitions (pending → processing → paid | failed | cancelled | expired).
  • Console commands to check the integration, settle open transactions and create a sandbox test payment.

Requirements

  • PHP 8.2+
  • Laravel 10, 11 or 12

Installation

composer require simonet85/laravel-saspay

The service provider and the SasPay facade are discovered automatically.

Publish the config file (and, optionally, the migration):

php artisan vendor:publish --tag=saspay-config
php artisan vendor:publish --tag=saspay-migrations
php artisan migrate

Environment

# sandbox or live — the environment is selected by the base URL, never by the key prefix
SASPAY_ENVIRONMENT=sandbox

# sk_test_... or sk_live_... (server-side secret, never in frontend code)
SASPAY_API_KEY=

# shown once in the SasPay dashboard; used to verify webhook signatures
SASPAY_WEBHOOK_SECRET=

Optional keys: SASPAY_BASE_URL (custom endpoint), SASPAY_TIMEOUT, SASPAY_RETRY_TIMES, SASPAY_ROUTE_PREFIX, SASPAY_WEBHOOK_TOLERANCE, SASPAY_WEBHOOK_DEDUPE, SASPAY_VERIFY_BEFORE_DELIVER, SASPAY_DB_CONNECTION. See config/saspay.php for the full list and their defaults.

An sk_test_ key used against the live base URL moves real money: the test/live prefix is only a label, the URL decides the environment. saspay:doctor warns on a mismatch.

Usage

use Simonet85\LaravelSasPay\Facades\SasPay;

// Mobile money payment pushed to the customer phone
$payment = SasPay::softpay([
    'amount' => '2500.00',
    'currency' => 'XOF',
    'country' => 'BJ',
    'network' => 'mtn_bj',
    'description' => 'Order #1042',
    'customer' => [
        'email' => 'buyer@example.com',
        'first_name' => 'Ada',
        'last_name' => 'Doe',
        'phone' => '+2290197234560',
    ],
]);

$checkoutUrl = $payment['checkout_url'] ?? null;

// Later, or from a queue worker
$status = SasPay::verifyPayment($payment['id']);
$payout = SasPay::verifyPayout($payoutId);

Every method maps to one documented endpoint; the full list is in the @method annotations of src/Facades/SasPay.php and in src/SasPayService.php (payments, checkout sessions, payouts, transactions and exports, payment links, balances, exchange rates, wallet transfers, countries, networks, rates, webhooks, attempts and status logs).

All money-moving calls accept an optional idempotency key as last argument:

SasPay::softpay($payload, 'order-1042-attempt-1');

Webhooks

The package registers POST {prefix}/webhook (default POST /api/saspay/webhook, outside the web middleware group so CSRF does not apply). Configure that URL in the SasPay dashboard — webhook creation and secret rotation are dashboard-only.

Each delivery is verified before the body is decoded or trusted:

  1. X-Webhook-Signature: hex HMAC-SHA256 of "{X-Webhook-Timestamp}.{raw body}", compared in constant time against the configured secret.
  2. |now − X-Webhook-Timestamp| ≤ 300 seconds (configurable).
  3. Identical deliveries are ignored for 10 minutes (configurable).
  4. transaction.* events are matched to SasPayTransaction records by provider id and applied through guarded transitions. A paid event whose amount does not match the stored amount is logged and never credited.

Responses: 200 processed or duplicate-accepted, 202 acknowledged but not linked to a known transaction, 400 malformed or stale request, 401 invalid signature.

Every delivery is acknowledged quickly; the handler never calls the SasPay API.

Events

Event When
SasPayWebhookReceived Every signature-verified delivery
SasPayPaymentSucceeded A payment or payout became paid
SasPayPaymentFailed A payment became failed, cancelled or expired
SasPayPaymentRequiresVerification Paid announced, but the configured mode requires GET /payments/{id}/verify/ first
Event::listen(SasPayPaymentSucceeded::class, function ($event) {
    $event->transaction->payable?->markPaid();
});

Verify before deliver

When SASPAY_VERIFY_BEFORE_DELIVER=true (or when the transaction row carries requires_verification), a paid webhook only moves the transaction to processing and fires SasPayPaymentRequiresVerification. The transaction is credited once a verification returns success — saspay:verify-pending does this for open rows:

php artisan saspay:verify-pending --limit=50
php artisan saspay:verify-pending --id=<provider-id>

Transaction model

Simonet85\LaravelSasPay\Models\SasPayTransaction (table saspay_transactions) stores the local mirror of a payment or payout:

  • kind (payin, payout, transfer, checkout) and status are native enums (TransactionKind, TransactionStatus).
  • transitionTo(TransactionStatus $status, array $attributes = []) applies a transition only when allowed and returns true only on a real change; repeated webhooks are therefore harmless.
  • isPaid(), isFinal(), statusValue(), amountMatches($amount) (decimal-safe).
  • Scopes open(), paid(), forPayable($model) and a payable() morph relation.
$transaction->update(['payable_type' => Order::class, 'payable_id' => $order->id]);
// or stay generic:
SasPayTransaction::query()->forPayable($order)->first();

Console commands

Command Purpose
saspay:doctor Print the environment, base URL and masked key; call a public and an authenticated endpoint; warn on key/environment mismatch or missing webhook secret
saspay:verify-pending Re-verify open transactions with the API, apply the final status and dispatch events
saspay:test-payment Create a sandbox softpay payment and print the status/checkout URL; refuses to run on live without --force

Errors

All API failures throw a subclass of Simonet85\LaravelSasPay\Exceptions\SasPayException:

Exception HTTP
SasPayAuthenticationException 401, 403
SasPayRequestException other 4xx — status(), errors() (field validation), payload(), isIdempotencyConflict()
SasPayRateLimitException 429 — retryAfter()
SasPayServerException 5xx
SasPayConnectionException transport failure
SasPayConfigurationException missing base URL or unsupported call
WebhookVerificationException invalid/stale signature — httpStatus()

GET/PUT/DELETE requests and POSTs carrying an Idempotency-Key are retried on 429, 5xx and connection errors with exponential backoff capped by SASPAY_RETRY_MAX_SLEEP. A POST without a key is never retried automatically.

Testing

composer install
composer test

The suite uses Orchestra Testbench with an in-memory SQLite database and Http::fake(): no network access and no SasPay credentials are required.

Security

  • Keep SASPAY_API_KEY and SASPAY_WEBHOOK_SECRET server-side only; never commit them.
  • Payouts requested with an API key are restricted to whitelisted server IPs — keep calls out of browser code.
  • The webhook secret is shown once (creation or rotation) in the dashboard; rotate it there and redeploy.

License

MIT. See LICENSE.