微信支付之公众号发红包和企业付款

微信现金红包,是微信支付商户平台提供的营销工具之一,上线以来深受广大商户与用户的喜爱。商户可以通过本平台向微信支付用户发放现金红包。用户领取红包后,资金到达用户微信支付零钱账户,在日常运营中为商户的营销活动带来热烈的反响。

在前几篇文章中都写过微信支付与退款,小程序的支付,企业付款等等,今天写一写公众号发红包的,对于发红包,需要明白一下几点:
1.发红包不需要支付授权目录,但是需要在商户后台调用红包Api的IP地址,也就是你的发起红包请求的服务器的IP,操作路径::【登录商户平台——>产品中心——>现金红包——>产品设置】(注:“产品设置”操作按钮仅当你开通现金红包功能之后才会出现)。
2.发红包需要Api证书。
3.在发放现金红包之前,请确保你的资金充足。别人通过在你的平台买东西微信支付付给你的钱和你发红包需要支出的钱不在一起,这里的钱需要单独充值的,操作路径:【登录商户平台——>账户中心——>资金管理——>充值】。
4.发红包可以借权的,比如公众号A是开通了微信支付的认证服务号,你的活动在公众号B(订阅号服务号均可)中举办,公众号B可以使用A的微信支付发红包,但是发红包需要知道用户的openid,获取这个openid的时候也需要借用公众号A来获取,也就是通过A发红包的这个openid就必须是该用户对应于A的openid。

操作前期准备,也就是微信支付商户平台的一些配置
,请参看文档: https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_3&index=2

其实微信公众号 发红包和微信公众号 企业付款差不多,我也就顺带把企业付款再整理一下,好了话不多说,直接上代码:
/**
 * 公众号发红包
 * @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;
}
这是封装好的类,调用方法也超级简单:
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);  //发红包
就这么简单吗?对!就这么简单,然而这里边使用了很多自己封装的函数方法,源码下载: http://download.csdn.net/download/sinat_35861727/9956485
如果真的感觉好用,请点个赞留个好评,谢谢!有问题也可以评论区告诉我!

猜你喜欢

转载自blog.csdn.net/sinat_35861727/article/details/77703812