2017 © Pedro Peláez
 

library laravel-zarinpal

A Laravel package to accept and verify payments from ZarinPal gateway based on REST

image

rasulian/laravel-zarinpal

A Laravel package to accept and verify payments from ZarinPal gateway based on REST

  • Friday, October 13, 2017
  • by rasulian
  • Repository
  • 1 Watchers
  • 6 Stars
  • 98 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 2 Open issues
  • 4 Versions
  • 3 % Grown

The README.md

laravel-zarinpal

A laravel package for ZarinPal gateway based on REST, (*1)

This pacakge enables you to accept and verify payments from ZarinPal gateway which is based on REST., (*2)

Installation

The package can be installed through Composer:, (*3)

composer require rasulian/laravel-zarinpal

You'll need to register the service provider:, (*4)

// config/app.php

'providers' => [
    // ...
    Rasulian\ZarinPal\ZarinPalServiceProvider::class,
];

To publish the config file to config/zarinpal.php run:, (*5)

php artisan vendor:publish --provider="Rasulian\ZarinPal\ZarinPalServiceProvider"

This is the default contents of the configuration:, (*6)

// config/zarinpal.php

<?php

return [
  'params' => [
    'merchant-id' => '',

    // Leave it empty if you're passing the callback url when doing the request
    'callback-url' => '',

    // A summary of your product or application, if needed
    'description' => '',
  ],

  // Set to true if you want to test the payment in sandbox mode
  'testing' => false
];


Usage

1. Redirecting the customer to the Zarin Pal, (*7)

Let's get technical. In the controller in which you will redirect the customer to the ZarinPal you must inject the payment gateway like so:, (*8)

  use Rasulian\ZarinPal\Payment;

  class CheckoutConfirmOrderController extends Controller {


    /**
     * @param $zarinPal
     */
    protected $zarinPal;

    public function __construct(Payment $zarinPal)
    {
      ...
      $this->zarinPal = $zarinPal;
      ...
    }

In the same controller in the method in which you redirect the customer to the ZarinPal you must set the $order that you've probably build up during the checkout-process., (*9)

public function doPayment(Request $request)
{
    $invoice = $this->invoiceRepo->getCurrentInvoice();
    // Doing the payment
    $payment = $this->zarinPal->request(

        // The total price for the order
        $invoice->totalPrice,

        // Pass any parameter you want when the customer successfully do the payment
        // and gets back to your site
        ['paymentId' => $invoice->payment_id],

        // Callback URL
        route('checkout.payment.callback'),

        // A summary of your product or application
        'Good product'
    );

    // Throw an exception if the payment request result had any error
    if ($payment->get('result') == 'warning')
        throw new Exception($payment->get('error'));

    // Redirect the customer to the ZarinPal gateway to do the payment
    return redirect()->away($payment->get('url'));
}

2. Verifying the payment, (*10)

So now we've redirected the customer to the payment provider. The customer did some actions there (hopefully he or she paid the order) and now gets redirected back to our shop site., (*11)

The payment provider will redirect the customer to the url of the route that is specified in the third parameter of therequest method or in the description option of the config file., (*12)

We must validate if the redirect to our site is a valid request., (*13)

In the controller that handles the request:, (*14)

  use Rasulian\ZarinPal\Payment;

  class CheckoutPaymentVerificationController extends Controller {


    /**
     * @param $zarinPal
     */
    protected $zarinPal;

    public function __construct(Payment $zarinPal)
    {
        ...
        $this->zarinPal = $zarinPal;
        ...
    }

    ...

Then, in the same controller, in the method you use to handle the request coming from the payment provider, use the verify method:, (*15)

public function verifyPayment(Request $request)
{
    $authority = $request->input('Authority');
    $invoice = $this->invoiceRepo->getCurrentInvoice();

    $verify = $this->zarinPal->verify($invoice->totalPrice, $authority);

    if ($verify->get('result') == 'success') {

        ...
        // Do the needed stuff If the verify was success
        ...

        // If not, we can check which status code is given back to us from the ZarinPal gateway
        // and show a message error correspond to the status code.

    } else if (in_array($verify->get('code'), [-42, -54])) {
        return view('shopping.payment')->with(['error' => $verify->get('error')]);
    }
}

The Versions

13/10 2017

dev-master

9999999-dev

A Laravel package to accept and verify payments from ZarinPal gateway based on REST

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mehran Rasulian

laravel gateway zarinpal زرین پال لاراول درگاه پرداخت

13/10 2017

v1.2

1.2.0.0

A Laravel package to accept and verify payments from ZarinPal gateway based on REST

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mehran Rasulian

laravel gateway zarinpal زرین پال لاراول درگاه پرداخت

22/08 2017

v1.1

1.1.0.0

A Laravel package to accept and verify payments from ZarinPal gateway based on REST

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mehran Rasulian

laravel gateway zarinpal زرین پال لاراول درگاه پرداخت

19/08 2017

v1.0

1.0.0.0

A Laravel package to accept and verify payments from ZarinPal gateway based on REST

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mehran Rasulian

laravel gateway zarinpal زرین پال لاراول درگاه پرداخت