|
|
@@ -12,6 +12,8 @@ import { createHash } from 'crypto'
|
|
|
import axios from 'axios'
|
|
|
import { InternalServerException } from 'App/Exceptions/Common'
|
|
|
import { Exception } from '@adonisjs/core/build/standalone'
|
|
|
+import RechargeOrder, { RechargeOrderStatus } from 'App/Models/RechargeOrder'
|
|
|
+import { DateTime } from 'luxon'
|
|
|
|
|
|
export default class UserBalancesController {
|
|
|
private paginationService = new PaginationService(UserBalance)
|
|
|
@@ -74,16 +76,22 @@ export default class UserBalancesController {
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
- public async recharge({ request }: HttpContextContract) {
|
|
|
+ public async recharge({ request, auth }: HttpContextContract) {
|
|
|
+ const amount = '100'
|
|
|
+ const order = await RechargeOrder.create({
|
|
|
+ userId: auth.user!.id,
|
|
|
+ amount: new Decimal(amount),
|
|
|
+ status: RechargeOrderStatus.Pending
|
|
|
+ })
|
|
|
const head = {
|
|
|
mchtId: '2000713000197336',
|
|
|
version: '20',
|
|
|
biz: 'qr101'
|
|
|
}
|
|
|
const body = {
|
|
|
- orderId: new Date().getTime() + '',
|
|
|
+ orderId: order.id.toString(),
|
|
|
orderTime: format(new Date(), 'yyyyMMddHHmmss'),
|
|
|
- amount: '100',
|
|
|
+ amount: amount,
|
|
|
currencyType: 'IDR',
|
|
|
goods: 'recharge',
|
|
|
notifyUrl: 'https://kaliartdrama.run.place/api/userBalances/rechargeNotify',
|
|
|
@@ -121,5 +129,21 @@ export default class UserBalancesController {
|
|
|
public async rechargeNotify({ request }: HttpContextContract) {
|
|
|
const data = request.all()
|
|
|
Logger.info('rechargeNotify', data)
|
|
|
+ if (data.head.respCode === '0000') {
|
|
|
+ const order = await RechargeOrder.findOrFail(parseInt(data.body.orderId))
|
|
|
+ if (order.status === RechargeOrderStatus.Pending) {
|
|
|
+ order.status = RechargeOrderStatus.Success
|
|
|
+ order.transactionId = data.body.tradeId
|
|
|
+ order.payTime = DateTime.now()
|
|
|
+ await order.save()
|
|
|
+
|
|
|
+ await UserBalanceService.modifiyBalance({
|
|
|
+ userId: order.userId,
|
|
|
+ amount: order.amount,
|
|
|
+ type: BalanceRecordType.Recharge
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 'SUCCESS'
|
|
|
}
|
|
|
}
|