PHP implements WeChat code scanning payment function

小云云
Release: 2018-03-20 14:20:09
Original
2823 people have browsed it

This article mainly shares with you the method of implementing WeChat scan code payment function in PHP, hoping to help everyone.

Perform WeChat payment on the WeChat mobile phone and directly call up JSAPI payment. This can enable payment on the page opened in WeChat, such as WeChat Mall. For details on JSAPI payment on WeChat: PHP implements WeChat payment (jsapi payment) and refund (no need to integrate payment SDK); but sometimes the mall also has a PC side, and you need to use WeChat payment on the PC side, you need to generate the payment QR code on the PC side, and then scan the code on WeChat to complete Pay. For example:


Here we mainly talk about the specific implementation of scan code payment and refund on PC:

<span style="font-size: 14px;">/**<br/> * 微信支付请求接口(POST)<br/> * @param string $goods_id 	商品ID<br/> * @param string $body 		商品简单描述<br/> * @param string $order_sn  订单编号<br/> * @param string $total_fee 金额<br/> * @return  json的数据<br/> */<br/>public function wxpay($goods_id,$total_fee,$body,$order_sn){<br/>	$config = $this->config;<br/>	<br/>	//统一下单参数构造<br/>	$unifiedorder = array(<br/>		&#39;appid&#39;			=> $config[&#39;appid&#39;],<br/>		&#39;mch_id&#39;		=> $config[&#39;mch_id&#39;],<br/>		&#39;device_info&#39;	=> &#39;WEB&#39;,<br/>		&#39;nonce_str&#39;		=> self::getNonceStr(),<br/>		&#39;body&#39;			=> $body,<br/>		&#39;out_trade_no&#39;	=> $order_sn,<br/>		&#39;total_fee&#39;		=> $total_fee * 100,<br/>		&#39;spbill_create_ip&#39;	=> self::getip(),<br/>		&#39;notify_url&#39;	=> &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].&#39;/notify.php&#39;,<br/>		&#39;trade_type&#39;	=> &#39;NATIVE&#39;,<br/>		&#39;product_id&#39;	=> $goods_id<br/>	);<br/>	$unifiedorder[&#39;sign&#39;] = self::makeSign($unifiedorder);<br/>	<br/>	//return $unifiedorder;<br/>	<br/>	//请求数据,统一下单<br/>	$xmldata = self::array2xml($unifiedorder);<br/>	$url = &#39;https://api.mch.weixin.qq.com/pay/unifiedorder&#39;;<br/>	$res = self::curl_post_ssl($url, $xmldata);<br/>	if(!$res){<br/>		return array(&#39;status&#39;=>0, &#39;msg&#39;=>"Can&#39;t connect the server" );<br/>	}<br/>	// 这句file_put_contents是用来查看服务器返回的结果 测试完可以删除了<br/>	file_put_contents(&#39;./log.txt&#39;,$res,FILE_APPEND);<br/>	<br/>	$content = self::xml2array($res);<br/>	if(strval($content[&#39;result_code&#39;]) == &#39;FAIL&#39;){<br/>		return array(&#39;status&#39;=>0, &#39;msg&#39;=>strval($content[&#39;err_code&#39;]).&#39;:&#39;.strval($content[&#39;err_code_des&#39;]));<br/>	}<br/>	if(strval($content[&#39;return_code&#39;]) == &#39;FAIL&#39;){<br/>		return array(&#39;status&#39;=>0, &#39;msg&#39;=>strval($content[&#39;return_msg&#39;]));<br/>	}<br/>	<br/>	return $content;<br/>}<br/><br/>/**<br/> * 微信退款(POST)<br/> * @param string(28) $transaction_id 	在微信支付的时候,微信服务器生成的订单流水号,在支付通知中有返回<br/> * @param string $out_refund_no 		商品简单描述<br/> * @param string $total_fee 			微信支付的时候支付的总金额(单位:分)<br/> * @param string $refund_fee 			此次要退款金额(单位:分)<br/> * @return string						xml格式的数据<br/> */<br/>public function refund($transaction_id,$out_refund_no,$total_fee,$refund_fee){<br/>	$config = $this->config;<br/>	<br/>	//退款参数<br/>	$refundorder = array(<br/>		&#39;appid&#39;			=> $config[&#39;appid&#39;],<br/>		&#39;mch_id&#39;		=> $config[&#39;mch_id&#39;],<br/>		&#39;nonce_str&#39;		=> self::getNonceStr(),<br/>		&#39;transaction_id&#39;=> $transaction_id,<br/>		&#39;out_refund_no&#39;	=> $out_refund_no,<br/>		&#39;total_fee&#39;		=> $total_fee * 100,<br/>		&#39;refund_fee&#39;	=> $refund_fee * 100<br/>	);<br/>	$refundorder[&#39;sign&#39;] = self::makeSign($refundorder);<br/>	<br/>	//请求数据,进行退款<br/>	$xmldata = self::array2xml($refundorder);<br/>	$url = &#39;https://api.mch.weixin.qq.com/secapi/pay/refund&#39;;<br/>	$res = self::curl_post_ssl($url, $xmldata);<br/>	if(!$res){<br/>		return array(&#39;status&#39;=>0, &#39;msg&#39;=>"Can&#39;t connect the server" );<br/>	}<br/>	// 这句file_put_contents是用来查看服务器返回的结果 测试完可以删除了<br/>	//file_put_contents(&#39;./log3.txt&#39;,$res,FILE_APPEND);<br/>	<br/>	$content = self::xml2array($res);<br/>	if(strval($content[&#39;result_code&#39;]) == &#39;FAIL&#39;){<br/>		return array(&#39;status&#39;=>0, &#39;msg&#39;=>strval($content[&#39;err_code&#39;]).&#39;:&#39;.strval($content[&#39;err_code_des&#39;]));<br/>	}<br/>	if(strval($content[&#39;return_code&#39;]) == &#39;FAIL&#39;){<br/>		return array(&#39;status&#39;=>0, &#39;msg&#39;=>strval($content[&#39;return_msg&#39;]));<br/>	}<br/>	<br/>	return $content;<br/>}<br/></span>
Copy after login

Payment and refund are so simple, and there is no need to obtain the user openid, no certificate file, and no need to configure the payment authorization directory when paying. This is the implementation of the encapsulated payment class file, and the calling method is simpler :

<span style="font-size: 14px;">require_once "webwxpay.class.php";<br/><br/>$config = array(<br/>	&#39;appid&#39;			=> &#39;wx123456789876&#39;,<br/>	&#39;mch_id&#39;	 	=> &#39;123456789&#39;,<br/>	&#39;pay_apikey&#39; 	=> &#39;123456789876123456789876123456789876&#39;<br/>);<br/><br/>$wxpay = new WxPay($config);<br/>$result = $wxpay->paytest();<br/>//print_r($result);<br/>scerweima($result[&#39;code_url&#39;]);		//生成的支付二维码,用户可以扫码付款<br/></span>
Copy after login

At this time, the payment QR code will be generated, and then scan it on WeChat to complete the payment:


As for payment callback verification, I won’t go into details here. If you don’t understand, you can read the process of implementing WeChat payment (jsapi payment) in ThinkPHP. Here is a detailed explanation of how to handle callbacks.

Related recommendations:

WeChat payment refund function development

PHP implementation of WeChat payment function development code Share

10 course recommendations on WeChat payment development

The above is the detailed content of PHP implements WeChat code scanning payment function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template