6
0
Fork 0
jiqu-library-server/app/Services/VipService.php

57 lines
1.7 KiB
PHP

<?php
namespace App\Services;
use App\Models\{User, Vip, UserVip};
use App\Exceptions\BizException;
use App\Enums\{PayWay, SocialiteType};
use App\Services\Payment\WxpayService;
class VipService
{
public function buy(User $user, Vip $vip)
{
if (!$vip->status) {
throw new BizException('该会员卡已关闭');
}
$user_vip = $user->vips()->create([
'vip_id' => $vip->id,
'name' => $vip->name,
'price' => $vip->price,
'times' => $vip->times,
'gift' => $vip->gift,
]);
return $user_vip;
}
public function pay(UserVip $userVip, $pay_way)
{
if ($userVip->status == 2) {
throw new BizException('支付处理中,请勿重复操作');
}
$userVip->update([
'status' => 2
]);
$pay_log = $user_vip->pay()->create([
'pay_sn' => serial_number(),
'pay_way' => $pay_way
]);
// 微信小程序支付
if ($pay_log->pay_way === PayWay::WxpayMiniProgram->value) {
$openid = $user->socialites()->where('socialite_type', SocialiteType::WechatMiniProgram)->value('socialite_id');
if (!$openid) {
throw new BizException('未找到用户的微信授权信息, 无法使用微信支付');
}
$params = [
'body' => app_settings('app.app_name').'-会员-' . $userVip->name,
'out_trade_no' => $pay_log->pay_sn,
'total_fee' => $userVip->price,
'trade_type' => 'JSAPI',
'openid' => $openid,
];
}
throw new BizException('未知的支付方式');
}
}