new5db79b134e9f6b82c0b36e0489ee08ed', courier; font-size: 13px;>写这篇文章的目的主要是由于在微信公众平台提供的sdk中并没有提供此功能的sdk实现,
其实最后实现还是借助 微信公众平台开发文档 和 sdk 。
企业付款的应用场景: 公众号向已关注用户付款,比如处理退款、财务结算等
先说一下实现思路:
在sdk中自带类库的基础上扩展wxmchpay组件, 实现企业付款功能的扩展。
话不多说,上代码, 下面是继承sdk,实现企业付款的组件:
$parameters参数参考: 企业付款api的文档
<?php// 引入sdkimport('common.util.wxpay');/**
* 微信企业付款操作类
* author : max.wen
* datetime: <15/9/16 11:00> */class wxmchpay extends wxpay_client_pub
{ /**
* api 参数
* @var array
* 'mch_appid' # 公众号appid
* 'mchid' # 商户号
* 'device_info' # 设备号
* 'nonce_str' # 随机字符串
* 'partner_trade_no' # 商户订单号
* 'openid' # 收款用户openid
* 'check_name' # 校验用户姓名选项 针对实名认证的用户
* 're_user_name' # 收款用户姓名
* 'amount' # 付款金额
* 'desc' # 企业付款描述信息
* 'spbill_create_ip' # ip地址
* 'sign' # 签名 */
public $parameters = []; public function construct()
{ $this->url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; $this->curl_timeout = wxpayconf_pub::curl_timeout;
} /**
* 生成请求xml数据
* @return string */
public function createxml()
{ $this->parameters['mch_appid'] = wxpayconf_pub::appid; $this->parameters['mchid'] = wxpayconf_pub::mchid; $this->parameters['nonce_str'] = $this->createnoncestr(); $this->parameters['sign'] = $this->getsign($this->parameters); return $this->arraytoxml($this->parameters);
} /**
* 作用:使用证书,以post方式提交xml到对应的接口url */
function postxmlsslcurl($xml,$url,$second=30)
{ $ch = curl_init(); //超时时间
curl_setopt($ch,curlopt_timeout,$second); //这里设置代理,如果有的话
//curl_setopt($ch,curlopt_proxy, '8.8.8.8');
//curl_setopt($ch,curlopt_proxyport, 8080);
curl_setopt($ch,curlopt_url, $url);
curl_setopt($ch,curlopt_ssl_verifypeer,false);
curl_setopt($ch,curlopt_ssl_verifyhost,false); //设置header
curl_setopt($ch,curlopt_header,false); //要求结果为字符串且输出到屏幕上
curl_setopt($ch,curlopt_returntransfer,true); //设置证书
curl_setopt($ch,curlopt_cainfo, wxpayconf_pub::sslrootca_path); //使用证书:cert 与 key 分别属于两个.pem文件
//默认格式为pem,可以注释
curl_setopt($ch,curlopt_sslcerttype,'pem');
curl_setopt($ch,curlopt_sslcert, wxpayconf_pub::sslcert_path); //默认格式为pem,可以注释
curl_setopt($ch,curlopt_sslkeytype,'pem');
curl_setopt($ch,curlopt_sslkey, wxpayconf_pub::sslkey_path); //post提交方式
curl_setopt($ch,curlopt_post, true);
curl_setopt($ch,curlopt_postfields,$xml); $data = curl_exec($ch); //返回结果
if($data){
curl_close($ch); return $data;
} else { $error = curl_errno($ch); echo "curl出错,错误码:$error"."<br>"; echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
curl_close($ch); return false;
}
}
}
controller层功能实现:
<?php/**
* author : max.wen
* datetime: <15/9/20 16:47> */namespace home\controller;class testcontroller extends commoncontroller
{ /**
* 企业付款测试 */
public function rebate()
{
import('common.util.wxmchpay'); $mchpay = new \wxmchpay(); // 用户openid
$mchpay->setparameter('openid', 'oy2lbszxkgvlekthrzqezikebzqu'); // 商户订单号
$mchpay->setparameter('partner_trade_no', 'test-'.time()); // 校验用户姓名选项
$mchpay->setparameter('check_name', 'no_check'); // 企业付款金额 单位为分
$mchpay->setparameter('amount', 100); // 企业付款描述信息
$mchpay->setparameter('desc', '开发测试'); // 调用接口的机器ip地址 自定义
$mchpay->setparameter('spbill_create_ip', '127.0.0.1'); # getclientip()
// 收款用户姓名
// $mchpay->setparameter('re_user_name', 'max wen');
// 设备信息
// $mchpay->setparameter('device_info', 'dev_server');
$response = $mchpay->postxmlssl(); if( !empty($response) ) { $data = simplexml_load_string($response, null, libxml_nocdata); echo json_encode($data);
}else{ echo json_encode( array('return_code' => 'fail', 'return_msg' => 'transfers_接口出错', 'return_ext' => array()) );
}
}
}
完成上述两部分代码,基本就可以成功调用企业付款api了。
返回结果的数据结构示例:
{
"return_code": "success",
"return_msg": { },
"mch_appid": "wx519cae424099ed6b",
"mchid": "1228636402",
"device_info": { },
"nonce_str": "qjupk84q4iqxkb578hb5h2qiatgcwxwg",
"result_code": "success",
"partner_trade_no": "test-1442801966",
"payment_no": "1000018301201509210739170397",
"payment_time": "2015-09-21 10:19:26"
}
可能遇到的问题:
1、ca证书错误
在wxmchpay中大家可以看到,我重写了sdk中 wxpay_client_pub 的 postxmlsslcurl()方法
因为默认在sdk中的这个方法在curl post请求的时候没有附带ca证书。
相比之下就多了
curl_setopt($ch,curlopt_cainfo, wxpayconf_pub::sslrootca_path);这么一行代码。
作用就是请求时附带ca证书。
2、对同一用户转账操作过于频繁,请稍候重试.
这个错误属于微信服务端的限制,具体限制频率也没有找到那里有说明,不过经过实际测试大概在1分钟左右。
所以在开发的时候还需要多注意。
【相关推荐】
1. 微信公众号平台源码下载
2. 微信投票源码免费下载
以上就是小程序开发之企业付款的详细内容。