侧边栏壁纸
博主昵称
梦之中小俊

以前高中时,羡慕大学考试只要及格就行;现在大学了,怀念高中考试及不及格都行??

PHP

微信支付 V3 API

梦之中小俊
2023-06-09 / 0 评论 / 311 阅读 / 推送成功!
appid = '';//小程序appid
    $this->secret = '';//小程序secret
    $this->mch_id = '';//商户id
    $this->key = '';//v3key
    $this->serial_no = '';//v3序列号
}
public function ffff(){
   $rrr=  $this->unifiedOrder(1,'oDdl-6TWLTkclAZVNh7U1y7-kX4c',rand(1000,9999));
    return json(['code' => 200, 'msg' => '获取成功', 'data' => $rrr]);
}
/**
     * 下单方法
     * @param   $params 下单参数
     */
    public function unifiedOrder($product_type, $openid, $phone = '')
    {
        //订单总价
        $total_fee = 79900;
        if ($product_type == 1) {
            $total_fee = 79900;
        }
        if ($product_type == 2) {
            $total_fee = 99900;
        }
        $money = $total_fee;
        $url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
        $urlarr = parse_url($url);
        $appid =  '';
      
        $mchid ='';//商户id
        $xlid = '';//秘钥序列号
        $data = [];
        $time = time();
        $data['appid'] = $appid;
        $data['mchid'] = $mchid;
        $data['description'] = '描述';//商品描述
        $data['out_trade_no'] = $phone . '_' . time() . '_xingji';//订单编号
        //$data['notify_url'] = $this->request->domain() . "/api/xjmpzl/notify";//回调接口 需根据自己的情况修改
        $data['notify_url'] =  "/notify";//回调接口 需根据自己的情况修改
        //$money = 1;
        $data['amount']['total'] = $money;//金额 单位 分
        $data['payer']['openid'] = $openid;//用户openID
      
        $data = json_encode($data);
        $noncestr = $time;
        $key = $this->getSign($data, $urlarr['path'], $noncestr, $time);//签名
     
        $token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"', $mchid, $xlid, $noncestr, $time, $key);//头部信息
        $header = [
            '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);
        
        $ret = ltrim($ret, '{"prepay_id":"');
        $ret = rtrim($ret, '}"');
        //微信支付(小程序)签名
        $str = $this->getWechartSign($appid, time(), $noncestr, 'prepay_id=' . $ret);
        
        $arr = [
            'appId' => $appid,
            'timeStamp' => time() . "",
            'package' => 'prepay_id=' . $ret,
            'paySign' => $str,
            'nonceStr' => $noncestr . ""
        ];
        
        
        
        
        return $arr;
      
    }

    //微信支付签名
    public function getSign($data = [], $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;
    }

    //调起支付的签名
    public 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;
    }

    public function getSha256WithRSA($content, $privateKey)
    {
        $binary_signature = "";
        $algo = "SHA256";
        openssl_sign($content, $binary_signature, $privateKey, $algo);
        $sign = base64_encode($binary_signature);
        return $sign;
    }

    /* PHP CURL HTTPS POST */
    public function curl_post_https($url, $data, $header)
    {
       $action = curl_init();
       curl_setopt($action, CURLOPT_URL, $url);
       curl_setopt($action, CURLOPT_CONNECTTIMEOUT, 60);
       curl_setopt($action, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($action, CURLOPT_HEADER, 0);
       curl_setopt($action, CURLOPT_SSL_VERIFYPEER, 0);
       curl_setopt($action, CURLOPT_SSL_VERIFYHOST, 0);
       curl_setopt($action, CURLOPT_POST, 1);
       curl_setopt($action, CURLOPT_POSTFIELDS, $data);
       curl_setopt($action, CURLOPT_HTTPHEADER, $header);
       $result = curl_exec($action);
       curl_close($action);
       return $result;
    }
}


本文共 个字数,平均阅读时长 ≈ 分钟,您已阅读:0时0分0秒。
3

打赏

评论 (0)

OωO
  • ::(呵呵)
  • ::(哈哈)
  • ::(吐舌)
  • ::(太开心)
  • ::(笑眼)
  • ::(花心)
  • ::(小乖)
  • ::(乖)
  • ::(捂嘴笑)
  • ::(滑稽)
  • ::(你懂的)
  • ::(不高兴)
  • ::(怒)
  • ::(汗)
  • ::(黑线)
  • ::(泪)
  • ::(真棒)
  • ::(喷)
  • ::(惊哭)
  • ::(阴险)
  • ::(鄙视)
  • ::(酷)
  • ::(啊)
  • ::(狂汗)
  • ::(what)
  • ::(疑问)
  • ::(酸爽)
  • ::(呀咩爹)
  • ::(委屈)
  • ::(惊讶)
  • ::(睡觉)
  • ::(笑尿)
  • ::(挖鼻)
  • ::(吐)
  • ::(犀利)
  • ::(小红脸)
  • ::(懒得理)
  • ::(勉强)
  • ::(爱心)
  • ::(心碎)
  • ::(玫瑰)
  • ::(礼物)
  • ::(彩虹)
  • ::(太阳)
  • ::(星星月亮)
  • ::(钱币)
  • ::(茶杯)
  • ::(蛋糕)
  • ::(大拇指)
  • ::(胜利)
  • ::(haha)
  • ::(OK)
  • ::(沙发)
  • ::(手纸)
  • ::(香蕉)
  • ::(便便)
  • ::(药丸)
  • ::(红领巾)
  • ::(蜡烛)
  • ::(音乐)
  • ::(灯泡)
  • ::(开心)
  • ::(钱)
  • ::(咦)
  • ::(呼)
  • ::(冷)
  • ::(生气)
  • ::(弱)
  • ::(狗头)
泡泡
阿鲁
颜文字
取消
  1. 头像
    6767 Lv.1
    iPhone · Safari
    沙发

    画图

    回复
  2. 头像
    6767 Lv.1
    Windows 10 · Google Chrome
    板凳

    表情

    回复
  3. 头像
    透露 Lv.2
    Android · QQ Browser
    第30楼

    649494848

    回复