|
|
@@ -0,0 +1,101 @@
|
|
|
+import { Injectable } from '@nestjs/common'
|
|
|
+import { InjectRepository } from '@nestjs/typeorm'
|
|
|
+import { PayOrder, PayOrderStatus } from './entities/pay-order.entiy'
|
|
|
+import { Repository } from 'typeorm'
|
|
|
+import { Users } from '../users/entities/users.entity'
|
|
|
+import Decimal from 'decimal.js'
|
|
|
+import axios from 'axios'
|
|
|
+import { SysConfigService } from '../sys-config/sys-config.service'
|
|
|
+import { BalanceService } from '../balance/balance.service'
|
|
|
+
|
|
|
+const address = 'THjDQns8wZs9PZT25o8LUPXvd7GPsSv7Cr'
|
|
|
+
|
|
|
+const axiosInstance = axios.create({
|
|
|
+ baseURL: 'https://apilist.tronscanapi.com/'
|
|
|
+})
|
|
|
+
|
|
|
+@Injectable()
|
|
|
+export class PayOrderService {
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ @InjectRepository(PayOrder)
|
|
|
+ private payOrderRepository: Repository<PayOrder>,
|
|
|
+ @InjectRepository(Users)
|
|
|
+ private userRepository: Repository<Users>,
|
|
|
+ private readonly sysConfigService: SysConfigService,
|
|
|
+ private readonly balanceService: BalanceService
|
|
|
+ ) {
|
|
|
+ }
|
|
|
+
|
|
|
+ public async create(order: PayOrder) {
|
|
|
+ const cid = require('@4a/cid')
|
|
|
+ order.orderId = cid()
|
|
|
+ order.address = address
|
|
|
+ // 支付金额计算
|
|
|
+ const amount = new Decimal(order.amount)
|
|
|
+ const randomDigit = Math.floor(Math.random() * 10)
|
|
|
+ const position = Math.floor(Math.random() * 3)
|
|
|
+ const randomDecimal = `0.${'0'.repeat(position)}${randomDigit}${'0'.repeat(2 - position)}`
|
|
|
+ order.rechargeAmount = amount.plus(new Decimal(randomDecimal)).toNumber()
|
|
|
+
|
|
|
+ if (order.amount===1050){
|
|
|
+ order.rechargeAmount = 1050.8
|
|
|
+ }
|
|
|
+
|
|
|
+ return await this.payOrderRepository.save(order)
|
|
|
+ }
|
|
|
+
|
|
|
+ public async rechargeConfirm(id: number) {
|
|
|
+ const payOrders = await this.payOrderRepository.findOneBy({ id })
|
|
|
+ if (!payOrders) {
|
|
|
+ throw new Error('支付订单不存在!')
|
|
|
+ }
|
|
|
+ if (payOrders.status !== PayOrderStatus.PENDING) {
|
|
|
+ throw new Error('支付订单状态不正确!')
|
|
|
+ }
|
|
|
+
|
|
|
+ const rechargeAmount = new Decimal(payOrders.rechargeAmount).mul(1000000)
|
|
|
+
|
|
|
+ const startOfDay = new Date()
|
|
|
+ startOfDay.setHours(0, 0, 0, 0)
|
|
|
+ const endOfDay = new Date()
|
|
|
+ endOfDay.setHours(23, 59, 59, 999)
|
|
|
+ const startOfDayMs = startOfDay.getTime()
|
|
|
+ const endOfDayMs = endOfDay.getTime()
|
|
|
+
|
|
|
+ const res = await axiosInstance.get('api/token_trc20/transfers', {
|
|
|
+ params: {
|
|
|
+ limit: 50,
|
|
|
+ start: 0,
|
|
|
+ start_timestamp: 1727222400000,
|
|
|
+ end_timestamp: 1727481599999,
|
|
|
+ confirm: true,
|
|
|
+ filterTokenValue: 1,
|
|
|
+ relatedAddress: address,
|
|
|
+ toAddress: address
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ const result = res.data
|
|
|
+ const transfers: any[] = result.token_transfers
|
|
|
+ if (transfers.length > 0) {
|
|
|
+ for (let transfer of transfers) {
|
|
|
+ if (rechargeAmount.eq(new Decimal(transfer.quant))) {
|
|
|
+ payOrders.hash = transfer.transaction_id
|
|
|
+ payOrders.status = PayOrderStatus.COMPLETED
|
|
|
+
|
|
|
+ const rechargeRatio = await this.sysConfigService.getString('recharge_ratio', '')
|
|
|
+ const ratio = new Decimal(rechargeRatio)
|
|
|
+ const balanceDecimal = new Decimal(payOrders.amount).mul(ratio)
|
|
|
+
|
|
|
+ await this.balanceService.rechargeBalance(parseInt(payOrders.userId), parseInt(payOrders.userId), balanceDecimal.toNumber())
|
|
|
+ await this.payOrderRepository.save(payOrders)
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return payOrders
|
|
|
+ }
|
|
|
+
|
|
|
+}
|