inapp-purchase for dpage

This commit is contained in:
2026-04-06 12:28:38 +03:30
parent f5cc00ccba
commit 9f0390901f
4 changed files with 50 additions and 11 deletions
+3 -6
View File
@@ -12,21 +12,18 @@ export class ApiKeyGuard implements CanActivate {
const request = context.switchToHttp().getRequest<FastifyRequest>();
const apiKey = request.headers["x-api-key"] as string;
console.log("apiKey", apiKey);
if (!apiKey) throw new UnauthorizedException(AuthMessage.UNAUTHORIZED_ACCESS);
if (!apiKey) throw new UnauthorizedException(AuthMessage.UNAUTHORIZED_ACCESS);
// Get allowed API keys from environment (comma-separated)
const allowedApiKeys = this.configService.getOrThrow<string>("EXTERNAL_API_KEYS");
const validApiKeys = allowedApiKeys.split(",").filter((key) => key.trim().length > 0);
console.log("validApiKeys", validApiKeys);
if (validApiKeys.length === 0) throw new UnauthorizedException(AuthMessage.UNAUTHORIZED_ACCESS);
const isValidApiKey = validApiKeys.includes(apiKey);
console.log("isValidApiKey", isValidApiKey);
if (!isValidApiKey) throw new UnauthorizedException(AuthMessage.UNAUTHORIZED_ACCESS);
console.log("true");
@@ -14,7 +14,7 @@ export class ContactService {
constructor(
@Inject(DMENU_CONFIG) private readonly config: IDmenuConfig,
private readonly httpService: HttpService,
) {}
) { }
private getHeaders(): Record<string, string> {
const credentials = Buffer.from(`${this.config.username}:${this.config.password}`).toString("base64");
@@ -42,7 +42,7 @@ export class ContactService {
}
async findOne(id: number) {
try {
try {
const { data } = await firstValueFrom(
this.httpService.get(`${this.config.baseUrl}/super-admin/contact/${id}`, { headers: this.getHeaders() }).pipe(
catchError((err: AxiosError) => {
+32
View File
@@ -192,6 +192,38 @@ export class DPageService {
throw error;
}
}
async finishPurchaseCatalogue(businessId: string, count: number) {
try {
const { data } = await firstValueFrom(
this.httpService
.post(`${this.config.baseUrl}/super-admin/catalogue/purchase/finish`, {
businessId,
count
}, {
headers: this.getHeaders(),
})
.pipe(
catchError((err: AxiosError) => {
this.logger.error(`Failed to perform finish purchase catalogue: ${err.message}`, err.stack);
return throwError(() => err);
}),
),
);
return data;
} catch (error: unknown) {
if (error instanceof AxiosError && error.response) {
this.logger.error(`External API error response:`, error.response.data);
const errorResponse = {
...error.response.data,
message: error.response.data.error?.message || error.message,
};
throw new HttpException(errorResponse, error.response.status);
}
this.logger.error(`Error performing to finish purchase catalogue: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
}
@@ -778,7 +778,7 @@ export class InvoicesService {
queryRunner = this.dataSource.createQueryRunner(),
) {
let transactionStarted = false;
try {
try {
if (!queryRunner.isTransactionActive) {
await queryRunner.connect();
await queryRunner.startTransaction();
@@ -806,7 +806,7 @@ export class InvoicesService {
const purpose = invoice.purpose
if (purpose == InvoicePurpose.UPGRADE) {
// This is an upgrade - update subscription with the new plan
const upgradePlan = invoiceItem.plan!;
@@ -903,7 +903,7 @@ export class InvoicesService {
if (userSubscription.plan.service.slug?.toLowerCase().includes('dpage')) {
try {
this.logger.log(`Calling external API for dpage setup: subscription ${userSubscription.id}`);
console.log('userSubscription.plan.count',userSubscription.plan.count)
console.log('userSubscription.plan.count', userSubscription.plan.count)
await this.dpageService.createDpageAccount({
danakSubscriptionId: userSubscription.id,
name: userSubscription.businessName,
@@ -946,6 +946,16 @@ export class InvoicesService {
await this.walletsService.createSupportPlanTransaction(invoice.totalPrice, userWallet.id, queryRunner);
await this.addNotifyForWalletDeduction(invoice, user, userWallet, WalletMessage.SUPPORT_PLAN_WALLET_TRANSFER);
//
} else if (invoice.isExternal) {
if (invoice.items[0]?.name == 'purchase_new_catalogue') {
// call dpage api
const businessId = invoice.externalBusinessId
if (!businessId) {
throw new BadRequestException("business id not found")
}
const count = invoice.items[0].count
this.dpageService.finishPurchaseCatalogue(businessId, count)
}
} else {
await this.walletsService.createInvoiceTransaction(invoice.totalPrice, userWallet.id, queryRunner);
await this.addNotifyForWalletDeduction(invoice, user, userWallet, WalletMessage.INVOICE_WALLET_TRANSFER);