feat: add country selection during onboarding and implement payment management features
- Introduced CountryStep component for selecting the country during onboarding, affecting payment currency. - Updated FirstGroupStep to include country information. - Enhanced PaymentsProvider to manage payment states and renewals. - Created PaymentRow component for displaying individual payment details. - Implemented PaymentsView for listing and filtering payments by group and status. - Added API endpoints for fetching and creating payments, including batch processing. - Defined payment schemas for validation and data handling. - Updated GroupHeader and GroupDetailView to include charging functionality for group members.
This commit is contained in:
87
apps/backend/src/modules/payments/jobs/reconcile-payments.ts
Normal file
87
apps/backend/src/modules/payments/jobs/reconcile-payments.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import type { PrismaClient } from '@generated/prisma/client';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { logger } from '@/logger';
|
||||
import { getMpAccessToken } from '@/modules/mp';
|
||||
import { MpApiError, searchMpPaymentByExternalReference } from '@/modules/mp/lib';
|
||||
|
||||
/**
|
||||
* Pulls payments from Mercado Pago instead of trusting a webhook. A payment
|
||||
* link is just a preference until someone pays it; this job asks the seller's
|
||||
* account for our `external_reference` and marks approved payments as PAID.
|
||||
*
|
||||
* This is the stopgap and the source of truth: if a webhook is ever added it
|
||||
* gives you the confirmation, but this job is what keeps the Cobros screen
|
||||
* correct even when a notification is lost.
|
||||
*/
|
||||
const RECONCILE_AFTER_MS = 5 * 60 * 1000;
|
||||
const BATCH_LIMIT = 50;
|
||||
|
||||
export type ReconcilePaymentsResult = {
|
||||
paid: number;
|
||||
skipped: number;
|
||||
failed: number;
|
||||
};
|
||||
|
||||
type ReconcilePaymentsDeps = {
|
||||
db?: PrismaClient;
|
||||
getToken?: typeof getMpAccessToken;
|
||||
search?: typeof searchMpPaymentByExternalReference;
|
||||
now?: Date;
|
||||
};
|
||||
|
||||
export class ReconcilePayments {
|
||||
constructor(private readonly deps: ReconcilePaymentsDeps = {}) {}
|
||||
|
||||
async execute(): Promise<ReconcilePaymentsResult> {
|
||||
const db = this.deps.db ?? prisma;
|
||||
const now = this.deps.now ?? new Date();
|
||||
const getToken = this.deps.getToken ?? getMpAccessToken;
|
||||
const search = this.deps.search ?? searchMpPaymentByExternalReference;
|
||||
|
||||
const awaiting = await db.payment.findMany({
|
||||
where: {
|
||||
status: 'PENDING',
|
||||
checkoutUrl: { not: null },
|
||||
createdAt: { lt: new Date(now.getTime() - RECONCILE_AFTER_MS) },
|
||||
},
|
||||
include: { group: { select: { createdById: true } } },
|
||||
take: BATCH_LIMIT,
|
||||
});
|
||||
|
||||
const totals: ReconcilePaymentsResult = { paid: 0, skipped: 0, failed: 0 };
|
||||
|
||||
for (const payment of awaiting) {
|
||||
const token = await getToken(payment.group.createdById);
|
||||
if (!token.ok) {
|
||||
// No usable token (not linked, or needs re-auth). Not a payment problem:
|
||||
// the row stays PENDING and the account notice tells the professor what to do.
|
||||
totals.skipped += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const found = await search(token.value.accessToken, payment.id);
|
||||
if (found && found.status === 'approved' && found.externalReference === payment.id) {
|
||||
await db.payment.update({
|
||||
where: { id: payment.id },
|
||||
data: {
|
||||
status: 'PAID',
|
||||
mpPaymentId: found.id,
|
||||
paidAt: found.dateApproved ? new Date(found.dateApproved) : new Date(),
|
||||
},
|
||||
});
|
||||
totals.paid += 1;
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof MpApiError) {
|
||||
totals.failed += 1;
|
||||
} else {
|
||||
logger.error({ err: error }, 'Unexpected payment reconciliation failure');
|
||||
totals.failed += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return totals;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user