<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. php語言

    php銀聯網頁支付實現方法

    時間:2025-04-22 16:52:36 php語言 我要投稿
    • 相關推薦

    php銀聯網頁支付實現方法

      php銀聯網頁支付實現方法,實例分析了php操作銀聯網支付接口的技巧,具有一定參考借鑒價值,需要的朋友可以參考下.

      這里介紹的銀聯WAP支付功能,僅限消費功能。

      1. PHP代碼如下:

      復制代碼 代碼如下:

      <?php

      namespace common\services;

      class UnionPay

      {

      /**

      * 支付配置

      * @var array

      */

      public $config = [];

      /**

      * 支付參數,提交到銀聯對應接口的所有參數

      * @var array

      */

      public $params = [];

      /**

      * 自動提交表單模板

      * @var string

      */

      private $formTemplate = <<<'HTML'

      <!DOCTYPE HTML>

      <html>

      <head>

      <meta charset="utf-8">

      <title>支付</title>

      </head>

      <body>

      <p style="text-align:center">跳轉中...</p>

      <form id="pay_form" name="pay_form" action="%s" method="post">

      %s

      </form>

      <script type="text/javascript">

      document.onreadystatechange = function(){

      if(document.readyState == "complete") {

      document.pay_form.submit();

      }

      };

      </script>

      </body>

      </html>

      HTML;

      /**

      * 構建自動提交HTML表單

      * @return string

      */

      public function createPostForm()

      {

      $this->params['signature'] = $this->sign();

      $input = '';

      foreach($this->params as $key => $item) {

      $input .= "\t\t<input type=\"hidden\" name=\"{$key}\" value=\"{$item}\">\n";

      }

      return sprintf($this->formTemplate, $this->config['frontUrl'], $input);

      }

      /**

      * 驗證簽名

      * 驗簽規則:

      * 除signature域之外的所有項目都必須參加驗簽

      * 根據key值按照字典排序,然后用&拼接key=value形式待驗簽字符串;

      * 然后對待驗簽字符串使用sha1算法做摘要;

      * 用銀聯公鑰對摘要和簽名信息做驗簽操作

      *

      * @throws \Exception

      * @return bool

      */

      public function verifySign()

      {

      $publicKey = $this->getVerifyPublicKey();

      $verifyArr = $this->filterBeforSign();

      ksort($verifyArr);

      $verifyStr = $this->arrayToString($verifyArr);

      $verifySha1 = sha1($verifyStr);

      $signature = base64_decode($this->params['signature']);

      $result = openssl_verify($verifySha1, $signature, $publicKey);

      if($result === -1) {

      throw new \Exception('Verify Error:'.openssl_error_string());

      }

      return $result === 1 ? true : false;

      }

      /**

      * 取簽名證書ID(SN)

      * @return string

      */

      public function getSignCertId()

      {

      return $this->getCertIdPfx($this->config['signCertPath']);

      }

      /**

      * 簽名數據

      * 簽名規則:

      * 除signature域之外的所有項目都必須參加簽名

      * 根據key值按照字典排序,然后用&拼接key=value形式待簽名字符串;

      * 然后對待簽名字符串使用sha1算法做摘要;

      * 用銀聯頒發的私鑰對摘要做RSA簽名操作

      * 簽名結果用base64編碼后放在signature域

      *

      * @throws \InvalidArgumentException

      * @return multitype|string

      */

      private function sign() {

      $signData = $this->filterBeforSign();

      ksort($signData);

      $signQueryString = $this->arrayToString($signData);

      if($this->params['signMethod'] == 01) {

      //簽名之前先用sha1處理

      //echo $signQueryString;exit;

      $datasha1 = sha1($signQueryString);

      $signed = $this->rsaSign($datasha1);

      } else {

      throw new \InvalidArgumentException('Nonsupport Sign Method');

      }

      return $signed;

      }

      /**

      * 數組轉換成字符串

      * @param array $arr

      * @return string

      */

      private function arrayToString($arr)

      {

      $str = '';

      foreach($arr as $key => $value) {

      $str .= $key.'='.$value.'&';

      }

      return substr($str, 0, strlen($str) - 1);

      }

      /**

      * 過濾待簽名數據

      * signature域不參加簽名

      *

      * @return array

      */

      private function filterBeforSign()

      {

      $tmp = $this->params;

      unset($tmp['signature']);

      return $tmp;

      }

      /**

      * RSA簽名數據,并base64編碼

      * @param string $data 待簽名數據

      * @return mixed

      */

      private function rsaSign($data)

      {

      $privatekey = $this->getSignPrivateKey();

      $result = openssl_sign($data, $signature, $privatekey);

      if($result) {

      return base64_encode($signature);

      }

      return false;

      }

      /**

      * 取.pfx格式證書ID(SN)

      * @return string

      */

      private function getCertIdPfx($path)

      {

      $pkcs12certdata = file_get_contents($path);

      openssl_pkcs12_read($pkcs12certdata, $certs, $this->config['signCertPwd']);

      $x509data = $certs['cert'];

      openssl_x509_read($x509data);

      $certdata = openssl_x509_parse($x509data);

      return $certdata['serialNumber'];

      }

      /**

      * 取.cer格式證書ID(SN)

      * @return string

      */

      private function getCertIdCer($path)

      {

      $x509data = file_get_contents($path);

      openssl_x509_read($x509data);

      $certdata = openssl_x509_parse($x509data);

      return $certdata['serialNumber'];

      }

      /**

      * 取簽名證書私鑰

      * @return resource

      */

      private function getSignPrivateKey()

      {

      $pkcs12 = file_get_contents($this->config['signCertPath']);

      openssl_pkcs12_read($pkcs12, $certs, $this->config['signCertPwd']);

      return $certs['pkey'];

      }

      /**

      * 取驗證簽名證書

      * @throws \InvalidArgumentException

      * @return string

      */

      private function getVerifyPublicKey()

      {

      //先判斷配置的驗簽證書是否銀聯返回指定的證書是否一致

      if($this->getCertIdCer($this->config['verifyCertPath']) != $this->params['certId']) {

      throw new \InvalidArgumentException('Verify sign cert is incorrect');

      }

      return file_get_contents($this->config['verifyCertPath']);

      }

      }

      2. 配置示例

      復制代碼 代碼如下:

      //銀聯支付設置

      'unionpay' => [

      //測試環境參數

      'frontUrl' => 'https://101.231.204.80:5000/gateway/api/frontTransReq.do', //前臺交易請求地址

      //'singleQueryUrl' => 'https://101.231.204.80:5000/gateway/api/queryTrans.do', //單筆查詢請求地址

      'signCertPath' => __DIR__.'/../keys/unionpay/test/sign/700000000000001_acp.pfx', //簽名證書路徑

      'signCertPwd' => '000000', //簽名證書密碼

      'verifyCertPath' => __DIR__.'/../keys/unionpay/test/verify/verify_sign_acp.cer', //驗簽證書路徑

      'merId' => 'xxxxxxx',

      //正式環境參數

      //'frontUrl' => 'https://101.231.204.80:5000/gateway/api/frontTransReq.do', //前臺交易請求地址

      //'singleQueryUrl' => 'https://101.231.204.80:5000/gateway/api/queryTrans.do', //單筆查詢請求地址

      //'signCertPath' => __DIR__.'/../keys/unionpay/test/sign/PM_700000000000001_acp.pfx', //簽名證書路徑

      //'signCertPwd' => '000000', //簽名證書密碼

      //'verifyCertPath' => __DIR__.'/../keys/unionpay/test/verify/verify_sign_acp.cer', //驗簽證書路徑

      //'merId' => 'xxxxxxxxx', //商戶代碼

      ],

      3. 支付示例

      復制代碼 代碼如下:

      $unionPay = new UnionPay();

      $unionPay->config = Yii::$app->params['unionpay'];//上面的配置

      $unionPay->params = [

      'version' => '5.0.0', //版本號

      'encoding' => 'UTF-8', //編碼方式

      'certId' => $unionPay->getSignCertId(), //證書ID

      'signature' => '', //簽名

      'signMethod' => '01', //簽名方式

      'txnType' => '01', //交易類型

      'txnSubType' => '01', //交易子類

      'bizType' => '000201', //產品類型

      'channelType' => '08',//渠道類型

      'frontUrl' => Url::toRoute(['payment/unionpayreturn'], true), //前臺通知地址

      'backUrl' => Url::toRoute(['payment/unionpaynotify'], true), //后臺通知地址

      //'frontFailUrl' => Url::toRoute(['payment/unionpayfail'], true), //失敗交易前臺跳轉地址

      'accessType' => '0', //接入類型

      'merId' => Yii::$app->params['unionpay']['merId'], //商戶代碼

      'orderId' => $orderNo, //商戶訂單號

      'txnTime' => date('YmdHis'), //訂單發送時間

      'txnAmt' => $sum * 100, //交易金額,單位分

      'currencyCode' => '156', //交易幣種

      ];

      $html = $unionPay->createPostForm();

      4. 異步通知示例

      復制代碼 代碼如下:

      $unionPay = new UnionPay();

      $unionPay->config = Yii::$app->params['unionpay'];

      $unionPay->params = Yii::$app->request->post(); //銀聯提交的參數

      if(empty($unionPay->params)) {

      return 'fail!';

      }

      if($unionPay->verifySign() && $unionPay->params['respCode'] == '00') {

      //.......

      }

    【php銀聯網頁支付實現方法】相關文章:

    php頁面緩存實現方法07-20

    PHP實現多線程的方法08-02

    PHP列表頁實現的方法05-24

    PHP多線程的實現方法09-06

    PHP實現多線程的方法09-01

    PHP實現同步遠程Mysql的方法08-31

    PHP實現多線程的方法技巧08-16

    php實現偽靜態的方法實例09-25

    PHP實現獲取域名的方法小結10-05

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人