PHP Library for the MyFatoorah API
The preferred way to install this library is through composer.
Either run
$ composer require bawes/myfatoorah-php
or add
"bawes/myfatoorah-php": "^1.0"
to the require section of your application's composer.json
file.
First we need to decide which environment we want to use
<?php
use bawes/myfatoorah/MyFatoorah;
$my = MyFatoorah::test();
<?php
use bawes/myfatoorah/MyFatoorah;
$merchantCode = "[Your merchant code here]";
$username = "[Your merchant username here]";
$password = "[Your merchant password here]";
$my = MyFatoorah::live($merchantCode, $username, $password);
<?php
use bawes/myfatoorah/MyFatoorah;
$merchantCode = "[Your merchant code here]";
$username = "[Your merchant username here]";
$password = "[Your merchant password here]";
$my = MyFatoorah::live($merchantCode, $username, $password);
$my->setPaymentMode(MyFatoorah::GATEWAY_ALL)
->setReturnUrl("https://google.com")
->setErrorReturnUrl("https://google.com")
->setCustomer("Khalid", "[email protected]", "97738271")
->setReferenceId() //Pass unique order number or leave empty to use time()
->addProduct("iPhone", 5.350, 3)
->addProduct("Samsung", 12.000, 1)
->getPaymentLinkAndReference();
$paymentUrl = $my['paymentUrl'];
$myfatoorahRefId = $my['paymentRef']; //good idea to store this for later status checks
// Redirect to payment url
header("Location: $paymentUrl");
die();
Use MyFatoorah::getOrderStatus($referenceId)
to get an update on the status of the payment.
This is best called after receiving a callback from MyFatoorah's returnUrl or errorReturnUrl.
You can also manually call this function after an interval if you store the reference id locally.
<?php
use bawes/myfatoorah/MyFatoorah;
// Example Ref ID
$myfatoorahRefId = $_GET['id'];
// Order status on Test environment
$orderStatus = MyFatoorah::test()
->getOrderStatus($myfatoorahRefId);
// Order status on Live environment
$merchantCode = "[Your merchant code here]";
$username = "[Your merchant username here]";
$password = "[Your merchant password here]";
$orderStatus = MyFatoorah::live($merchantCode, $username, $password)
->getOrderStatus($myfatoorahRefId);
<?php
$orderStatus = [
'responseCode' => '0', //MyFatoorah::REQUEST_SUCCESSFUL
'responseMessage' => 'SUCCESS',
'result' => 'CAPTURED',
// Successful payment fields
'payMode' => 'KNET',
'orderId' => '1085183',
'payTransactionId' => '673386261283050',
'grossAmountPaid' => '32.500',
'netAmountToBeDeposited' => '32.300',
// User defined fields
'udf1' => '',
'udf2' => '',
'udf3' => '',
'udf4' => '',
'udf5' => ''
]
<?php
$orderStatus = [
'responseCode' => '2009',
'responseMessage' => 'Transaction Failed Messages',
'result' => 'Payment Server detected an error',
// User defined fields
'udf1' => '',
'udf2' => '',
'udf3' => '',
'udf4' => '',
'udf5' => ''
]
Configure the gateway you wish to use by passing GATEWAY constants available on the MyFatoorah
class to MyFatoorah::setPaymentMode
.
MyFatoorah::GATEWAY_ALL
- Generated link sends to MyFatoorah page with all payment methodsMyFatoorah::GATEWAY_KNET
- Generated link sends user directly to KNET portalMyFatoorah::GATEWAY_VISA_MASTERCARD
- Generated link sends user directly to VISA/MASTER portalMyFatoorah::GATEWAY_SAUDI_SADAD
- Generated link sends user directly to Sadad Saudi portalMyFatoorah::GATEWAY_BAHRAIN_BENEFIT
- Generated link sends user directly to BENEFIT BAHRAIN portalMyFatoorah::GATEWAY_QATAR_QPAY
- Generated link sends user directly to Qpay Qatar portalMyFatoorah::GATEWAY_UAECC
- Generated link sends user directly to UAE debit cards portal
Usage Example:
<?php
use bawes/myfatoorah/MyFatoorah;
$my = MyFatoorah::live($merchantCode, $username, $password);
$my->setPaymentMode(MyFatoorah::GATEWAY_ALL)
These cards will only work if you initialize using MyFatoorah::test()
environment.
Card Number | Pin/Expiry | Result |
---|---|---|
8888880000000001 | anything | CAPTURED |
8888880000000002 | anything | NOT CAPTURED |
Card Number | Expiry Date | Pin | Result |
---|---|---|---|
2222220123456789 | 12/27 | 1234 | CAPTURED |
7777770123456789 | 12/27 | 1234 | NOT CAPTURED |
1111110123456789 | 12/27 | 1234 | NOT CAPTURED |
Card Number | Expiry Date | CVV |
---|---|---|
4005550000000001 | 05/18 | 123 |
4557012345678902 | 05/18 | 123 |
Card Number | Expiry Date | CVV |
---|---|---|
5123456789012346 | 05/18 | 123 |
5313581000123430 | 05/18 | 123 |
Card Number | Expiry Date | Pin |
---|---|---|
345678901234564 | 05/17 | 1234 |
Payment Method | Card Number | Expiry Date | CVV |
---|---|---|---|
Mastercard | 5271045423029111 | anything | anything |
Visa | 4012001037141112 | 01/2022 | 684 |
Payment Method | Account ID | Password |
---|---|---|
Sadad account | arun123 | Aa123456 |