最近项目中需要使用微信提现功能(企业付款到零钱),开通条件微信公众号平台入驻90天,废话不多说,直接上代码。
第一步:你必须要在微信支付平台下载cert证书,放在指定目录下,地址到时要填绝对路径地址,可以使用getcwd()方法
第二步:创建一个提现的文件Withdraw.php
protected $appid = "xxxxxxx"; //商户账号appid
protected $secret = "xxxxxxxx"; //api密码
protected $mch_id = "xxxxxx"; //商户号
protected $mch_no = "xxxxxxx"; //商户秘钥
function transfer($data) {
//支付信息
$wxchat['appid'] = $this->appid;
$wxchat['mchid'] = $this->mch_id;
$webdata = array(
'mch_appid' => $wxchat['appid'], //商户账号appid
'mchid' => $wxchat['mchid'], //商户号
'nonce_str' => md5(time()), //随机字符串
'partner_trade_no' => date('YmdHis'), //商户订单号,需要唯一
'openid' => $data['openid'], //转账用户的openid
'check_name' => 'NO_CHECK', //OPTION_CHECK不强制校验真实姓名, FORCE_CHECK:强制 NO_CHECK:
'amount' => $data['money'] * 100, //付款金额单位为分
'desc' => '微信企业付款到零钱', //企业付款描述信息
'spbill_create_ip' => '192.168.5.8', //获取IP
);
foreach ($webdata as $k => $v) {
$tarr[] = $k . '=' . $v;
}
sort($tarr);
$sign = implode($tarr, '&');
$sign .= '&key=' . $this->mch_no;
$webdata['sign'] = strtoupper(md5($sign));
// print_r($webdata);
$wget = $this->ArrToXml($webdata); //数组转XML
$pay_url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //api地址
$res = $this->curl_post_ssl($pay_url, $wget, 30, array(), 1);
// print_r($rdata);
if (!$res) {
return array('status' => 1, 'msg' => "Can't connect the server");
}
$content = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
if (strval($content->return_code) == 'FAIL') {
return array('status' => 1, 'msg' => strval($content->return_msg));
}
if (strval($content->result_code) == 'FAIL') {
return array('status' => 1, 'msg' => strval($content->err_code), ':' . strval($content->err_code_des));
}
$rdata = array(
'mch_appid' => strval($content->mch_appid),
'mchid' => strval($content->mchid),
'device_info' => strval($content->device_info),
'nonce_str' => strval($content->nonce_str),
'result_code' => strval($content->result_code),
'partner_trade_no' => strval($content->partner_trade_no),
'payment_no' => strval($content->payment_no),
'payment_time' => strval($content->payment_time),
);
return $rdata;
}
//数组转XML
function ArrToXml($arr) {
if (!is_array($arr) || count($arr) == 0)
return '';
$xml = "<xml>";
foreach ($arr as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
function curl_post_ssl($url, $vars, $second = 30, $aHeader = array()) {
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_TIMEOUT, $second); //设置执行最长秒数
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_URL, $url); //抓取指定网页
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 终止从服务端进行验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); //证书类型
curl_setopt($ch, CURLOPT_SSLCERT, getcwd() . '/cert/apiclient_cert.pem'); //证书位置
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM'); //CURLOPT_SSLKEY中规定的私钥的加密类型
curl_setopt($ch, CURLOPT_SSLKEY, getcwd() . '/cert/apiclient_key.pem'); //证书位置
// curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
// curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');
if (count($aHeader) >= 1) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader); //设置头部
}
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars); //全部数据使用HTTP协议中的"POST"操作来发送
$data = curl_exec($ch); //执行回话
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
}
第三步:调用这个文件
$data = $user->with($member['openid'], $money);
if ($data['msg'] == 'SUCCESS') {//提现成功
}else if ($data['msg'] == 'NOTENOUGH') { //余额不足
//告诉用户0-3个工作日
echo json_encode(array('code' => 3029, 'msg' => '平台余额不足,请提醒充值!'));
exit();
}
//创建提现订单,修改用户余额
基本上代码就这些,如果运行中遇到其他问题,请一步一步查看原因,希望可以帮到你。