微信商家转账到零钱,支付宝商户转账
注意事项
一:开通条件:需满足入驻满90天,连续正常交易30天,保持正常健康交易。
二:分为页面发起和api接口操作,均需要手动开启才可。
三:需要设置IP白名单,否则不能发起接口调用。
四:商家转账的户口为商户平台的运营账户,非商户基本账户,注意保证运营账户的余额充足!
五:注意提现额度的变化,超过设置的免密额度,该笔提现会自动发送给超级管理员审核,输入密码确定此笔提现操作。
- 微信商家转账到零钱
/**
* 微信提现
* @param $openid
* @param $money
* @param $base_config
* @param $desc
* @return mixed
* @throws \Exception
*/
public function transfer_wx($openid, $money, $base_config, $desc = '备注说明')
{
$trade_no = cmf_get_order_sn();
$money = (int)($money * 100);
//只要APPID 商户号 证书序列号 私钥证书
$post_data = [
"appid" => $base_config['app_id'],//appid
"out_batch_no" => $trade_no,//商家批次单号
"batch_name" => $desc,//批次名称
"batch_remark" => $desc,//批次备注
"total_amount" => $money,// 转账金额单位为“分”
"total_num" => 1, // 转账总笔数
//此处可以多笔提现 组合二维数组放到transfer_detail_list即可 我这里单笔操作
"transfer_detail_list" => [
[
'out_detail_no' => $trade_no,
'transfer_amount' => $money,
'transfer_remark' => $desc,
'openid' => $openid,
]
]
];
$url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
//JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE 防止中文被转义
$result = $this->wx_post($url, json_encode($post_data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $base_config);
$result = json_decode($result, true);
if (isset($result['code'])) {
throw \Exception($result['message']);
}
return $result;
}
/**post请求
* @param $url
* @param $param
* @return bool|string
*/
private function wx_post($url, $param, $base_config)
{
$authorization = $this->getV3Sign($url, "POST", $param, $base_config);
$curl = curl_init();
$headers = [
'Authorization:' . $authorization,
'Accept:application/json',
'Content-Type:application/json;charset=utf-8',
'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
curl_setopt($curl, CURLOPT_POST, true);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
/**
* 微信提现V3签名
* @param $url
* @param $http_method
* @param $body
* @return string
*/
private function getV3Sign($url, $http_method, $body, $base_config)
{
$nonce = strtoupper($this->createNonceStr(32));
$timestamp = time();
$url_parts = parse_url($url);
$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
//只要APPID 商户号 证书序列号 私钥证书
$sslKeyPath = getAttachmentById($base_config['PrivateKeyFile']);
//拼接参数
$message = $http_method . "\n" .
$canonical_url . "\n" .
$timestamp . "\n" .
$nonce . "\n" .
$body . "\n";
//获取私钥
$private_key = openssl_get_privatekey(file_get_contents($sslKeyPath));
openssl_sign($message, $raw_sign, $private_key, 'sha256WithRSAEncryption');
$sign = base64_encode($raw_sign);
$token = sprintf('WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",timestamp="%s",serial_no="%s",signature="%s"', $base_config['mch_id'], $nonce, $timestamp, $base_config['cert_no'], $sign);
return $token;
}
/**
* 生成随机32位字符串
* @param $length
* @return string
*/
public function createNonceStr($length = 16)
{ //生成随机16个字符的字符串
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
- 支付宝商户转账
支付宝sdk下载 https://opendocs.alipay.com/open-v3/065bsc
/**
* 支付宝转账
* @param $data
* @param $price
* @param $base_config
* @param $remark
* @return true
* @throws \Exception
*/
private function transfer_ali($data = [], $price = 0, $base_config = [], $remark = '提现到账')
{
//account_name 用户名 account 账号
if ($price < 0.1) throw new \Exception('提现金额不能低于0.1');
if (empty($data) || count($data) <= 0 || !isset($data['account_name']) || !isset($data['account'])) throw new \Exception('账号有误');
require_once ALI_PATH . '/AopClient.php';
require_once ALI_PATH . '/AopCertClient.php';
require_once ALI_PATH . '/AopCertification.php';
require_once ALI_PATH . '/request/AlipayTradeQueryRequest.php';
require_once ALI_PATH . '/request/AlipayFundTransUniTransferRequest.php';
require_once ALI_PATH . '/request/AlipayTradeWapPayRequest.php';
require_once ALI_PATH . '/request/AlipayTradeAppPayRequest.php';
$privateKey = $base_config['privateKey'];
$alipayConfig = new \AlipayConfig();
$alipayConfig->setPrivateKey($privateKey);
$alipayConfig->setServerUrl("https://openapi.alipay.com/gateway.do");
$alipayConfig->setAppId($base_config['ali_app_id']);
$alipayConfig->setCharset("UTF-8");
$alipayConfig->setSignType("RSA2");
$alipayConfig->setEncryptKey("");
$alipayConfig->setFormat("json");
$alipayConfig->setAppCertPath(rtrim(root_path(), '/') . '/public' . get_file_path($base_config['appCertPath']));
$alipayConfig->setAlipayPublicCertPath(rtrim(root_path(), '/') . '/public' . get_file_path($base_config['alipayPublicCertPath']));
$alipayConfig->setRootCertPath(rtrim(root_path(), '/') . '/public' . get_file_path($base_config['alipayRootCertPath']));
$alipayClient = new \AopCertClient($alipayConfig);
$alipayClient->isCheckAlipayPublicCert = true;
$request = new \AlipayFundTransUniTransferRequest();
$body = array(
"out_biz_no" => cmf_get_order_sn(),
"remark" => $remark,
"business_params" => ["payer_show_name_use_alias" => true],
"biz_scene" => "DIRECT_TRANSFER",
"payee_info" => ['identity' => $data['account'], "identity_type" => "ALIPAY_LOGON_ID", "name" => $data['account_name']],
"trans_amount" => $price,
"product_code" => "TRANS_ACCOUNT_NO_PWD",
"order_title" => "用户提现"
);
$request->setBizContent(json_encode($body));
$responseResult = $alipayClient->execute($request);
$responseApiName = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$response = $responseResult->$responseApiName;
if (!empty($response->code) && $response->code == 10000) {
return true;
} else {
return throw new \Exception($response->sub_msg);
}
}