1 官方下载sdk 包
2 修改sdk 包内容
alipay/aop/AopCertClient.php 133 行为: return $dec; 修改函数,同时修改调用方法:AopClient.php decrypt 改为 alipayDecrypt;同时修改调用方法 encrypt 改为 alipayEncrypt
3 sdk放入vendor 目录,composer引入并安装
"autoload": {
"classmap": [
"vendor/alipay/aop/",
"vendor/alipay/aop/request"
],
}4 根据官网提示去完成证书配置,并且本地存储起来
5 支付参数配置:
public static function userOption(){
$appId = config('alipay.user_app_id');// 支付宝应用id
$rsaPrivateKeyPath = base_path() . config('alipay.rsaPrivateKeyPath');// 使用支付宝开放平台开发助手生成的应用私钥
$appCertPath = base_path() . config('alipay.usr_appCertPath');// 上传证书后生成的crt文件 证书
$rootCertPath = base_path() . config('alipay.usr_rootCertPath');// 上传证书后生成的crt文件 支付宝根证书
$alipayrsaPublicKeyPath = base_path() . config('alipay.usr_alipayrsaPublicKeyPath');// 上传证书后生成的txt文件 支付宝公钥证书(不能使用支付宝开放平台开发助手生成的应用公钥,这样子会导致支付回调验签失败)
$alipayrsaPublicKey = openssl_pkey_get_public($alipayrsaPublicKeyPath);// 代码示例中是这样获取的($aop->getPublicKey($alipayCertPath);//调用getPublicKey从支付宝公钥证书中提取公钥)
$rsaPrivateKey = file_get_contents($rsaPrivateKeyPath);
//1、execute 使用
$aop = new \AopCertClient();
$aop->gatewayUrl = config('alipay.gatewayUrl');//网关
$aop->apiVersion = '1.0';
$aop->signType = config('alipay.sign_type');
$aop->postCharset = config('alipay.charset');
$aop->format = config('alipay.format');
$aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
$aop->appId = $appId;
$aop->rsaPrivateKey = $rsaPrivateKey;
$aop->alipayrsaPublicKey = $alipayrsaPublicKey;
$aop->appCertSN = $aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
$aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号
return $aop;
}6 下单:(app 支付为例,其他支付方式调整参数即可)
public static function pay($subject, $total_amount, $sn) {
$aop = self::userOption();
$requestPay = new \AlipayTradeAppPayRequest();
$bizcontent = json_encode([
'body'=>'body',
'subject'=>'name-'.$subject,
'out_trade_no'=> $sn,//此订单号为商户唯一订单号
'total_amount'=>strval($total_amount),//保留两位小数
'product_code'=>'QUICK_MSECURITY_PAY'
]);
$requestPay->setNotifyUrl('https://'.config('constants.FORMAL_URL').config('alipay.notify_url'));
$requestPay->setBizContent($bizcontent);
//这里和普通的接口调用不同,使用的是sdkExecute
$response = $aop->sdkExecute($requestPay);
return ['code' => 1, 'data' => $response];
}7 退款示例(支付宝只允许退款一次)
public static function refund($transaction_id,$re_amount,$out_trade_no){
$body['out_request_no'] = $out_trade_no;//系统订单号
$body['refund_amount'] = $re_amount;
$body['trade_no'] = $transaction_id;//交易流水号,商家返回
$requestReturn = new \AlipayTradeRefundRequest();
$requestReturn->setBizContent(json_encode($body));
$responseResult = $aop->execute($requestReturn);
$responseApiName = str_replace(".","_",$requestReturn->getApiMethodName())."_response";
$response = $responseResult->$responseApiName;
if(!empty($response->code)&&$response->code==10000){
if($response->fund_change == 'N'){
return ['0','已经退款了,无法二次退款!'];
}else{
return ['1','退款成功'];
}
}
else{
return ['0','退款申请失败,请稍后重试'];
}
}