E企盈营销工具技术服务商 热线:4006-838-530

PHP+H5微信小程序webview嵌套H5支付(公司项目笔记)

E企盈直播平台营销卖货系统

1.h5转后台mui.openWindow({                    url: “转向后台的网址/doMiniPay?payMoney=” + n + “&buyContent=” + buyContent + “&uid=” + uid,                    waiting: {                        autoShow: true, //自动显示等待框,默认为true                        title: ‘正在加载支付页面……’, //等待对话框上显示的提示内容                    }                })url:转向后台的网址payMoney:支付金额buyContent:支付说明 举例:XXX公司充值uid:用户id 用于将下单写入数据库2.跳转小程序获取code // 小程序webview支付    public function doMiniPay()    {//        $oid = I(“oid”);        $oid = $this-getOrderId();        $payjs = ”            script type=’text/javascript’ src=’https://res.wx.qq.com/open/js/jweixin-1.3.2.js’/script            script type=’text/javascript’            wx.miniProgram.getEnv(function(res) {                if (res.miniprogram) {                    // 走在小程序的逻辑                    var params = ‘?oid=” . $oid . “‘;                    var path = ‘/pages/wxPay/wxPay’+params;                    //document.write(params);                    wx.miniProgram.navigateTo({url: path});                }            });            /script          “;        echo $payjs;    }getOrderId方法为获取随机数方法public function getOrderId($uid)    {        //日期:10位 timestamp        $datetime = time();        //随机数        $newnum = $this-getNonceNums();        return $datetime . $newnum;    }3.小程序端获取code 向后台请求获取openid var app = getApp()Page({  onLoad: function (options) {    var that = this    wx.setStorageSync(‘oid’, options.oid);    wx.login({      success: function (res) {        // 成功的话会返回:        // {errMsg: “login:ok”, code: “获取用户OpenID的ticket”}        that.getOpenId(res.code)      }    })  },  getOpenId: function (jsCode) {    var that = this    wx.request({      url: ‘后台获取openid方法的网址login方法’,      data: {        js_code: jsCode // wx.login()时得到的ticket      },      success: function (res) {        that.getPrePayId(res.data.openid)      }    })  },})4.后端获取openid方法 //小程序支付后端    public function login()    {        $params[‘appid’] = ”;// 小程序的appID        $params[‘secret’] = ”;//小程序的appsecret        $params[‘js_code’] = $_GET[‘js_code’];//      测试用的将值写入文档  // file_put_contents(‘./lilllsss.txt’,$_GET[‘js_code’],FILE_APPEND);        $params[‘grant_type’] = ‘authorization_code’;        $urls = “https://api.weixin.qq.com/sns/jscode2session?appid=”.$params[‘appid’].”&secret=”.$params[‘secret’].”&grant_type=authorization_code&js_code=”.$params[‘js_code’].””;        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $urls);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        curl_setopt($ch, CURLOPT_POST, 1);        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);        $output = curl_exec($ch);        if (false === $output) {            echo ‘CURL Error:’ . curl_error($ch);        }        echo $output;    }5.小程序方法拿着openid请求prepayid并进行下单 getPrePayId: function (openId) {    var that = this    wx.request({      url: ‘后台wxpay方法获取prepayid’,      data: {        openid: openId,        oid: wx.getStorageSync(‘oid’)      },      success: function (res) {        that.pay(res.data);      }    })  },  pay: function (data) {    var that = this    data.success = function (res) {      that.payOver()    }    data.fail = function (res) {      that.payOver()    }    wx.requestPayment(data)  },  payOver: function () {    wx.reLaunch({      url: ‘/pages/index/index?oid=’ + wx.getStorageSync(‘oid’)    })  }6.后台下单 微信返回xml信息 将xml信息解析取到prepayid 打包数据返回给小程序端// 小程序支付后端    public function wxpay(){        $oid = I(“oid”);        $dao = M();//        $sql = “这里是去订单信息的sql”;//        $orderRs = $dao-query($sql);        $weiXin = new Weixin();        $param[‘appid’]             = ‘小程序的appid’;        $param[‘mch_id’]            = “商户号”;        $param[‘nonce_str’]         = $weiXin-getNonceNum();// 随机字符串        $param[‘body’]              = “粮多多充值”;        $param[‘out_trade_no’]      = $this-getOrderId();//随机数+时间生成订单号        $param[‘total_fee’]         = 1;//支付金额        $param[‘spbill_create_ip’]  = $_SERVER[‘REMOTE_ADDR’];        $param[‘notify_url’]        = ‘https://luoluotech.com/lddServer/pay/PayNotify.php’;        $param[‘trade_type’]        = “JSAPI”; //H5 支付类型        $param[‘openid’]        = $_GET[‘openid’];        // 按照要求计算sign//        file_put_contents(‘./ldsdasdqweqwd.txt’,$sequence,FILE_APPEND);        ksort($param);        $sequence = ”;        foreach ($param as $key = $value) {            $sequence .= “$key=$value&”;        }//        file_put_contents(‘./ldsdhiudsadqweqweqwd.txt’,$sequence,FILE_APPEND);        $apikey = “key商户平台自己设置的”;// 这个是手动设置的        $sequence = $sequence .”key=$apikey”;//        file_put_contents(‘./ldsddasdqwessfd.txt’,$sequence,FILE_APPEND);        $param[‘sign’] = strtoupper(md5($sequence));//        file_put_contents(‘./ldsdhirethbgfb.txt’,$param[‘sign’],FILE_APPEND);        // 给微信发出的请求,整个参数是个XML        $xml = ‘xml’ . PHP_EOL;        foreach ($param as $key = $value) {            $xml .= “$key$value/$key” . PHP_EOL;        }        $xml .= ‘/xml’;        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, ‘https://api.mch.weixin.qq.com/pay/unifiedorder’);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        curl_setopt($ch, CURLOPT_POST, 1);        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);        $output = curl_exec($ch);        if (false === $output) {            echo ‘CURL Error:’ . curl_error($ch);        }        // 下单成功的话,微信返回个XML,里面包含prepayID,提取出来 prepay_id![CDATA[这个就是我们要的值]]/prepay_id        if (0 === preg_match(‘/prepay_id![CDATA[(w+)]]/prepay_id/’, $output, $match)) {            echo $output;            exit(0);        }        file_put_contents(‘./ldadsadqweqewdsdggdfggfb.txt’,$output,FILE_APPEND);        $weiXin = new Weixin();        $prepayId = $match[1];        $response[‘appId’] = ‘小程序appid’;        $response[‘nonceStr’] = $weiXin-getNonceNum();        $response[‘package’] = ‘prepay_id=’ . $prepayId;        $response[‘signType’] = ‘MD5’;        $response[‘timeStamp’] =  (string) time();        $sequence = ”;        foreach ($response as $key = $value) {            $sequence .= “$key=$value&”;        }        $response[‘paySign’] = strtoupper(md5(“{$sequence}key=手动设置的商户key”));        echo json_encode($response);    }7.小程序调起微信支付pay: function (data) {    var that = this    data.success = function (res) {      that.payOver()    }    data.fail = function (res) {      that.payOver()    }    wx.requestPayment(data)  },  payOver: function () {    wx.reLaunch({      url: ‘/pages/index/index?oid=’ + wx.getStorageSync(‘oid’)    })  }

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » PHP+H5微信小程序webview嵌套H5支付(公司项目笔记)
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