当前位置:首页 > PHP教程 > php高级应用 > 列表

php银联网页支付实现方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-15 15:33:27 浏览: 评论:0 

这篇文章主要介绍了php银联网页支付实现方法,实例分析了php操作银联网支付接口的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php银联网页支付实现方法。分享给大家供大家参考。具体分析如下:

这里介绍的银联WAP支付功能,仅限消费功能。

1. PHP代码如下:

  1. <?php 
  2.  
  3. namespace common\services; 
  4. class UnionPay 
  5. { 
  6.     /** 
  7.      * 支付配置 
  8.      * @var array 
  9.      */ 
  10.     public $config = []; 
  11.     /** 
  12.      * 支付参数,提交到银联对应接口的所有参数 
  13.      * @var array 
  14.      */ 
  15.     public $params = []; 
  16.     /** 
  17.      * 自动提交表单模板 
  18.      * @var string 
  19.      */ 
  20.     private $formTemplate = <<<'HTML' 
  21. <!DOCTYPE HTML> 
  22. <html> 
  23. <head> 
  24.     <meta charset="utf-8"> 
  25.     <title>支付</title> 
  26. </head> 
  27. <body> 
  28.     <div style="text-align:center">跳转中...</div> 
  29.     <form id="pay_form" name="pay_form" action="%s" method="post"> 
  30.         %s 
  31.     </form> 
  32.     <script type="text/javascript"> 
  33.         document.onreadystatechange = function(){ 
  34.             if(document.readyState == "complete") { 
  35.                 document.pay_form.submit(); 
  36.             } 
  37.         }; 
  38.     </script> 
  39. </body> 
  40. </html> 
  41. HTML; 
  42. /** 
  43. * 构建自动提交HTML表单 
  44. * @return string 
  45. */ 
  46. public function createPostForm() 
  47. { 
  48.         $this->params['signature'] = $this->sign(); 
  49.         $input = ''; 
  50.         foreach($this->params as $key => $item) { 
  51.             $input .= "\t\t<input type=\"hidden\" name=\"{$key}\" value=\"{$item}\">\n"; 
  52.         } 
  53.         return sprintf($this->formTemplate, $this->config['frontUrl'], $input); 
  54. } 
  55. /** 
  56. * 验证签名 
  57. * 验签规则: 
  58. * 除signature域之外的所有项目都必须参加验签 
  59. * 根据key值按照字典排序,然后用&拼接key=value形式待验签字符串; 
  60. * 然后对待验签字符串使用sha1算法做摘要; 
  61. * 用银联公钥对摘要和签名信息做验签操作 
  62. * 
  63. * @throws \Exception 
  64. * @return bool 
  65. */ 
  66. public function verifySign() 
  67. { 
  68.         $publicKey = $this->getVerifyPublicKey(); 
  69.         $verifyArr = $this->filterBeforSign(); 
  70.         ksort($verifyArr); 
  71.         $verifyStr = $this->arrayToString($verifyArr); 
  72.         $verifySha1 = sha1($verifyStr); 
  73.         $signature = base64_decode($this->params['signature']); 
  74.         $result = openssl_verify($verifySha1, $signature, $publicKey); 
  75.         if($result === -1) { 
  76.             throw new \Exception('Verify Error:'.openssl_error_string()); 
  77.         } 
  78.         return $result === 1 ? true : false; 
  79. } 
  80. /** 
  81. * 取签名证书ID(SN) 
  82. * @return string 
  83. */ 
  84. public function getSignCertId() 
  85. { 
  86.         return $this->getCertIdPfx($this->config['signCertPath']); 
  87. }   
  88. /** 
  89. * 签名数据 
  90. * 签名规则: 
  91. * 除signature域之外的所有项目都必须参加签名 
  92. * 根据key值按照字典排序,然后用&拼接key=value形式待签名字符串; 
  93. * 然后对待签名字符串使用sha1算法做摘要; 
  94. * 用银联颁发的私钥对摘要做RSA签名操作 
  95. * 签名结果用base64编码后放在signature域 
  96. * 
  97. * @throws \InvalidArgumentException 
  98. * @return multitype|string 
  99. */ 
  100. private function sign() { 
  101.         $signData = $this->filterBeforSign(); 
  102.         ksort($signData); 
  103.         $signQueryString = $this->arrayToString($signData); 
  104.         if($this->params['signMethod'] == 01) { 
  105.             //签名之前先用sha1处理 
  106.             //echo $signQueryString;exit; 
  107.             $datasha1 = sha1($signQueryString); 
  108.             $signed = $this->rsaSign($datasha1); 
  109.         } else { 
  110.             throw new \InvalidArgumentException('Nonsupport Sign Method'); 
  111.         } 
  112.         return $signed; 
  113. } 
  114. /** 
  115. * 数组转换成字符串 
  116. * @param array $arr 
  117. * @return string 
  118. */ 
  119. private function arrayToString($arr) 
  120. { 
  121.         $str = ''; 
  122.         foreach($arr as $key => $value) { 
  123.             $str .= $key.'='.$value.'&'; 
  124.         } 
  125.         return substr($str, 0, strlen($str) - 1); 
  126. } 
  127. /** 
  128. * 过滤待签名数据 
  129. * signature域不参加签名 
  130. * 
  131. * @return array 
  132. */ 
  133. private function filterBeforSign() 
  134. { 
  135.         $tmp = $this->params; 
  136.         unset($tmp['signature']); 
  137.         return $tmp; 
  138. } 
  139. /** 
  140. * RSA签名数据,并base64编码 
  141. * @param string $data 待签名数据 
  142. * @return mixed 
  143. */ 
  144. private function rsaSign($data) 
  145. { 
  146.         $privatekey = $this->getSignPrivateKey(); 
  147.         $result = openssl_sign($data, $signature, $privatekey); 
  148.         if($result) { 
  149.             return base64_encode($signature); 
  150.         } 
  151.         return false; 
  152. } 
  153. /** 
  154. * 取.pfx格式证书ID(SN) 
  155. * @return string 
  156. */ 
  157. private function getCertIdPfx($path) 
  158. { 
  159.         $pkcs12certdata = file_get_contents($path); 
  160.         openssl_pkcs12_read($pkcs12certdata, $certs, $this->config['signCertPwd']); 
  161.         $x509data = $certs['cert']; 
  162.         openssl_x509_read($x509data); 
  163.         $certdata = openssl_x509_parse($x509data); 
  164.         return $certdata['serialNumber']; 
  165. } 
  166. /** 
  167. * 取.cer格式证书ID(SN) 
  168. * @return string 
  169. */ 
  170. private function getCertIdCer($path) 
  171. { 
  172.         $x509data = file_get_contents($path); 
  173.         openssl_x509_read($x509data); 
  174.         $certdata = openssl_x509_parse($x509data); 
  175.         return $certdata['serialNumber']; 
  176. } 
  177. /** 
  178. * 取签名证书私钥 
  179. * @return resource 
  180. */ 
  181. private function getSignPrivateKey() 
  182. { 
  183.         $pkcs12 = file_get_contents($this->config['signCertPath']); 
  184.         openssl_pkcs12_read($pkcs12, $certs, $this->config['signCertPwd']); 
  185.         return $certs['pkey']; 
  186. } 
  187. /** 
  188. * 取验证签名证书 
  189. * @throws \InvalidArgumentException 
  190. * @return string 
  191. */ 
  192. private function getVerifyPublicKey() 
  193. { 
  194.         //先判断配置的验签证书是否银联返回指定的证书是否一致 
  195.         if($this->getCertIdCer($this->config['verifyCertPath']) != $this->params['certId']) { 
  196.             throw new \InvalidArgumentException('Verify sign cert is incorrect'); 
  197.         } 
  198.         return file_get_contents($this->config['verifyCertPath']);       
  199.     } 
  200. } 

