feat(mp): add Mercado Pago reauthorization notice and status card components
- Implemented MpReauthNotice component to notify users when reauthorization is needed. - Created MpStatusCard component to display the current status of the Mercado Pago account. - Added utility functions and constants for handling Mercado Pago status and messages. - Introduced hooks for managing the connection and disconnection of Mercado Pago accounts. - Developed a disconnect confirmation modal for user actions. - Added tests for the new functionality and utility functions. - Defined schemas for Mercado Pago account status and callback reasons in shared package.
This commit is contained in:
@@ -12,10 +12,6 @@ export const weekdaySchema = z.enum([
|
||||
]);
|
||||
export type WeekDay = z.output<typeof weekdaySchema>;
|
||||
|
||||
// Supported payment processors.
|
||||
export const paymentProviderSchema = z.enum(['MERCADO_PAGO', 'STRIPE']);
|
||||
export type PaymentProvider = z.output<typeof paymentProviderSchema>;
|
||||
|
||||
// Billing frequency of a group.
|
||||
export const billingTypeSchema = z.enum(['MONTHLY', 'PER_CLASS']);
|
||||
export type BillingType = z.output<typeof billingTypeSchema>;
|
||||
66
packages/shared/src/schemas/mp.ts
Normal file
66
packages/shared/src/schemas/mp.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
// State of the teacher's Mercado Pago account, as the app sees it.
|
||||
// A `revoked` row (the teacher unlinked) is reported as 'none': there is
|
||||
// nothing usable left, but the row is kept so the Mercado Pago user stays
|
||||
// reserved for whoever linked it.
|
||||
export const mpAccountStatusSchema = z.enum(['none', 'active', 'requires_reauth']);
|
||||
export type MpAccountStatus = z.output<typeof mpAccountStatusSchema>;
|
||||
|
||||
// Why the OAuth callback failed. These codes are safe to hand to the browser:
|
||||
// they never describe an internal failure, only a reason the teacher can act on.
|
||||
export const mpCallbackReasonSchema = z.enum([
|
||||
// 'state' missing, unknown, expired or already used.
|
||||
'invalid_state',
|
||||
// The teacher rejected the consent screen on Mercado Pago.
|
||||
'mp_denied',
|
||||
// Mercado Pago refused to exchange the code, or was unreachable.
|
||||
'mp_error',
|
||||
// That Mercado Pago account is already linked to another teacher.
|
||||
'mp_account_in_use',
|
||||
]);
|
||||
export type MpCallbackReason = z.output<typeof mpCallbackReasonSchema>;
|
||||
|
||||
// Where the teacher started the flow, so the callback can send them back.
|
||||
export const mpReturnToSchema = z.enum(['onboarding', 'settings']);
|
||||
export type MpReturnTo = z.output<typeof mpReturnToSchema>;
|
||||
|
||||
export const MpConnectQuerySchema = z.strictObject({
|
||||
returnTo: mpReturnToSchema.default('settings'),
|
||||
});
|
||||
export type MpConnectQuery = z.output<typeof MpConnectQuerySchema>;
|
||||
|
||||
// Deliberately not `strictObject`: this query is written by Mercado Pago, not by
|
||||
// us. Alongside `error` it may send `error_description` and other fields we do
|
||||
// not model, and a strict schema would answer those with a 400 problem body in
|
||||
// the teacher's browser instead of the redirect this route exists to perform.
|
||||
// Unknown keys are stripped and ignored.
|
||||
export const MpCallbackQuerySchema = z.object({
|
||||
// Mercado Pago may answer with `error` instead of `code` when the teacher
|
||||
// rejects the consent screen, and omits `state` in some failure paths.
|
||||
code: z.string().min(1).optional(),
|
||||
state: z.string().min(1).optional(),
|
||||
error: z.string().min(1).optional(),
|
||||
});
|
||||
export type MpCallbackQuery = z.output<typeof MpCallbackQuerySchema>;
|
||||
|
||||
export const MpAuthorizationUrlDtoSchema = z.object({
|
||||
authorizationUrl: z.string(),
|
||||
});
|
||||
export type MpAuthorizationUrlDto = z.output<typeof MpAuthorizationUrlDtoSchema>;
|
||||
|
||||
export const MpStatusDtoSchema = z.object({
|
||||
status: mpAccountStatusSchema,
|
||||
liveMode: z.boolean(),
|
||||
expiresAt: z.string().nullable(),
|
||||
connectedAt: z.string().nullable(),
|
||||
// Last digits of the Mercado Pago id, so a teacher can tell whether they
|
||||
// linked the account they meant to. Never the full id.
|
||||
mpUserIdSuffix: z.string().nullable(),
|
||||
});
|
||||
export type MpStatusDto = z.output<typeof MpStatusDtoSchema>;
|
||||
|
||||
export const MpDisconnectResultDtoSchema = z.object({
|
||||
disconnected: z.boolean(),
|
||||
});
|
||||
export type MpDisconnectResultDto = z.output<typeof MpDisconnectResultDtoSchema>;
|
||||
@@ -1,27 +1,12 @@
|
||||
import { z } from 'zod';
|
||||
import { billingTypeSchema, paymentProviderSchema, weekdaySchema } from './enums.js';
|
||||
import { billingTypeSchema, weekdaySchema } from './enums.js';
|
||||
import { GroupDtoSchema } from './groups.js';
|
||||
|
||||
export type { BillingType, PaymentProvider, WeekDay } from './enums.js';
|
||||
export { billingTypeSchema, paymentProviderSchema, weekdaySchema } from './enums.js';
|
||||
export type { BillingType, WeekDay } from './enums.js';
|
||||
export { billingTypeSchema, weekdaySchema } from './enums.js';
|
||||
|
||||
// Step 2: link the teacher's payment account.
|
||||
export const ConnectPaymentSchema = z.strictObject({
|
||||
provider: paymentProviderSchema,
|
||||
// For the MVP it connects in simulation / sandbox mode.
|
||||
sandbox: z.boolean().default(true),
|
||||
accessToken: z.string().optional(),
|
||||
});
|
||||
export type ConnectPayment = z.output<typeof ConnectPaymentSchema>;
|
||||
|
||||
export const ConnectPaymentResultSchema = z.object({
|
||||
provider: paymentProviderSchema,
|
||||
sandbox: z.boolean(),
|
||||
connected: z.boolean(),
|
||||
});
|
||||
export type ConnectPaymentResult = z.output<typeof ConnectPaymentResultSchema>;
|
||||
|
||||
// Step 3: create the teacher's first group.
|
||||
// Step 3: create the teacher's first group. Linking Mercado Pago is step 2 and
|
||||
// is skippable, so nothing here may require a linked account.
|
||||
export const CreateFirstGroupSchema = z.strictObject({
|
||||
name: z
|
||||
.string()
|
||||
|
||||
Reference in New Issue
Block a user