Code Monkey home page Code Monkey logo

laravel-zarinpal's Introduction

laravel-zarinpal

A laravel package for ZarinPal gateway based on REST

This pacakge enables you to accept and verify payments from ZarinPal gateway which is based on REST.

Installation

The package can be installed through Composer:

composer require rasulian/laravel-zarinpal

You'll need to register the service provider:

// config/app.php

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

To publish the config file to config/zarinpal.php run:

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

This is the default contents of the configuration:

// 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 are in the development environment
  'testing' => false
];


Usage

1. Redirecting the customer to the Zarin Pal

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:

  use Rasulian\ZarinPal\Payment;

  class CheckoutConfirmOrderController extends Controller {


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

    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.

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

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.

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.

We must validate if the redirect to our site is a valid request.

In the controller that handles the request:

  use Rasulian\ZarinPal\Payment;

  class CheckoutPaymentVerificationController extends Controller {


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

    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:

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')]);
    }
}

laravel-zarinpal's People

Contributors

mehrancodes avatar

Stargazers

yaser darzi avatar

Watchers

James Cloos avatar yaser darzi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.