Home WeChat Applet WeChat Development ThinkPHP implements WeChat payment (jsapi payment)

ThinkPHP implements WeChat payment (jsapi payment)

Jun 07, 2018 pm 03:52 PM
tp5 pay

This article mainly introduces the detailed tutorial of the process of implementing WeChat payment (jsapi payment) in ThinkPHP. Friends in need can refer to it.

I have written an article before about the process of implementing WeChat payment (jsapi payment) in PHP. , see the article for details: PHP implements WeChat payment (jsapi payment) process.

The environment at that time did not use a framework. It was implemented by directly creating a new directory under the directory pointed to by a domain name and then accessing the directory. However, there were still some problems when applied to the framework. In ThinkPHP, due to routing rules There is a discrepancy with the payment authorization directory, so an error will be reported. This article talks about the process of integrating WeChat payment in TP.

The SDK and documentation produced by Goose Factory are difficult to understand, and you will know it after using it. Shouldn’t the documentation and SDK be as simple and understandable as possible? Is it possible that only vigorous refactoring can show the superb technology of Goose Factory programmers? Well...did I expose my rookie attributes...In fact, the SDK is quite easy to use, but as I saw in the previous article, the payment completion callback function is really confusing.

For those who don’t want to be bypassed by the official and want to use WeChat payment in TP, you can take a look at the payment SDK suitable for TP which was restructured and streamlined by a master based on the official documents. I downloaded the source code and read it. Well, the code is written in an elegant and concise manner, and the process is simple and easy to understand. See the blog post for details: http://baijunyao.com/article/78

I still frowned, used the official SDK, and successfully implemented the payment. Let me share the process with you:

1.SDK download and modification

I won’t go into too much detail about this. If you don’t know, you can read my previous article: PHP implements WeChat payment (jsapi payment) process , which details which downloaded files need to be modified.

2. Public account settings

A. You still need to set the web page authorized domain name, this is nothing special;

B. Pay attention to this Payment authorization directory, using TP, many people use the rewrite mode (REWRITE mode) or use the pseudo-static mode while using the REWRITE mode. The generated link at this time is: http://serverName/Home/Blog/read /id/1 ;

If you are using PATHINFO mode, the generated link is: http://serverName/index.php/Home/Blog/read/id/1, such as under the Home module Use a method in the Blog controller to pay. The authorized directory for our payment should be http://serverName/Home/Blog/ or http://serverName/index.php/Home/Blog/, which is based on our own TP. Depends on the URL pattern set.

3. Payment process

(1) Unified order placement

The payment parameter configuration for order placement is basically different from the previous one. Change, the important thing to pay attention to is the payment callback verification link. Because it needs to be called multiple times, I encapsulated the parameter configuration directly in Application/Common/Common/function.php. My SDK is placed in the Api directory under the project root directory. , so the Vendor function is not used when introducing the SDK.

/** 
 * 微信支付 
 * @param string $openId  openid 
 * @param string $goods  商品名称 
 * @param string $attach  附加参数,我们可以选择传递一个参数,比如订单ID 
 * @param string $order_sn 订单号 
 * @param string $total_fee 金额 
 */ 
function wxpay($openId,$goods,$order_sn,$total_fee,$attach){ 
 require_once APP_ROOT."/Api/wxpay/lib/WxPay.Api.php"; 
 require_once APP_ROOT."/Api/wxpay/payment/WxPay.JsApiPay.php"; 
 require_once APP_ROOT.'/Api/wxpay/payment/log.php'; 
 //初始化日志 
 $logHandler= new CLogFileHandler(APP_ROOT."/Api/wxpay/logs/".date('Y-m-d').'.log'); 
 $log = Log::Init($logHandler, 15); 
 $tools = new JsApiPay(); 
 if(empty($openId)) $openId = $tools->GetOpenid(); 
 $input = new WxPayUnifiedOrder(); 
 $input->SetBody($goods);     //商品名称 
 $input->SetAttach($attach);     //附加参数,可填可不填,填写的话,里边字符串不能出现空格 
 $input->SetOut_trade_no($order_sn);   //订单号 
 $input->SetTotal_fee($total_fee);   //支付金额,单位:分 
 $input->SetTime_start(date("YmdHis"));  //支付发起时间 
 $input->SetTime_expire(date("YmdHis", time() + 600));//支付超时 
 $input->SetGoods_tag("test3"); 
 //$input->SetNotify_url("http://".$_SERVER['HTTP_HOST']."/payment.php"); //支付回调验证地址 
 $input->SetNotify_url("http://".$_SERVER['HTTP_HOST']."/payment.php/WexinApi/WeixinPay/notify"); 
 $input->SetTrade_type("JSAPI");    //支付类型 
 $input->SetOpenid($openId);     //用户openID 
 $order = WxPayApi::unifiedOrder($input); //统一下单 
 $jsApiParameters = $tools->GetJsApiParameters($order); 
 return $jsApiParameters; 
}
Copy after login

