123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace app\common\library;
- use Alipay\EasySDK\Kernel\Factory;
- use Alipay\EasySDK\Kernel\Util\ResponseChecker;
- use app\common\model\Config;
- use think\Controller;
- class AliPay
- {
- /*
- 配置参数
- */
- public function __construct()
- {
- $this->appId = "2021003138683914";
- $this->rsaPrivateKey = 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDNQ36vpF2cveu4z09kbvaajPwJD2Lk4yPQL0d/yhW7oPlMAXOjUsHEJ43ooyG5AkRhMS0DbXVE0dbJZdnzEdGe/efCIXpcLR45xcLcvSS2Kmc8xvo3zhzvo45lA0+mBSwdE2nIlkQCLNgrZPnH0J9mliF3mJa3SgtX5kvu4UX76xE9uTj1oMFexnXo4S5Pw0d86YFTe3fSMdPnoI71tnA7f7cV0N5MyFnJnyRrd16EAJjn/IoX66mD7NtO6u7mFY71ijZ9PD6S7xCl+HnnUARIS4YlTMdsxgsNM9CtO0srI9uvW9Us9YaTNACdjK/TSCxWUC9hXXgjW1T2pBfs5cJRAgMBAAECggEBAKs/9XDqi6NanFBavV0Iaru0hg21BYf6rQVifJ9z5T/d5D9yMpz/TWOusGl3uInXB1+jHG/woVbiTvVGAVkW87FXGkVPkvtWNRJ0GMf7Eabdp7YAN//Y+TFBXmqOIaRcSlqTuLy+CU1bRpqlqvL27hYziQZzisKuhto17AIHdlDeZBYeS0RIb487g/qcTMywb9Rry7qjpa9SPzkK9pAjLJ3nkAi+dMGRLoEa0jSKh8B9QmkCn4BF64w2ykoukZGKBI/TM0BtZi6XsYGT2di5IgxEc78xN91RDWKlY/xjpfrakFFLrCx4bh3RjWmMfO8ctqC0xLkhnNhSeCaWzMvAVLECgYEA++/+u2xfkOByCaIZELcDo8Nx4Cjaf0ldtLO5iwi0Ve/0ObI8+TMcNs1cAeb+DrsyGrkdazpJKICMwN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ZwO3+3FdDeTMhZyZ8ka3dehACY5/yKF+upg+zbTuru5hWO9Yo2fTw+ku8Qpfh551AESEuGJUzHbMYLDTPQrTtLKyPbr1vVLPWGkzQAnYyv00gsVlAvYV14I1tU9qQX7OXCUQIDAQAB';
- }
- /**
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- * 支付宝支付
- */
- public static function ali_pay($subject,$totalAmount,$outTradeNo,$notifyUrl,$returnUrl='',$expire=null){
- Factory::setOptions(self::getOptions($notifyUrl));
- //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
- //$result = Factory::payment()->common()->create("iPhone6 16G", "20200326235526001", "88.88", "2088002656718920");
- $result = Factory::payment()->wap()->pay($subject,$outTradeNo , $totalAmount,'',$returnUrl,$expire);
- $responseChecker = new ResponseChecker();
- user_log('ali_pay.unify',['arguments'=>func_get_args(),$result->body]);
- //3. 处理响应或异常
- if ($responseChecker->success($result)) {
- return $result->body;
- } else {
- //echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
- }
- }
- public static function ali_pay_calcel($outTradeNo){
- Factory::setOptions(self::getOptions());
- $result = Factory::payment()->common()->close($outTradeNo);
- user_log('ali_pay_calcel',[
- compact('outTradeNo','result')
- ]);
- return $result;
- }
- public static function query($orderNo){
- Factory::setOptions(self::getOptions());
- $result = Factory::payment()->common()->query($orderNo);
- return $result;
- }
- public static function refund($orderNo,$amount){
- Factory::setOptions(self::getOptions());
- $result = Factory::payment()->common()->refund($orderNo,$amount);
- user_log('Alipay.refund',['arg'=>func_get_args(),$result]);
- return $result;
- }
- public static function ali_pay_pc($subject='商品',$totalAmount,$outTradeNo,$notifyUrl,$returnUrl){
- Factory::setOptions(self::getOptions($notifyUrl));
- try {
- $result = Factory::payment()->Page()->pay($subject,$outTradeNo,$totalAmount,$returnUrl);
- $responseChecker = new ResponseChecker();
- //3. 处理响应或异常
- if ($responseChecker->success($result)) {
- return $result->body;
- } else {
- echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
- }
- } catch (Exception $e) {
- return false;
- }
- }
- public static function getOptions($notifyUrl=''){
- $options = new \Alipay\EasySDK\Kernel\Config();
- $options->protocol = 'https';
- $options->gatewayHost = 'openapi.alipay.com';
- $options->signType = 'RSA2';
- $options->appId = '2021003138683914';
- // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
- $options->merchantPrivateKey = file_get_contents(sprintf('%s/data/alipay/play.wdmeta.art_私钥.txt',app()->getRootPath()));
- //$options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
- $options->alipayCertPath = sprintf('%s/data/alipay/alipayCertPublicKey_RSA2.crt',app()->getRootPath());
- //$options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
- $options->alipayRootCertPath = sprintf('%s/data/alipay/alipayRootCert.crt',app()->getRootPath());
- //$options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';
- $options->merchantCertPath = sprintf('%s/data/alipay/appCertPublicKey_2021003138683914.crt',app()->getRootPath());
- //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
- $options->alipayPublicKey ='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkI3OUjhrVtqqBaKBFmUgQHcio9rtcKdj2wwSIw7k+zzC7zaiFFQpnP+g115mz0V3WA+6r1jfr+Nh4kTKXDabiolzw/Ga2CkVGJRXoO/ZIyeOzIQWPLOJmPHicPTJG1BAWYDzZNnwz6gAmxq55m5VUva0ytojbSLdznWaUhD4nogkfgSI71eVzqDpVNcDaDBaaSnVE0INiIsf5pN2GRmlPJcIyZGq3w9wLka6TvL3DLYgaeEyLuzacO8epJ8m/hiLK85BFAP12eWIjjL7FkZV+xKH7a9qbU36KcRi7wymrFx55HNeRxRIZDmAvsX+R8fpwuIb7wNDwYY1enOas5HugQIDAQAB';
- //可设置异步通知接收服务地址(可选)
- $options->notifyUrl = $notifyUrl;
- //可设置AES密钥,调用AES加解密相关接口时需要(可选)
- $options->encryptKey = "";
- return $options;
- }
- public static function resulr(){
- Factory::setOptions(self::getOptions());
- return Factory::payment()->common()->verifyNotify(request()->post());
- }
- }
|