PHP code example of cinetpay-dev / cinetpay-php

1. Go to this page and download the library: Download cinetpay-dev/cinetpay-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/ */

    

cinetpay-dev / cinetpay-php example snippets




use CinetPay\CinetPay;
use CinetPay\Country;
use CinetPay\Currency;
use CinetPay\Language;
use CinetPay\Request\CreatePaymentRequest;

$cinetpay = CinetPay::sandbox(
    apiKey: $_ENV['CINETPAY_API_KEY'],
    apiPassword: $_ENV['CINETPAY_API_PASSWORD'],
    country: Country::IvoryCoast,
);

$payment = $cinetpay->payments()->create(new CreatePaymentRequest(
    currency: Currency::XOF,
    merchantTransactionId: 'ORDER-'.bin2hex(random_bytes(8)),
    amount: 500,
    successUrl: 'https://shop.example.com/payment/success',
    failedUrl: 'https://shop.example.com/payment/failed',
    notifyUrl: 'https://shop.example.com/webhooks/cinetpay',
    language: Language::French,
    designation: 'Commande #42',
    clientFirstName: 'Jane',
    clientLastName: 'Doe',
    clientEmail: '[email protected]',
));

header('Location: '.$payment->paymentUrl);

use CinetPay\CinetPay;
use CinetPay\Country;

$ivoryCoast = CinetPay::production(
    apiKey: $_ENV['CINETPAY_CI_API_KEY'],
    apiPassword: $_ENV['CINETPAY_CI_API_PASSWORD'],
    country: Country::IvoryCoast,
);

$senegal = CinetPay::production(
    apiKey: $_ENV['CINETPAY_SN_API_KEY'],
    apiPassword: $_ENV['CINETPAY_SN_API_PASSWORD'],
    country: Country::Senegal,
);

$payment = $cinetpay->payments()->find('ORDER-42');

if ($payment->isSuccessful()) {
    // Le paiement est confirmé par l'API CinetPay.
}

use CinetPay\Request\CreateTransferRequest;

$transfer = $cinetpay->transfers()->create(new CreateTransferRequest(
    currency: Currency::XOF,
    merchantTransactionId: 'PAYOUT-'.bin2hex(random_bytes(8)),
    phoneNumber: '+2250707000001',
    amount: 500,
    paymentMethod: 'OM_CI',
    reason: 'Remboursement commande #42',
    notifyUrl: 'https://shop.example.com/webhooks/cinetpay-transfer',
));

$currentTransfer = $cinetpay->transfers()->find($transfer->transactionId);

$balance = $cinetpay->balances()->get();

echo $balance->availableBalance.' '.$balance->currency;

$confirmed = $cinetpay->webhooks()->handlePayment(
    payload: $requestBody, // Tableau associatif ou corps JSON brut
    expectedNotifyToken: $notifyTokenStoredAtInitialization,
);

if ($confirmed->isSuccessful()) {
    // Le statut SUCCESS vient de l'API CinetPay, jamais du webhook.
}

$confirmed = $cinetpay->webhooks()->handleTransfer(
    payload: $requestBody,
    expectedNotifyToken: $notifyTokenStoredAtInitialization,
);

if ($confirmed->isFinal()) {
    // SUCCESS, FAILED ou autre statut final documenté.
}

$key = $confirmed->notification->deduplicationKey();

use CinetPay\Exception\ApiException;
use CinetPay\Exception\AuthenticationException;
use CinetPay\Exception\TransportException;
use CinetPay\Exception\ValidationException;

try {
    $balance = $cinetpay->balances()->get();
} catch (AuthenticationException $exception) {
    // Identifiants invalides ou jeton refusé après renouvellement.
} catch (ValidationException $exception) {
    // HTTP 422 : $exception->apiCode, apiStatus et response sont disponibles.
} catch (TransportException $exception) {
    // Timeout, DNS ou erreur réseau.
} catch (ApiException $exception) {
    // Autre erreur HTTP/API.
}

interface TokenStore
{
    public function get(string $accountKey): ?AccessToken;
    public function put(string $accountKey, AccessToken $token): void;
    public function forget(string $accountKey): void;
}
bash
composer