E-commerce example
Complete walkthrough: integrate GerminoPay into a small online shop with order management.
Scenario
- Customer adds items to cart on your shop
- Clicks "Checkout" → backend creates an order (status: pending)
- Backend calls GerminoPay → gets payment_url
- Customer is redirected to pay
- Webhook confirms payment → order marked as paid
Backend code (PHP)
checkout.php
<?php
session_start();
require "GerminoPayClient.php";
$client = new GerminoPayClient(
publicKey: getenv("GERMINOPAY_PUBLIC"),
secretKey: getenv("GERMINOPAY_SECRET")
);
// 1. Créer la commande en BDD
$panier = $_SESSION["panier"] ?? [];
$total = array_sum(array_column($panier, "prix"));
$reference = "CMD-" . time();
$pdo->prepare("INSERT INTO commandes (reference, total, statut) VALUES (?, ?, ?)")
->execute([$reference, $total, "en_attente"]);
// 2. Initier le paiement GerminoPay
$payment = $client->initierPaiement([
"montant" => $total,
"devise" => "XOF",
"fournisseur" => "auto",
"reference" => $reference,
"client_email" => $_SESSION["client_email"],
"return_url" => "https://monsite.com/paiement/succes?ref=" . $reference,
"cancel_url" => "https://monsite.com/paiement/annule?ref=" . $reference,
]);
// 3. Rediriger vers le paiement
header("Location: " . $payment["data"]["payment_url"]);
exit;
Frontend code (HTML/JS)
<div class="panier"> <p>Total : <strong>5 000 F CFA</strong></p> <a href="/checkout.php" class="btn-payer">Passer commande</a> </div>
Webhook handler
webhook_germinopay.php
<?php
// URL configurée dans le tableau de bord marchand
$secret = getenv("GERMINOPAY_SECRET");
$rawBody = file_get_contents("php://input");
$timestamp = $_SERVER["HTTP_X_GERMINOPAY_TIMESTAMP"] ?? "";
$signature = $_SERVER["HTTP_X_GERMINOPAY_SIGNATURE"] ?? "";
// Vérifier la signature
$expected = "sha256=" . hash_hmac("sha256", $timestamp . "." . $rawBody, $secret);
if (!hash_equals($expected, $signature)) {
http_response_code(401); exit;
}
if (abs(time() - (int)$timestamp) > 300) {
http_response_code(400); exit;
}
$payload = json_decode($rawBody, true);
$reference = $payload["data"]["reference"];
switch ($payload["event"]) {
case "payment.succeeded":
$pdo->prepare("UPDATE commandes SET statut=?, paye_le=NOW() WHERE reference=?")
->execute(["payee", $reference]);
// Optionnel : envoyer un email de confirmation
break;
case "payment.failed":
$pdo->prepare("UPDATE commandes SET statut=? WHERE reference=?")
->execute(["echouee", $reference]);
break;
}
http_response_code(200);
echo json_encode(["received" => true]);