1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- declare (strict_types=1);
- namespace app\services\pay;
- use app\services\user\UserRechargeServices;
- use app\services\wechat\WechatUserServices;
- use think\exception\ValidateException;
- class RechargeServices
- {
- protected $pay;
-
- public function __construct(PayServices $pay)
- {
- $this->pay = $pay;
- }
-
- public function recharge(int $recharge_id, string $authCode = '')
- {
-
- $rechargeServices = app()->make(UserRechargeServices::class);
- $recharge = $rechargeServices->getRecharge($recharge_id);
- if (!$recharge) {
- throw new ValidateException('订单失效或者不存在');
- }
- if ($recharge['paid'] == 1) {
- throw new ValidateException('订单已支付');
- }
- $openid = '';
-
- if (!$authCode && !in_array($recharge['recharge_type'], ['weixinh5', 'store', 'pc', 'alipay']) && !request()->isApp()) {
- $userType = '';
- switch ($recharge['recharge_type']) {
- case 'weixin':
- case 'weixinh5':
- $userType = 'wechat';
- break;
- case 'routine':
- $userType = 'routine';
- break;
- }
- if (!$userType) {
- throw new ValidateException('不支持该类型方式');
- }
-
- $wechatUser = app()->make(WechatUserServices::class);
- $openid = $wechatUser->uidToOpenid((int)$recharge['uid'], $userType);
- if (!$openid) {
- throw new ValidateException('获取用户openid失败,无法支付');
- }
- }
- return $this->pay->setAuthCode($authCode)->pay($recharge['recharge_type'], $openid, $recharge['order_id'], $recharge['price'], 'user_recharge', '用户充值');
- }
- }
|