RechargeServices.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 app\model\user\UserRecharge;
  14. use app\services\user\UserRechargeServices;
  15. use app\services\wechat\WechatUserServices;
  16. use crmeb\exceptions\ApiException;
  17. /**
  18. *
  19. * Class RechargeServices
  20. * @package app\services\pay
  21. */
  22. class RechargeServices
  23. {
  24. protected $pay;
  25. /**
  26. * RechargeServices constructor.
  27. * @param PayServices $pay
  28. */
  29. public function __construct(PayServices $pay)
  30. {
  31. $this->pay = $pay;
  32. }
  33. public function recharge(UserRecharge $recharge)
  34. {
  35. if (!$recharge) {
  36. throw new ApiException(410173);
  37. }
  38. if ($recharge['paid'] == 1) {
  39. throw new ApiException(410174);
  40. }
  41. $userType = '';
  42. switch ($recharge['recharge_type']) {
  43. case 'weixin':
  44. case 'weixinh5':
  45. $userType = 'wechat';
  46. break;
  47. case 'routine':
  48. $userType = 'routine';
  49. break;
  50. case PayServices::ALIAPY_PAY:
  51. $userType = PayServices::ALIAPY_PAY;
  52. break;
  53. }
  54. $userType = get_pay_type($userType);
  55. if (!$userType) {
  56. throw new ApiException(410278);
  57. }
  58. /** @var WechatUserServices $wechatUser */
  59. $wechatUser = app()->make(WechatUserServices::class);
  60. $openid = $wechatUser->uidToOpenid((int)$recharge['uid'], $userType);
  61. if (in_array($recharge['recharge_type'], ['weixin', 'routine']) && !request()->isApp()) {
  62. if (!$openid) {
  63. throw new ApiException(410275);
  64. }
  65. } else {
  66. $openid = '';
  67. }
  68. $res = $this->pay->pay($recharge['recharge_type'], $openid, $recharge['order_id'], $recharge['price'], 'user_recharge', '用户充值');
  69. if ($userType === PayServices::ALLIN_PAY) {
  70. $res['pay_type'] = $userType;
  71. }
  72. return $res;
  73. }
  74. }