Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\services\pay;
  4. use crmeb\services\AliPayService;
  5. use crmeb\services\wechat\Payment;
  6. use think\exception\ValidateException;
  7. /**
  8. * 支付统一入口
  9. * Class PayServices
  10. * @package app\services\pay
  11. */
  12. class PayServices
  13. {
  14. /**
  15. * 微信支付类型
  16. */
  17. const WEIXIN_PAY = 'weixin';
  18. /**
  19. * 余额支付
  20. */
  21. const YUE_PAY = 'yue';
  22. /**
  23. * 线下支付
  24. */
  25. const OFFLINE_PAY = 'offline';
  26. /**
  27. * 支付宝
  28. */
  29. const ALIAPY_PAY = 'alipay';
  30. /**
  31. * 现金支付
  32. */
  33. const CASH_PAY = 'cash';
  34. /**
  35. * 支付方式
  36. * @var string[]
  37. */
  38. const PAY_TYPE = [
  39. PayServices::WEIXIN_PAY => '微信支付',
  40. PayServices::YUE_PAY => '余额支付',
  41. PayServices::OFFLINE_PAY => '线下支付',
  42. PayServices::ALIAPY_PAY => '支付宝',
  43. PayServices::CASH_PAY => '现金支付',
  44. ];
  45. /**
  46. * 二维码条码值
  47. * @var string
  48. */
  49. protected $authCode;
  50. /**
  51. * 设置二维码条码值
  52. * @param string $authCode
  53. * @return $this
  54. */
  55. public function setAuthCode(string $authCode)
  56. {
  57. $this->authCode = $authCode;
  58. return $this;
  59. }
  60. /**
  61. * 发起支付
  62. * @param string $payType
  63. * @param string $openid
  64. * @param string $orderId
  65. * @param string $price
  66. * @param string $successAction
  67. * @param string $body
  68. * @return array|string
  69. */
  70. public function pay(string $payType, string $openid, string $orderId, string $price, string $successAction, string $body, bool $isCode = false)
  71. {
  72. try {
  73. switch ($payType) {
  74. case 'routine':
  75. if (request()->isApp()) {
  76. return Payment::appPay($openid, $orderId, $price, $successAction, $body);
  77. } else {
  78. return Payment::jsPay($openid, $orderId, $price, $successAction, $body);
  79. }
  80. case 'weixinh5':
  81. return Payment::paymentOrder(null, $orderId, $price, $successAction, $body, '', 'MWEB');
  82. case self::WEIXIN_PAY:
  83. if ($this->authCode) {
  84. return Payment::microPay($this->authCode, $orderId, $price, $successAction, $body);
  85. } else {
  86. if (request()->isApp()) {
  87. return Payment::appPay($openid, $orderId, $price, $successAction, $body);
  88. } else {
  89. return Payment::jsPay($openid, $orderId, $price, $successAction, $body);
  90. }
  91. }
  92. case self::ALIAPY_PAY:
  93. if ($this->authCode) {
  94. return AliPayService::instance()->microPay($this->authCode, $body, $orderId, $price, $successAction);
  95. } else {
  96. return AliPayService::instance()->create($body, $orderId, $price, $successAction, $openid, $openid, $isCode);
  97. }
  98. case 'pc':
  99. case 'store':
  100. return Payment::nativePay($openid, $orderId, $price, $successAction, $body);
  101. default:
  102. throw new ValidateException('支付方式不存在');
  103. }
  104. } catch (\Throwable $e) {
  105. throw new ValidateException($e->getMessage());
  106. }
  107. }
  108. }