1. Go to this page and download the library: Download shetabit/multipay library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shetabit / multipay example snippets
// Eg. if you want to use zarinpal.
'default' => 'zarinpal',
'drivers' => [
'zarinpal' => [
// Fill in the credentials here.
'apiPurchaseUrl' => 'https://www.zarinpal.com/pg/rest/WebGate/PaymentRequest.json',
'apiPaymentUrl' => 'https://www.zarinpal.com/pg/StartPay/',
'apiVerificationUrl' => 'https://www.zarinpal.com/pg/rest/WebGate/PaymentVerification.json',
'merchantId' => '',
'callbackUrl' => 'http://yoursite.com/path/to',
'description' => 'payment in '.config('app.name'),
],
...
]
use Shetabit\Multipay\Payment;
// load the config file from your project
$paymentConfig =
// At the top of the file.
use Shetabit\Multipay\Invoice;
...
// Create new invoice.
$invoice = new Invoice;
// Set invoice amount.
$invoice->amount(1000);
// Add invoice details: There are 4 syntax available for this.
// 1
$invoice->detail(['detailName' => 'your detail goes here']);
// 2
$invoice->detail('detailName','your detail goes here');
// 3
$invoice->detail(['name1' => 'detail1','name2' => 'detail2']);
// 4
$invoice->detail('detailName1','your detail1 goes here')
->detail('detailName2','your detail2 goes here');
// At the top of the file.
use Shetabit\Multipay\Invoice;
use Shetabit\Multipay\Payment;
...
// load the config file from your project
$paymentConfig = hase($invoice,function($driver, $transactionId) {
// We can store $transactionId in database.
});
// Purchase method accepts a callback function.
$payment->purchase($invoice, function($driver, $transactionId) {
// We can store $transactionId in database.
});
// You can specify callbackUrl
$payment->callbackUrl('http://yoursite.com/verify')->purchase(
$invoice,
function($driver, $transactionId) {
// We can store $transactionId in database.
}
);
// At the top of the file.
use Shetabit\Multipay\Invoice;
use Shetabit\Multipay\Payment;
...
// load the config file from your project
$paymentConfig = should use return statement to redirect user to the bank page.
return $payment->purchase($invoice, function($driver, $transactionId) {
// Store transactionId in database as we need it to verify payment in the future.
})->pay()->render();
// Do all things together in a single line.
return $payment->purchase(
(new Invoice)->amount(1000),
function($driver, $transactionId) {
// Store transactionId in database.
// We need the transactionId to verify payment in the future.
}
)->pay()->render();
// Retrieve json format of Redirection (in this case you can handle redirection to bank gateway)
return $payment->purchase(
(new Invoice)->amount(1000),
function($driver, $transactionId) {
// Store transactionId in database.
// We need the transactionId to verify payment in the future.
}
)->pay()->toJson();
// At the top of the file.
use Shetabit\Multipay\Payment;
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
...
// load the config file from your project
$paymentConfig = a good practice to add invoice amount as well.
try {
$receipt = $payment->amount(1000)->transactionId($transaction_id)->verify();
// You can show payment referenceId to the user.
echo $receipt->getReferenceId();
...
} catch (InvalidPaymentException $exception) {
/**
when payment is not verified, it will throw an exception.
We can catch the exception to handle invalid payments.
getMessage method, returns a suitable message that can be used in user interface.
**/
echo $exception->getMessage();
}
// At the top of the file.
use Shetabit\Multipay\Invoice;
use Shetabit\Multipay\Payment;
...
// load the config file from your project
$paymentConfig = ckUrl($url)->purchase(
$invoice,
function($driver, $transactionId) {
// We can store $transactionId in database.
}
);
// At the top of the file.
use Shetabit\Multipay\Invoice;
use Shetabit\Multipay\Payment;
...
// load the config file from your project
$paymentConfig = on($driver, $transactionId) {
// We can store $transactionId in database.
}
);
// At the top of the file.
use Shetabit\Multipay\Invoice;
use Shetabit\Multipay\Payment;
...
// load the config file from your project
$paymentConfig = riverName')->purchase(
$invoice,
function($driver, $transactionId) {
// We can store $transactionId in database.
}
);
// At the top of the file.
use Shetabit\Multipay\Invoice;
use Shetabit\Multipay\Payment;
...
// load the config file from your project
$paymentConfig = configs.
$payment->config('mechandId', 'your mechand id')->purchase(
$invoice,
function($driver, $transactionId) {
// We can store $transactionId in database.
}
);
// Also we can change multiple configs at the same time.
$payment->config(['key1' => 'value1', 'key2' => 'value2'])->purchase(
$invoice,
function($driver, $transactionId) {
// We can store $transactionId in database.
}
);
// At the top of the file.
use Shetabit\Multipay\Invoice;
...
// Create new invoice.
$invoice = (new Invoice)->amount(1000);
// Use invoice bag to store custom field values.
$invoice->detail([
'ResNum1' => $order->orderId,
'ResNum2' => $customer->verifiedCode,
'ResNum3' => $someValue,
'ResNum4' => $someOtherValue,
]);
namespace App\Packages\Multipay\Driver;
use Shetabit\Multipay\Abstracts\Driver;
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
use Shetabit\Multipay\{Contracts\ReceiptInterface, Invoice, RedirectionForm, Receipt};
class MyDriver extends Driver
{
protected $invoice; // Invoice.
protected $settings; // Driver settings.
public function __construct(Invoice $invoice, $settings)
{
$this->invoice($invoice); // Set the invoice.
$this->settings = (object) $settings; // Set settings.
}
// Purchase the invoice, save its transactionId and finaly return it.
public function purchase() {
// Request for a payment transaction id.
...
$this->invoice->transactionId($transId);
return $transId;
}
// Redirect into bank using transactionId, to complete the payment.
public function pay() : RedirectionForm {
// It is better to set bankApiUrl in config/payment.php and retrieve it here:
$bankUrl = $this->settings->bankApiUrl; // bankApiUrl is the config name.
// Prepare payment url.
$payUrl = $bankUrl.$this->invoice->getTransactionId();
// Redirect to the bank.
$url = $payUrl;
$inputs = [];
$method = 'GET';
return $this->redirectWithForm($url, $inputs, $method);
}
// Verify the payment (we must verify to ensure that user has paid the invoice).
public function verify(): ReceiptInterface {
$verifyPayment = $this->settings->verifyApiUrl;
$verifyUrl = $verifyPayment.$this->invoice->getTransactionId();
...
/**
Then we send a request to $verifyUrl and if payment is not valid we throw an InvalidPaymentException with a suitable message.
**/
throw new InvalidPaymentException('a suitable message');
/**
We create a receipt for this payment if everything goes normally.
**/
return new Receipt('driverName', 'payment_receipt_number');
}
}
// we can add multiple listeners and also remove them!!!
$firstListener = function($driver, $invoice) {
echo 'first listener';
};
$secondListener = function($driver, $invoice) {
echo 'second listener';
};
Payment::addVerifyListener($firstListener);
Payment::addVerifyListener($secondListener);
// remove first listener
Payment::removeVerifyListener($firstListener);
// if we call remove listener without any arguments, it will remove all listeners
Payment::removeVerifyListener(); // remove all verify listeners :D
$invoice = (new Invoice)->amount(10000);
$payment->via('local')->purchase($invoice, function($driver, $transactionId) {
// a fake transaction ID is generated and returned.
})->pay()->render();
$receipt = $payment->via('local')->verify();
[
'orderId' => // fake order number
'traceNo' => // fake trace number (this should be stored in databse)
'referenceNo' => // generated transaction ID in `purchase` method callback
'cardNo' => // fake last four digits of card
]
$invoice->detail([
// setting this value will cause `purchase` method to throw an `PurchaseFailedException`
// to simulate when a gateway can not initialize the payment.
'failedPurchase' => 'custom message to decribe the error',
// Setting this parameter will be shown in payment form.
'orderId' => 4444,
]);
'local' => [
// default callback url of the driver
'callbackUrl' => '/callback',
// main title of the form
'title' => 'Test gateway',
// a description to show under the title for more clarification
'description' => 'This gateway is for using in development environments only.',
// custom label to show as order No.
'orderLabel' => 'Order No.',
// custom label to show as payable amount
'amountLabel' => 'Payable amount',
// custom label of successful payment button
'payButton' => 'Successful Payment',
// custom label of cancel payment button
'cancelButton' => 'Cancel Payment',
],
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.