PayServices.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 PAY_TYPE = [
  38. PayServices::WEIXIN_PAY => '微信支付',
  39. PayServices::YUE_PAY => '余额支付',
  40. PayServices::OFFLINE_PAY => '线下支付',
  41. PayServices::ALIAPY_PAY => '支付宝',
  42. PayServices::FRIEND => '好友代付',
  43. PayServices::ALLIN_PAY => '通联支付',
  44. PayServices::BANK => '银行转账',
  45. ];
  46. /**
  47. * @var array
  48. */
  49. protected $options = [];
  50. /**
  51. * @param string $key
  52. * @param $value
  53. * @return $this
  54. * @author 等风来
  55. * @email 136327134@qq.com
  56. * @date 2023/1/16
  57. */
  58. public function setOption(string $key, $value)
  59. {
  60. $this->options[$key] = $value;
  61. return $this;
  62. }
  63. /**
  64. * @param array $value
  65. * @return $this
  66. * @author 等风来
  67. * @email 136327134@qq.com
  68. * @date 2023/1/16
  69. */
  70. public function setOptions(array $value)
  71. {
  72. $this->options = $value;
  73. return $this;
  74. }
  75. /**
  76. * @param string $key
  77. * @param null $default
  78. * @return mixed|null
  79. * @author 等风来
  80. * @email 136327134@qq.com
  81. * @date 2023/1/16
  82. */
  83. protected function getOption(string $key, $default = null)
  84. {
  85. return $this->options[$key] ?? $default;
  86. }
  87. /**
  88. * 发起支付
  89. * @param string $payType
  90. * @param string $openid
  91. * @param string $orderId
  92. * @param string $price
  93. * @param string $successAction
  94. * @param string $body
  95. * @return array|string
  96. */
  97. public function pay(string $payType, string $orderId, string $price, string $successAction, string $body, array $options = [])
  98. {
  99. try {
  100. //这些全都是微信支付
  101. if (in_array($payType, ['routine', 'weixinh5', 'weixin', 'pc', 'store'])) {
  102. $payType = 'wechat_pay';
  103. //判断是否使用v3
  104. if (sys_config('pay_wechat_type') == 1) {
  105. $payType = 'v3_wechat_pay';
  106. }
  107. } else {
  108. if ($payType == 'alipay') {
  109. $payType = 'ali_pay';
  110. } elseif ($payType == 'allinpay') {
  111. $payType = 'allin_pay';
  112. }
  113. }
  114. /** @var Pay $pay */
  115. $pay = app()->make(Pay::class, [$payType]);
  116. return $pay->create($orderId, $price, $successAction, $body, '', ['pay_new_weixin_open' => (bool)sys_config('pay_new_weixin_open')] + $options);
  117. } catch (\Exception $e) {
  118. if (strpos($e->getMessage(), 'api unauthorized rid') !== false) {
  119. throw new ApiException('请在微信支付配置中将小程序商户号选择改为商户号绑定');
  120. }
  121. throw new ApiException($e->getMessage());
  122. }
  123. }
  124. /**
  125. * TODO 发起支付 弃用
  126. * @param string $payType
  127. * @param string $openid
  128. * @param string $orderId
  129. * @param string $price
  130. * @param string $successAction
  131. * @param string $body
  132. * @return array|string
  133. */
  134. // public function pay(string $payType, string $openid, string $orderId, string $price, string $successAction, string $body, bool $isCode = false)
  135. // {
  136. // try {
  137. //
  138. // //这些全都是微信支付
  139. // if (in_array($payType, ['routine', 'weixinh5', 'weixin', 'pc', 'store'])) {
  140. // $payType = 'wechat_pay';
  141. // //判断是否使用v3
  142. // if (sys_config('pay_wechat_type') == 1) {
  143. // $payType = 'v3_wechat_pay';
  144. // }
  145. // }
  146. //
  147. // if ($payType == 'alipay') {
  148. // $payType = 'ali_pay';
  149. // }
  150. //
  151. //
  152. // $options = [];
  153. // if (self::ALLIN_PAY === $payType) {
  154. // $options['returl'] = $this->getOption('returl');
  155. // if ($options['returl']) {
  156. // $options['returl'] = str_replace('http://', 'https://', $options['returl']);
  157. // }
  158. // $options['is_wechat'] = $this->getOption('is_wechat', false);
  159. // $options['appid'] = sys_config('routine_appId');
  160. // $payType = 'allin_pay';
  161. // }
  162. //
  163. // /** @var Pay $pay */
  164. // $pay = app()->make(Pay::class, [$payType]);
  165. //
  166. //
  167. // return $pay->create($orderId, $price, $successAction, $body, '', ['openid' => $openid, 'isCode' => $isCode, 'pay_new_weixin_open' => (bool)sys_config('pay_new_weixin_open')] + $options);
  168. //
  169. // } catch (\Exception $e) {
  170. // if (strpos($e->getMessage(), 'api unauthorized rid') !== false) {
  171. // throw new ApiException('请在微信支付配置中将小程序商户号选择改为商户号绑定');
  172. // }
  173. // throw new ApiException($e->getMessage());
  174. // }
  175. // }
  176. }