微信小程序APIv3支付接口

class Weixinpay{
    protected $appid;
    protected $mch_id;
    protected $key;
    protected $openid;
    protected $out_trade_no;
    protected $body;
    protected $total_fee;
    protected $backurl;
    protected $noncestr;

    function __construct($appid, $openid, $mch_id, $key,$out_trade_no,$body,$total_fee,$backurl) {
        $this->appid = $appid;
        $this->openid = $openid;
        $this->mch_id = $mch_id;
        $this->key = $key;
        $this->out_trade_no = $out_trade_no;
        $this->body = $body;
        $this->total_fee = $total_fee;
        $this->backurl = $backurl;
        $this->noncestr = $this->createNoncestr();
    }
    public function pay() {
        $res = db('order')->where(['order_no'=>$this->out_trade_no,'pay_status'=>1])->count();
        if($res){
            returnError('该笔订单已支付');
        }
        $return = $this->weixinapp();
        return $return;
    }
    //微信小程序接口
    private function weixinapp() {
        //统一下单接口
        $url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi';
        $urlarr = parse_url($url);
        $timeStamp = $time = time();
        //$xlid = '微信支付公钥序列号';//秘钥序列号 可在这个网址中查询 https://myssl.com/cert_decode.html
        $xlid = 'XXXXXXXX';
        $data = array(
            'appid' => $this->appid, //小程序ID
            'mchid' => $this->mch_id, //商户号
            'description' => $this->body,//商品描述
            'out_trade_no'=> $this->out_trade_no,//商户订单号
            'amount' => array('total' =>$this->total_fee,'currency' => 'CNY'),//总金额 单位 分
            'notify_url' => $this->backurl, //通知地址  确保外网能正常访问
            'payer' => array('openid' => $this->openid),//用户id
        );
        $data = json_encode($data);
        $key = $this->getSign($data,$urlarr['path'],$this->noncestr,$time);//签名
        $token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"',$this->mch_id,$xlid,$this->noncestr,$time,$key);//头部信息
        $header  = array(
            'Content-Type:'.'application/json; charset=UTF-8',
            'Accept:application/json',
            'User-Agent:*/*',
            'Authorization: WECHATPAY2-SHA256-RSA2048 '.$token
        );
        $ret = $this->curl_post_https($url,$data,$header);
        $res = json_decode($ret,true);
        //微信支付(小程序)签名
        $str = $this->getWechartSign($this->appid,$timeStamp,$this->noncestr,'prepay_id='.$res['prepay_id']);
        $arr = array('appid'=>$this->appid,'timestamp'=>$timeStamp,'nonceStr'=>$this->noncestr,'package'=>'prepay_id='.$res['prepay_id'],'paySign'=>$str);
        return $arr;
    }
    //作用:产生随机字符串,不长于32位
    private function createNoncestr($length = 32) {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
            for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
    //作用:微信下单签名
    private function getSign($data=array(),$url,$randstr,$time) {
        $str = "POST"."\n".$url."\n".$time."\n".$randstr."\n".$data."\n";
        $key = file_get_contents('./apiclient_key.pem');//在商户平台下载的秘钥
        $str = $this->getSha256WithRSA($str,$key);
        return $str;
    }
    //调起支付的签名
    function getWechartSign($appid,$timeStamp,$noncestr,$prepay_id){
        $str = $appid."\n".$timeStamp."\n".$noncestr."\n".$prepay_id."\n";
        $key = file_get_contents('./apiclient_key.pem');
        $str = $this->getSha256WithRSA($str,$key);
        return $str;
    }
    function getSha256WithRSA($content, $privateKey){
        $binary_signature = "";
        openssl_sign($content, $binary_signature, $privateKey, "SHA256");
        $sign = base64_encode($binary_signature);
        return $sign;
    }
    function curl_post_https($url,$data,$header){ // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据,json格式
    }

}

你可能感兴趣的:(后端,php,小程序)