mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 03:40:56 +00:00
working on payment
This commit is contained in:
42
apps/edr-freight-api/src/modules/payment/payment.service.ts
Normal file
42
apps/edr-freight-api/src/modules/payment/payment.service.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { DataSource, Repository } from "typeorm";
|
||||
import { PaymentEntity } from "./entities/payment.entity";
|
||||
import { PaymentStrategy } from "./strategies/payment.strategy";
|
||||
import { PaymentTelebirrStrategy } from "./strategies/payment.telebirr.strategy";
|
||||
|
||||
type PaymentMethod = PaymentEntity["method"]
|
||||
@Injectable()
|
||||
export class PaymentService {
|
||||
private strategies: Map<PaymentMethod, PaymentStrategy>;
|
||||
private readonly paymentRepo: Repository<PaymentEntity>;
|
||||
|
||||
constructor(
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly telebirrPaymentStategy: PaymentTelebirrStrategy) {
|
||||
this.strategies = new Map([
|
||||
["telebirr", this.telebirrPaymentStategy as PaymentStrategy]
|
||||
])
|
||||
this.paymentRepo = this.dataSource.getRepository(PaymentEntity)
|
||||
}
|
||||
|
||||
async create(data: Pick<PaymentEntity, "amount" | "method" | "currency" | "type" | "tableId">): Promise<any> {
|
||||
const strategy = this.strategies.get(data.method)
|
||||
if (!strategy) throw new NotFoundException("strategy not found")
|
||||
return strategy.pay(data.amount, data.currency, "")
|
||||
// const payment = this.paymentRepo.create({
|
||||
// amount: data.amount,
|
||||
// method: data.method,
|
||||
// type: data.type,
|
||||
// currency: data.currency,
|
||||
// tableId: data.tableId,
|
||||
// })
|
||||
// await this.paymentRepo.save(payment)
|
||||
// return link;
|
||||
}
|
||||
|
||||
getPaymentByReferenceIdAndMethod(data: Pick<PaymentEntity, "method" | "refId">): Promise<PaymentEntity | null> {
|
||||
return this.paymentRepo.findOneBy(data)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user