telebirr configured

This commit is contained in:
Tria
2026-06-05 11:01:01 +03:00
parent 93020b3153
commit 7c124d9845
11 changed files with 328 additions and 54 deletions

View File

@@ -1,41 +1,90 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { DataSource, Repository } from "typeorm";
import { DataSource, QueryRunner } from "typeorm";
import { PaymentEntity } from "./entities/payment.entity";
import { PaymentStrategy } from "./strategies/payment.strategy";
import { PaymentTelebirrStrategy } from "./strategies/payment.telebirr.strategy";
import { PaymentRepository } from "./payment.repository";
import { ClientAction, PaymentPlatform } from "./strategies/payments.types";
import * as crypto from 'crypto';
type PaymentMethod = PaymentEntity["method"]
type CurrencyType = PaymentEntity["currency"]
@Injectable()
export class PaymentService {
private strategies: Map<PaymentMethod, PaymentStrategy>;
private readonly paymentRepo: Repository<PaymentEntity>;
constructor(
private readonly dataSource: DataSource,
private readonly datasource: DataSource,
private readonly paymentRepo: PaymentRepository,
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;
async pay(amount: number, currency: CurrencyType, method: PaymentMethod, cb: (qr: QueryRunner) => Promise<{ id: string, type: PaymentEntity["type"] }>, payform: PaymentPlatform = "web"): Promise<{
refId: string,
clientAction: ClientAction,
status: PaymentEntity["status"],
paidAt?: string,
failureCode?: string,
failureMessage?: string,
}> {
const strategy = this.strategies.get(method)
if (!strategy) {
throw new NotFoundException("strategy not found")
}
const orderId = `freigh${Date.now()}${crypto.randomBytes(4).toString('hex')}` //todo: make it dynamic
const paymentResp = await strategy.pay({
amountMinor: amount,
currency: currency,
merchantOrderId: orderId,
platform: payform,
});
const queryRunner = this.datasource.createQueryRunner()
await queryRunner.connect()
await queryRunner.startTransaction()
console.log(paymentResp.expiresAt)
try {
const resp = await cb(queryRunner)
const payment = await this.paymentRepo.createTr(queryRunner, {
amount,
currency,
method,
refId: resp.id,
type: resp.type,
merchantOrderId: orderId,
rawInitiation: paymentResp.rawInitiation,
clientAction: paymentResp.clientAction,
expiresAt: paymentResp.expiresAt
})
await queryRunner.commitTransaction()
return {
refId: payment.refId,
clientAction: paymentResp.clientAction,
status: payment.status,
paidAt: payment.paidAt?.toISOString(),
failureCode: payment.failerCode ?? undefined,
failureMessage: payment.failureMessage ?? undefined,
}
} catch (err) {
await queryRunner.rollbackTransaction()
throw new Error("payment failed")
} finally {
await queryRunner.release()
}
}
getPaymentByReferenceIdAndMethod(data: Pick<PaymentEntity, "method" | "refId">): Promise<PaymentEntity | null> {
return this.paymentRepo.findOneBy(data)
async getActivePaymentByRefIdAndMethod(refId: string, method: PaymentEntity["method"]): Promise<PaymentEntity | null> {
return this.paymentRepo.getActivePaymentByRefIdAndMethod(refId, method)
}