PayServices.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\pay;
  13. use crmeb\exceptions\ApiException;
  14. use crmeb\services\pay\Pay;
  15. /**
  16. * 支付统一入口
  17. * Class PayServices
  18. * @package app\services\pay
  19. */
  20. class PayServices
  21. {
  22. //微信支付类型
  23. const WEIXIN_PAY = 'weixin';
  24. //余额支付
  25. const YUE_PAY = 'yue';
  26. //线下支付
  27. const OFFLINE_PAY = 'offline';
  28. //支付宝
  29. const ALIAPY_PAY = 'alipay';
  30. //通联支付
  31. const ALLIN_PAY = 'allinpay';
  32. //好友代付
  33. const FRIEND = 'friend';
  34. //银行转账
  35. const BANK = 'bank';
  36. //贡献值支付
  37. const BROKERAGE_PAY = 'brokerage';
  38. //支付方式
  39. const PAY_TYPE = [
  40. PayServices::WEIXIN_PAY => '微信支付',
  41. PayServices::YUE_PAY => '余额支付',
  42. PayServices::OFFLINE_PAY => '线下支付',
  43. PayServices::ALIAPY_PAY => '支付宝',
  44. PayServices::FRIEND => '好友代付',
  45. PayServices::ALLIN_PAY => '通联支付',
  46. PayServices::BANK => '银行转账',
  47. PayServices::BROKERAGE_PAY => '贡献值支付',
  48. ];
  49. /**
  50. * @var array
  51. */
  52. protected $options = [];
  53. /**
  54. * @param string $key
  55. * @param $value
  56. * @return $this
  57. * @author 等风来
  58. * @email 136327134@qq.com
  59. * @date 2023/1/16
  60. */
  61. public function setOption(string $key, $value)
  62. {
  63. $this->options[$key] = $value;
  64. return $this;
  65. }
  66. /**
  67. * @param array $value
  68. * @return $this
  69. * @author 等风来
  70. * @email 136327134@qq.com
  71. * @date 2023/1/16
  72. */
  73. public function setOptions(array $value)
  74. {
  75. $this->options = $value;
  76. return $this;
  77. }
  78. /**
  79. * @param string $key
  80. * @param null $default
  81. * @return mixed|null
  82. * @author 等风来
  83. * @email 136327134@qq.com
  84. * @date 2023/1/16
  85. */
  86. protected function getOption(string $key, $default = null)
  87. {
  88. return $this->options[$key] ?? $default;
  89. }
  90. /**
  91. * 发起支付
  92. * @param string $payType
  93. * @param string $openid
  94. * @param string $orderId
  95. * @param string $price
  96. * @param string $successAction
  97. * @param string $body
  98. * @return array|string
  99. */
  100. public function pay(string $payType, string $orderId, string $price, string $successAction, string $body, array $options = [])
  101. {
  102. try {
  103. //这些全都是微信支付
  104. if (in_array($payType, ['routine', 'weixinh5', 'weixin', 'pc', 'store'])) {
  105. $payType = 'wechat_pay';
  106. //判断是否使用v3
  107. if (sys_config('pay_wechat_type') == 1) {
  108. $payType = 'v3_wechat_pay';
  109. }
  110. } else {
  111. if ($payType == 'alipay') {
  112. $payType = 'ali_pay';
  113. } elseif ($payType == 'allinpay') {
  114. $payType = 'allin_pay';
  115. }
  116. }
  117. /** @var Pay $pay */
  118. $pay = app()->make(Pay::class, [$payType]);
  119. return $pay->create($orderId, $price, $successAction, $body, '', ['pay_new_weixin_open' => (bool)sys_config('pay_new_weixin_open')] + $options);
  120. } catch (\Exception $e) {
  121. if (strpos($e->getMessage(), 'api unauthorized rid') !== false) {
  122. throw new ApiException('请在微信支付配置中将小程序商户号选择改为商户号绑定');
  123. }
  124. throw new ApiException($e->getMessage());
  125. }
  126. }
  127. /**
  128. * TODO 发起支付 弃用
  129. * @param string $payType
  130. * @param string $openid
  131. * @param string $orderId
  132. * @param string $price
  133. * @param string $successAction
  134. * @param string $body
  135. * @return array|string
  136. */
  137. // public function pay(string $payType, string $openid, string $orderId, string $price, string $successAction, string $body, bool $isCode = false)
  138. // {
  139. // try {
  140. //
  141. // //这些全都是微信支付
  142. // if (in_array($payType, ['routine', 'weixinh5', 'weixin', 'pc', 'store'])) {
  143. // $payType = 'wechat_pay';
  144. // //判断是否使用v3
  145. // if (sys_config('pay_wechat_type') == 1) {
  146. // $payType = 'v3_wechat_pay';
  147. // }
  148. // }
  149. //
  150. // if ($payType == 'alipay') {
  151. // $payType = 'ali_pay';
  152. // }
  153. //
  154. //
  155. // $options = [];
  156. // if (self::ALLIN_PAY === $payType) {
  157. // $options['returl'] = $this->getOption('returl');
  158. // if ($options['returl']) {
  159. // $options['returl'] = str_replace('http://', 'https://', $options['returl']);
  160. // }
  161. // $options['is_wechat'] = $this->getOption('is_wechat', false);
  162. // $options['appid'] = sys_config('routine_appId');
  163. // $payType = 'allin_pay';
  164. // }
  165. //
  166. // /** @var Pay $pay */
  167. // $pay = app()->make(Pay::class, [$payType]);
  168. //
  169. //
  170. // return $pay->create($orderId, $price, $successAction, $body, '', ['openid' => $openid, 'isCode' => $isCode, 'pay_new_weixin_open' => (bool)sys_config('pay_new_weixin_open')] + $options);
  171. //
  172. // } catch (\Exception $e) {
  173. // if (strpos($e->getMessage(), 'api unauthorized rid') !== false) {
  174. // throw new ApiException('请在微信支付配置中将小程序商户号选择改为商户号绑定');
  175. // }
  176. // throw new ApiException($e->getMessage());
  177. // }
  178. // }
  179. }