rasulian / laravel-zarinpal

一个基于REST的Laravel包,用于接收和验证来自ZarinPal网关的支付

v1.2 2017-10-13 11:49 UTC

This package is not auto-updated.

Last update: 2024-09-18 21:45:46 UTC


README

一个基于REST的ZarinPal网关的laravel包

此包使您能够接收和验证来自基于REST的ZarinPal网关的支付。

安装

此包可以通过Composer安装

composer require rasulian/laravel-zarinpal

您需要注册服务提供者

// config/app.php

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

要发布配置文件到config/zarinpal.php,请运行以下命令:

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

这是配置的默认内容

// 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
];


用法

1. 将客户重定向到Zarin Pal

让我们来探讨一下技术细节。在您将客户重定向到ZarinPal的控制器中,您必须如此注入支付网关:

  use Rasulian\ZarinPal\Payment;

  class CheckoutConfirmOrderController extends Controller {


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

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

在相同的控制器中,在您将客户重定向到ZarinPal的方法中,您必须设置订单(您可能在结账过程中构建了这个订单)。

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. 验证支付

现在我们已经将客户重定向到支付提供商。客户在那里执行了一些操作(希望他或她已经支付了订单)并且现在被重定向回到我们的商店网站。

支付提供商将客户重定向到请求方法的第三个参数指定的路由URL或配置文件中的description选项。

我们必须验证重定向到我们的网站是否是一个有效的请求。

在处理请求的控制器中

  use Rasulian\ZarinPal\Payment;

  class CheckoutPaymentVerificationController extends Controller {


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

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

然后,在相同的控制器中,在您用于处理来自支付提供商的请求的方法中,使用verify方法。

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