zarinpal payment
This commit is contained in:
@@ -61,12 +61,17 @@ export class Order extends BaseEntity {
|
|||||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||||
paidAmount!: number;
|
paidAmount!: number;
|
||||||
|
|
||||||
// balance!: number;
|
|
||||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
|
||||||
get balance():number{
|
|
||||||
return this.total- this.paidAmount
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// private _balance!: number;
|
||||||
|
|
||||||
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||||
|
balance!: number;
|
||||||
|
// get balance(): number {
|
||||||
|
// return this._balance
|
||||||
|
// }
|
||||||
|
// set balance(value) {
|
||||||
|
// this._balance = this.total - this.paidAmount
|
||||||
|
// }
|
||||||
// @Property({ type: 'text', nullable: true })
|
// @Property({ type: 'text', nullable: true })
|
||||||
// description?: string;
|
// description?: string;
|
||||||
|
|
||||||
|
|||||||
@@ -150,6 +150,7 @@ export class OrderService {
|
|||||||
targetOrder.discount = discount
|
targetOrder.discount = discount
|
||||||
|
|
||||||
targetOrder.total = subTotal - discount
|
targetOrder.total = subTotal - discount
|
||||||
|
targetOrder.balance = subTotal - discount
|
||||||
|
|
||||||
// change status
|
// change status
|
||||||
targetOrder.status = OrderStatusEnum.INVOICED
|
targetOrder.status = OrderStatusEnum.INVOICED
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import {
|
|||||||
Controller, Get, Post, Body,
|
Controller, Get, Post, Body,
|
||||||
Param,
|
Param,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
|
Query,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { PaymentService } from '../services/payments.service';
|
import { PaymentService } from '../services/payments.service';
|
||||||
import {
|
import {
|
||||||
@@ -41,7 +42,7 @@ export class PaymentController {
|
|||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@Get('public/payments')
|
@Get('public/payments')
|
||||||
@ApiOperation({ summary: 'get all payments as admin' })
|
@ApiOperation({ summary: 'get all payments as admin' })
|
||||||
findAllByUser(@UserId() userId: string, @Body() dto: FindPaymentsDto) {
|
findAllByUser(@UserId() userId: string, @Query() dto: FindPaymentsDto) {
|
||||||
return this.paymentsService.findAllByUser(dto, userId);
|
return this.paymentsService.findAllByUser(dto, userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ export class PaymentService {
|
|||||||
gateway: null,
|
gateway: null,
|
||||||
order,
|
order,
|
||||||
status: PaymentStatusEnum.Paid,
|
status: PaymentStatusEnum.Paid,
|
||||||
paidAt: new Date()
|
paidAt: new Date(),
|
||||||
});
|
});
|
||||||
// 3. Create Credit transaction record
|
// 3. Create Credit transaction record
|
||||||
const newCreditTransaction = this.creditRepository.create({
|
const newCreditTransaction = this.creditRepository.create({
|
||||||
@@ -130,7 +130,7 @@ export class PaymentService {
|
|||||||
amount,
|
amount,
|
||||||
type: CreditTransactionType.WITHDRAW,
|
type: CreditTransactionType.WITHDRAW,
|
||||||
balance: newBalance,
|
balance: newBalance,
|
||||||
orderId: order.id
|
orderId: order.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
em.persist([payment, newCreditTransaction]);
|
em.persist([payment, newCreditTransaction]);
|
||||||
@@ -148,21 +148,19 @@ export class PaymentService {
|
|||||||
|
|
||||||
const gateway = this.gatewayManager.get(requestedGateway!);
|
const gateway = this.gatewayManager.get(requestedGateway!);
|
||||||
|
|
||||||
const payment = this.paymentRepository.create({
|
|
||||||
order,
|
|
||||||
amount,
|
|
||||||
method: PaymentMethodEnum.Online,
|
|
||||||
status: PaymentStatusEnum.Pending
|
|
||||||
})
|
|
||||||
|
|
||||||
const { token, paymentUrl } = await gateway.requestPayment({
|
const { token, paymentUrl } = await gateway.requestPayment({
|
||||||
amount: ctx.amount,
|
amount: ctx.amount,
|
||||||
orderId: ctx.order.id,
|
orderId: ctx.order.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
payment.gateway = requestedGateway;
|
const payment = this.paymentRepository.create({
|
||||||
payment.token = token;
|
order,
|
||||||
payment.status = PaymentStatusEnum.Pending;
|
amount,
|
||||||
|
method: PaymentMethodEnum.Online,
|
||||||
|
status: PaymentStatusEnum.Pending,
|
||||||
|
gateway: requestedGateway,
|
||||||
|
token,
|
||||||
|
})
|
||||||
|
|
||||||
await this.em.persistAndFlush(payment);
|
await this.em.persistAndFlush(payment);
|
||||||
|
|
||||||
@@ -211,6 +209,7 @@ export class PaymentService {
|
|||||||
// TODO : use decimal js
|
// TODO : use decimal js
|
||||||
// does need persist or not
|
// does need persist or not
|
||||||
payment.order.paidAmount += payment.amount
|
payment.order.paidAmount += payment.amount
|
||||||
|
payment.order.balance -= payment.amount
|
||||||
|
|
||||||
await em.flush();
|
await em.flush();
|
||||||
return payment;
|
return payment;
|
||||||
@@ -248,6 +247,7 @@ export class PaymentService {
|
|||||||
|
|
||||||
// TODO : use decimal js
|
// TODO : use decimal js
|
||||||
payment.order.paidAmount += payment.amount
|
payment.order.paidAmount += payment.amount
|
||||||
|
payment.order.balance -= payment.amount
|
||||||
|
|
||||||
em.persist(payment);
|
em.persist(payment);
|
||||||
em.persist(payment.order);
|
em.persist(payment.order);
|
||||||
|
|||||||
Reference in New Issue
Block a user