PHP code example of money-pulse / money-pulse-php
1. Go to this page and download the library: Download money-pulse/money-pulse-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/ */
money-pulse / money-pulse-php example snippets
MoneyPulse\MoneyPulseClient;
$client = new MoneyPulseClient('mp_live_votre_cle_api');
// Créer un paiement
$payment = $client->payments->create([
'amount' => 10000,
'currency' => 'XOF',
'country' => 'CI',
'customer' => ['email' => '[email protected] ', 'phone' => '+22507000000'],
'callback_url' => 'https://votre-site.com/webhook',
]);
// Rediriger vers le checkout hosted
header('Location: ' . $payment['checkout_url']);
$payout = $client->payouts->create([
'amount' => 50000,
'currency' => 'XOF',
'country' => 'CI',
'recipient' => [
'type' => 'mobile_money',
'phone' => '+22507000000',
'name' => 'Jean Kouassi',
],
'description' => 'Retrait marchand #4521',
]);
echo $payout['payout_id'];
$payment = $client->payments->create([
'amount' => 10000,
'currency' => 'XOF',
'country' => 'CI',
'customer' => ['phone' => '+22507000000'],
'idempotency_key' => 'order-4521-attempt-1',
]);
$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_MONEYPULSE_SIGNATURE'] ?? '';
$secret = getenv('MP_WEBHOOK_SECRET');
$expected = hash_hmac('sha256', $payload, $secret);
if (!hash_equals($expected, $signature)) {
http_response_code(401);
exit('Signature invalide');
}
$event = json_decode($payload, true);
switch ($event['event']) {
case 'payment.succeeded':
$transactionId = $event['data']['transactionId'];
// → marquer la commande comme payée, retrouvée par $transactionId
break;
case 'payment.failed':
// → notifier le client
break;
}
http_response_code(200);
echo 'OK';
use App\Http\Controllers\MoneyPulseWebhookController;
Route::post('/webhooks/money-pulse', [MoneyPulseWebhookController::class, 'handle'])
->middleware('moneypulse.signature');
namespace App\Http\Middleware;
use Closure;
class VerifyMoneyPulseSignature
{
public function handle($request, Closure $next)
{
$expected = hash_hmac(
'sha256',
$request->getContent(),
config('services.moneypulse.webhook_secret')
);
if (!hash_equals($expected, $request->header('X-MoneyPulse-Signature', ''))) {
abort(401, 'Invalid Money-Pulse signature');
}
return $next($request);
}
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Order;
class MoneyPulseWebhookController extends Controller
{
public function handle(Request $request)
{
$event = $request->json()->all();
if ($event['event'] === 'payment.succeeded') {
$transactionId = $event['data']['transactionId'];
Order::where('money_pulse_transaction_id', $transactionId)->update([
'status' => 'paid',
'paid_amount' => $event['data']['amount'],
'paid_at' => now(),
]);
}
return response()->json(['received' => true]);
}
}
use MoneyPulse\MoneyPulseClient;
public function checkout(Order $order)
{
$client = new MoneyPulseClient(config('services.moneypulse.api_key'));
$payment = $client->payments->create([
'amount' => $order->total,
'currency' => 'XOF',
'country' => 'CI',
'customer' => ['email' => $order->customer_email, 'phone' => $order->customer_phone],
'callback_url' => route('webhooks.moneypulse'),
'return_url' => route('orders.success', $order),
]);
$order->update(['money_pulse_transaction_id' => $payment['transaction_id'] ?? $payment['id']]);
return redirect($payment['checkout_url']);
}
/*
Plugin Name: Money-Pulse for WooCommerce
*/
defined('ABSPATH') || exit;
add_action('plugins_loaded', function () {
if (!class_exists('WC_Payment_Gateway')) return;
class WC_MoneyPulse_Gateway extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'moneypulse';
$this->method_title = 'Money-Pulse';
$this->title = 'Mobile Money & Cartes';
$this->has_fields = false;
$this->init_form_fields();
$this->init_settings();
add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']);
}
public function init_form_fields() {
$this->form_fields = [
'enabled' => ['title' => 'Activer', 'type' => 'checkbox', 'default' => 'yes'],
'api_key' => ['title' => 'Clé API', 'type' => 'text'],
'webhook_secret' => ['title' => 'Webhook secret', 'type' => 'password'],
];
}
public function process_payment($order_id) {
$order = wc_get_order($order_id);
ontents('php://input');
$signature = $_SERVER['HTTP_X_MONEYPULSE_SIGNATURE'] ?? '';
$settings = get_option('woocommerce_moneypulse_settings');
$expected = hash_hmac('sha256', $payload, $settings['webhook_secret']);
if (!hash_equals($expected, $signature)) {
status_header(401); exit('bad signature');
}
$event = json_decode($payload, true);
if ($event['event'] === 'payment.succeeded') {
$transactionId = $event['data']['transactionId'];
$orders = wc_get_orders([
'meta_key' => '_moneypulse_payment_id',
'meta_value' => $transactionId,
'limit' => 1,
]);
if (!empty($orders)) {
$orders[0]->payment_complete($transactionId);
}
}
status_header(200); echo 'ok'; exit;
});
try {
$payment = $client->payments->create([...]);
} catch (\MoneyPulse\MoneyPulseException $e) {
echo $e->getMessage(); // description lisible
echo $e->getErrorCode(); // ex: 'invalid_amount', 'insufficient_balance'
echo $e->getHttpCode(); // 400, 401, 422...
}