小程序微信支付

2020-10-26  本文已影响0人  Stonesy

小程序前端JS代码

import {
  zf
} from '../../api/bx.js';
//通过zf  API(创建zf   API阅读绑定执行event(‘方法’,【参数】)); 获取timeStamp,nonceStr,package,signType,paySign
zf(value).then(res => {
      console.log("数据");
      var jsConfig = res.data.result;
      wx.requestPayment({
        timeStamp: jsConfig.timestamp,
        nonceStr: jsConfig.nonceStr,
        package: jsConfig.package,
        signType: jsConfig.signType,
        paySign: jsConfig.paySign,
        success: function (res) {
          console.log('成功失败');
          console.log(res);
          

        },
        fail: function (e) {
          console.log('成功失败');
          console.log(e);
          wx.hideLoading();
          return app.Tips({ title: '取消支付' }, { tab: 5, url: goPages + '&status=2' });
        },
        complete: function (e) {
          console.log('失败');
          console.log(e);
          wx.hideLoading();
          //关闭当前页面跳转至订单状态
          if (res.errMsg == 'requestPayment:cancel') return app.Tips({ title: '取消支付' }, { tab: 5, url: goPages + '&status=2' });
        },
      })
    }).catch(msg => {
      console.log("失败");
      console.log(msg);
    });

后台创建好的API

public function zf(Request $request){
         list($openid, $money) = UtilService::postMore([
            ['openid', ''],['money', '']
        ], $request, true);
        $openid = WechatUser::uidToRoutineOpenid($openid);
        
        // var_dump(WechatUser::uidToRoutineOpenid($openid));
        // exit();
         $openid = $openid; //"用户的openid" ; 
         $money = $money;//"需要支付的金额" ;  // 
        (!$openid) &&  $this->response(500,"参数错误");
        (!$money)  &&  $this->response(500,"选择金额错误!");
        $orderCode = date("YmdHis");   //  订单号
//        随机字符串
        $str="QWERTYUIPADGHJKLZXCVNM1234567890";
        $nonce = str_shuffle($str);
        $appid = 'wxdd3f0df653716910';  
        $key = "ZfshuNh1w27tBsy9iehdgwR568kBYD5B";  
        $mch_id = "1592248421";  
        $pay['appid'] = $appid;
        $pay['body'] = 'test';               //商品描述
        $pay['mch_id'] = $mch_id;            //商户号
        $pay['nonce_str'] = $nonce;        //随机字符串
        $pay['notify_url'] = '异步接收微信支付结果通知的回调地址';
        $pay['openid'] = $openid;
        $pay['out_trade_no'] = $orderCode;       //订单号
        $pay['spbill_create_ip'] = $_SERVER['SERVER_ADDR']; // 终端IP
        // $pay['total_fee'] = 100*$money; //支付金额
        $pay['total_fee'] = $money; //支付金额
        $pay['trade_type'] = 'JSAPI';    //交易类型
//        组建签名(不可换行 空格  否则哭吧)
        $stringA="appid=".$pay['appid']."&body=".$pay['body']."&mch_id=".$pay['mch_id']."&nonce_str=".$pay['nonce_str']."&notify_url=".$pay['notify_url']."&openid=". $pay['openid']."&out_trade_no=".$pay['out_trade_no']."&spbill_create_ip=". $pay['spbill_create_ip']."&total_fee=".$pay['total_fee']."&trade_type=".$pay['trade_type'];
        $stringSignTemp=$stringA."&key=".$key; //注:key为商户平台设置的密钥key(这个还需要再确认一下)
        $sign= strtoupper(md5($stringSignTemp)); //注:MD5签名方式
        $pay['sign'] = $sign;              //签名
//        统一下单请求
        $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        $data = $this->arrayToXml($pay);

       $res = MiniProgramService::jsPay($openid,$pay['out_trade_no'],$money,'tcc',$pay['body']);
        return app('json')->status('success', '数据获取成功', $res);
    }
上一篇下一篇

猜你喜欢

热点阅读