1. 程式人生 > 實用技巧 >php 實現銀聯商務H5支付的示例程式碼

php 實現銀聯商務H5支付的示例程式碼

銀聯商務H5支付介面文件:文件地址

一:H5支付的介面地址:

1:支付寶支付

測試地址:http://58.247.0.18:29015/v1/netpay/trade/h5-pay

正式地址:https://api-mop.chinaums.com/v1/netpay/trade/h5-pay

2:銀聯支付

測試地址:http://58.247.0.18:29015/v1/netpay/uac/order

正式地址:https://api-mop.chinaums.com/v1/netpay/uac/order

二:介面需要的基本引數

介面使用的是get傳參,直接將介面引數放到介面地址後,此介面是由瀏覽器直接跳轉到介面

(1)authorization

認證方式,直接填入:OPEN-FORM-PARAM

(2)appId

銀聯商務使用者H5支付產品的AppID

(3)timestamp

時間戳,格式為yyyyMMddHHmmss,如20191001121212

(4)nonce

隨機數

(5)content

業務內容,為json格式,並且需要進行url編碼,內部的具體資訊下面介紹

(6)signature

簽名,需要進行url編碼,具體生成方式如:Base64_Encode(HmacSHA256(appId + timestamp + nonce + SHA256_HEX(content), AppKey))

業務內容content引數內部具體引數說明:

1:requestTimestamp

報文請求時間,格式為yyyy-MM-dd HH:mm:ss,如2019-10-01 12:12:12

2:merOrderId

商戶自己生成的訂單號,這裡注意:我們需要在我們自己生成的訂單號前面加上1017字首

3:mid

銀聯商務使用者H5支付產品的商戶號

4:tid

銀聯商務使用者H5支付產品的終端號

5:instMid

業務型別,直接填入:H5DEFAULT

6:totalAmount

支付總金額,單位為分

7:expireTime

訂單過期時間,格式為yyyy-MM-dd HH:mm:ss,如2019-10-02 12:12:12

8:notifyUrl

支付結果通知地址

9:returnUrl

網頁跳轉地址

三:H5支付的支付寶支付例項

$appId = '10037e6f6a4e6da4016a670fd4530012';
$appKey = 'f7a74b6c02ae4e1e94aaba311c04acf2';
$mid = '898310148160568';
$tid = '88880001';
//業務內容
$time = time();
$content = [
  'requestTimestamp' => date('Y-m-d H:i:s', $time),//報文請求時間
  'merOrderId' => '1017' . date('YmdHis'),//商戶訂單號
  'mid' => $mid,//商戶號
  'tid' => $tid,//終端號
  'instMid' => 'H5DEFAULT',//業務型別
  'totalAmount' => '1',//支付總金額
  'expireTime' => date('Y-m-d H:i:s', strtotime('+1 day', $time)),//過期時間
  'notifyUrl' => '',//支付通知地址
  'returnUrl' => ''//網頁跳轉地址
];
$timestamp = date('YmdHis', $time);
//隨機數
$str = md5(uniqid(mt_rand(), true));
$uuid = substr($str, 0, 8) . '-';
$uuid .= substr($str, 8, 4) . '-';
$uuid .= substr($str, 12, 4) . '-';
$uuid .= substr($str, 16, 4) . '-';
$uuid .= substr($str, 20, 12);
$nonce = $uuid;
//簽名
$hash = bin2hex(hash('sha256', json_encode($content), true));
$hashStr = $appId . $timestamp . $nonce . $hash;
$signature = base64_encode((hash_hmac('sha256', $hashStr, $appKey, true))); //$appKey銀聯商戶H5支付產品的AppKey
$data = [
  'timestamp' => $timestamp,//時間戳
  'authorization' => 'OPEN-FORM-PARAM',//認證方式
  'appId' => $appId,//APPID
  'nonce' => $nonce,//隨機數
  'content' => urlencode(json_encode($content)),//業務內容
  'signature' => urlencode($signature),//簽名
];
//介面返回資訊
//支付寶:http://58.247.0.18:29015/v1/netpay/trade/h5-pay
//銀聯線上無卡:http://58.247.0.18:29015/v1/netpay/qmf/h5-pay
//銀聯:http://58.247.0.18:29015/v1/netpay/uac/order
$options = '';
foreach ($data as $key => $value) {
  $options .= $key . '=' . $value .'&';
}
$options = rtrim($options, '&');
//存在轉義字元,那麼去掉轉義
if(get_magic_quotes_gpc()){
  $options = stripslashes($options);
}
$url = 'http://58.247.0.18:29015/v1/netpay/trade/h5-pay?' . $options;

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援碼農教程。