Home  >  Article  >  WeChat Applet  >  WeChat public account red envelope distribution and corporate payment implementation methods

WeChat public account red envelope distribution and corporate payment implementation methods

小云云
小云云Original
2018-03-20 14:17:323689browse

WeChat cash red envelope is one of the marketing tools provided by the WeChat payment merchant platform. It has been deeply loved by merchants and users since its launch. Merchants can issue cash red envelopes to WeChat Pay users through this platform. After the user receives the red envelope, the funds arrive in the user's WeChat payment change account, which brings enthusiastic response to the merchant's marketing activities in daily operations.
1. You do not need to pay the authorization directory to send red envelopes, but you need to call the IP address of the red envelope API in the merchant's backend, which is the IP of your server that initiates the red envelope request. The operation path is: [Log in to the merchant platform——> Product Center——>Cash Red Envelope——>Product Settings] (Note: The “Product Settings” operation button will only appear after you activate the cash red envelope function).
2. Api certificate is required to send red envelopes.
3. Before issuing cash red envelopes, please make sure you have sufficient funds. The money others pay you through WeChat Pay for buying things on your platform is not the same as the money you need to spend to send red envelopes. The money here needs to be recharged separately. The operation path is: [Log in to the merchant platform——>Account Center——> ;Fund Management——>Recharge].
4. You can borrow rights when sending red envelopes. For example, public account A is a certification service account that has opened WeChat payment. Your event is held in public account B (subscription account or service account is acceptable). Public account B can use A. WeChat Pay sends red envelopes, but sending red envelopes requires knowing the user's openid. When obtaining this openid, you also need to borrow the public account A to obtain it. That is, the openid used to send red envelopes through A must be the openid corresponding to A of the user.

Pre-operation preparation, that is, some configurations of the WeChat payment merchant platform, please refer to the document: https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter= 13_3&index=2

In fact, sending red envelopes on WeChat public accounts is similar to corporate payments on WeChat public accounts, so I will sort out corporate payments by the way. Without further ado, I will directly upload the code:

/**
 * 公众号发红包
 * @param string $openid 	用户openID
 * @param string $money 	金额
 * @param string $trade_no  订单编号
 * @param string $act_name  活动名称
 * @return multitype 		支付结果
 */
public function sendredpack($openid,$money,$trade_no,$act_name){
	$config = $this->config;
	
	$data = array(
		'nonce_str' 		=> self::getNonceStr(),
		'mch_billno'     	=> $trade_no,
		'mch_id' 			=> $config['mch_id'],
		'wxappid' 			=> $config['wxappid'],
		'send_name' 		=> '江南极客',
		're_openid'    		=> $openid,
		'total_amount'    	=> $money * 100, //付款金额单位为分
		'total_num'    		=> 1,
		'wishing'      		=> '祝您天天开心!',
		'client_ip' 		=> self::getip(),
		'act_name' 			=> $act_name,
		'remark' 			=> 'From 江南极客'
	);
	
	$data['sign'] = self::makeSign($data);
	
	//构造XML数据
	$xmldata = self::array2xml($data);
	
	$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
	//发送post请求
	$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['return_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['return_msg']));
	}
	if(strval($content['result_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des']));
	}
	return $content;
}
	
/**
 * 公众号企业支付
 * @param string $openid 	用户openID
 * @param string $money 	金额
 * @param string $trade_no  订单编号
 * @param string $desc  	付款操作说明信息(比如:提现)
 * @return string 	支付结果
 */
public function mchpay($openid,$money,$trade_no,$desc){
	$config = $this->config;
	$data = array(
		'mch_appid' => $config['wxappid'],
		'mchid'     => $config['mch_id'],
		'nonce_str' => self::getNonceStr(),
		'partner_trade_no' => $trade_no, 
		'openid'    => $openid,
		'check_name'=> 'NO_CHECK', 			//OPTION_CHECK不强制校验真实姓名, FORCE_CHECK:强制 NO_CHECK:
		'amount'    => $money * 100, 		//付款金额单位为分
		'desc'      => $desc,
		'spbill_create_ip' => self::getip()
	);
	
	//生成签名
	$data['sign'] = self::makeSign($data);
	
	//return $config;
	
	//构造XML数据
	$xmldata = self::array2xml($data);
	$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
	//发送post请求
	$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('./log1.txt',$res,FILE_APPEND);
	
	//付款结果分析
	$content = self::xml2array($res);
	if(strval($content['return_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['return_msg']));
	}
	if(strval($content['result_code']) == 'FAIL'){
		return array('status'=>0, 'msg'=>strval($content['err_code']).':'.strval($content['err_code_des']));
	}
   
	return $content;
}

This is an encapsulated class, and calling the method is super simple:

include 'wxmerpay.class.php';		//引入类文件
$config = array(
	'wxappid'		=> 'wx123456789',
	'mch_id'	 	=> '1123456781',
	'pay_apikey' 	=> '1234567898765432123456789',
	'api_cert'		=> $cert_path . '/apiclient_cert.pem',	
	'api_key'		=> $cert_path . '/apiclient_key.pem',
	'rootca'		=> $cert_path . '/rootca.pem'
);
$redpack = new WxRedpack($config);	//初始化
$redpack->sendredpack($openid,$money,$trade_no,$act_name);  //发红包

Is it that simple? right! It’s that simple, but it uses a lot of self-encapsulated functions and methods. Source code download: http://download.csdn.net/download/sinat_35861727/9956485
If you really find it useful, please give it a like and leave a like Good review, thank you! If you have any questions, you can tell me in the comment area!

Related recommendations:

WeChat payment refund function development

PHP development examples of WeChat payment and Alipay payment

Research and Sharing on WeChat Payment Interface

The above is the detailed content of WeChat public account red envelope distribution and corporate payment implementation methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:The use of Mocha and chaiNext article:The use of Mocha and chai