PayServices.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\services\AliPayService;
  14. use crmeb\services\wechat\Payment;
  15. use think\exception\ValidateException;
  16. /**
  17. * 支付统一入口
  18. * Class PayServices
  19. * @package app\services\pay
  20. */
  21. class PayServices
  22. {
  23. /**
  24. * 微信支付类型
  25. */
  26. const WEIXIN_PAY = 'weixin';
  27. /**
  28. * 余额支付
  29. */
  30. const YUE_PAY = 'yue';
  31. /**
  32. * 线下支付
  33. */
  34. const OFFLINE_PAY = 'offline';
  35. /**
  36. * 支付宝
  37. */
  38. const ALIAPY_PAY = 'alipay';
  39. /**
  40. * 现金支付
  41. */
  42. const CASH_PAY = 'cash';
  43. /**
  44. * 支付方式
  45. * @var string[]
  46. */
  47. const PAY_TYPE = [
  48. PayServices::WEIXIN_PAY => '微信支付',
  49. PayServices::YUE_PAY => '余额支付',
  50. PayServices::OFFLINE_PAY => '线下支付',
  51. PayServices::ALIAPY_PAY => '支付宝',
  52. PayServices::CASH_PAY => '现金支付',
  53. ];
  54. /**
  55. * 二维码条码值
  56. * @var string
  57. */
  58. protected $authCode;
  59. /**
  60. * 设置二维码条码值
  61. * @param string $authCode
  62. * @return $this
  63. */
  64. public function setAuthCode(string $authCode)
  65. {
  66. $this->authCode = $authCode;
  67. return $this;
  68. }
  69. /**
  70. * 发起支付
  71. * @param string $payType
  72. * @param string $openid
  73. * @param string $orderId
  74. * @param string $price
  75. * @param string $successAction
  76. * @param string $body
  77. * @return array|string
  78. */
  79. public function pay(string $payType, string $openid, string $orderId, string $price, string $successAction, string $body, bool $isCode = false)
  80. {
  81. try {
  82. switch ($payType) {
  83. case 'routine':
  84. //微信支付,从APP端请求过来
  85. if (request()->isApp()) {
  86. return Payment::appPay($openid, $orderId, $price, $successAction, $body);
  87. } else {
  88. //判断有没有打开小程序支付
  89. if (sys_config('pay_routine_open', 0)) {
  90. return Payment::miniPay($openid, $orderId, $price, $successAction, $body);
  91. } else {
  92. //开启了v3支付
  93. if (Payment::instance()->isV3PAy) {
  94. return Payment::instance()->application()->v3pay->miniprogPay($openid, $orderId, $price, $body, $successAction);
  95. }
  96. return Payment::jsPay($openid, $orderId, $price, $successAction, $body);
  97. }
  98. }
  99. case 'weixinh5':
  100. ////开启了v3支付
  101. if (Payment::instance()->isV3PAy) {
  102. return Payment::instance()->application()->v3pay->h5Pay($orderId, $price, $body, $successAction);
  103. }
  104. //旧版v2支付
  105. return Payment::paymentOrder(null, $orderId, $price, $successAction, $body, '', 'MWEB');
  106. case self::WEIXIN_PAY:
  107. //微信支付,付款码支付,付款码支付使用v2支付接口
  108. if ($this->authCode) {
  109. return Payment::microPay($this->authCode, $orderId, $price, $successAction, $body);
  110. } else {
  111. //微信支付,从APP端请求过来
  112. if (request()->isApp()) {
  113. return Payment::appPay($openid, $orderId, $price, $successAction, $body);
  114. } else {
  115. //开启了v3支付
  116. if (Payment::instance()->isV3PAy) {
  117. return Payment::instance()->application()->v3pay->jsapiPay($openid, $orderId, $price, $body, $successAction);
  118. }
  119. //使用v2旧版支付接口
  120. return Payment::jsPay($openid, $orderId, $price, $successAction, $body);
  121. }
  122. }
  123. case self::ALIAPY_PAY:
  124. if ($this->authCode) {
  125. return AliPayService::instance()->microPay($this->authCode, $body, $orderId, $price, $successAction);
  126. } else {
  127. return AliPayService::instance()->create($body, $orderId, $price, $successAction, $openid, $openid, $isCode);
  128. }
  129. case 'pc':
  130. case 'store':
  131. //方法内部已经做了区分v2和v3
  132. return Payment::nativePay($openid, $orderId, $price, $successAction, $body);
  133. default:
  134. throw new ValidateException('支付方式不存在');
  135. }
  136. } catch (\Throwable $e) {
  137. throw new ValidateException($e->getMessage());
  138. }
  139. }
  140. }