php对接支付宝转账到第三方接口实战
公司项目有一个用户钱包系统,用户创作的内容可以收到游客的打赏,当然就需要提现的接口了。
最终选定的是支付宝转账接口,公司代收账户直接打款给用户绑定的支付宝账号,再也不用人工手动打款了。
上代码
准备好工具,支付宝sdk,去支付宝开放平台下载啦。
我把sdk放到了根目录下的extend目录下,证书放在extend/alipayPEM/目录下,这个版本的sdk有点老掉牙了,最新的sdk支持composer加载,但是。。。。还能用为啥要去改。。。
这个是转账代码,等着被调用的,调用方写自己的业务逻辑就行了。
<?php
namespace App\Http\Controllers\Extend;
use App\Http\Controllers\Controller;
use App\Model\Financing\FinancingModel;
use App\Model\Financing\FinancingProfitModel;
use App\Model\Member\MemberWalletModel;
use App\Model\Member\WithdrawalModel;
use Illuminate\Http\Request;
use Illuminate\Support\Env;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
class ExtendController extends Controller
{
public $alipayRsaPrivateKey = "这段肯定不能让你们看到啊,支付宝私钥";
public $alipayAppId = "支付宝应用id";
public $alipayFormat = "json";
public $alipayCharset = "UTF-8";
public $alipaySignType = "RSA2";
public $alipayGatewayUrl = "https://openapi.alipay.com/gateway.do";
public $alipayRsaPublic = "../extend/alipayPEM/支付宝公钥证书.crt";
public $alipayRsaApp = "../extend/alipayPEM/应用公钥证书.crt";
public $alipayRsaRoot = "../extend/alipayPEM/支付宝根证书.crt";
/**
* 支付宝转账到第三方
*/
public function alipay_transfer(WithdrawalModel $withdrawalModel)
{
try {
require_once '../extend/alipay/AopCertClient.php';
require_once '../extend/alipay/AopClient.php';
require_once "../extend/alipay/request/AlipayFundTransUniTransferRequest.php";
$aop = new \AopCertClient();
$aop->gatewayUrl = $this->alipayGatewayUrl;
$aop->appId = $this->alipayAppId;
$aop->signType = $this->alipaySignType;
$aop->postCharset = $this->alipayCharset;
$aop->format = $this->alipayFormat;
$aop->rsaPrivateKey = $this->alipayRsaPrivateKey;
$aop->alipayrsaPublicKey = $aop->getPublicKey($this->alipayRsaPublic);
$aop->isCheckAlipayPublicCert = true;
$aop->appCertSN = $aop->getCertSN($this->alipayRsaApp);
$aop->alipayRootCertSN = $aop->getRootCertSN($this->alipayRsaRoot);
$request = new \AlipayFundTransUniTransferRequest();
//TRANS_BANKCARD_NO_PWD转账到银行卡 TRANS_ACCOUNT_NO_PWD转账到支付宝
$request->setBizContent("{" .
"\"out_biz_no\":\"" . $withdrawalModel['order_id'] . "\"," .
"\"trans_amount\":" . $withdrawalModel['money'] . "," .
"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," .
"\"biz_scene\":\"DIRECT_TRANSFER\"," .
"\"order_title\":\"提现代发\"," .
"\"payee_info\":{" .
"\"identity\":\"" . $withdrawalModel['payment']['account'] . "\"," .
"\"identity_type\":\"ALIPAY_LOGON_ID\"," .
"\"name\":\"" . $withdrawalModel['payment']['name'] . "\"" .
" }," .
"\"remark\":\"" . $withdrawalModel['remark'] . "\"," .
" }");
$request->setNotifyUrl(env('APP_URL') . "/api/extend/alipay_transfer_notify");
$result = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($result && $resultCode) && $resultCode == 10000) {
return $result->$responseNode->pay_fund_order_id;
return true;
} else {
self::returnError($result->$responseNode->sub_msg);
}
} catch (\Exception $exception) {
self::returnError($exception->getMessage());
}
}
}
