1
回答
翼支付PHP TP5的demo
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
use phpseclib\Crypt\AES;
use phpseclib\Crypt\RSA;

function bestpay_back(){
        $apiInfo=Db::name('api')->find($this->apiId);
        $str='UPTRANSEQ='.$this->data['UPTRANSEQ'].'&MERCHANTID='.$this->data['MERCHANTID'].'&ORDERSEQ='.$this->data['ORDERSEQ'].'&ORDERAMOUNT='.$this->data['ORDERAMOUNT'].'&RETNCODE='.$this->data['RETNCODE'].'&RETNINFO='.$this->data['RETNINFO'].'&TRANDATE='.$this->data['TRANDATE'].'&KEY='.$apiInfo['merchantkey'];
        $SIGN=strtoupper(md5($str));
        if($SIGN!=$this->data['SIGN']) die('校验失败!');
        $oid=substr($this->data['ORDERSEQ'],14);

        $order=Db::name('order')->find($oid);
        if($order['pay_status']!=0)die('订单已支付!');

        Db::name('order')->where('id',$oid)->update(['pay_back_info'=>json_encode($this->data),'pay_status'=>1]);


        $apiInfo=Db::name('api')->find($this->apiId);
        $apiPay=$this->apiPost('standard/order-pay',['body'=>['oid'=>$order['api_oid'],'pay_type'=>$apiInfo['pay_type'],'no_send'=>1]]);
        if($apiPay['code']!='200') return $this->retErr('支付成功、出票失败,请联系客服!');

        die('UPTRANSEQ_'.$this->data['UPTRANSEQ']);

    }

    protected  function bestpay($order)
    {
        $apiInfo=Db::name('api')->find($this->apiId);
        $MerchantPwd=$apiInfo['merchantpwd'];
        $URL="https://capi.bestpay.com.cn";

        //步骤1、调用下单接口、返回下单结果
        $orderseq = date('YmdHis').$order['id']; //订单号,唯一
        $orderdate = date('Ymd',strtotime($order['crtime']));
        $ordereqtranseq = $order['id'] . 'a' . time().mt_rand(1000,9999); //订单请求流水号,唯一
        $subject="门票支付"; //商品描述

        $order_amount= (int)(((float)$order['pay_money'])*100);
        $time=date('YmdHis',strtotime($order['crtime']));
        //$mac = 'MERCHANTID='.$apiInfo['merchantid'].'&ORDERSEQ='.$orderseq.'&ORDERREQTRANSEQ='.$ordereqtranseq.'&ORDERREQTIME='.$time.'&KEY='.$payment['bestpay_key'];
        $mac = 'MERCHANTID='.$apiInfo['merchantid'].'&ORDERSEQ='.$orderseq.'&ORDERREQTRANSEQ='.$ordereqtranseq.'&ORDERREQTIME='.$time.'&KEY='.$apiInfo['merchantkey'];
        $mac = strtoupper(md5($mac));



        $order_params=array(
            'MERCHANTID' => $apiInfo['merchantid'],//由翼支付网关平台统一分配给各接入商户
            'SUBMERCHANTID'=>'',    //由商户平台自己分配,如没有可以不填写
            'ORDERSEQ'=>$orderseq,  //订单号,唯一
            'ORDERREQTRANSEQ'=>$ordereqtranseq, //订单请求流水号,唯一
            'ORDERREQTIME'=>$time,    //订单请求时间
            'TRANSCODE'=>'01',  //交易代码,固定:01
            'ORDERAMT'=>$order_amount, //订单金额(分)
            'ORDERCCY'=>'RMB', //币种补充 固定:RMB
            'SERVICECODE'=>'05', //接入渠道固定:05
            'PRODUCTID'=>'04',  //商品代码,固定:04
            'PRODUCTDESC'=>$subject, //商品描述
            'requestSystem'=>'1', //请求来源,固定:1 此参数必传, 请关注
            'MAC'=>$mac //MAC验证信息,需采用ENCODETYPE代表的加密方式加密
        );
        $paramsJoined = array();
        foreach($order_params as $param => $value)  $paramsJoined[] = "$param=$value";
        $paramData = implode('&', $paramsJoined);

        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL                =>  "https://webpaywg.bestpay.com.cn/order.action",
            CURLOPT_POST               =>  1,
            CURLOPT_SSL_VERIFYPEER     =>  FALSE,
            CURLOPT_SSL_VERIFYHOST     =>  FALSE,
            CURLOPT_RETURNTRANSFER     =>  1,
            CURLOPT_POSTFIELDS         =>  $paramData
        ));

        $data = curl_exec($ch);
        curl_close($ch);

        if($data!="00&手机客户端下单成功") return ['status'=>0,'message'=>'翼支付订单下单失败!']; //return '翼支付订单下单失败';

        //步骤2、调用h5通用Http接口获取公钥  3.6.1.获取公钥接口
        $params=array('keyIndex' => '','encryKey' => '','encryStr' => '', 'interCode' => 'INTER.SYSTEM.001');
        $paramData=json_encode($params);

        $ch = curl_init();
        $header =  array('Content-Type: application/json');
        curl_setopt_array($ch, array(
            CURLOPT_URL                =>  $URL."/common/interface",
            CURLOPT_HTTPHEADER         =>  $header,
            CURLOPT_POST               =>  1,
            CURLOPT_SSL_VERIFYPEER     =>  FALSE,
            CURLOPT_SSL_VERIFYHOST     =>  FALSE,
            CURLOPT_RETURNTRANSFER     =>  1,
            CURLOPT_POSTFIELDS         =>  $paramData
        ));

        $data = curl_exec($ch);
        curl_close($ch);

        $res_arr = json_decode($data,true);
        if($res_arr && $res_arr['success'])
        {

            $keyIndex=$res_arr['result']['keyIndex'];
            $pubKey=$res_arr['result']['pubKey'];
            $time=date('YmdHis');
            $pay_params=array(
                'SERVICE' => 'mobile.securitypay.pay',
                'MERCHANTID' => $apiInfo['merchantid'],
                'MERCHANTPWD' => $MerchantPwd,
                'BEFOREMERCHANTURL' => $this->conf['site_url'].Url::build('pay/bestpay_before',['product_id'=>$order['product_id']]),//rul/book/product_id/10541
                'BACKMERCHANTURL' => $this->conf['site_url'].Url::build('pay/bestpay_back'),
                'ORDERSEQ'=>$orderseq,
                'ORDERREQTRANSEQ'=>$ordereqtranseq,
                'ORDERTIME'=>"$time",
                'CURTYPE'=>'RMB',
                'ORDERAMOUNT'=>$order_amount/100,
                'SUBJECT'=>$subject,
                'PRODUCTID'=>'04',
                'SIGNTYPE'=>'MD5',
                'PRODUCTDESC'=>$subject,
                'PRODUCTAMOUNT'=>$order_amount/100,
                'ATTACHAMOUNT'=>'0',
                'CUSTOMERID'=>'1',
                'BUSITYPE'=>'04',
                'SWTICHACC'=>'false'
            );

            $paramsJoined = array();
            foreach($pay_params as $param => $value) {
                $paramsJoined[] = "$param=$value";
            }
            $pay_paramData = implode('&', $paramsJoined);

            $sign_params=array(
                'SERVICE' => 'mobile.securitypay.pay',
                'MERCHANTID' => $apiInfo['merchantid'],
                'MERCHANTPWD' => $MerchantPwd,
                'SUBMERCHANTID' => '',
                'BACKMERCHANTURL'=>$this->conf['site_url'].Url::build('pay/bestpay_back'),
                'ORDERSEQ'=>$orderseq,
                'ORDERREQTRANSEQ'=>$ordereqtranseq,
                'ORDERTIME'=>"$time",
                'ORDERVALIDITYTIME'=>'',
                'CURTYPE'=>'RMB',
                'ORDERAMOUNT'=>$order_amount/100,
                'SUBJECT'=>$subject,
                'PRODUCTID'=>'04',
                'PRODUCTDESC'=>$subject,
                'CUSTOMERID'=>'1',
                'SWTICHACC'=>'false',
                'KEY'=>$apiInfo['merchantkey']
            );


            $paramsJoined = array();
            foreach($sign_params as $param => $value) {
                $paramsJoined[] = "$param=$value";
            }
            $sign_paramData = implode('&', $paramsJoined);





            $sign = strtoupper(md5($sign_paramData));

            $pay_paramData.="&SIGN=".$sign;


            $random_key=md5(mt_rand());
            //$random_key=$res_arr['result']['aesRandomId'];
            $cipher = new AES();
            $cipher->setKey($random_key);
            $crypttext = base64_encode($cipher->encrypt($pay_paramData));

            $rsa = new RSA();
            $rsa->loadKey($pubKey);
            $rsa->setEncryptionMode(2);
            $encrypted = base64_encode($rsa->encrypt($random_key));

            $webUrl = $URL."/gateway.pay?platform=wap_3.0&encryStr=".$crypttext."&keyIndex=".$keyIndex."&encryKey=".$encrypted;
            $webUrl = str_replace("+","%2B",$webUrl);

            //echo $webUrl;die;
            //$this->redirect($webUrl);
            //return ['status'=>200,'url'=>$webUrl];
            return ['status'=>1,'info'=>$webUrl];

        }
        //return '翼支付订单提交失败!';
        return ['status'=>0,'message'=>'翼支付订单提交失败!'];
    }

 

$this->data 是统一的Request数组

$apiInfo 里面主要是两个 merchantid 和 merchantkey  参数,都是翼支付提供的

 

bestpay_back 翼支付的回调 , bestpay 是生成支付url ,我是前台跳转的

 

https://github.com/phpseclib/phpseclib

举报
风弥
发帖于5个月前 1回/233阅
顶部