初始化:职引项目 v1.0
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { Controller, Post, Body, UseGuards, HttpException, HttpStatus } from '@nestjs/common'
|
||||
import { InjectModel } from '@nestjs/mongoose'
|
||||
import { Model } from 'mongoose'
|
||||
import { User, UserDocument } from '../user/user.schema'
|
||||
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'
|
||||
import { CurrentUser } from '../../common/decorators/current-user.decorator'
|
||||
import { WechatPayService } from './wechat-pay.service'
|
||||
import { Public } from '../../common/decorators/public.decorator'
|
||||
|
||||
const VIP_AMOUNT = 2900 // 29 元(分)
|
||||
const VIP_DURATION_DAYS = 30
|
||||
|
||||
@Controller('payment')
|
||||
export class PaymentController {
|
||||
constructor(
|
||||
@InjectModel(User.name) private userModel: Model<UserDocument>,
|
||||
private wechatPay: WechatPayService,
|
||||
) {}
|
||||
|
||||
/** 创建订单(H5:Native 扫码支付) */
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('create')
|
||||
async create(@CurrentUser('userId') userId: string) {
|
||||
const user = await this.userModel.findById(userId).exec()
|
||||
if (!user) throw new HttpException('用户不存在', HttpStatus.NOT_FOUND)
|
||||
if (user.plan === 'vip') throw new HttpException('已是会员', HttpStatus.BAD_REQUEST)
|
||||
|
||||
const outTradeNo = `VIP${Date.now()}${userId.slice(-6)}`
|
||||
const result = await this.wechatPay.nativePay(
|
||||
'职引月度会员',
|
||||
outTradeNo,
|
||||
VIP_AMOUNT,
|
||||
)
|
||||
return {
|
||||
outTradeNo: result.outTradeNo,
|
||||
codeUrl: result.codeUrl, // 二维码链接
|
||||
amount: VIP_AMOUNT,
|
||||
title: '职引月度会员',
|
||||
}
|
||||
}
|
||||
|
||||
/** JSAPI 支付(微信小程序/公众号内使用) */
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('jsapi')
|
||||
async jsapi(@CurrentUser('userId') userId: string, @Body('openid') openid: string) {
|
||||
const user = await this.userModel.findById(userId).exec()
|
||||
if (!user) throw new HttpException('用户不存在', HttpStatus.NOT_FOUND)
|
||||
if (!openid) throw new HttpException('缺少 openid', HttpStatus.BAD_REQUEST)
|
||||
if (user.plan === 'vip') throw new HttpException('已是会员', HttpStatus.BAD_REQUEST)
|
||||
|
||||
const outTradeNo = `VIP${Date.now()}${userId.slice(-6)}`
|
||||
const result = await this.wechatPay.jsapiPay('职引月度会员', outTradeNo, VIP_AMOUNT, openid)
|
||||
return result
|
||||
}
|
||||
|
||||
/** 支付回调通知 */
|
||||
@Public()
|
||||
@Post('notify')
|
||||
async notify(@Body() body: any, @Body('headers') headers: any) {
|
||||
// 实际运行时从 request 读取 header
|
||||
try {
|
||||
const decrypted = this.wechatPay.verifyAndDecrypt(body, '', '', '')
|
||||
if (!decrypted) return { code: 'FAIL', message: '验签失败' }
|
||||
// 处理成功支付
|
||||
const outTradeNo = decrypted.out_trade_no
|
||||
const userId = outTradeNo.slice(-6) // 从订单号取 userId
|
||||
const user = await this.userModel.findOne({ _id: { $regex: userId + '$' } }).exec()
|
||||
if (user && user.plan !== 'vip') {
|
||||
const expireAt = new Date()
|
||||
expireAt.setDate(expireAt.getDate() + VIP_DURATION_DAYS)
|
||||
user.plan = 'vip'
|
||||
user.vipExpireAt = expireAt
|
||||
user.remaining = 999
|
||||
await user.save()
|
||||
}
|
||||
return { code: 'SUCCESS', message: '成功' }
|
||||
} catch {
|
||||
return { code: 'FAIL', message: '处理失败' }
|
||||
}
|
||||
}
|
||||
|
||||
/** 查询订单 */
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('query')
|
||||
async query(@Body('outTradeNo') outTradeNo: string) {
|
||||
return this.wechatPay.queryOrder(outTradeNo)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user