PayServices.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. */
  43. const FRIEND = 'friend';
  44. /**
  45. * 支付方式
  46. * @var string[]
  47. */
  48. const PAY_TYPE = [
  49. PayServices::WEIXIN_PAY => '微信支付',
  50. PayServices::YUE_PAY => '余额支付',
  51. PayServices::OFFLINE_PAY => '线下支付',
  52. PayServices::ALIAPY_PAY => '支付宝',
  53. PayServices::FRIEND => '好友代付',
  54. ];
  55. /**
  56. * 发起支付
  57. * @param string $payType
  58. * @param string $openid
  59. * @param string $orderId
  60. * @param string $price
  61. * @param string $successAction
  62. * @param string $body
  63. * @return array|string
  64. */
  65. public function pay(string $payType, string $openid, string $orderId, string $price, string $successAction, string $body, bool $isCode = false)
  66. {
  67. try {
  68. switch ($payType) {
  69. case 'routine':
  70. if (request()->isApp()) {
  71. return MiniProgramService::appPay($openid, $orderId, $price, $successAction, $body);
  72. } else {
  73. return MiniProgramService::jsPay($openid, $orderId, $price, $successAction, $body);
  74. }
  75. case 'weixinh5':
  76. return WechatService::paymentPrepare(null, $orderId, $price, $successAction, $body, '', 'MWEB');
  77. case 'weixin':
  78. if (request()->isApp()) {
  79. return WechatService::appPay($openid, $orderId, $price, $successAction, $body);
  80. } else {
  81. return WechatService::jsPay($openid, $orderId, $price, $successAction, $body);
  82. }
  83. case 'alipay':
  84. return AliPayService::instance()->create($body, $orderId, $price, $successAction, $openid, $openid, $isCode);
  85. case 'pc':
  86. case 'store':
  87. return WechatService::nativePay($openid, $orderId, $price, $successAction, $body);
  88. default:
  89. throw new ValidateException('支付方式不存在');
  90. }
  91. } catch (\Exception $e) {
  92. throw new ValidateException($e->getMessage());
  93. }
  94. }
  95. }