AlipayService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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() + (14 * 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 payAlipayBarCode($out_trade_no, $total_fee, $body, $auth_code, $detail = '')
  79. {
  80. $data = [
  81. 'trade_no' => $out_trade_no,
  82. 'scene' => 'bar_code',
  83. 'auth_code' => $auth_code,
  84. 'subject' => $body,
  85. 'amount' => floatval($total_fee),
  86. 'time_expire' => time() + (14 * 60),
  87. ];
  88. try {
  89. $res = $this->application->pay(Client::ALI_CHANNEL_BAR, $data);
  90. $paid = 0;
  91. if($res['code'] == '10000' && $res['msg'] == 'Success') {
  92. $paid = 1;
  93. $message = '支付成功';
  94. $transaction_id = $res['trade_no'];
  95. }else{
  96. $message = '支付失败:'.$res['sub_msg'];
  97. }
  98. return [
  99. 'paid' => $paid,
  100. 'message' => $message,
  101. 'transaction_id' => $transaction_id ?? 0,
  102. 'payInfo' => $res
  103. ];
  104. } catch (\Exception $e) {
  105. throw new ValidateException('支付宝支付错误返回:' . $e->getMessage());
  106. }
  107. }
  108. public function appPaymentPrepare($out_trade_no, $total_fee, $body, $detail = '')
  109. {
  110. $data = [
  111. 'body' => $detail ?: $body,
  112. 'subject' => $body,
  113. 'trade_no' => $out_trade_no,
  114. 'amount' => floatval($total_fee),
  115. 'time_expire' => time() + (14 * 60),
  116. 'goods_type' => 1,
  117. 'return_params' => $out_trade_no,
  118. ];
  119. try {
  120. $res = $this->application->pay(Client::ALI_CHANNEL_APP, $data);
  121. } catch (\Exception $e) {
  122. throw new ValidateException('支付宝支付错误返回:' . $e->getMessage());
  123. }
  124. return $res;
  125. }
  126. public function wapPaymentPrepare($out_trade_no, $total_fee, $body, $return_url = '', $detail = '')
  127. {
  128. $data = [
  129. 'body' => $detail ?: $body,
  130. 'subject' => $body,
  131. 'trade_no' => $out_trade_no,
  132. 'amount' => floatval($total_fee),
  133. 'time_expire' => time() + (14 * 60),
  134. 'goods_type' => 1,
  135. 'return_params' => $out_trade_no,
  136. ];
  137. $config = AlipayProxy::$config;
  138. if ($return_url)
  139. $config->offsetSet('return_url', $return_url);
  140. $data['quit_url'] = $config->get('return_url');
  141. try {
  142. $res = $this->application->pay(Client::ALI_CHANNEL_WAP, $data);
  143. } catch (\Exception $e) {
  144. throw new ValidateException('支付宝支付错误返回:' . $e->getMessage());
  145. }
  146. return $res;
  147. }
  148. public function payOrderRefund($trade_sn, array $data)
  149. {
  150. $data = [
  151. 'trade_no' => $trade_sn,
  152. 'refund_fee' => floatval($data['refund_price']),
  153. 'reason' => $data['refund_id'],
  154. 'refund_no' => $data['refund_id'],
  155. ];
  156. return $this->application->refund($data);
  157. }
  158. public function notify($type)
  159. {
  160. $this->application->notify(new AlipayNotify($type));
  161. }
  162. public function query(string $trade_sn)
  163. {
  164. $data = [
  165. 'trade_no' => $trade_sn
  166. ];
  167. try {
  168. return $this->application->tradeQuery($data);
  169. } catch (\Exception $e) {
  170. throw new ValidateException('支付宝支付错误返回:' . $e->getMessage());
  171. }
  172. }
  173. }