RechargeServices.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if (!$userType) {
  55. throw new ApiException(410278);
  56. }
  57. /** @var WechatUserServices $wechatUser */
  58. $wechatUser = app()->make(WechatUserServices::class);
  59. $openid = $wechatUser->uidToOpenid((int)$recharge['uid'], $userType);
  60. if (in_array($recharge['recharge_type'], ['weixin', 'routine']) && !request()->isApp()) {
  61. if (!$openid) {
  62. throw new ApiException(410275);
  63. }
  64. } else {
  65. $openid = '';
  66. }
  67. return $this->pay->pay($recharge['recharge_type'], $openid, $recharge['order_id'], $recharge['price'], 'user_recharge', '用户充值');
  68. }
  69. }