【发布时间】:2020-03-07 09:32:34
【问题描述】:
我正在尝试将 PayHere 支付网关集成到我使用 Laravel 5.5 构建的网站。所以作为他们的documentation,我已经集成了他们的api,如下所示。我正在 live sever 中使用 sandbox credentials 进行测试。在这里,我首先通过 <from> 传递付款详细信息(如他们的文档中所示)
.因此,当我进行交易时,它应该可以正常工作,并且我收到了 来自支付网关的成功邮件。
但我的问题是 我无法获取从支付网关发送的通知以检查我的付款的付款状态以更新数据库。所以我写了一个简单的代码来检查支付状态码。 (2 - 成功 / 0 - 待定 / -1 - 取消 / -2 - 失败 / -3 -charged-back )
所以谁能帮我解决这个问题。请参考下面我编写的示例代码,用于测试实时服务器中的支付网关
- Payment Gateway api details
- Laravel 5.5 版
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' ) ;
结帐表格样本取自他们的文档
<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>
更新 01(正如@RicardoAlbear 所说)
【问题讨论】:
-
我经常使用的第一个建议是确保平台以正确的方式发送 webhook,为此,我通常使用此站点 webhook.site 并在提供程序中设置生成 url,所以当您执行付款,您将在此站点中看到 webhook 的所有详细信息。随意复制结果,以便我可以帮助你。
-
@RicardoAlbear 非常感谢您的评论。请检查更新后的问题。
-
你在 Laravel 上启用了错误日志吗?在那里你可以看到当你的应用收到支付网关回调时发生了什么。
-
@RicardoAlbear 它不会显示任何错误。看起来我的通知网址有问题。但我想不通
标签: php laravel api payment-gateway payment