pay as admin
This commit is contained in:
@@ -16,6 +16,8 @@ import { CreditService } from 'src/modules/user/providers/credit.service';
|
||||
import { CreditTransactionType } from 'src/modules/user/interface/credit';
|
||||
import { Payment } from '../entities/payment.entity';
|
||||
import { FindPaymentsDto } from '../dto/find-payments.dto';
|
||||
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
|
||||
|
||||
|
||||
|
||||
@@ -31,11 +33,12 @@ export class PaymentService {
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
private readonly creditService: CreditService,
|
||||
private readonly creditRepository: CreditRepository,
|
||||
private readonly adminRepository: AdminRepository,
|
||||
) { }
|
||||
|
||||
async payOrder(orderId: string, dto: PayOrderDto): Promise<{ paymentUrl: string | null }> {
|
||||
async payOrder(orderId: string, dto: PayOrderDto, adminId?: string): Promise<{ paymentUrl: string | null }> {
|
||||
|
||||
const ctx = await this.loadAndValidateOrder(orderId, dto)
|
||||
const ctx = await this.loadAndValidateOrder(orderId, dto, adminId)
|
||||
|
||||
switch (dto.method) {
|
||||
case PaymentMethodEnum.Cash:
|
||||
@@ -55,7 +58,7 @@ export class PaymentService {
|
||||
}
|
||||
|
||||
|
||||
private async loadAndValidateOrder(orderId: string, dto: PayOrderDto): Promise<OrderPaymentContext> {
|
||||
private async loadAndValidateOrder(orderId: string, dto: PayOrderDto, adminId?: string): Promise<OrderPaymentContext> {
|
||||
const { amount, method, attachments, description, gateway } = dto
|
||||
|
||||
const order = await this.orderService.findOneOrFail(orderId)
|
||||
@@ -64,8 +67,12 @@ export class PaymentService {
|
||||
throw new NotFoundException(OrderMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
if (order.balance == 0) {
|
||||
throw new BadRequestException("you can not pay beause Balance is zero")
|
||||
let admin: null | Admin = null
|
||||
if (adminId) {
|
||||
admin = await this.adminRepository.findOne({ id: adminId })
|
||||
if (!admin) {
|
||||
throw new BadRequestException("Admin not found")
|
||||
}
|
||||
}
|
||||
|
||||
if (amount <= 0) {
|
||||
@@ -77,6 +84,7 @@ export class PaymentService {
|
||||
}
|
||||
|
||||
return {
|
||||
admin,
|
||||
user: order.user,
|
||||
order,
|
||||
amount,
|
||||
@@ -88,8 +96,9 @@ export class PaymentService {
|
||||
}
|
||||
|
||||
private async handleCashPayment(ctx: OrderPaymentContext): Promise<void> {
|
||||
const { order, amount, method, attachments, description } = ctx
|
||||
const { order, amount, method, attachments, description, admin } = ctx
|
||||
const payment = this.paymentRepository.create({
|
||||
creator: admin,
|
||||
order,
|
||||
amount,
|
||||
method,
|
||||
@@ -103,7 +112,7 @@ export class PaymentService {
|
||||
|
||||
|
||||
private async handleCreditPayment(ctx: OrderPaymentContext): Promise<void> {
|
||||
const { order, amount, user } = ctx
|
||||
const { order, amount, user, admin } = ctx
|
||||
|
||||
await this.em.transactional(async em => {
|
||||
// 1. get User remained Credit
|
||||
@@ -116,6 +125,7 @@ export class PaymentService {
|
||||
const newBalance = remainedCredit - amount;
|
||||
// 2. Create payment record
|
||||
const payment = this.paymentRepository.create({
|
||||
creator: admin,
|
||||
amount: ctx.amount,
|
||||
method: PaymentMethodEnum.Credit,
|
||||
gateway: null,
|
||||
@@ -143,7 +153,7 @@ export class PaymentService {
|
||||
}
|
||||
|
||||
private async handleOnlinePayment(ctx: OrderPaymentContext): Promise<{ paymentUrl: string }> {
|
||||
const { amount, order, gateway: requestedGateway } = ctx
|
||||
const { amount, order, gateway: requestedGateway, admin } = ctx
|
||||
|
||||
const gateway = this.gatewayManager.get(requestedGateway!);
|
||||
|
||||
@@ -153,6 +163,7 @@ export class PaymentService {
|
||||
});
|
||||
|
||||
const payment = this.paymentRepository.create({
|
||||
creator: admin,
|
||||
order,
|
||||
amount,
|
||||
method: PaymentMethodEnum.Online,
|
||||
|
||||
Reference in New Issue
Block a user