Home > Backend Development > PHP Tutorial > Detailed explanation of implementing WeChat scan code payment function with PHP

Detailed explanation of implementing WeChat scan code payment function with PHP

小云云
Release: 2023-03-22 22:00:01
Original
2863 people have browsed it

Perform WeChat payment on the mobile WeChat side 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 side: 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 WeChat Scan the QR code to complete payment. For example:


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

/**
 * 微信支付请求接口(POST)
 * @param string $goods_id 	商品ID
 * @param string $body 		商品简单描述
 * @param string $order_sn  订单编号
 * @param string $total_fee 金额
 * @return  json的数据
 */
public function wxpay($goods_id,$total_fee,$body,$order_sn){
	$config = $this->config;
	
	//统一下单参数构造
	$unifiedorder = array(
		'appid'			=> $config['appid'],
		'mch_id'		=> $config['mch_id'],
		'device_info'	=> 'WEB',
		'nonce_str'		=> self::getNonceStr(),
		'body'			=> $body,
		'out_trade_no'	=> $order_sn,
		'total_fee'		=> $total_fee * 100,
		'spbill_create_ip'	=> self::getip(),
		'notify_url'	=> 'http://'.$_SERVER['HTTP_HOST'].'/notify.php',
		'trade_type'	=> 'NATIVE',
		'product_id'	=> $goods_id
	);
	$unifiedorder['sign'] = self::makeSign($unifiedorder);
	
	//return $unifiedorder;
	
	//请求数据,统一下单
	$xmldata = self::array2xml($unifiedorder);
	$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
	$res = self::curl_post_ssl($url, $xmldata);
	if(!$res){
		return array('status'=>0, 'msg'=>"Can't connect the server" );
	}
	// 这句file_put_contents是用来查看服务器返回的结果 测试完可以删除了
	file_put_contents('./log.txt',$res,FILE_APPEND);
	
	$content = self::xml2array($res);
	if(strval($content['result_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des']));
	}
	if(strval($content['return_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['return_msg']));
	}
	
	return $content;
}

/**
 * 微信退款(POST)
 * @param string(28) $transaction_id 	在微信支付的时候,微信服务器生成的订单流水号,在支付通知中有返回
 * @param string $out_refund_no 		商品简单描述
 * @param string $total_fee 			微信支付的时候支付的总金额(单位:分)
 * @param string $refund_fee 			此次要退款金额(单位:分)
 * @return string						xml格式的数据
 */
public function refund($transaction_id,$out_refund_no,$total_fee,$refund_fee){
	$config = $this->config;
	
	//退款参数
	$refundorder = array(
		'appid'			=> $config['appid'],
		'mch_id'		=> $config['mch_id'],
		'nonce_str'		=> self::getNonceStr(),
		'transaction_id'=> $transaction_id,
		'out_refund_no'	=> $out_refund_no,
		'total_fee'		=> $total_fee * 100,
		'refund_fee'	=> $refund_fee * 100
	);
	$refundorder['sign'] = self::makeSign($refundorder);
	
	//请求数据,进行退款
	$xmldata = self::array2xml($refundorder);
	$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
	$res = self::curl_post_ssl($url, $xmldata);
	if(!$res){
		return array('status'=>0, 'msg'=>"Can't connect the server" );
	}
	// 这句file_put_contents是用来查看服务器返回的结果 测试完可以删除了
	//file_put_contents('./log3.txt',$res,FILE_APPEND);
	
	$content = self::xml2array($res);
	if(strval($content['result_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des']));
	}
	if(strval($content['return_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['return_msg']));
	}
	
	return $content;
}
Copy after login

Payment It is so simple to make a refund, 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:

require_once "webwxpay.class.php";

$config = array(
	'appid'			=> 'wx123456789876',
	'mch_id'	 	=> '123456789',
	'pay_apikey' 	=> '123456789876123456789876123456789876'
);

$wxpay = new WxPay($config);
$result = $wxpay->paytest();
//print_r($result);
scerweima($result['code_url']);		//生成的支付二维码,用户可以扫码付款
Copy after login

This The payment QR code will be generated when the payment is completed, and then scan it on WeChat to complete the payment:


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

Related recommendations:

PHP implements WeChat scan code payment function

PHP code to implement WeChat scan code payment function Share

PHP example tutorial: Automatically jump after successful payment by scanning QR code on PC via WeChat

The above is the detailed content of Detailed explanation of implementing WeChat scan code payment function with PHP. 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