RechargeServices.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 app\services\user\UserRechargeServices;
  14. use app\services\wechat\WechatUserServices;
  15. use think\exception\ValidateException;
  16. /**
  17. *
  18. * Class RechargeServices
  19. * @package app\services\pay
  20. */
  21. class RechargeServices
  22. {
  23. protected $pay;
  24. /**
  25. * RechargeServices constructor.
  26. * @param PayServices $pay
  27. */
  28. public function __construct(PayServices $pay)
  29. {
  30. $this->pay = $pay;
  31. }
  32. public function recharge(int $recharge_id)
  33. {
  34. /** @var UserRechargeServices $rechargeServices */
  35. $rechargeServices = app()->make(UserRechargeServices::class);
  36. $recharge = $rechargeServices->getRecharge($recharge_id);
  37. if (!$recharge) {
  38. throw new ValidateException('订单失效或者不存在');
  39. }
  40. if ($recharge['paid'] == 1) {
  41. throw new ValidateException('订单已支付');
  42. }
  43. $userType = '';
  44. switch ($recharge['recharge_type']) {
  45. case 'weixin':
  46. case 'weixinh5':
  47. $userType = 'wechat';
  48. break;
  49. case 'routine':
  50. $userType = 'routine';
  51. break;
  52. }
  53. if (!$userType) {
  54. throw new ValidateException('不支持该类型方式');
  55. }
  56. /** @var WechatUserServices $wechatUser */
  57. $wechatUser = app()->make(WechatUserServices::class);
  58. $openid = $wechatUser->uidToOpenid((int)$recharge['uid'], $userType);
  59. if ($recharge['recharge_type'] != 'weixinh5' && !request()->isApp()) {
  60. if (!$openid) {
  61. throw new ValidateException('获取用户openid失败,无法支付');
  62. }
  63. }else{
  64. $openid = '';
  65. }
  66. return $this->pay->pay($recharge['recharge_type'], $openid, $recharge['order_id'], $recharge['price'], 'user_recharge', '用户充值');
  67. }
  68. }