AliPay.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\common\library;
  3. use Alipay\EasySDK\Kernel\Factory;
  4. use Alipay\EasySDK\Kernel\Util\ResponseChecker;
  5. use app\common\model\Config;
  6. use think\Controller;
  7. class AliPay
  8. {
  9. /*
  10. 配置参数
  11. */
  12. public function __construct()
  13. {
  14. $this->appId = "2021003138683914";
  15. $this->rsaPrivateKey = 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDNQ36vpF2cveu4z09kbvaajPwJD2Lk4yPQL0d/yhW7oPlMAXOjUsHEJ43ooyG5AkRhMS0DbXVE0dbJZdnzEdGe/efCIXpcLR45xcLcvSS2Kmc8xvo3zhzvo45lA0+mBSwdE2nIlkQCLNgrZPnH0J9mliF3mJa3SgtX5kvu4UX76xE9uTj1oMFexnXo4S5Pw0d86YFTe3fSMdPnoI71tnA7f7cV0N5MyFnJnyRrd16EAJjn/IoX66mD7NtO6u7mFY71ijZ9PD6S7xCl+HnnUARIS4YlTMdsxgsNM9CtO0srI9uvW9Us9YaTNACdjK/TSCxWUC9hXXgjW1T2pBfs5cJRAgMBAAECggEBAKs/9XDqi6NanFBavV0Iaru0hg21BYf6rQVifJ9z5T/d5D9yMpz/TWOusGl3uInXB1+jHG/woVbiTvVGAVkW87FXGkVPkvtWNRJ0GMf7Eabdp7YAN//Y+TFBXmqOIaRcSlqTuLy+CU1bRpqlqvL27hYziQZzisKuhto17AIHdlDeZBYeS0RIb487g/qcTMywb9Rry7qjpa9SPzkK9pAjLJ3nkAi+dMGRLoEa0jSKh8B9QmkCn4BF64w2ykoukZGKBI/TM0BtZi6XsYGT2di5IgxEc78xN91RDWKlY/xjpfrakFFLrCx4bh3RjWmMfO8ctqC0xLkhnNhSeCaWzMvAVLECgYEA++/+u2xfkOByCaIZELcDo8Nx4Cjaf0ldtLO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ZL7uFF++sRPbk49aDBXsZ16OEuT8NHfOmBU3t30jHT56CO9bZwO3+3FdDeTMhZyZ8ka3dehACY5/yKF+upg+zbTuru5hWO9Yo2fTw+ku8Qpfh551AESEuGJUzHbMYLDTPQrTtLKyPbr1vVLPWGkzQAnYyv00gsVlAvYV14I1tU9qQX7OXCUQIDAQAB';
  17. }
  18. /**
  19. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  20. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  21. * @throws \GuzzleHttp\Exception\GuzzleException
  22. * 支付宝支付
  23. */
  24. public static function ali_pay($subject,$totalAmount,$outTradeNo,$notifyUrl,$returnUrl='',$expire=null){
  25. Factory::setOptions(self::getOptions($notifyUrl));
  26. //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
  27. //$result = Factory::payment()->common()->create("iPhone6 16G", "20200326235526001", "88.88", "2088002656718920");
  28. $result = Factory::payment()->wap()->pay($subject,$outTradeNo , $totalAmount,'',$returnUrl,$expire);
  29. $responseChecker = new ResponseChecker();
  30. user_log('ali_pay.unify',['arguments'=>func_get_args(),$result->body]);
  31. //3. 处理响应或异常
  32. if ($responseChecker->success($result)) {
  33. return $result->body;
  34. } else {
  35. //echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
  36. }
  37. }
  38. public static function ali_pay_calcel($outTradeNo){
  39. Factory::setOptions(self::getOptions());
  40. $result = Factory::payment()->common()->close($outTradeNo);
  41. user_log('ali_pay_calcel',[
  42. compact('outTradeNo','result')
  43. ]);
  44. return $result;
  45. }
  46. public static function query($orderNo){
  47. Factory::setOptions(self::getOptions());
  48. $result = Factory::payment()->common()->query($orderNo);
  49. return $result;
  50. }
  51. public static function refund($orderNo,$amount){
  52. Factory::setOptions(self::getOptions());
  53. $result = Factory::payment()->common()->refund($orderNo,$amount);
  54. user_log('Alipay.refund',['arg'=>func_get_args(),$result]);
  55. return $result;
  56. }
  57. public static function ali_pay_pc($subject='商品',$totalAmount,$outTradeNo,$notifyUrl,$returnUrl){
  58. Factory::setOptions(self::getOptions($notifyUrl));
  59. try {
  60. $result = Factory::payment()->Page()->pay($subject,$outTradeNo,$totalAmount,$returnUrl);
  61. $responseChecker = new ResponseChecker();
  62. //3. 处理响应或异常
  63. if ($responseChecker->success($result)) {
  64. return $result->body;
  65. } else {
  66. echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
  67. }
  68. } catch (Exception $e) {
  69. return false;
  70. }
  71. }
  72. public static function getOptions($notifyUrl=''){
  73. $options = new \Alipay\EasySDK\Kernel\Config();
  74. $options->protocol = 'https';
  75. $options->gatewayHost = 'openapi.alipay.com';
  76. $options->signType = 'RSA2';
  77. $options->appId = '2021003138683914';
  78. // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
  79. $options->merchantPrivateKey = file_get_contents(sprintf('%s/data/alipay/play.wdmeta.art_私钥.txt',app()->getRootPath()));
  80. //$options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
  81. $options->alipayCertPath = sprintf('%s/data/alipay/alipayCertPublicKey_RSA2.crt',app()->getRootPath());
  82. //$options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
  83. $options->alipayRootCertPath = sprintf('%s/data/alipay/alipayRootCert.crt',app()->getRootPath());
  84. //$options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';
  85. $options->merchantCertPath = sprintf('%s/data/alipay/appCertPublicKey_2021003138683914.crt',app()->getRootPath());
  86. //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
  87. $options->alipayPublicKey ='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkI3OUjhrVtqqBaKBFmUgQHcio9rtcKdj2wwSIw7k+zzC7zaiFFQpnP+g115mz0V3WA+6r1jfr+Nh4kTKXDabiolzw/Ga2CkVGJRXoO/ZIyeOzIQWPLOJmPHicPTJG1BAWYDzZNnwz6gAmxq55m5VUva0ytojbSLdznWaUhD4nogkfgSI71eVzqDpVNcDaDBaaSnVE0INiIsf5pN2GRmlPJcIyZGq3w9wLka6TvL3DLYgaeEyLuzacO8epJ8m/hiLK85BFAP12eWIjjL7FkZV+xKH7a9qbU36KcRi7wymrFx55HNeRxRIZDmAvsX+R8fpwuIb7wNDwYY1enOas5HugQIDAQAB';
  88. //可设置异步通知接收服务地址(可选)
  89. $options->notifyUrl = $notifyUrl;
  90. //可设置AES密钥,调用AES加解密相关接口时需要(可选)
  91. $options->encryptKey = "";
  92. return $options;
  93. }
  94. public static function resulr(){
  95. Factory::setOptions(self::getOptions());
  96. return Factory::payment()->common()->verifyNotify(request()->post());
  97. }
  98. }