PHP code example of mobupay / mobupay-php

1. Go to this page and download the library: Download mobupay/mobupay-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mobupay / mobupay-php example snippets


use Mobupay\MobupayClient;

// Clé test (sandbox) ou live. 2e argument optionnel = base API (défaut : https://api.mobupay.nc)
$client = new MobupayClient('sk_test_xxx');

$session = $client->createCheckoutSession(
    ['reference' => 'CMD-1042', 'amount' => 2500, 'currency' => 'EUR'], // montant en centimes
    'https://maboutique.nc/commande/merci',   // retour client
    'https://maboutique.nc/?wc-api=mobupay',  // URL webhook
    ['externalId' => '1042'],                 // id de commande boutique (clé de rapprochement)
    'CMD-1042'                                 // Idempotency-Key (anti double-paiement)
);

header('Location: ' . $session['checkoutUrl']); // rediriger le client

use Mobupay\Webhook;
use Mobupay\MobupayException;

$payload = file_get_contents('php://input'); // CORPS BRUT, non re-décodé
$secret  = 'whsec_xxx'; // = $client->getSigningSecret(), à récupérer/stocker une fois

try {
    $event = Webhook::verify($payload, getallheaders(), $secret);
} catch (MobupayException $e) {
    http_response_code(403);
    exit;
}

// $event = ['id', 'type', 'createdAt', 'data' => ['paymentId', 'externalId', 'status', 'amount', 'currency', ...]]
$orderId = $event['data']['externalId']; // votre id de commande
switch ($event['type']) {
    case 'payment.captured':
    case 'payment.authorized':
        // marquer la commande payée
        break;
    case 'payment.failed':
    case 'payment.cancelled':
    case 'payment.expired':
        // marquer la commande échouée / abandonnée
        break;
    case 'payment.refunded':
    case 'payment.partially_refunded':
        // refléter le remboursement
        break;
}
http_response_code(200);

$client->refund('pay_xxx');        // total
$client->refund('pay_xxx', 1000);  // partiel : 10,00 EUR (centimes)

$secret = $client->getSigningSecret(); // 'whsec_...'
bash
composer