AlipayService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use crmeb\services\alipay\AlipayNotify;
  13. use Payment\Client;
  14. use Payment\Proxies\AlipayProxy;
  15. use think\exception\ValidateException;
  16. use think\facade\Route;
  17. class AlipayService
  18. {
  19. /**
  20. * @var Client
  21. */
  22. protected $application;
  23. /**
  24. * @var array
  25. */
  26. protected $config;
  27. public function __construct(array $config)
  28. {
  29. $this->config = $config;
  30. $this->application = new Client(Client::ALIPAY, $config);
  31. }
  32. public static function create($type = '')
  33. {
  34. return new self(self::getConfig($type));
  35. }
  36. public static function getConfig($type = '')
  37. {
  38. $config = systemConfig(['site_url', 'alipay_app_id', 'alipay_public_key', 'alipay_private_key', 'alipay_open']);
  39. if (!$config['alipay_open']) throw new ValidateException('支付宝支付未开启');
  40. $siteUrl = $config['site_url'];
  41. return [
  42. 'app_id' => $config['alipay_app_id'],
  43. 'sign_type' => 'RSA2', // RSA RSA2
  44. 'limit_pay' => [
  45. // 'balance',// 余额
  46. // 'moneyFund',// 余额宝
  47. // 'debitCardExpress',// 借记卡快捷
  48. //'creditCard',//信用卡
  49. //'creditCardExpress',// 信用卡快捷
  50. //'creditCardCartoon',//信用卡卡通
  51. //'credit_group',// 信用支付类型(包含信用卡卡通、信用卡快捷、花呗、花呗分期)
  52. ], // 用户不可用指定渠道支付当有多个渠道时用“,”分隔
  53. // 支付宝公钥字符串
  54. 'ali_public_key' => $config['alipay_public_key'],
  55. // 自己生成的密钥字符串
  56. 'rsa_private_key' => $config['alipay_private_key'],
  57. 'notify_url' => rtrim($siteUrl, '/') . Route::buildUrl('alipayNotify', ['type' => $type])->build(),
  58. 'return_url' => $siteUrl,
  59. ];
  60. }
  61. public function qrPaymentPrepare($out_trade_no, $total_fee, $body, $detail = '')
  62. {
  63. $data = [
  64. 'body' => $detail ?: $body,
  65. 'subject' => $body,
  66. 'trade_no' => $out_trade_no,
  67. 'amount' => floatval($total_fee),
  68. 'time_expire' => time() + (15 * 60),
  69. 'return_params' => $out_trade_no,
  70. ];
  71. try {
  72. $res = $this->application->pay(Client::ALI_CHANNEL_QR, $data);
  73. } catch (\Exception $e) {
  74. throw new ValidateException('支付宝支付错误返回:' . $e->getMessage());
  75. }
  76. return $res['qr_code'];
  77. }
  78. public function wapPaymentPrepare($out_trade_no, $total_fee, $body, $return_url = '', $detail = '')
  79. {
  80. $data = [
  81. 'body' => $detail ?: $body,
  82. 'subject' => $body,
  83. 'trade_no' => $out_trade_no,
  84. 'amount' => floatval($total_fee),
  85. 'time_expire' => time() + (15 * 60),
  86. 'goods_type' => 1,
  87. 'return_params' => $out_trade_no,
  88. ];
  89. $config = AlipayProxy::$config;
  90. if ($return_url)
  91. $config->offsetSet('return_url', $return_url);
  92. $data['quit_url'] = $config->get('return_url');
  93. try {
  94. $res = $this->application->pay(Client::ALI_CHANNEL_WAP, $data);
  95. } catch (\Exception $e) {
  96. throw new ValidateException('支付宝支付错误返回:' . $e->getMessage());
  97. }
  98. return $res;
  99. }
  100. public function payOrderRefund($trade_sn, array $data)
  101. {
  102. $data = [
  103. 'trade_no' => $trade_sn,
  104. 'refund_fee' => floatval($data['refund_price']),
  105. 'reason' => $data['refund_id'],
  106. 'refund_no' => $data['refund_id'],
  107. ];
  108. return $this->application->refund($data);
  109. }
  110. public function notify($type, array $data)
  111. {
  112. $post = $_POST;
  113. $_POST = $data;
  114. $this->application->notify(new AlipayNotify($type));
  115. $_POST = $post;
  116. }
  117. }