无法在这个位置找到: head2.htm
当前位置: 建站首页 > 新闻 > 公司新闻 >

laravel完成手机微信JS付款

时间:2021-04-08 19:57来源:未知 作者:jianzhan 点击:
//获得openidpublic function getOpenid(Request $request) { $code = $request- input('code'); if(empty($code)) { return response()- json(['status'= 1,'msg'= 'code为空']); } $url='sns/jscode2session?appid='.env('APPID').' secret='.env('SECRE
//获得openidpublic function getOpenid(Request $request) { $code = $request- input('code'); if(empty($code)) { return response()- json(['status'= 1,'msg'= 'code为空']); } $url='sns/jscode2session?appid='.env('APPID').' secret='.env('SECRET').' js_code='.$code.' grant_type=authorization_code'; $res = file_get_contents($url); return response()- json(['status'= 200,'msg'= $res]); }   //获得付款必须的sign public function getPaySign(Request $request) { $ip = $_SERVER["REMOTE_ADDR"]; $openid = $request- input('openid'); $price = $request- input('price'); $aid = $request- input('aid'); $member = MemberModel::where(['openid'= $openid])- first(); if(empty($openid) || empty($member) || empty($ip)) { return response()- json(['status'= 500,'msg'= '主要参数不正确']); } if(empty($aid) || $aid == 0) { if(empty($price)) { return response()- json(['status'= 500,'msg'= '主要参数不正确']); } $type = 1; $aid = null; } else { $res=ActivityModel::select('target')- where(['id'= $aid,'status'= 1])- first(); if(empty($res)) { return response()- json(['status'= 500,'msg'= '主题活动完毕']); } $price = $res- target; $type = 2; } $order_num = 'CMT'.time().$member- id; $order=RechargeOrderModel::create(['mid'= $member- id,'price'= $price,'order_number'= $order_num,'type'= $type,'aid'= $aid]); if(empty($order)) { return response()- json(['status'= 200,'msg'= '定单转化成不正确']); } //拼装主要参数 $notify_url = 'home/payNotify'; $order_num = $order- order_number; $data = [ //'appid' = 'wxf0d27e41678c566c', 'appid' = env('APPID'), 'body' = '美容护肤消費', //'mch_id' = '', 'mch_id' = env('MCHID'), 'nonce_str' = md5(date('YmdHis') . time() . rand(1000, 9999)), 'notify_url' = $notify_url, 'openid' = $openid, 'out_trade_no' = $order_num, 'spbill_create_ip' = $ip, 'total_fee' = $price*100, 'trade_type' = 'JSAPI' ]; //产生sign $string = $this- getSign($data).'key='.env('MCHKEY'); $stringTemp = MD5($string); $sign = strtoupper($stringTemp); //提前准备恳求prepay插口 $data['sign'] = $sign; $str_xml = " xml "; foreach ($data as $key = $val) { $str_xml .= " " . $key . " " . $val . " /" . $key . " "; } $str_xml .= " /xml "; $prepay_url = 'pay/unifiedorder'; //恳求插口而且分析xml $result = $this- xmlDecode($this- httpCurl($prepay_url,$str_xml)); if ($result['return_code'] !== 'SUCCESS' || $result['result_code'] !== 'SUCCESS') { return response()- json(['status'= 200,'msg'= $result]); } //这里主要参数务必和前端开发调手机微信js主要参数一致,包含尺寸写及其数据信息种类 $params = [ 'appId' = $data['appid'], 'nonceStr' = $data['nonce_str'], 'package' = 'prepay_id='.$result['prepay_id'], 'signType' = 'MD5', 'timeStamp' = (string)time() ]; //拼凑前端开发必须主要参数 $stringxml = $this- getSign($params).'key='.env('MCHKEY'); $stringxml = MD5($stringxml); $sign2 = strtoupper($stringxml); $pay_result['paySign'] = $sign2; $pay_result['timeStamp'] = $params['timeStamp']; $pay_result['nonceStr'] = $params['nonceStr']; $pay_result['package'] = $params['package']; $pay_result['signType'] = 'MD5'; return response()- json(['status'= 200,'msg'= $pay_result]); }   protected function httpCurl($url,$data) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: text' ) ); $data = curl_exec($ch); curl_close($ch); return ($data); }  protected function xmlDecode($xml) { $message = (array)simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); return $message; }   protected function getSign($arr) { ksort($arr); $string = ''; foreach ($arr as $k = $v) { if ($k != "sign" $v != "" !is_array($v)) { $string .= $k . "=" . $v . " "; } } return $string; }   //付款回调函数详细地址 public function payNotify() { $postXml = $GLOBALS["HTTP_RAW_POST_DATA"]; //接受手机微信主要参数 if (empty($postXml)) { return false; } //将xml文件格式变换成数字能量数组 $attr = $this- xmlDecode($postXml); $total_fee = $attr['total_fee']*0.01; $open_id = $attr['openid']; $out_trade_no = $attr['out_trade_no']; if($attr['result_code'] = 'SUCCESS' || $attr['return_code'] = 'SUCCESS') { die('success'); } else { die('fail'); } } } }

注:必须留意尺寸写的难题,也有手机微信配备难题,有别的疑惑能够在下边留言板留言,时尚博主看到后将给予一定协助。

(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
无法在这个位置找到: ajaxfeedback.htm
栏目列表
推荐内容


扫描二维码分享到微信

在线咨询
联系电话

400-888-8866