search
HomeWeChat AppletWeChat DevelopmentWeChat public account red envelope distribution and corporate payment implementation methods

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use