fastadmin支付宝微信插件转账完整函数示例如下,需要安装 overtrue/wechat
function wechat_transfer($orderSn, $openid, $amount, $notifyUrl)
{
$payConfig = \addons\epay\library\Service::getConfig();
// 商户号
$merchantId = $payConfig['mch_id'];
// 从本地文件中加载「商户API私钥」,「商户API私钥」会用来生成请求的签名
// $merchantPrivateKeyFilePath = 'file://' . '/www/wwwroot/www.example.com/addons/epay/certs/apiclient_key.pem';
$merchantPrivateKeyFilePath = 'file://' . $payConfig['cert_key'];
$merchantPrivateKeyInstance = Rsa::from($merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);
// 「商户API证书」的「证书序列号」
//serial=
$merchantCertificateSerial = '这里填写微信的证书序列号';
// 从本地文件中加载「微信支付平台证书」,用来验证微信支付应答的签名
$filePath = root_path('/addons/epay/certs/') . 'wechatpay_17C92D85342F634DA3F018D9D09405D60CB06.pem';
$platformCertificateFilePath = 'file://' . $filePath;
$platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);
// 从「微信支付平台证书」中获取「证书序列号」
$platformCertificateSerial = PemUtil::parseCertificateSerialNo($platformCertificateFilePath);
// 构造一个 APIv3 客户端实例
$instance = \WeChatPay\Builder::factory([
'mchid' => $merchantId,
'serial' => $merchantCertificateSerial,
'privateKey' => $merchantPrivateKeyInstance,
'certs' => [
$platformCertificateSerial => $platformPublicKeyInstance,
],
]);
// 加密敏感信息
$encryptor = static function (string $msg) use ($platformPublicKeyInstance): string {
return \WeChatPay\Crypto\Rsa::encrypt($msg, $platformPublicKeyInstance);
};
//php CertificateDownloader.php
// -k ${32位密码}
// -m ${商户号}
// -f ${商户API私钥}
// -s ${商户API证书的证书序列号}
// -o ${获取平台证书后的保存路径}
// 商家转账到零钱
$response = $instance
->chain('v3/transfer/batches')
->post(['json' => [
'appid' => $payConfig['miniapp_id'],
'out_batch_no' => $orderSn, //商户系统内部的商家批次单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一,
'batch_name' => date('Y-m', time()) . ' - 领取红包', //该笔批量转账的名称
'batch_remark' => "领取红包", //转账说明,UTF8编码,最多允许32个字符
'total_amount' => intval(strval($amount * 100)), //转账总金额 单位为“分”
'total_num' => 1,
'transfer_detail_list' => [
[
'out_detail_no' => makePaySn(1),
'transfer_amount' => intval(strval($amount * 100)),
'transfer_remark' => "领取红包",
'openid' => $openid,
//'user_name' => $encryptor($params['name']) // 金额超过`2000`才填写
]
]
]]);
Log::info("转账结果");
Log::info($orderSn);
Log::info($response);
return $response;
}