话不多说直接上代码
先上前端调用代码
function commit(){let goods_num = $(".ddxx_e .box .a1 .b2 em").html();let goods_id=$('#goods_id').val();let products_id=$('#products_id').val();let name=$('#name').val();let phone=$('#phone').val();let message=$('#message').val();if (name===''||phone===''){layer.open({content: '联系方式不能为空',skin: 'msg',time: 2});return false;}$.ajax({url:"{:url('wap/order/commit')}",data:{// 参数根据接口需要自行替换goods_id:goods_id,products_id:products_id,goods_num:goods_num,name:name,phone:phone,message:message},type:'post',success:function (res){if (res.code===0){WeixinJSBridge.invoke('getBrandWCPayRequest', {"appId":res.data.appId,//公众号ID"timeStamp":(res.data.timeStamp).toString(), //时间戳,这里时间戳转化为字符串格式,不然IOS调用会提示缺少参数timeStamp,而且此处的时间戳必须和生成微信预支付订单使用的时间戳保持一致"nonceStr":res.data.nonceStr, //随机串,随机串必须和生成微信预支付订单使用的随机串保持一致"package":res.data.package,// 生成微信预支付订单返回的prepay_id,格式为:'prepay_id=微信返回的prepay_id'"signType":res.data.signType, //微信签名方式:默认MD5"paySign":res.data.sign //微信签名},function(wx_res){if(wx_res.err_msg == "get_brand_wcpay_request:ok" ){// 循环查询订单是否支付成功timer = self.setInterval("checkOrderStatus("+ res.data.order_number +")",1000)}else{layer.open({content: '支付失败',skin: 'msg',time: 2});//提示内容,根据自己项目自行替换功用提示框函数}});}else{layer.open({content: '下单失败',skin: 'msg',time: 2});//提示内容,根据自己项目自行替换功用提示框函数}}})}// 检查订单是否支付成功functioncheckOrderStatus(order_number) {$.ajax({url:"{:url('填自己的接口地址')}",data:{order_number:order_number},type:'post',success:function (res){if (res.code===0){// 支付成功执行后续逻辑,如果支付失败忽略layer.open({content: '支付成功',skin: 'msg',time: 2});clearTimeout(timer);// 关闭定时器window.location.href="订单支付成功后跳转的页面地址";}}})}
后台程序代码,我这里用的是TP5,easyWechat 4.0
public function commit(){$wx_data = $this->wx_order($order_num, $total_price, $member_id);if( $wx_data['result_code'] == "SUCCESS" && $wx_data['return_code'] == "SUCCESS" && $wx_data['return_msg'] == "OK" ){$params['appId'] = $this->config['app_id'];$params['nonceStr'] = rand(1000, 9999) . time() . rand(1000, 9999);$params['package'] = "prepay_id={$wx_data['prepay_id']}";$params['signType'] = "MD5";$params['timeStamp'] = time();ksort($params);$sing_string = '';foreach ($params as $k => $v){$sing_string .= $k . '=' . $v . "&";}$params['sign'] = md5($sing_string . "key={$this->config['key']}");$params['order_number'] = $order_num;return ['code' => 0, 'data' => $params];}}/*** 生成微信订单* @param string $order_num订单号* @param int $total_price金额(分)* @param int $member_id 会员ID* @return array*/public function wx_order(string $order_num, int $total_price, int $member_id){$app = Factory::payment($this->config);$member_info = Member::where("member_id", $member_id)->find();$param = ['body' => '购买商品','out_trade_no' => $order_num,'total_fee' => $total_price * 100,'spbill_create_ip' => $_SERVER["REMOTE_ADDR"], // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型'openid' => $member_info['web_open_id'],'nonce_str' => rand(1000, 9999) . time() . rand(1000, 9999),'time_start' => time(),];// 调用微信接口获取数据,不使用easyWechat可以直接使用CURL调用。$result = $app->order->unify($param);return $result;}
如果觉得《在网页调用微信支付 并解决IOS调用提示“缺少参数timeStamp”问题》对你有帮助,请点赞、收藏,并留下你的观点哦!