Attention, attention, here’s the key point:

The payment callback verification link must be verified without permission. If you access that link yourself, you still need to log in and register for verification. , don't try it, the link must be accessible, and there must be no series of parameters passed.

The best is simple and crude http://serverName/xxx.php. I re-wrote a special entry file payment for payment callback in the following directory, similar to index.php. .php, and its corresponding module (WexinApi), controller (WeixinPay) and method (notify) in the Application/ directory:

// 检测PHP环境 
if(version_compare(PHP_VERSION,&#39;5.3.0&#39;,&#39;<&#39;)) die(&#39;require PHP > 5.3.0 !&#39;); 
// $_GET[&#39;m&#39;]=&#39;Admin&#39;; 
// 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false 
define(&#39;APP_DEBUG&#39;,True); 
//指定模块控制器和方法 
$_GET[&#39;m&#39;]=&#39;WexinApi&#39;; 
$_GET[&#39;c&#39;]=&#39;WeixinPay&#39;; 
$_GET[&#39;a&#39;]=&#39;notify&#39;; 
// 定义应用目录 
define(&#39;APP_PATH&#39;,&#39;./Application/&#39;); 
define("APP_ROOT",dirname(__FILE__)); 
// 引入ThinkPHP入口文件 
require &#39;./ThinkCore/ThinkCore.php&#39;; 
// 亲^_^ 后面不需要任何代码了 就是如此简单
Copy after login

Now visit http://serverName/payment.php, you will directly Enter http://serverName/payment.php/WexinApi/WeixinPay/notify, so that the callback verification link can be written as http://serverName/payment.php or http://serverName/payment.php/WexinApi/WeixinPay /notify .

(2) Initiating payment

is still very simple:

/** 
* 支付测试 
* 微信访问:http://daoshi.sdxiaochengxu.com/payment.php/WexinApi/WeixinPay/pay 
*/ 
public function pay(){ 
 $order_sn = getrand_num(true); 
 $openId = &#39;&#39;; 
 $jsApiParameters = wxpay($openId,&#39;江南极客&#39;,$order_sn,1); 
 $this->assign(array( 
  &#39;data&#39; => $jsApiParameters 
 )); 
 $this->display(); 
} 

<html> 
<head> 
 <meta http-equiv="content-type" content="text/html;charset=utf-8"/> 
 <meta name="viewport" content="width=device-width, initial-scale=1"/> 
 <title>小尤支付测试</title> 
 <script type="text/javascript"> 
 //调用微信JS api 支付 
 function jsApiCall() 
 { 
  var data={$data}; 
  WeixinJSBridge.invoke( 
   &#39;getBrandWCPayRequest&#39;, data, 
   function(res){ 
    WeixinJSBridge.log(res.err_msg); 
    //alert(&#39;err_code:&#39;+res.err_code+&#39;err_desc:&#39;+res.err_desc+&#39;err_msg:&#39;+res.err_msg); 
    //alert(res.err_code+res.err_desc+res.err_msg); 
    //alert(res); 
    if(res.err_msg == "get_brand_wcpay_request:ok"){ 
     alert("支付成功!"); 
     window.location.href="http://m.blog.csdn.net/article/details?id=72765676" rel="external nofollow" ; 
    }else if(res.err_msg == "get_brand_wcpay_request:cancel"){ 
     alert("用户取消支付!"); 
    }else{ 
     alert("支付失败!"); 
    } 
   } 
  ); 
 } 
 function callpay() 
 { 
  if (typeof WeixinJSBridge == "undefined"){ 
   if( document.addEventListener ){ 
    document.addEventListener(&#39;WeixinJSBridgeReady&#39;, jsApiCall, false); 
   }else if (document.attachEvent){ 
    document.attachEvent(&#39;WeixinJSBridgeReady&#39;, jsApiCall); 
    document.attachEvent(&#39;onWeixinJSBridgeReady&#39;, jsApiCall); 
   } 
  }else{ 
   jsApiCall(); 
  } 
 } 
 </script> 
</head> 
<body> 
 <br/> 
 <font color="#9ACD32"><b>该笔订单支付金额为<span style="color:#f00;font-size:50px">1分</span>钱</b></font><br/><br/> 
 <font color="#9ACD32"><b><span style="color:#f00;font-size:50px;margin-left:40%;">1分</span>钱也是爱</b></font><br/><br/> 
 <p align="center"> 
  <button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;" type="button" onclick="callpay()" >果断买买买^_^</button> 
 </p> 
</body> 
</html>
Copy after login

However, you should pay attention to the URL of the payment page, because the URL of the payment page must have many parameters. Just now Speaking of the REWRITE mode used in TP, your link is similar to [ http://serverName/Home/Blog/read/id/1 ], which may have more parameters. At this time, WeChat Pay will consider your payment The authorization directory is [http://serverName/Home/Blog/read/id/], but your real authorization directory is [http://serverName/Home/Blog/], so an error will be reported. The solution is to reconstruct the URL when entering the payment page and write it in normal mode, which is [http://serverName/Home/Blog/read?id=1], and that's it.

(3) Support success callback

Now that the payment is completed, you will enter the method corresponding to the previously written link, that is, [http:// serverName/payment.php/WexinApi/WeixinPay/notify]:

//微信支付回调验证 
public function notify(){ 
 $xml = $GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;]; 
 // 这句file_put_contents是用来查看服务器返回的XML数据 测试完可以删除了 
 file_put_contents(&#39;./Api/wxpay/logs/log.txt&#39;,$xml,FILE_APPEND); 
 //将服务器返回的XML数据转化为数组 
 //$data = json_decode(json_encode(simplexml_load_string($xml,&#39;SimpleXMLElement&#39;,LIBXML_NOCDATA)),true); 
 $data = xmlToArray($xml); 
 // 保存微信服务器返回的签名sign 
 $data_sign = $data[&#39;sign&#39;]; 
 // sign不参与签名算法 
 unset($data[&#39;sign&#39;]); 
 $sign = $this->makeSign($data); 
 // 判断签名是否正确 判断支付状态 
 if ( ($sign===$data_sign) && ($data[&#39;return_code&#39;]==&#39;SUCCESS&#39;) && ($data[&#39;result_code&#39;]==&#39;SUCCESS&#39;) ) { 
  $result = $data; 
  // 这句file_put_contents是用来查看服务器返回的XML数据 测试完可以删除了 
  file_put_contents(&#39;./Api/wxpay/logs/log1.txt&#39;,$xml,FILE_APPEND); 
  //获取服务器返回的数据 
  $order_sn = $data[&#39;out_trade_no&#39;]; //订单单号 
  $order_id = $data[&#39;attach&#39;];  //附加参数,选择传递订单ID 
  $openid = $data[&#39;openid&#39;];   //付款人openID 
  $total_fee = $data[&#39;total_fee&#39;]; //付款金额 
  //更新数据库 
  $this->updateDB($order_id,$order_sn,$openid,$total_fee); 
 }else{ 
  $result = false; 
 } 
 // 返回状态给微信服务器 
 if ($result) { 
  $str=&#39;<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>&#39;; 
 }else{ 
  $str=&#39;<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>&#39;; 
 } 
 echo $str; 
 return $result; 
}
Copy after login

For security reasons, the returned signature must be re-verified:

/** 
* 生成签名 
* @return 签名,本函数不覆盖sign成员变量 
*/ 
protected function makeSign($data){ 
 //获取微信支付秘钥 
 require_once APP_ROOT."/Api/wxpay/lib/WxPay.Api.php"; 
 $key = \WxPayConfig::KEY; 
 // 去空 
 $data=array_filter($data); 
 //签名步骤一:按字典序排序参数 
 ksort($data); 
 $string_a=http_build_query($data); 
 $string_a=urldecode($string_a); 
 //签名步骤二:在string后加入KEY 
 //$config=$this->config; 
 $string_sign_temp=$string_a."&key=".$key; 
 //签名步骤三:MD5加密 
 $sign = md5($string_sign_temp); 
 // 签名步骤四:所有字符转为大写 
 $result=strtoupper($sign); 
 return $result; 
}
Copy after login

至此,TP中微信支付也就搞定了。这是集成了官方的SDK实现的,如果不使用SDK,可以使用更简单的方法,见:PHP实现微信支付(jsapi支付)和退款(无需集成支付SDK)

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

php服务端集成支付宝APP支付

基于thinkPHP实现的微信自定义分享功能


The above is the detailed content of ThinkPHP implements WeChat payment (jsapi payment). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to pay for a taxi ride on Baidu Maps. Introduction to the payment steps for a taxi ride. How to pay for a taxi ride on Baidu Maps. Introduction to the payment steps for a taxi ride. Mar 13, 2024 am 10:04 AM

Baidu Map APP has now become the preferred travel navigation software for many users, so some of the functions here are comprehensive and can be selected and operated for free to solve some of the problems that you may encounter in daily travel. You can all check some of your own travel routes and plan some of your own travel plans. After checking the corresponding routes, you can choose appropriate travel methods according to your own needs. So whether you choose some public transportation, Cycling, walking or taking a taxi can all satisfy your needs. There are corresponding navigation routes that can successfully lead you to a certain place. Then everyone will feel more convenient if they choose to take a taxi. There are many drivers They are all able to take orders online, and taxi-hailing has become super

How uniapp application implements payment and order management How uniapp application implements payment and order management Oct 19, 2023 am 10:37 AM

uniapp is a cross-platform application development framework that can develop small programs, Apps and H5 at the same time. In uniapp applications, payment and order management are very common needs. This article will introduce how to implement payment functions and order management in the uniapp application, and give specific code examples. 1. Implementing the payment function The payment function is the key to realizing online transactions, and it usually requires integrating the SDK of a third-party payment platform. The following are the specific steps to implement the payment function in uniapp: Register and obtain a third-party payment platform

Pay using PHP and PayPal API Pay using PHP and PayPal API Jun 19, 2023 pm 04:13 PM

With the increasing popularity of online transactions, payment methods are gradually diversifying, among which PayPal is very popular as a widely used payment method. If you want to use PayPal to process transactions on your website or application, then you can use PHP and PayPal API to complete the payment process easily. PayPalAPI is a set of programming interfaces for interacting with PayPal. Through the API, you can receive notifications from PayPal, query the latest transaction information, and initiate payments.

How to pay for taking the bus in Wuhan How to pay for taking the bus in Wuhan Oct 13, 2022 pm 02:17 PM

Payment methods for taking buses in Wuhan: 1. For cash payment, you need to prepare sufficient change in advance and put the money directly into the coin slot; 2. Swipe the Wuhan Tong card. The full name of Wuhan Tong is Wuhan City Card, which is an integrated circuit Card, also known as chip card; 3. Alipay electronic bus card, first get a Wuhan electronic bus card in Alipay, and then directly scan the QR code to deduct the money when getting on the bus; 4. WeChat bus code payment , open the WeChat "Ride Code" applet, activate the Wuhan "Ride Code", and you can directly scan the QR code to deduct payment and get on the bus.

How to close Meituan Takeout Express Payment How to close Meituan Takeout Express Payment Mar 27, 2024 am 10:41 AM

In the fast-paced modern life, Meituan Takeaway is deeply loved by consumers for its convenient services and rich choices. Among them, the ultra-fast payment function brings great convenience to users. Payment can be completed with one click, eliminating tedious input steps. However, many users don't like paying directly without confirmation, so they want to turn off this feature. So how to turn off the fast payment of Meituan Waimai? In the following, the editor of this website will bring you a detailed step-by-step setup tutorial, I hope it can help you! 1. Click the &quot;Meituan Takeout&quot; shortcut icon on the mobile phone desktop. 2. Log in to the Meituan takeout app on your mobile phone and click &quot;My&quot; in the lower right corner. 3. In the My interface, click &quot;Enter Wallet&quot;. 4. On the Meituan Wallet interface, click the &quot;Settings&quot; icon in the upper right corner

What does paynow payment mean? What does paynow payment mean? Sep 30, 2022 am 11:01 AM

PayNow payment is an electronic transfer service. Users can directly initiate real-time SGD transfers to the payee through the mobile phone number, ID card/FIN number, UEN number or PayNow QR code specified by the payee without the need for the other party. Bank account information.

Establish an order payment table for the grocery shopping system in MySQL Establish an order payment table for the grocery shopping system in MySQL Nov 01, 2023 pm 03:48 PM

To establish the order payment table of the grocery shopping system in MySQL, specific code examples are required. With the development of the Internet, shopping has become more and more convenient. In the shopping process, order payment is an important part of the shopping process. The grocery shopping system not only needs to have an order generation function, but also must have a complete payment process, because only successful payment can be regarded as completing a transaction. This article will describe how to create an order payment table for the grocery shopping system in MySQL and provide specific code examples. 1. Design of order payment table. The order payment table in the grocery shopping system stores orders.

Where can Meituan instant discount be deducted by default when payment is enabled_ Tutorial on Meituan instant discount being deducted by default when payment is enabled Where can Meituan instant discount be deducted by default when payment is enabled_ Tutorial on Meituan instant discount being deducted by default when payment is enabled Mar 28, 2024 am 11:00 AM

1. First open the [Meituan] APP, click the [My] button in the bottom navigation bar. 2. Then click the [My Wallet] function button and click the [Cash Voucher] button. 3. Then in the top function bar, click the [Instant Deduction] button, and click the switch button to turn on [Default Deduction during Payment] to successfully turn it on.

See all articles