WeChatDeveloper发起H5微信支付

      发布在:后端技术      评论:0 条评论

参考:https://www.kancloud.cn/zoujingli/wechat-developer/521163

 $jine      = $this->request->param('jine');
       $order_sn
= date("ymdHis",time()).rand(10,99);
       
try {

           
require_once "../ThirdParty/WeChatDeveloper/include.php";
           $config
=include CMF_ROOT."data/conf/wxpay.php";

           
// 3. 创建接口实例
           // $wechat = new \WeChat\Pay($config);
           // $wechat = \We::WeChatPay($config);
           $wechat = \WeChat\Pay::instance($config);

           
// 4. 组装参数,可以参考官方商户文档
           $options = [
               
'body'             => '会员充值',
               
'out_trade_no'     => $order_sn,
               
'total_fee'        => $jine*100,
//                'openid'           => 'o38gpszoJoC9oJYz3UHHf6bEp0Lo',
//                'trade_type'       => 'JSAPI',
               'trade_type'       => 'MWEB',
               
'notify_url'       => url('portal/index/wxnotify','',false,true),
               
'spbill_create_ip' => $this->request->ip(),
           ];
           
// 生成预支付码
           $result = $wechat->createOrder($options);
           
// 创建JSAPI参数签名
           $options = $wechat->createParamsForJsApi($result['prepay_id']);
           $user
= cmf_get_current_user();
           $data1
= array();
           $data1[
'user_id'] = $user['id'];
           $data1[
'money'] = $jine;
           $data1[
'style'] = '充值';
           $data1[
'content'] = '用户微信充值收入';
           $data1[
'pay_time'] = time();
           $data1[
'order_id'] = $order_sn;
           
Db::name("user_moneyrecord")->insert($data1);
           
if($result['mweb_url']){
//                加入
exit(json_encode(['error'=>0,'msg'=>'获取成功','data'=>['mweb_url'=>$result['mweb_url'].'&redirect_url='.urlencode(url('wap/user/index','',false,true))]]));
           }
//            --- 创建预支付码 ---
//array (
//  'return_code' => 'SUCCESS',
//  'return_msg' => 'OK',
//  'result_code' => 'SUCCESS',
//  'mch_id' => '1524092251',
//  'appid' => 'wx57c46650e6c7cc11',
//  'nonce_str' => 'T13ebQLQBvPh339d',
//  'sign' => '13A51ECE8D1129E261708130B51A2C80',
//  'prepay_id' => 'wx2812295288640181430b23adbe78f80000',
//  'trade_type' => 'MWEB',
//  'mweb_url' => 'https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx2812295288640181430b23adbe78f80000&package=3800536694',
//)
//
//--- JSAPI H5 参数 ---
//array (
//  'appId' => 'wx57c46650e6c7cc11',
//  'timeStamp' => '1640665792',
//  'nonceStr' => 'eud4ak9d5rhf2sq3wdiiur8jbcidua1t',
//  'package' => 'prepay_id=wx2812295288640181430b23adbe78f80000',
//  'signType' => 'MD5',
//  'paySign' => 'E3CBF51A0BEF5F590BA924ABC3084045',
//  'timestamp' => '1640665792',
//)
           echo '<pre>';
           
echo "\n--- 创建预支付码 ---\n";
           
var_export($result);

           
echo "\n\n--- JSAPI H5 参数 ---\n";
           
var_export($options);

       }
catch (Exception $e) {

           
// 出错啦,处理下吧
           echo $e->getMessage() . PHP_EOL;

       }

微信H5支付官方文档:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_4

相关文章
热门推荐