Skip to content

moncashconnect/php-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

MonCashConnect PHP SDK

Official PHP SDK for the MonCashConnect payment platform.

Note: MonCashConnect is not affiliated with Digicel or the official MonCash service.

Requirements

  • PHP 7.4+
  • ext-curl
  • ext-json

Installation

composer require moncashconnect/php-sdk

Quick Start

use MonCashConnect\Client;
use MonCashConnect\Exception\MonCashException;

$client = new Client($_ENV['MCC_SECRET_KEY']);

try {
    $payment = $client->createPayment(1500, 'ORDER-001', [
        'return_url'    => 'https://yoursite.com/payment/success',
        'customer_name' => 'Jean Dupont',
    ]);

    // Redirect the customer to MonCash
    header('Location: ' . $payment['paymentUrl']);
    exit;

} catch (MonCashException $e) {
    echo 'Payment error: ' . $e->getMessage();
}

Your secret key starts with sk_proj_ — get it from Developer → Projects in your dashboard.

Check Payment Status

$tx = $client->getPaymentStatus('ORDER-001');

echo $tx['status'];    // "pending" | "completed" | "failed"
echo $tx['netAmount']; // Amount after commission deduction

Get Account Balance

$balance = $client->getBalance();

echo $balance['balanceHtg'];      // Total available
echo $balance['withdrawableHtg']; // Can withdraw now

Webhooks

Configure a webhook URL in your project. MonCashConnect sends a signed POST when a payment is finalized.

Always read the raw body before any json_decode().

use MonCashConnect\Webhook;
use MonCashConnect\Exception\MonCashException;

$rawBody   = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_MCC_SIGNATURE'] ?? '';
$timestamp = $_SERVER['HTTP_X_MCC_TIMESTAMP'] ?? '';

try {
    $event = Webhook::constructEvent(
        $rawBody,
        $signature,
        $timestamp,
        $_ENV['MCC_WEBHOOK_SECRET']
    );
} catch (MonCashException $e) {
    http_response_code($e->getCode() ?: 400);
    exit($e->getMessage());
}

switch ($event['event']) {
    case 'payment.completed':
        // Mark order as paid in your database
        markOrderPaid($event['reference']);
        break;

    case 'payment.failed':
        // Handle failure
        markOrderFailed($event['reference']);
        break;
}

http_response_code(200);
echo 'OK';

Laravel

// routes/api.php
Route::post('/webhooks/moncash', [MonCashWebhookController::class, 'handle']);

// app/Http/Controllers/MonCashWebhookController.php
public function handle(Request $request): Response
{
    $event = Webhook::constructEvent(
        $request->getContent(),
        $request->header('X-MCC-Signature', ''),
        $request->header('X-MCC-Timestamp', ''),
        config('services.moncash.webhook_secret')
    );

    if ($event['event'] === 'payment.completed') {
        Order::where('reference', $event['reference'])->update(['status' => 'paid']);
    }

    return response('OK');
}

Error Handling

catch (MonCashException $e) {
    $e->getMessage();  // "referenceId already exists for this project"
    $e->getCode();     // HTTP status: 400, 401, 409, 429, 502…
    $e->getContext();  // Full API response array, or null
}

License

MIT

About

PHP SDK for MonCashConnect — MonCash payment integration for Haiti

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages