最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php銀聯(lián)網(wǎng)頁(yè)支付實(shí)現(xiàn)方法

 更新時(shí)間:2015年03月04日 12:03:07   作者:OSC首席鍵客  
這篇文章主要介紹了php銀聯(lián)網(wǎng)頁(yè)支付實(shí)現(xiàn)方法,實(shí)例分析了php操作銀聯(lián)網(wǎng)支付接口的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了php銀聯(lián)網(wǎng)頁(yè)支付實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:
這里介紹的銀聯(lián)WAP支付功能,僅限消費(fèi)功能。

1. PHP代碼如下:

復(fù)制代碼 代碼如下:
<?php
namespace common\services;
class UnionPay
{
    /**
     * 支付配置
     * @var array
     */
    public $config = [];
    /**
     * 支付參數(shù),提交到銀聯(lián)對(duì)應(yīng)接口的所有參數(shù)
     * @var array
     */
    public $params = [];
    /**
     * 自動(dòng)提交表單模板
     * @var string
     */
    private $formTemplate = <<<'HTML'
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>支付</title>
</head>
<body>
    <div style="text-align:center">跳轉(zhuǎn)中...</div>
    <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;
/**
* 構(gòu)建自動(dòng)提交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);
}
/**
* 驗(yàn)證簽名
* 驗(yàn)簽規(guī)則:
* 除signature域之外的所有項(xiàng)目都必須參加驗(yàn)簽
* 根據(jù)key值按照字典排序,然后用&拼接key=value形式待驗(yàn)簽字符串;
* 然后對(duì)待驗(yàn)簽字符串使用sha1算法做摘要;
* 用銀聯(lián)公鑰對(duì)摘要和簽名信息做驗(yàn)簽操作
*
* @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;
}
/**
* 取簽名證書(shū)ID(SN)
* @return string
*/
public function getSignCertId()
{
        return $this->getCertIdPfx($this->config['signCertPath']);
}  
/**
* 簽名數(shù)據(jù)
* 簽名規(guī)則:
* 除signature域之外的所有項(xiàng)目都必須參加簽名
* 根據(jù)key值按照字典排序,然后用&拼接key=value形式待簽名字符串;
* 然后對(duì)待簽名字符串使用sha1算法做摘要;
* 用銀聯(lián)頒發(fā)的私鑰對(duì)摘要做RSA簽名操作
* 簽名結(jié)果用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;
}
/**
* 數(shù)組轉(zhuǎn)換成字符串
* @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);
}
/**
* 過(guò)濾待簽名數(shù)據(jù)
* signature域不參加簽名
*
* @return array
*/
private function filterBeforSign()
{
        $tmp = $this->params;
        unset($tmp['signature']);
        return $tmp;
}
/**
* RSA簽名數(shù)據(jù),并base64編碼
* @param string $data 待簽名數(shù)據(jù)
* @return mixed
*/
private function rsaSign($data)
{
        $privatekey = $this->getSignPrivateKey();
        $result = openssl_sign($data, $signature, $privatekey);
        if($result) {
            return base64_encode($signature);
        }
        return false;
}
/**
* 取.pfx格式證書(shū)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格式證書(shū)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'];
}
/**
* 取簽名證書(shū)私鑰
* @return resource
*/
private function getSignPrivateKey()
{
        $pkcs12 = file_get_contents($this->config['signCertPath']);
        openssl_pkcs12_read($pkcs12, $certs, $this->config['signCertPwd']);
        return $certs['pkey'];
}
/**
* 取驗(yàn)證簽名證書(shū)
* @throws \InvalidArgumentException
* @return string
*/
private function getVerifyPublicKey()
{
        //先判斷配置的驗(yàn)簽證書(shū)是否銀聯(lián)返回指定的證書(shū)是否一致
        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. 配置示例    
復(fù)制代碼 代碼如下:
//銀聯(lián)支付設(shè)置
 'unionpay' => [
     //測(cè)試環(huán)境參數(shù)
     'frontUrl' => 'https://101.231.204.80:5000/gateway/api/frontTransReq.do', //前臺(tái)交易請(qǐng)求地址
     //'singleQueryUrl' => 'https://101.231.204.80:5000/gateway/api/queryTrans.do', //單筆查詢請(qǐng)求地址
     'signCertPath' => __DIR__.'/../keys/unionpay/test/sign/700000000000001_acp.pfx', //簽名證書(shū)路徑
     'signCertPwd' => '000000', //簽名證書(shū)密碼
     'verifyCertPath' => __DIR__.'/../keys/unionpay/test/verify/verify_sign_acp.cer', //驗(yàn)簽證書(shū)路徑
     'merId' => 'xxxxxxx',
     //正式環(huán)境參數(shù)
     //'frontUrl' => 'https://101.231.204.80:5000/gateway/api/frontTransReq.do', //前臺(tái)交易請(qǐng)求地址
     //'singleQueryUrl' => 'https://101.231.204.80:5000/gateway/api/queryTrans.do', //單筆查詢請(qǐng)求地址
     //'signCertPath' => __DIR__.'/../keys/unionpay/test/sign/PM_700000000000001_acp.pfx', //簽名證書(shū)路徑
     //'signCertPwd' => '000000', //簽名證書(shū)密碼
     //'verifyCertPath' => __DIR__.'/../keys/unionpay/test/verify/verify_sign_acp.cer', //驗(yàn)簽證書(shū)路徑
     //'merId' => 'xxxxxxxxx', //商戶代碼
 ],

3. 支付示例    
復(fù)制代碼 代碼如下:
$unionPay = new UnionPay();
$unionPay->config = Yii::$app->params['unionpay'];//上面的配置
$unionPay->params = [
    'version' => '5.0.0', //版本號(hào)
    'encoding' => 'UTF-8', //編碼方式
    'certId' => $unionPay->getSignCertId(), //證書(shū)ID
    'signature' => '', //簽名
    'signMethod' => '01', //簽名方式
    'txnType' => '01', //交易類型
    'txnSubType' => '01', //交易子類
    'bizType' => '000201', //產(chǎn)品類型
    'channelType' => '08',//渠道類型
    'frontUrl' => Url::toRoute(['payment/unionpayreturn'], true), //前臺(tái)通知地址
    'backUrl' => Url::toRoute(['payment/unionpaynotify'], true), //后臺(tái)通知地址
    //'frontFailUrl' => Url::toRoute(['payment/unionpayfail'], true), //失敗交易前臺(tái)跳轉(zhuǎn)地址
    'accessType' => '0', //接入類型
    'merId' => Yii::$app->params['unionpay']['merId'], //商戶代碼
    'orderId' => $orderNo, //商戶訂單號(hào)
    'txnTime' => date('YmdHis'), //訂單發(fā)送時(shí)間
    'txnAmt' => $sum * 100, //交易金額,單位分
    'currencyCode' => '156', //交易幣種
];
$html = $unionPay->createPostForm();

4. 異步通知示例
復(fù)制代碼 代碼如下:
$unionPay = new UnionPay();
$unionPay->config = Yii::$app->params['unionpay'];
$unionPay->params = Yii::$app->request->post(); //銀聯(lián)提交的參數(shù)
if(empty($unionPay->params)) {
    return 'fail!';
}
if($unionPay->verifySign() && $unionPay->params['respCode'] == '00') {
    //.......
}

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • php中3des加密代碼(完全與.net中的兼容)

    php中3des加密代碼(完全與.net中的兼容)

    php中3des加密的結(jié)果與.Net/java不同的帖子與話題實(shí)在是太多了,我前不久也在倒騰這些,不過(guò)今天已經(jīng)搞定了,完全與.net中的兼容
    2012-08-08
  • PHP實(shí)踐教程之過(guò)濾、驗(yàn)證、轉(zhuǎn)義與密碼詳解

    PHP實(shí)踐教程之過(guò)濾、驗(yàn)證、轉(zhuǎn)義與密碼詳解

    我們?cè)陂_(kāi)發(fā)應(yīng)用時(shí),一般有個(gè)約定:不要信任任何來(lái)自不受自己控制的數(shù)據(jù)源中的數(shù)據(jù)。所以這個(gè)時(shí)候就用到了這篇文章介紹的內(nèi)容,本文主要給大家介紹了關(guān)于PHP實(shí)踐教程之過(guò)濾、驗(yàn)證、轉(zhuǎn)義與密碼的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-07-07
  • PHP實(shí)現(xiàn)PDO的mysql數(shù)據(jù)庫(kù)操作類

