This article mainly introduces the relevant information about Thinkphp integrating WeChat payment function. It is very good and has reference value. Friends who need it can refer to it.
First, let’s look at the renderings: I want to tell you about this. This article is about WeChat payment (unified order JSPI for ordinary merchants rather than service provider merchants) WeChat payment:
In fact, I failed to integrate the SDK myself. I used the code integrated by a blogger. Write a note here:
Preparation:
1. WeChat public account:
Set the domain name that can obtain the user ID information permission among the unique appid, appscrect, and interface permissions (each user will have different permissions for different publics) A unique ID. Use this ID to obtain the basic information of the user's WeChat account. For details, see the WeChat developer documentation), set the WeChat payment authorization directory on the WeChat payment button (write to the level of the controller that initiates the request), and set the developer WeChat account. To test the whitelist (required when using WeChat developer tools)
2. WeChat payment platform:
Merchant platform login account and payment key (you can create it yourself at any time Settings, there can only be one),
3. Integrate into thinkphp logic:
Front-end WeChat payment button settings click to call the payment initiation controller method,
The controller runs, references the WeChat payment class, obtains the user openid, obtains the order data, splices out all the data required by ordinary merchants’ prepayment jsp, and displays the customized payment page.
During payment Click on the page to pay, call the jspi script function provided by WeChat to initiate payment,
After the payment is completed, the page will redirect to (the jump directory set in the script function of the custom payment page {:U( 'controller/function)}), and asynchronous (silent) settings for asynchronous processing of order logic (record payment time, mark as paid, mark as WeChat payment) and the like,
Code:
WeChat payment button on my order page:
微信支付
Initiate payment controller Wxpay:
$_GET['order_key_num'],'expire'=>3600)); S('pay_now_id',$_GET['order_key_num'],3600); } vendor('Weixinpay.WxPayPubHelper'); //使用jsapi接口 $jsApi = new \JsApi_pub(); //=========步骤1:网页授权获取用户openid============ //通过code获得openid if($_GET['code'] == ''){ //跳转 $redirect_uri = 'https://当前域名+模块+控制器+方法'; $url = 'https://open.weixin.qq.com/connect/oauth2/authorize ?appid=公众号特有IDredirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'; header("Location: $url"); exit(); }else{ //获取openid $url = 'https://api.weixin.qq.com/sns/oauth2/access_token ?appid=公众号ID&secret=公众号scrept&code='.$_GET['code'].'&grant_type=authorization_code'; $openid_arr = json_decode(file_get_contents($url),true); } $openid=$openid_arr['openid']; $pay_now_id = S('pay_now_id'); if($pay_now_id){ $id=$pay_now_id; $o = D('order_info'); $order_info = $o->where('order_id = %d',$id)->find(); if(empty($order_info['paycode'])){ $order_info['paycode'] = 'weixin'; } if($order_info['is_pay']){ $this->error('当前订单已经支付'); } }else{ $this->error("不存在当前订单编号!"); } $res = array( 'order_sn' => $order_info['order_sn'], 'order_amount' => $order_info['pay_money'] ); //=========步骤2:使用统一支付接口,获取prepay_id============ //使用统一支付接口 $unifiedOrder = new \UnifiedOrder_pub(); //设置统一支付接口参数 //设置必填参数 //appid已填,商户无需重复填写 //mch_id已填,商户无需重复填写 //noncestr已填,商户无需重复填写 //spbill_create_ip已填,商户无需重复填写 //sign已填,商户无需重复填写 $total_fee = $order_info['pay_money']*100; // $total_fee = $res['order_amount']; //$total_fee = 1; // var_dump($order_info['pay_money']);die; $body = "订单支付"; $unifiedOrder->setParameter("openid", "$openid");//用户标识 $unifiedOrder->setParameter("body", '商品采购');//商品描述 //自定义订单号,此处仅作举例 $unifiedOrder->setParameter("out_trade_no", $order_info['order_sn']);//商户订单号 $unifiedOrder->setParameter("total_fee", $total_fee);//总金额 //$unifiedOrder->setParameter("attach", "order_sn={$res['order_sn']}");//附加数据 $unifiedOrder->setParameter("notify_url", \WxPayConf_pub::NOTIFY_URL);//通知地址 $unifiedOrder->setParameter("trade_type", "JSAPI");//交易类型 //非必填参数,商户可根据实际情况选填 //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号 //$unifiedOrder->setParameter("device_info","XXXX");//设备号 //$unifiedOrder->setParameter("attach","XXXX");//附加数据 //$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间 //$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间 //$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记 //$unifiedOrder->setParameter("openid","XXXX");//用户标识 //$unifiedOrder->setParameter("product_id","XXXX");//商品ID $prepay_id = $unifiedOrder->getPrepayId(); // var_dump($prepay_id);die; //=========步骤3:使用jsapi调起支付============ $jsApi->setPrepayId($prepay_id); $jsApiParameters = $jsApi->getParameters(); $wxconf = json_decode($jsApiParameters, true); if ($wxconf['package'] == 'prepay_id=') { $this->error('当前订单存在异常!'); } $this->assign('res', $res); $this->assign('jsApiParameters', $jsApiParameters); $this->display('jsapi'); } //异步通知url,商户根据实际开发过程设定 public function notify_url() { vendor('Weixinpay.WxPayPubHelper'); //使用通用通知接口 $notify = new \Notify_pub(); //存储微信的回调 $xml = $GLOBALS['HTTP_RAW_POST_DATA']; $notify->saveData($xml); //验证签名,并回应微信。 //对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败, //微信会通过一定的策略(如30分钟共8次)定期重新发起通知, //尽可能提高通知的成功率,但微信不保证通知最终能成功。 if($notify->checkSign() == FALSE){ $notify->setReturnParameter("return_code", "FAIL");//返回状态码 $notify->setReturnParameter("return_msg", "签名失败");//返回信息 }else{ $notify->setReturnParameter("return_code", "SUCCESS");//设置返回码 } $returnXml = $notify->returnXml(); //==商户根据实际情况设置相应的处理流程,此处仅作举例======= //以log文件形式记录回调信息 //$log_name = "notify_url.log";//log文件路径 //$this->log_result($log_name, "【接收到的notify通知】:\n".$xml."\n"); $parameter = $notify->xmlToArray($xml); //$this->log_result($log_name, "【接收到的notify通知】:\n".$parameter."\n"); if($notify->checkSign() == TRUE){ if ($notify->data["return_code"] == "FAIL") { //此处应该更新一下订单状态,商户自行增删操作 //$this->log_result($log_name, "【通信出错】:\n".$xml."\n"); //更新订单数据【通信出错】设为无效订单 echo 'error'; } else if($notify->data["result_code"] == "FAIL"){ //此处应该更新一下订单状态,商户自行增删操作 //$this->log_result($log_name, "【业务出错】:\n".$xml."\n"); //更新订单数据【通信出错】设为无效订单 echo 'error'; } else{ //$this->log_result($log_name, "【支付成功】:\n".$xml."\n"); //我这里用到一个process方法,成功返回数据后处理,返回地数据具体可以参考微信的文档 if ($this->process($parameter)) { //处理成功后输出success,微信就不会再下发请求了 echo 'success'; }else { //没有处理成功,微信会间隔的发送请求 echo 'error'; } } } } //订单处理 private function process($parameter) { //此处应该更新一下订单状态,商户自行增删操作 /* * 返回的数据最少有以下几个 * $parameter = array( 'out_trade_no' => xxx,//商户订单号 'total_fee' => XXXX,//支付金额 'openid' => XXxxx,//付款的用户ID ); */ $data = array( 'order_sn'=>$parameter['out_trade_no'], 'des'=>('订单交易:'.$parameter['out_trade_no']), 'money'=>$parameter['total_fee'], ); orderhandlestarysdgdss($data);//这是一个common方法,他会将该订单状态设置为已支付之类的 return true; } } ?>
After initiating payment, the prepayment data parameters are spliced (for the parameter list, please refer to WeChat ordinary merchant developer documentation - WeChat payment - unified order) display page:
下
Then there are the class files:
The cacert is the certificate storage directory; the certificate is not necessarily needed;
vendor Just look for the folder in My Files.
The above is the Thinkphp integrated WeChat payment function introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!
For more articles related to Thinkphp integrating WeChat payment function, please pay attention to the PHP Chinese website!