RechargeServices.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /**
  33. * @param int $recharge_id
  34. * @param string $authCode
  35. * @return array|string
  36. */
  37. public function recharge(int $recharge_id, string $authCode = '')
  38. {
  39. /** @var UserRechargeServices $rechargeServices */
  40. $rechargeServices = app()->make(UserRechargeServices::class);
  41. $recharge = $rechargeServices->getRecharge($recharge_id);
  42. if (!$recharge) {
  43. throw new ValidateException('订单失效或者不存在');
  44. }
  45. if ($recharge['paid'] == 1) {
  46. throw new ValidateException('订单已支付');
  47. }
  48. $openid = '';
  49. //没有付款码,不是微信H5支付,门店支付,PC支付,不再APP端,需要判断用户openid
  50. if (!$authCode && !in_array($recharge['recharge_type'], ['weixinh5', 'store', 'pc', 'alipay']) && !request()->isApp()) {
  51. $userType = '';
  52. switch ($recharge['recharge_type']) {
  53. case 'weixin':
  54. case 'weixinh5':
  55. $userType = 'wechat';
  56. break;
  57. case 'routine':
  58. $userType = 'routine';
  59. break;
  60. }
  61. if (!$userType) {
  62. throw new ValidateException('不支持该类型方式');
  63. }
  64. /** @var WechatUserServices $wechatUser */
  65. $wechatUser = app()->make(WechatUserServices::class);
  66. $openid = $wechatUser->uidToOpenid((int)$recharge['uid'], $userType);
  67. if (!$openid) {
  68. throw new ValidateException('获取用户openid失败,无法支付');
  69. }
  70. }
  71. return $this->pay->setAuthCode($authCode)->pay($recharge['recharge_type'], $openid, $recharge['order_id'], $recharge['price'], 'user_recharge', '用户充值');
  72. }
  73. }