2. 配置示例,代码如下:

  1. //银联支付设置 
  2.  'unionpay' => [ 
  3.      //测试环境参数 
  4.      'frontUrl' => 'https://101.231.204.80:5000/gateway/api/frontTransReq.do', //前台交易请求地址 
  5.      //'singleQueryUrl' => 'https://101.231.204.80:5000/gateway/api/queryTrans.do', //单笔查询请求地址 
  6.      'signCertPath' => __DIR__.'/../keys/unionpay/test/sign/700000000000001_acp.pfx', //签名证书路径 
  7.      'signCertPwd' => '000000', //签名证书密码 
  8.      'verifyCertPath' => __DIR__.'/../keys/unionpay/test/verify/verify_sign_acp.cer', //验签证书路径 
  9.      'merId' => 'xxxxxxx', 
  10.      //正式环境参数 
  11.      //'frontUrl' => 'https://101.231.204.80:5000/gateway/api/frontTransReq.do', //前台交易请求地址 
  12.      //'singleQueryUrl' => 'https://101.231.204.80:5000/gateway/api/queryTrans.do', //单笔查询请求地址 
  13.      //'signCertPath' => __DIR__.'/../keys/unionpay/test/sign/PM_700000000000001_acp.pfx', //签名证书路径 
  14.      //'signCertPwd' => '000000', //签名证书密码 
  15.      //'verifyCertPath' => __DIR__.'/../keys/unionpay/test/verify/verify_sign_acp.cer', //验签证书路径 
  16.      //'merId' => 'xxxxxxxxx', //商户代码 
  17.  ], 

3. 支付示例:

  1. $unionPay = new UnionPay(); 
  2. $unionPay->config = Yii::$app->params['unionpay'];//上面的配置 
  3. $unionPay->params = [ 
  4.     'version' => '5.0.0', //版本号 
  5.     'encoding' => 'UTF-8', //编码方式 
  6.     'certId' => $unionPay->getSignCertId(), //证书ID 
  7.     'signature' => '', //签名 
  8.     'signMethod' => '01', //签名方式 
  9.     'txnType' => '01', //交易类型 
  10.     'txnSubType' => '01', //交易子类 
  11.     'bizType' => '000201', //产品类型 
  12.     'channelType' => '08',//渠道类型 
  13.     'frontUrl' => Url::toRoute(['payment/unionpayreturn'], true), //前台通知地址 
  14.     'backUrl' => Url::toRoute(['payment/unionpaynotify'], true), //后台通知地址 
  15.     //'frontFailUrl' => Url::toRoute(['payment/unionpayfail'], true), //失败交易前台跳转地址 
  16.     'accessType' => '0', //接入类型 
  17.     'merId' => Yii::$app->params['unionpay']['merId'], //商户代码 
  18.     'orderId' => $orderNo, //商户订单号 
  19.     'txnTime' => date('YmdHis'), //订单发送时间 
  20.     'txnAmt' => $sum * 100, //交易金额,单位分 
  21.     'currencyCode' => '156', //交易币种 
  22. ]; 
  23. $html = $unionPay->createPostForm(); 

4. 异步通知示例:

  1. $unionPay = new UnionPay(); 
  2. $unionPay->config = Yii::$app->params['unionpay']; 
  3. $unionPay->params = Yii::$app->request->post(); //银联提交的参数 
  4. if(emptyempty($unionPay->params)) { 
  5.     return 'fail!'; 
  6. } 
  7. if($unionPay->verifySign() && $unionPay->params['respCode'] == '00') { 
  8.     //....... 
  9. }

Tags: php银联网页支付

分享到: