laravel 支付宝 SDK 交易信息 对账单下载
-
进入支付宝 官方api文档查找对应的php sdk,地址:https://opendocs.alipay.com/open/54/cyz7do/#SDK
-
使用新版的sdk,地址:https://opendocs.alipay.com/open/00y8k9
-
点击php Composer 项目依赖,地址:https://packagist.org/packages/alipaysdk/easysdk
-
安装Composer 安装 alipay SDK 命令:composer require alipaysdk/easysdk
-
SDK接口文档:https://github.com/alipay/alipay-easysdk/blob/master/APIDoc.md
-
SDK接口文档:https://github.com/alipay/alipay-easysdk/tree/master/php
-
请求下载对账单示例:
<?php namespace App\Console\Commands; use Alipay\EasySDK\Kernel\Config; use Alipay\EasySDK\Kernel\Factory; use Illuminate\Console\Command; use Illuminate\Support\Facades\Log; use PHPUnit\Exception; class Sync_Trade_Query extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'SyncTradeQuery'; /** * The console command description. * * @var string */ protected $description = 'Sync Trade Query'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * https://github.com/alipay/alipay-easysdk/tree/master/php * https://github.com/alipay/alipay-easysdk/blob/master/APIDoc.md * https://opendocs.alipay.com/apis/api_1/alipay.trade.query * https://opendocs.alipay.com/apis/api_15/alipay.data.dataservice.bill.downloadurl.query * https://opendocs.alipay.com/open/00y8k9 * https://opendocs.alipay.com/open/54/cyz7do/#SDK * Execute the console command. * * @return mixed */ public function handle() { Factory::setOptions($this->getOptions()); //支付宝api #$this->syncAlipay($out_trade_no); //下载对账单 $billDate = '2020-06-03'; $this->downloadAlipayBill($billDate); } /** * 查询交易 * @param $out_trade_no * @throws \Exception */ private function syncAlipay($out_trade_no){ Log::info('syncAlipay', ['syncAlipay']); try{ $result = Factory::payment()->common()->query($out_trade_no); Log::info('syncAlipay result', [$result]); if (!empty($result->code) && $result->code == 10000) { Log::info('syncAlipay chargeAmount', [$result->chargeAmount]); Log::info('syncAlipay success', [$result->httpBody]); } else { Log::info('syncAlipay error', [$result->chargeAmount]); } }catch (Exception $e){ Log::info('syncAlipay Exception', [$e->getMessage()]); } } /** * 支付宝对账单下载地址 * @throws \Exception */ private function downloadAlipayBill($billDate){ Log::info('downloadAlipayBill', ['downloadAlipayBill']); try{ $billType ='signcustomer'; #$billType ='trade'; $result = Factory::payment()->common()->downloadBill($billType, $billDate); if (!empty($result->code) && $result->code == 10000) { Log::info('downloadAlipayBill success', [$result]); Log::info('downloadAlipayBill success', [$result->billDownloadUrl]); } else { Log::info('downloadAlipayBill error', [$result]); } }catch (Exception $e){ Log::info('downloadAlipayBill Exception', [$e->getMessage()]); } } //设置参数 public function getOptions() { $options = new Config(); $options->protocol = 'https'; $options->gatewayHost = 'openapi.alipay.com'; $options->signType = 'RSA2'; $options->appId = config('pay.alipay.app_id'); // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中 $options->merchantPrivateKey = config('pay.alipay.private_key'); //$options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->'; //$options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->'; //$options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->'; //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可 $options->alipayPublicKey = config('pay.alipay.ali_public_key'); //可设置异步通知接收服务地址(可选) $options->notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址,例如:https://www.test.com/callback -->"; //可设置AES密钥,调用AES加解密相关接口时需要(可选) $options->encryptKey = "<-- 请填写您的AES密钥,例如:aa4BtZ4tspm2wnXLb1ThQA== -->"; return $options; } }
.env文件
ALI_APP_ID=2017082518448464 ALI_PUBLIC_KEY=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgSV6ePfSKZa9nY5opAmIKP+zFwvBCXLuj4GvKBbog6CW7EcZvoQqOn+lTxdBFJ3EVgdTxMXzCjhIDMsOnwh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