Skip to main content

AdjeminPay PHP SDK

Latest Stable Version Total Downloads Latest Unstable Version License

The AdjeminPay PHP SDK provides convenient access to the AdjeminPay API from applications written in the PHP language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the AdjeminPay API

Requirements#

PHP 5.6.0 and later.

Composer#

You can install the bindings via Composer. Run the following command:

composer require adjeminpay/adjeminpay-php-sdk

Use case#

<?php
use AdjeminPay\AdjeminPay;
use AdjeminPay\Transaction;
//Create AdjeminPay instance
$clientId = "CLIENT_ID"; //Client ID of an application created on Merchant backoffice
$clientSecret = "CLIENT_SECRET"; //Client Secret of an application created on Merchant backoffice
$adjeminPay = new AdjeminPay($clientId, $clientSecret);
//Make Payment transaction
/** @var Transaction $transaction Transaction*/
$transaction = $adjeminPay->createTransaction([
'merchant_transaction_id' => 'b72e51dc-7211-4e85-a937-5372c8769d36', //required You create a merchant_transaction_id
'designation' => 'Test', //required
'currency_code' => 'XOF', //required
'buyer_name' => 'Ange Bagui', //required
'notification_url' => 'https://adjemin.com', //required
'payment_method_reference' => 'MTN_CI', //required //Enum = ["MTN_CI", "ORANGE_CI"]
'buyer_reference' => '2250556888385', //required
'amount' => '10', //required
'otp' => '' //used when payment_method_reference is ORANGE_CI
]);
var_dump($transaction);
//Get Transaction Status by merchant_transaction_id
/** @var Transaction $transaction Transaction*/
$merchant_transaction_id = 'b72e51dc-7211-4e85-a937-5372c8769d36';
$transaction = $adjeminPay->getTransactionStatus($merchant_transaction_id);
var_dump($transaction);

Capture the payment notification in your backend#

Do you remember the parameter notification_url YOUR NOTIFICATION URL ?

You must now implement it in your backend to be notified of the progress of the payment and update your database.

When you initiate a transaction you provide a notification URL to which we notify you of the outcome of the transaction to allow you to have the details of the transaction.

AdjeminPay makes an HTTP request to your NOTIFICATION URL with the POST method and the following parameters in BODY:

The Content-Type of the request is application/x-www-form-urlencoded

  • transaction_id : the transaction reference provided by the merchant (you)
  • status : the payment status
  • amount : the amount of the transaction
  • phone_number : the phone number provided by the user making the payment
  • buyer_name : user's name
  • buyer_reference :the phone number or reference provided by the user making the payment
  • currency_code : currency
  • cancelled_at : if the transaction is canceled, the date on which the transaction is canceled
  • approuved_at : if the transaction is approved, the date the transaction is approved
  • transaction_type : the payment method used ORANGE_CI or MTN_CI

Code PHP#

Example of management of notifications from AdjeminPay

hook.php
<?php
public function notifyAdjeminPay(Request $request)
{
$input = $request->all();
$transaction_reference = $input['transaction_id'];
$status = $input['status'];
if(empty($transaction_reference)){
return response()->json([
'error' => [
'message' => "Transaction not found",
'transaction_reference' => $transaction_reference
],
]);
}
// Recuperation de la ligne de la transaction dans votre base de données
$transaction = InvoicePayment::where(['payment_reference' => $transaction_reference])->first();
if(empty($transaction)){
return response()->json([
'error' => [
'message' => "Transaction not found",
'transaction_reference' => $transaction_reference
],
]);
}
$invoice = Invoice::where(["id" => $transaction->invoice_id])->first();
$responseData = null;
if($invoice->status == 'UNPAID'){
if ($transaction != null) {
switch ($status) {
case 'SUCCESSFUL' :
$transaction->status = 'SUCCESSFUL';
$transaction->is_waiting = false;
$transaction->is_completed = true;
$transaction->save();
$this->validateTransaction($transaction_reference);
break;
case 'FAILED' :
$transaction->status = 'FAILED';
$transaction->is_waiting = false;
$transaction->is_completed = true;
$transaction->save();
$this->cancelTransaction($transaction_reference);
break;
case 'CANCELLED' :
$transaction->status = 'CANCELLED';
$transaction->is_waiting = false;
$transaction->is_completed = true;
$transaction->save();
$this->cancelTransaction($transaction_reference);
break;
case 'EXPIRED' :
$transaction->status = 'EXPIRED';
$transaction->is_waiting = false;
$transaction->is_completed = true;
$transaction->save();
break;
default:
return response()->json([
'error' => [
'message' => 'MISSING_TRANSACTION_STATUS'
],
]);
break;
}
}
}
?>