Xinu

php 建设银行支付接口,支付+验签
<?php /** * Created by PhpStorm. * User: Xiny i@xiny...
扫描右侧二维码阅读全文
27
2018/04

php 建设银行支付接口,支付+验签

<?php
/**
 * Created by PhpStorm.
 * User: Xiny i@xiny9.com
 * Date: 2018-03-15
 * Time: 13:23
 */
class Dragonpay
{
    private $pubstr = '';
    //建行提供的密钥,需要登陆建行商户后台下载
    private $MERCHANTID = '';
    //商户代码
    private $POSID = '';
    //商户柜台代码
    private $BRANCHID = '';
    //银行分行代码
    private $ORDERID = '';
    //订单编号
    private $PAYMENT = '';
    //订单金额
    private $CURCODE = '01';
    //币种
    private $TXCODE = '520100';
    //交易码
    private $REMARK1 = '';
    //备注1
    private $REMARK2 = '';
    //备注2
    private $TYPE = '';
    //接口类型
    private $GATEWAY = '';
    //网关类型
    private $CLIENTIP = '';
    //客户端ip地址
    private $PUB32TR2 = '';
    //公钥后30位
    private $bankURL = '';
    //提交url
    private $REGINFO = '';
    //注册信息
    private $PROINFO = '';
    //商品信息
    private $REFERER = '';
    //商户域名
    private $URL = '';
    //聚合二维码配置---start---------------------
    //二维码返回形式
    private $RETURNTYPE = 3;
    //超时时间
    private $TIMEOUT = '';
    //二维码URL
    private $QRURL = 'https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6&';
    //textcode
    private $TEXTCODE2 = '530550';
    //聚合二维码配置---end------------------------
    private $tmp = '';
    private $temp_New = '';
    private $temp_New1 = '';
    /**
     * Dragonpay constructor.
     * @param $pubkey string 商户的公钥
     * @param $merchantid string 商户号
     * @param $posid string 商户柜台号
     * @param $branchid string 分行代码
     * @param string $order_sn string 订单号
     * @param string $payment string 支付金额
     * @param string $proinfo string 商品信息
     * @param string $remark1 string 备注1 订单id
     * @param string $remark2 string 备注2 订单支付方式
     */
    public function __construct($pubkey, $merchantid, $posid, $branchid, $order_sn = '', $payment = '', $proinfo = '', $remark1 = '', $remark2 = '')
    {
        $this->pubstr = trim($pubkey);
        $this->MERCHANTID = trim($merchantid);
        $this->POSID = trim($posid);
        $this->BRANCHID = trim($branchid);
        $this->ORDERID = $order_sn;
        $this->PAYMENT = $payment;
        $this->CURCODE = '01';
        $this->TXCODE = '520100';
        $this->REMARK1 = $remark1;
        $this->REMARK2 = $remark2;
        $this->bankURL = 'https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain';
        $this->TYPE = 1;
        $this->PUB32TR2 = substr($this->pubstr, -30);
        $this->GATEWAY = '';
        $this->CLIENTIP = '';
        //get_client_ip();
        //可以自己写个方法,我这里自己调用系统里
        $this->REGINFO = '';
        $this->PROINFO = $proinfo;
        $this->REFERER = '';
        //以下为聚合二维码方式需要的参数
        $this->RETURNTYPE = 3;
    }
    /*获取参数值*/
    public function getVar($name)
    {
        return $this->{$name};
    }
    /**
     * @return string 返回建行卡支付访问的url
     */
    public function getUrl()
    {
        $this->tmp = 'MERCHANTID=' . $this->MERCHANTID . '&POSID=' . $this->POSID . '&BRANCHID=' . $this->BRANCHID . '&ORDERID=' . $this->ORDERID . '&PAYMENT=' . $this->PAYMENT . '&CURCODE=' . $this->CURCODE . '&TXCODE=' . $this->TXCODE . '&REMARK1=' . $this->REMARK1 . '&REMARK2=' . $this->REMARK2;
        $this->temp_New = $this->tmp . "&TYPE=" . $this->TYPE . "&PUB=" . $this->PUB32TR2 . "&GATEWAY=" . $this->GATEWAY . "&CLIENTIP=" . $this->CLIENTIP . "&REGINFO=" . $this->REGINFO . "&PROINFO=" . $this->PROINFO . "&REFERER=" . $this->REFERER;
        $this->temp_New1 = $this->tmp . "&TYPE=" . $this->TYPE . "&GATEWAY=" . $this->GATEWAY . "&CLIENTIP=" . $this->CLIENTIP . "&REGINFO=" . $this->REGINFO . "&PROINFO=" . $this->PROINFO . "&REFERER=" . $this->REFERER;
        $strMD5 = md5($this->temp_New);
        $this->URL = $this->bankURL . "?" . $this->temp_New1 . "&MAC=" . $strMD5;
        return $this->URL;
    }
    /*
     * 得到二维码上传的地址
     */
    public function getQrCodeUrl()
    {
        $this->URL = '';
        $this->temp_New = '';
        $this->temp_New1 = '';
        $this->tmp = 'MERCHANTID=' . $this->MERCHANTID . '&POSID=' . $this->POSID . '&BRANCHID=' . $this->BRANCHID . '&ORDERID=' . $this->MERCHANTID . $this->ORDERID . '&PAYMENT=' . $this->PAYMENT . '&CURCODE=' . $this->CURCODE . '&TXCODE=' . $this->TEXTCODE2 . '&REMARK1=' . $this->REMARK1 . '&REMARK2=' . $this->REMARK2;
        $this->temp_New .= $this->tmp . "&RETURNTYPE=" . $this->RETURNTYPE . "&TIMEOUT=" . $this->TIMEOUT . "&PUB=" . $this->PUB32TR2;
        $this->temp_New1 .= $this->tmp . "&RETURNTYPE=" . $this->RETURNTYPE . "&TIMEOUT=" . $this->TIMEOUT;
        $strMD5 = md5($this->temp_New);
        $this->URL = $this->QRURL . $this->temp_New1 . "&MAC=" . $strMD5;
        return $this->URL;
    }
    /*记录支付日志信息*/
    public function writeLog($order, $type)
    {
        $path = ROOT_PATH . '/Log/' . date("Y-m-d") . '/';
        if (!is_dir($path)) {
            mkdir($path, 0755);
        }
        $fp = fopen($path . $order['order_sn'] . '.txt', 'a');
        if (flock($fp, LOCK_EX)) {
            fwrite($fp, "支付发起时间:\r");
            fwrite($fp, date('Y-m-d H:i:s'));
            fwrite($fp, PHP_EOL);
            fwrite($fp, '支付方式:' . $type . PHP_EOL);
            fwrite($fp, "传递url参数信息:" . PHP_EOL);
            fwrite($fp, $type == 'wechat' ? $this->getQrCodeUrl() : $this->getUrl());
            fwrite($fp, PHP_EOL . "记录支付前数据信息:" . PHP_EOL);
            fwrite($fp, "订单号:" . $order['order_sn'] . PHP_EOL . "订单金额:" . $order['order_amount']);
            fwrite($fp, PHP_EOL);
            flock($fp, LOCK_UN);
        }
        fclose($fp);
    }
    /**
     * 更新日志,记录错误信息
     * @param $order_sn string 订单号
     * @param $mess string 存储的信息
     */
    public function writeLogAdd($order_sn, $mess)
    {
        $path = ROOT_PATH . '/Log/' . date("Y-m-d") . '/';
        if (!is_dir($path)) {
            mkdir($path, 0755);
        }
        $fp = fopen($path . $order_sn . '.txt', 'a');
        if (flock($fp, LOCK_EX)) {
            fwrite($fp, PHP_EOL);
            fwrite($fp, $mess . ' Time:' . date('Y-m-d H:i:s'));
            fwrite($fp, PHP_EOL);
            flock($fp, LOCK_UN);
        }
        fclose($fp);
    }
    /** 将公钥转换为pem格式
     * @param $public_key string 公钥内容
     * @return string pem格式证书
     */
    private function der2pem($public_key)
    {
        $pem = chunk_split(base64_encode(hex2bin($public_key)), 64, "\n");
        $pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
        return $pem;
    }
    /**
     * @param $val string 建行返回的字符串不包含sign
     * @param $sign string 签名
     * @return bool
     */
    public function verifySign($val, $sign)
    {
        //$val即是建行返回的参数字符串(特别注意,返回的什么样保持原样,否则验签会失败)
        $public_key = $this->pubstr;
        //公钥串转成pem格式
        $public_key = $this->der2pem($public_key);
        $pkeyid = openssl_get_publickey($public_key);
        $verifyResult = openssl_verify($val, pack("H" . strlen($sign), $sign), $pkeyid, OPENSSL_ALGO_MD5);
        openssl_free_key($pkeyid);
        return $verifyResult == 1 ? true : false;
    }
    /**
     * @param $url
     * @param string $type
     * @param string $paras
     * @param string $refer
     * @return mixed
     */
    public function curlatt($url, $type = 'post', $paras = '', $refer = '')
    {
        #设置执行时间不限时
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_REFERER, $refer);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_SSLVERSION, 0);
        //设置SSL协议版本号
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ('post' == $type) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $paras);
        }
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }
}

基本流程根据参数生成url,访问url
支付完成后,接收反馈的参数,签名验证,实现业务逻辑

Last modification:April 27th, 2018 at 05:42 pm

One comment

  1. php菜鸟

    大佬求步骤有木有啊 做不出来

Leave a Comment