PHP Library 2.1
Installation Steps
Install the PHP library package via myfatoorah/library composer by running the below command:
composer require myfatoorah/library:2.1.*
Library as a Zip File
If your web application does not support the composer package, be free to download the library as a zip file from here.
Online Toolkit
For the best usage of the library, you can explore the PHP Toolkit. Moreover, You can download it and host it on your server to examine it.
Payment Operations
To handle any payment operation, create an object of the PaymentMyfatoorahApiV2 class. Then you can use one of the following functions.
The getInvoiceURL function
It gets the invoice or payment URL as well as the invoice id. As it appliances both of SendPayment and ExecutePayment endpoints. Moreover, It implements the embedded payment.
<?php
$paymentMethodId = 0; //to be redirect to MyFatoorah invoice page
$postFields = [
'InvoiceValue' => '50',
'CustomerName' => 'fname lname',
];
try {
$mfPayment = new PaymentMyfatoorahApiV2($apiKey, $countryCode, $isTestMode);
$data = $mfPayment->getInvoiceURL($postFields, $paymentMethodId);
$invoiceId = $data->InvoiceId;
$paymentLink = $data->InvoiceURL;
echo "Click on <a href='$paymentLink' target='_blank'>$paymentLink</a> to pay with invoiceID $invoiceId.";
} catch (Exception $ex) {
echo $ex->getMessage();
}
The getVendorGateways function
It lists the available payment gateways and implements the InitiatePayment endpoint.
<?php
try {
$mfPayment = new PaymentMyfatoorahApiV2($apiKey, $countryCode, $isTestMode);
$paymentMethods = $mfPayment->getVendorGateways();
print_r($paymentMethods);
} catch (Exception $ex) {
echo $ex->getMessage();
}
The getPaymentStatus function
It gets the payment transaction status and implements the GetPaymentStatus endpoint.
<?php
$keyId = '32090';
$KeyType = 'invoiceid';
try {
$mfPayment = new PaymentMyfatoorahApiV2($apiKey, $countryCode, $isTestMode);
$data = $mfPayment->getPaymentStatus($keyId, $KeyType);
print_r($data);
} catch (Exception $ex) {
echo $ex->getMessage();
}
Shipping Operations
To handle any shipping operation, create an object of the ShippingMyfatoorahApiV2 class. Then you can use one of the following functions:
The calculateShippingCharge function
It calculates the shipping charge and implements CalculateShippingCharge endpoint.
<?php
$invoiceItems[] = [
'ProductName' => 'Product Name',
"Description" => 'Description',
'weight' => 5,
'Width' => 10,
'Height' => 15,
'Depth' => 20,
'Quantity' => 1,
'UnitPrice' => 123,
];
$curlData = [
'ShippingMethod' => 1,
'Items' => $invoiceItems,
'CountryCode' => 'EG',
'CityName' => 'Alexandria',
'PostalCode' => '12345',
];
try {
$mfShipping = new ShippingMyfatoorahApiV2($apiKey, $countryCode, $isTestMode);
$json = $mfShipping->calculateShippingCharge($curlData);
print_r($json);
} catch (Exception $ex) {
echo $ex->getMessage();
}
The getShippingCountries function
It gets MyFatoorah shipping countries and implements GetCountries endpoint.
<?php
try {
$mfShipping = new ShippingMyfatoorahApiV2($apiKey, $countryCode, $isTestMode);
$json = $mfShipping->getShippingCountries();
print_r($json);
} catch (Exception $ex) {
echo $ex->getMessage();
}
The getShippingCities function
It gets MyFatoorah shipping cities and implements GetCities endpoint.
<?php
$method = 1;
$countryCode = 'EG';
$searchValue = 'Alex';
try {
$mfShipping = new ShippingMyfatoorahApiV2($apiKey, $countryCode, $isTestMode);
$json = $mfShipping->getShippingCities($method, $countryCode, $searchValue);
print_r($json);
} catch (Exception $ex) {
echo $ex->getMessage();
}
General Operations
The MyfatoorahApiV2 handles some useful operations like:
The getCurrencyRate function
It gets the rate of a given currency according to the default currency of the MyFatoorah portal account.
<?php
try {
$mfObj = new MyfatoorahApiV2($apiKey, $countryCode, $isTestMode);
$rate = $mfObj->getCurrencyRate('EGP');
echo($rate);
} catch (Exception $ex) {
echo $ex->getMessage();
}
The static getPhone function
It gets the country code and the phone after applying the MyFatoorah restriction.
<?php
try {
$phoneArr = MyfatoorahApiV2::getPhone('+2123456789');
print_r($phoneArr);
} catch (Exception $ex) {
echo $ex->getMessage();
}
The static getWeightRate function
It gets the rate that will convert the given weight unit to MyFatoorah's default weight unit.
<?php
try {
$weightRate = MyfatoorahApiV2::getWeightRate('lbs');
echo($weightRate);
} catch (Exception $ex) {
echo $ex->getMessage();
}
The static getDimensionRate function
It gets the rate that will convert the given dimension unit to MyFatoorah's default dimension unit.
<?php
try {
$dimensionRate = MyfatoorahApiV2::getDimensionRate('in');
echo($dimensionRate);
} catch (Exception $ex) {
echo $ex->getMessage();
}
Updated about 1 year ago