ffb128/zarinpal

PHP Wrapper of Zarinpal API.

1.1 2019-05-26 12:25 UTC

This package is auto-updated.

Last update: 2024-04-28 23:25:43 UTC


README

GitHub GitHub issues
A PHP Full API Wrapper for Zarinpal.
This library allow you to work with zarinpal api as easy as possible.

Features

  • Easy to use API
  • Full API Methods
  • Support both REST & Soap Client (Also you can add your driver)
  • Full Documented

Installation

Using Composer

composer require ffb128/zarinpal 

Quick Start

Request Authority (and Redirect user)

use  ffb128\Zarinpal\Zarinpal;
$zarinpal  =  new  Zarinpal("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");

$zarinpal->setAmount(5500);
$zarinpal->setDescription("Buying test product");
$zarinpal->setCallback("http://example.com/verify.php");

$result  =  $zarinpal->request();
if($result->ok  ==  true){
    echo  $zarinpal->redirect();
} else{
    echo "We have an error: ". $result->message;
}

Verify Transaction (verify.php)

use  ffb128\Zarinpal\Zarinpal;
$zarinpal  =  new  Zarinpal("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");

$zarinpal->setAmount(5500);

$result  =  $zarinpal->verify();
if($result->ok  ==  true){
    echo  "Successful Transaction!";
}

More!

Set uesr information

You can pass user information to Zarinpal, this is recommended to have more control on your transactions.

$zarinpal->setEmail("foo@bar.com");
$zarinpal->setMobile("09123456789");

Using Sandbox

You can use Sandbox mode when you want to do some testing transaction with custom result (succeeded or failed)

$zarinpal->enableSandbox();

Using Zaringate

By default, users will redirect to Zarinpal Webgate page, but you can transfer your users to payment gateway directly.
Notice that you need to activate some necessary service for using Zaringate (Call Zarinpal Supports)

$zarinpal->enableZaringate();

Shared Pay Off

This method is suitable for those sellers whose benefit from entered price must be distributed in a special way. For example you own a web site that presents ceremony services and you have some contributions with several contractors. In this way you would keep some money and settle the rest of it to the contractors' account.

// Means that 4500 Toman from the main transaction is sent
// to ZP.199998.1 with this Description: Testing Profit
$zarinpal->addSharedPay("ZP.199998.1",  4500,  "Testing Profit");

You can also add multiple item for shared pay:

$zarinpal->addSharedPay("ZP.199996.1",  4500,  "Testing Profit");
$zarinpal->addSharedPay("ZP.133476.2",  1200,  "More Testing Profit");
$zarinpal->addSharedPay("ZP.197825.1",  6700,  "More than More Testing Profit");

Long time Authority

By default, an Authority will expire at 15 minutes after generating.
But you can set your custom lifetime for your generated authority by using expireIn() method (between 1800 to 3888000 seconds)
You should use it before request()

// Will expire after 7200 seconds (2 hours)
$zarinpal->expireIn(7200);

Notice that you need to activate some necessary service for using thins method (Call Zarinpal Supports)

Get redirect URL

Sometimes you need to have Gateway URL instead of redirect()

$zarinpal->getRedirectURL();

Pay attention to use this method after $zarinpal->request()
Look at to this more complete example:

$result  =  $zarinpal->request();
if($result->ok  ==  true){
    // Will return something like:
    // https://www.zarinpal.com/pg/StartPay/xxxxx/
    $zarinpal->getRedirectURL();
}

Get Generated Authority

Normally you don't need this method, but it's possible to get pure generated authority.
It also need to run after request()

$result  =  $zarinpal->request();
if($result->ok  ==  true){
    $zarinpal->getAuthority();
}

Refresh Authority

If you want to refresh your authority lifetime, use refreshAuthority(). It does the same thing that expireIn() does. but refreshAuthority() can be used when you generated an authority in past and expireIn() will help you to set your authority life span before generating.

// This will refresh your authority (xxxxxxxxxx) for 3600 seconds (1 hour)
$zarinpal->refreshAuthority("xxxxxxxxxx", 3600);

Get list of Unverified Transactions

This method return a list of transactions that you didn't verify() on them, so it sound that all transactions on this list should be uncompleted.

$result  =  $zarinpal->getUnverified();
if($result->ok  ==  true){
    // contain an array of array
    print_r($result->body->Authorities);
} else{
    echo $result->message;
}

Http Drivers

This library allow you to use both REST and Soap Zarinpal Client as well.
By default the Curl driver will used for sending requests but you can change it by setHttpDriver() .
There are two ready to use driver (Curl and Soap) and also you can have your own (take a look at src/Http/Drivers)

// Switch to Soap Client (Need to install ext-soap)
$zarinpal->setHttpDriver(new  \ffb128\Zarinpal\Http\Drivers\Soap);
// Swith to Curl Client (No Dependency!)
$zarinpal->setHttpDriver(new  \ffb128\Zarinpal\Http\Drivers\Curl);

It's recommended to use REST API (for better performance & speed)

Response

All response from API will return to you in Http\Response object.
this object contains these:

{
  "ok": true|false,
  "message": string,
  "status": int,
  "body": {
    ...
  }
}

You can check if the request was successful or not using ok , get returned status in status and get translated status message in message
Also you can access to API result in body

License

Licensed under the terms of the MIT License