PayServices.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. * 微信支付类型
  24. */
  25. const WEIXIN_PAY = 'weixin';
  26. /**
  27. * 余额支付
  28. */
  29. const YUE_PAY = 'yue';
  30. /**
  31. * 线下支付
  32. */
  33. const OFFLINE_PAY = 'offline';
  34. /**
  35. * 支付宝
  36. */
  37. const ALIAPY_PAY = 'alipay';
  38. /**
  39. * 好友代付
  40. */
  41. const FRIEND = 'friend';
  42. /**
  43. * 支付方式
  44. * @var string[]
  45. */
  46. const PAY_TYPE = [
  47. PayServices::WEIXIN_PAY => '微信支付',
  48. PayServices::YUE_PAY => '余额支付',
  49. PayServices::OFFLINE_PAY => '线下支付',
  50. PayServices::ALIAPY_PAY => '支付宝',
  51. PayServices::FRIEND => '好友代付',
  52. ];
  53. /**
  54. * 发起支付
  55. * @param string $payType
  56. * @param string $openid
  57. * @param string $orderId
  58. * @param string $price
  59. * @param string $successAction
  60. * @param string $body
  61. * @return array|string
  62. */
  63. public function pay(string $payType, string $openid, string $orderId, string $price, string $successAction, string $body, bool $isCode = false)
  64. {
  65. try {
  66. //这些全都是微信支付
  67. if (in_array($payType, ['routine', 'weixinh5', 'weixin', 'pc', 'store'])) {
  68. $payType = 'wechat_pay';
  69. //判断是否使用v3
  70. if (sys_config('pay_wechat_type') == 1) {
  71. $payType = 'v3_wechat_pay';
  72. }
  73. }
  74. if ($payType == 'alipay') {
  75. $payType = 'ali_pay';
  76. }
  77. /** @var Pay $pay */
  78. $pay = app()->make(Pay::class, [$payType]);
  79. return $pay->create($orderId, $price, $successAction, $body, '', ['openid' => $openid, 'isCode' => $isCode, 'pay_new_weixin_open' => (bool)sys_config('pay_new_weixin_open')]);
  80. } catch (\Exception $e) {
  81. if (strpos($e->getMessage(), 'api unauthorized rid') !== false) {
  82. throw new ApiException('请在微信支付配置中将小程序商户号选择改为商户号绑定');
  83. }
  84. throw new ApiException($e->getMessage());
  85. }
  86. }
  87. }