第一步需要将支付宝SDK下载放在vendor,并且重命名为“Alipay”
下一步直接上代码:
/**
* 单笔转账接口
* @param $order_number 订单号
* @param $pay_name 转账用户名
* @param $pay_no 转账账号
* @param $amount 转账金额
* @param $memo 备注
*/
function alipay_transfer($order_number,$pay_name,$pay_no,$amount,$memo="用户提现"){
$item = db('setting')->where('id',1)->find();
$item = json_decode($item['data'],true);
$payer_name = $item['app_name'];
vendor("Alipay.AopClient"); //引入sdk
vendor("Alipay.request.AlipayFundTransToaccountTransferRequest"); //引入sdk
$aop = new \AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = $item['app_id'];//支付宝APPID
$aop->rsaPrivateKey = $item['private_key'];//应用私钥
$aop->alipayrsaPublicKey=$item['public_key'];//支付宝公钥
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new \AlipayFundTransToaccountTransferRequest ();
$request->setBizContent("{" .
"\"out_biz_no\":\"".$order_number."\"," .//商户生成订单号
"\"payee_type\":\"ALIPAY_LOGONID\"," .//收款方支付宝账号类型
"\"payee_account\":\"".$pay_no."\"," .//收款方账号
"\"amount\":\"".$amount."\"," .//总金额
"\"payer_show_name\":\"".$payer_name."\"," .//付款方账户
"\"payee_real_name\":\"".$pay_name."\"," .//收款方姓名
"\"remark\":\"".$memo."\"" .//转账备注
"}");
$result = $aop->execute ($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
//修改转账日志
if(!empty($resultCode)&&$resultCode == 10000){
//转账成功处理返回
return "";
} else {
//转账失败原因提示
return $result->$responseNode->sub_msg;
}
}
|