php - laravel 5.5 中的 payhere 支付网关集成

标签 php laravel api payment-gateway payment

<分区>

我正在尝试整合 PayHere 我网站的支付网关是用 Laravel 5.5 构建的。所以作为他们的documentation我已经整合了他们的 api,如下所示。我在 live server 中使用sandbox credentials 进行测试。在这里我所做的是首先通过 <from> 传递付款细节。 (在他们的文档中)

.So when I make a transaction it works as it should be and I receive the success mail from the payment gateway too.

但我的问题是我无法获取从支付网关发送来检查我的支付状态以更新数据库的通知。所以我写了一个简单的代码来检查支付状态码。 ( 2 - 成功/0 - 待处理/-1 - 已取消/-2 - 失败/-3 - 退款)

那么谁能帮我解决这个问题。请引用下面我编写的示例代码,用于测试实时服务器中的支付网关

CheckOutController

 public function notifyUrl(Request $request){

 //As their documentation
 $merchant_id = $request -> input('merchant_id');
 $order_id =  $request -> input('order_id');
 $payhere_amount =  $request -> input('payhere_amount');
 $payhere_currency =  $request -> input('payhere_currency');
 $status_code =  $request -> input('status_code');
 $md5sig =  $request -> input('md5sig');

 $merchant_secret = 'xxxxxxxxxxxxxxxxxxxx'; // Replace with your Merchant Secret (Can be found on your PayHere account's Settings page)

 $local_md5sig = strtoupper (md5 ( $merchant_id . $order_id . $payhere_amount . $payhere_currency . $status_code . strtoupper(md5($merchant_secret)) ) );
// return $status_code;
if (($local_md5sig === $md5sig) AND ($status_code == 2) ){

 $newStatus = new Status();
 $newStatus ->status= 2;
 $newStatus ->save();

 }elseif(($local_md5sig === $md5sig) AND ($status_code == 0) ){

 $newStatus = new Status();
 $newStatus ->status= 0;
 $newStatus ->save();

 }elseif(($local_md5sig === $md5sig) AND ($status_code == -1) ){

 $newStatus = new Status();
 $newStatus ->status= -1;
 $newStatus ->save();

 }elseif(($local_md5sig === $md5sig) AND ($status_code == -2) ){

 $newStatus = new Status();
 $newStatus ->status= -2;
 $newStatus ->save();

 }elseif(($local_md5sig === $md5sig) AND ($status_code == -3) ){

 $newStatus = new Status();
 $newStatus ->status= -3;
 $newStatus ->save();

 }else{

 $newStatus = new Status();
 $newStatus ->status= 'Fail';
 $newStatus ->save();

 }
}

 public function cancelUrl(){
  return redirect()->route('shop.home'); 
 }
 public function returnUrl(){
  return redirect()->route('shop.cart'); 
 }

路线

$router //**using POST method hens they pass data via this url**
    -> post ( 'payment/notify' , 'CheckOutController@notifyUrl' )
    -> name ( 'shop.notifyUrl' ) ;

$router 
    -> get ( 'payment/cancelUrl' , 'CheckOutController@cancelUrl' )
    -> name ( 'shop.cancelUrl' ) ;

$router
    -> get ( 'payment/returnUrl' , 'CheckOutController@returnUrl' )
    -> name ( 'shop.returnUrl' ) ;

Checkout Form 从他们的文档中提取的示例

<form method="post" action="https://sandbox.payhere.lk/pay/checkout" >  

            <input type="hidden" name="merchant_id" value="000000">    <!-- Replace your Merchant ID -->
            <input type="hidden" name="return_url" value="{{route ( 'shop.returnUrl' )}}">
            <input type="hidden" name="cancel_url" value="{{route ( 'shop.cancelUrl' )}}">
            <input type="hidden" name="notify_url" value="{{route ( 'shop.notifyUrl' )}}">  

            <input hidden type="text" name="order_id" value="xxxxx">
            <input hidden type="text" name="items" value="xxxxxx">
            <input hidden type="text" name="currency" value="LKR">
            <input hidden type="text" name="amount" value="5000.0"> 

            <br><br>Customer Details</br></br>
            <input type="text" name="first_name" value="xxxxx">
            <input type="text" name="last_name" value="xxxx">
            <input type="text" name="email" value="xxxx">
            <input type="text" name="phone" value="xxxx">
            <input type="text" name="address" value=" xxxxx / xxxxxx/ xxxxxx/ Sri Lanka">
            <input type="text" name="city" value="Colombo">
            <input type="hidden" name="country" value="Sri Lanka">  
        </form> 

Update 01 (As @RicardoAlbear said )

update

最佳答案

感谢payhere的帮助我能够解决我的问题。问题是我没有为我的 payhere 通知 url 排除 CSRF。所以 laravel 会自动拒绝通过通知 url 传递的数据。所以我所做的只是添加 *(接受所有 url s。我们也可以指定 url,如“www.exapmle.com/payhere/nofity”),现在它开始工作了。所以问题是我们需要为 payhere 通知 url 排除 CSRF。 @RecardoAlbear 非常感谢您的支持。我能够从您的评论中获取很多新内容。谢谢

为了将来的引用,payhere 可以在 Laravel 中集成如下

  1. 创建您的 payhere 帐户并获取 merchant_id
  2. documentation按照步骤
  3. app\Http\Middleware\VerifyCsrfToken.php

    中排除 CSRF 保护
      <?php
    
     namespace App\Http\Middleware ;
    
     use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware ;
    
    class VerifyCsrfToken extends Middleware
    {
    
        /**
      * The URIs that should be excluded from CSRF verification.
      *
      * @var array
      */
      //I have used * - means that it allows for all the url. But you can specify excluded url s as you wanted 
            protected $except = [
                 '*', //this exclude all urls
                 'www.exapmle.com/payhere/nofity'
               ] ;
      }
    

关于php - laravel 5.5 中的 payhere 支付网关集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58805715/

相关文章:

javascript - 如果 JS 条件满足,则阻止 Blade 显示某些内容

c++ Mysql C API 连接问题

php - 删除 () 或 forceDelete()。更好的判断方法

php - grep 命令输出中的特定字符串

php - 将大小值输入数据库 PHP

php - 我想更新两个表中的值

php - laravel undefined variable : host

javascript - 所见即所得 JavaScript HTML 编辑器,具有 API 和高级文本处理支持(如单词检测)

perl - 我如何在图书馆中使用 NYTProf

php - ORM 中的对象是否应该包含 CRUD 以外的操作?