php接入eGhl支付

 官方文档

参数

 1  # 订单信息
 2   $order = $order->toArray();
 3   $products    = "商品信息";
 4   $PaymentID = substr(md5(time()), 0,19);
 5   $PageTimeout = 780;
 6   $url = env('APP_URL', '');
 7   # 支付成功链接 post
 8   $MerchantApprovalURL = sprintf('%s/eGhl/success?order_number=%s',$url, $order['number']);
 9   # 取消支付或者支付失败链接 get
10   $MerchantReturnURL = sprintf('%s/orders/%s/pay', $url, $order['number']);
11   # 回调post
12   $MerchantCallbackURL = sprintf('%s/eGhl/callback', $url);
13 
14   // todo beikshop货币编号问题,beikshop是MRY实际e-ghl支持的编码是MYR
15   if ($order['currency_code'] == 'MRY') {
16       $order['currency_code'] = 'MYR';
17   }
18   // 订单金额,eghl支持两位小数
19   $order['total'] = sprintf("%.2f", $order['total']);
20   // HashValue 值生成
21   $stringParam = $payment_setting['merchant_password'] . $payment_setting['merchant_id'] . $PaymentID . $MerchantReturnURL .
22   $MerchantApprovalURL .$MerchantCallbackURL . $order['total'] . $order['currency_code'] . $order['ip'] . $PageTimeout;
23   $hash = hash('sha256',$stringParam);

 

Sample HTML Form Post Payment Request

 1 <form name="frmPayment" method="post" action="{{ $payment_setting['api_url'] }}">
 2   <input type="hidden" name="TransactionType" value="SALE">
 3   <input type="hidden" name="PymtMethod" value="ANY">
 4   <input type="hidden" name="ServiceID" value="{{ $payment_setting['merchant_id'] }}">
 5   <input type="hidden" name="PaymentID" value="{{ $PaymentID }}">
 6   <input type="hidden" name="OrderNumber" value="{{ $order['number'] }}">
 7   <input type="hidden" name="PaymentDesc" value="{{ $PaymentDesc }}">
 8   <input type="hidden" name="MerchantReturnURL" value="{{ $MerchantReturnURL }}">
 9   <input type="hidden" name="MerchantApprovalURL" value="{{ $MerchantApprovalURL }}">
10   <input type="hidden" name="MerchantCallbackURL" value="{{ $MerchantCallbackURL }}">
11   <input type="hidden" name="Amount" value="{{ $order['total'] }}">
12   <input type="hidden" name="CurrencyCode" value="{{ $order['currency_code'] }}">
13   <input type="hidden" name="CustIP" value="{{ $order['ip'] }}">
14   <input type="hidden" name="HashValue" value="{{ $hash }}">
15   <input type="hidden" name="LanguageCode" value="{{ $order['locale'] }}">
16   <input type="hidden" name="CustName" value="{{ $order['payment_customer_name'] }}">
17   <input type="hidden" name="CustEmail" value="{{ $order['email'] }}">
18   <input type="hidden" name="CustPhone" value="{{ $order['shipping_telephone'] }}">
19   <input type="hidden" name="PageTimeout" value="{{ $PageTimeout }}">
20   <input class="btn btn-primary btn-lg" type="submit" name="submit" value="{{
21         __('EGhl::common.btn_submit') }}">
22 </form>

 异步回调处理

 1     public function callback (Request $request)
 2     {
 3         try {
 4             $requestData = $request->all();
 5             Log::info( 'Request data: ' . json_encode( $requestData ) );
 6 
 7             $txnStatus   = $requestData['TxnStatus'];
 8             $orderNumber = $requestData['OrderNumber'] ?? '';
 9             $order       = OrderRepo::getOrderByNumber( $orderNumber );
10             $setting     = plugin_setting( $order['payment_method_code'] );
11 
12             Log::info( 'Request TxnStatus: ' . $txnStatus );
13             Log::info( 'Request number: ' . $orderNumber );
14             if ( $txnStatus == 0 && $this->checkHashValue( $setting, $requestData ) ) {
15                 StateMachineService::getInstance( $order )->setShipment()->changeStatus( StateMachineService::PAID );
16                 return json_encode( $requestData );
17             }
18             return "fail";
19 
20         } catch ( \Exception $e ) {
21             Log::info( 'eGhl error: ' . $e->getMessage() );
22             return "fail";
23         }
24     }
25 
26 /**
27      * 验证回调参数的合法性
28      *
29      * @param array $setting 配置信息
30      * @param array $data    回调数据
31      * @return bool
32      */
33     private function checkHashValue (array $setting, array $data) : bool
34     {
35         $string = $setting['merchant_password'] .
36             $data["TxnID"] .
37             $setting['merchant_id'] .
38             $data["PaymentID"] .
39             $data["TxnStatus"] .
40             $data["Amount"] .
41             $data["CurrencyCode"] .
42             $data["AuthCode"];
43 
44         $hash = ( hash( 'sha256', $string ) );
45         if ( $hash === $data["HashValue"] ) {
46             return true;
47         }
48 
49         return false;
50     }

 同步回调处理

同步回调这边和异步返回的参数一致,根据自己的业务逻辑做处理,这边不贴代码了。

 

 
posted @ 2024-06-03 16:12  php的自我修养  阅读(46)  评论(0编辑  收藏  举报