PayServices.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\MiniProgramService;
  15. use crmeb\services\WechatService;
  16. use think\exception\ValidateException;
  17. /**
  18. * 支付统一入口
  19. * Class PayServices
  20. * @package app\services\pay
  21. */
  22. class PayServices
  23. {
  24. /**
  25. * 微信支付类型
  26. */
  27. const WEIXIN_PAY = 'weixin';
  28. /**
  29. * 余额支付
  30. */
  31. const YUE_PAY = 'yue';
  32. /**
  33. * 线下支付
  34. */
  35. const OFFLINE_PAY = 'offline';
  36. /**
  37. * 支付宝
  38. */
  39. const ALIAPY_PAY = 'alipay';
  40. /**
  41. * 支付方式
  42. * @var string[]
  43. */
  44. const PAY_TYPE = [
  45. PayServices::WEIXIN_PAY => '微信支付',
  46. PayServices::YUE_PAY => '余额支付',
  47. PayServices::OFFLINE_PAY => '线下支付',
  48. PayServices::ALIAPY_PAY => '支付宝'
  49. ];
  50. /**
  51. * 发起支付
  52. * @param string $payType
  53. * @param string $openid
  54. * @param string $orderId
  55. * @param string $price
  56. * @param string $successAction
  57. * @param string $body
  58. * @return array|string
  59. */
  60. public function pay(string $payType, string $openid, string $orderId, string $price, string $successAction, string $body, bool $isCode = false)
  61. {
  62. try {
  63. switch ($payType) {
  64. case 'routine':
  65. if (request()->isApp()) {
  66. return MiniProgramService::appPay($openid, $orderId, $price, $successAction, $body);
  67. } else {
  68. return MiniProgramService::jsPay($openid, $orderId, $price, $successAction, $body);
  69. }
  70. case 'weixinh5':
  71. return WechatService::paymentPrepare(null, $orderId, $price, $successAction, $body, '', 'MWEB');
  72. case 'weixin':
  73. if (request()->isApp()) {
  74. return WechatService::appPay($openid, $orderId, $price, $successAction, $body);
  75. } else {
  76. return WechatService::jsPay($openid, $orderId, $price, $successAction, $body);
  77. }
  78. case 'alipay':
  79. return AliPayService::instance()->create($body, $orderId, $price, $successAction, $openid, $openid, $isCode);
  80. case 'pc':
  81. case 'store':
  82. return WechatService::nativePay($openid, $orderId, $price, $successAction, $body);
  83. default:
  84. throw new ValidateException('支付方式不存在');
  85. }
  86. } catch (\Exception $e) {
  87. throw new ValidateException($e->getMessage());
  88. }
  89. }
  90. }