RechargeServices.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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 app\model\user\UserRecharge;
  14. use app\services\user\UserRechargeServices;
  15. use app\services\wechat\WechatUserServices;
  16. use crmeb\exceptions\ApiException;
  17. use Darabonba\GatewaySpi\Models\InterceptorContext\request;
  18. /**
  19. *
  20. * Class RechargeServices
  21. * @package app\services\pay
  22. */
  23. class RechargeServices
  24. {
  25. protected $pay;
  26. /**
  27. * RechargeServices constructor.
  28. * @param PayServices $pay
  29. */
  30. public function __construct(PayServices $pay)
  31. {
  32. $this->pay = $pay;
  33. }
  34. public function recharge(UserRecharge $recharge)
  35. {
  36. if (!$recharge) {
  37. throw new ApiException(410173);
  38. }
  39. if ($recharge['paid'] == 1) {
  40. throw new ApiException(410174);
  41. }
  42. $payType = '';
  43. switch ($recharge['recharge_type']) {
  44. case 'weixin':
  45. case 'weixinh5':
  46. case 'routine':
  47. $payType = PayServices::WEIXIN_PAY;
  48. break;
  49. case PayServices::ALIAPY_PAY:
  50. $payType = PayServices::ALIAPY_PAY;
  51. break;
  52. }
  53. $payType = app()->make(OrderPayServices::class)->getPayType($payType);
  54. if (!$payType) {
  55. throw new ApiException(410278);
  56. }
  57. if ($recharge['recharge_type'] == PayServices::WEIXIN_PAY && !request()->isH5()) {
  58. /** @var WechatUserServices $wechatUser */
  59. $wechatUser = app()->make(WechatUserServices::class);
  60. if (request()->isApp()) {
  61. $userType = 'app';
  62. } else if (request()->isRoutine()) {
  63. $userType = 'routine';
  64. } else if (request()->isWechat()) {
  65. $userType = 'wechat';
  66. } else {
  67. throw new ApiException(410275);
  68. }
  69. $openid = $wechatUser->uidToOpenid((int)$recharge['uid'], $userType);
  70. if (!$openid) {
  71. throw new ApiException(410275);
  72. }
  73. } else {
  74. $openid = '';
  75. }
  76. $res = $this->pay->pay($recharge['recharge_type'], $recharge['order_id'], $recharge['price'], 'user_recharge', '用户充值', ['openid' => $openid]);
  77. if ($payType == PayServices::WEIXIN_PAY) {
  78. if (request()->isH5()) {
  79. $payStstus = 'wechat_h5_pay';
  80. } else {
  81. $payStstus = 'wechat_pay';
  82. }
  83. } else if ($payType == PayServices::ALIAPY_PAY) {
  84. $payStstus = 'alipay_pay';
  85. } else if ($payType == PayServices::ALLIN_PAY) {
  86. $payStstus = 'allinpay_pay';
  87. }
  88. return ['jsConfig' => $res, 'pay_key' => md5($recharge['order_id']), 'order_id' => $recharge['order_id'], 'pay_type' => strtoupper($payStstus)];
  89. }
  90. }