AlipayService.php 4.4 KB

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