PayServices.php 5.9 KB

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