---
updatedAt: 2025-11-11T23:55:25.000Z
agentTools:
  projectIndex: https://docs.myfatoorah.com/llms.txt
---

# PHP Library 2.1

## Installation Steps

Install the PHP library package via [myfatoorah/library](https://packagist.org/packages/myfatoorah/library) composer by running the below command:

```text
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](https://dev.azure.com/myfatoorahsc/Public-Repo/_git/Public-Repo?path=/php/library/previous%20releases/myfatoorah-library-2.1.zip\&version=GBmaster&_a=contents).

<hr />

## Payment Operations

To handle any payment operation, create an object of the **PaymentMyfatoorahApiV2** class. Then you can use one of the following functions.

#### <ins>The **getInvoiceURL** function</ins>

It gets the invoice or payment URL as well as the invoice id. As it appliances both of [SendPayment](https://docs.myfatoorah.com/docs/send-payment) and [ExecutePayment](https://docs.myfatoorah.com/docs/execute-payment) endpoints. Moreover, It implements the [embedded payment](https://docs.myfatoorah.com/docs/embedded-payment).

```php
<?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();
}
```

#### <ins>The **getVendorGateways** function</ins>

It lists the available payment gateways and implements the [InitiatePayment](https://docs.myfatoorah.com/docs/initiate-payment) endpoint.

```php
<?php

try {
    $mfPayment      = new PaymentMyfatoorahApiV2($apiKey, $countryCode, $isTestMode);
    $paymentMethods = $mfPayment->getVendorGateways();

    print_r($paymentMethods);
} catch (Exception $ex) {
    echo $ex->getMessage();
}
```

#### <ins>The **getPaymentStatus** function</ins>

It gets the payment transaction status and implements the [GetPaymentStatus](https://docs.myfatoorah.com/docs/get-payment-status)  endpoint.

```php
<?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();
}
```

<hr />

## Shipping Operations

To handle any shipping operation, create an object of the **ShippingMyfatoorahApiV2** class. Then you can use one of the following functions:

#### <ins>The **calculateShippingCharge** function</ins>

It calculates the shipping charge and implements [CalculateShippingCharge](https://docs.myfatoorah.com/docs/calculate-shipping-charge) endpoint.

```php
<?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();
}
```

#### <ins>The **getShippingCountries** function</ins>

It gets **MyFatoorah** shipping countries and implements [GetCountries](https://docs.myfatoorah.com/docs/get-countries) endpoint.

```php
<?php

try {
    $mfShipping = new ShippingMyfatoorahApiV2($apiKey, $countryCode, $isTestMode);
    $json       = $mfShipping->getShippingCountries();

    print_r($json);
} catch (Exception $ex) {
    echo $ex->getMessage();
}
```

#### <ins>The **getShippingCities** function</ins>

It gets **MyFatoorah** shipping cities and implements [GetCities](https://docs.myfatoorah.com/docs/get-cities) endpoint.

```php
<?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();
}
```

<hr />

## General Operations

The **MyfatoorahApiV2** handles some useful operations like:

#### <ins>The **getCurrencyRate** function</ins>

It gets the rate of a given currency according to the default currency of the MyFatoorah portal account.

```php
<?php

try {
    $mfObj = new MyfatoorahApiV2($apiKey, $countryCode, $isTestMode);
    $rate  = $mfObj->getCurrencyRate('EGP');

    echo($rate);
} catch (Exception $ex) {
    echo $ex->getMessage();
}
```

#### <ins>The static **getPhone** function</ins>

It gets the country code and the phone after applying the MyFatoorah restriction.

```php
<?php

try {
    $phoneArr = MyfatoorahApiV2::getPhone('+2123456789');

    print_r($phoneArr);
} catch (Exception $ex) {
    echo $ex->getMessage();
}
```

#### <ins>The static **getWeightRate** function</ins>

It gets the rate that will convert the given weight unit to MyFatoorah's default weight unit.

```php
<?php

try {
    $weightRate = MyfatoorahApiV2::getWeightRate('lbs');

    echo($weightRate);
} catch (Exception $ex) {
    echo $ex->getMessage();
}
```

#### <ins>The static **getDimensionRate** function</ins>

It gets the rate that will convert the given dimension unit to MyFatoorah's default dimension unit.

```php
<?php

try {
    $dimensionRate = MyfatoorahApiV2::getDimensionRate('in');

    echo($dimensionRate);
} catch (Exception $ex) {
    echo $ex->getMessage();
}
```

<hr />