    PHP實(shí)現(xiàn)PDO的mysql數(shù)據(jù)庫(kù)操作類

    這篇文章主要介紹了PHP實(shí)現(xiàn)PDO的mysql數(shù)據(jù)庫(kù)操作類,其中dbconfig類負(fù)責(zé)配置數(shù)據(jù)庫(kù)訪問(wèn)信息,dbtemplate類集合了對(duì)數(shù)據(jù)庫(kù)的訪問(wèn)操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-12-12
  • PHP的中使用非緩沖模式查詢數(shù)據(jù)庫(kù)的方法

    PHP的中使用非緩沖模式查詢數(shù)據(jù)庫(kù)的方法

    緩沖查詢和非緩沖查詢(Buffered and Unbuffered queries)。PHP的查詢?nèi)笔∧J绞蔷彌_模式。也就是說(shuō),查詢數(shù)據(jù)結(jié)果會(huì)一次全部提取到內(nèi)存里供PHP程序處理,需要的朋友可以參考下
    2017-02-02
  • 關(guān)于PHP5 Session生命周期介紹

    關(guān)于PHP5 Session生命周期介紹

    PHP5有很多值得學(xué)習(xí)的地方,這里我們主要介紹PHP5 Session的使用,Session 是如何來(lái)判斷客戶端用戶的呢?
    2010-03-03
  • php安裝grpc擴(kuò)展的具體步驟

    php安裝grpc擴(kuò)展的具體步驟

    在本篇文章里小編給大家整理的是一篇關(guān)于php安裝grpc擴(kuò)展的具體步驟,有需要的朋友們可以跟著學(xué)習(xí)參考下。
    2021-07-07
  • php生成QRcode實(shí)例

    php生成QRcode實(shí)例

    這篇文章主要介紹了php生成QRcode實(shí)例,可實(shí)現(xiàn)生成二維碼的功能,是一個(gè)非常實(shí)用的技巧,需要的朋友可以參考下
    2014-09-09
  • PHP5.3.1 不再支持ISAPI

    PHP5.3.1 不再支持ISAPI

    今天發(fā)現(xiàn)PHP5.3.1發(fā)布了,但是安裝的時(shí)候沒(méi)有找到ISAPI模式,安裝后也沒(méi)有找到php5isapi.dll這個(gè)文件,找了好久,終于弄清楚。
    2010-01-01
  • php自定義錯(cuò)誤處理用法實(shí)例

    php自定義錯(cuò)誤處理用法實(shí)例

    這篇文章主要介紹了php自定義錯(cuò)誤處理用法,實(shí)例分析了php通過(guò)自定義函數(shù)進(jìn)行錯(cuò)誤處理的技巧,需要的朋友可以參考下
    2015-03-03
  • php驗(yàn)證碼生成器

    php驗(yàn)證碼生成器

    這篇文章主要為大家詳細(xì)介紹了php驗(yàn)證碼生成器的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05

最新評(píng)論

霍邱县| 阜南县| 铜陵市| 广南县| 商都县| 甘泉县| 连江县| 宁武县| 双桥区| 伊春市| 神农架林区| 民丰县| 会东县| 积石山| 亳州市| 新田县| 平阴县| 富源县| 湘乡市| 靖安县| 丁青县| 松潘县| 康马县| 宜春市| 银川市| 游戏| 米脂县| 洱源县| 农安县| 平罗县| 苍山县| 任丘市| 荃湾区| 芷江| 安多县| 原阳县| 盖州市| 溆浦县| 湟中县| 泰安市| 呼图壁县|