rspace-online/modules/rpayments/schemas.ts

124 lines
3.1 KiB
TypeScript

/**
* rPayments Automerge document schemas.
*
* Single document type:
* - Payment request: one doc per payment (QR invoice, subscription, or one-shot).
* DocId: {space}:payments:{paymentId}
*/
import type { DocSchema } from '../../shared/local-first/document';
/** Individual payment record in a subscription's history. */
export interface PaymentRecord {
txHash: string | null;
transakOrderId: string | null;
paymentMethod: 'transak' | 'wallet' | 'encryptid' | null;
payerIdentity: string | null;
payerEmail: string | null;
amount: string;
paidAt: number;
}
export interface PaymentRequestMeta {
id: string;
description: string;
amount: string;
amountEditable: boolean;
token: string;
chainId: number;
recipientAddress: string;
fiatAmount: string | null;
fiatCurrency: string;
creatorDid: string;
creatorUsername: string;
status: 'pending' | 'paid' | 'confirmed' | 'expired' | 'cancelled' | 'filled';
paymentMethod: 'transak' | 'wallet' | 'encryptid' | null;
txHash: string | null;
payerIdentity: string | null;
transakOrderId: string | null;
paymentType: 'single' | 'subscription' | 'payer_choice';
maxPayments: number;
paymentCount: number;
enabledMethods: {
card: boolean;
wallet: boolean;
encryptid: boolean;
};
interval: 'weekly' | 'biweekly' | 'monthly' | 'quarterly' | 'yearly' | null;
nextDueAt: number;
subscriberEmail: string | null;
/** Linked shopping cart ID (for rCart contribute-pay flow). rCart listens for this via onPaymentPaid. */
linkedCartId: string | null;
paymentHistory: PaymentRecord[];
createdAt: number;
updatedAt: number;
paidAt: number;
expiresAt: number;
}
export interface PaymentRequestDoc {
meta: {
module: string;
collection: string;
version: number;
spaceSlug: string;
createdAt: number;
};
payment: PaymentRequestMeta;
}
export const paymentRequestSchema: DocSchema<PaymentRequestDoc> = {
module: 'payments',
collection: 'payments',
version: 1,
init: (): PaymentRequestDoc => ({
meta: {
module: 'payments',
collection: 'payments',
version: 1,
spaceSlug: '',
createdAt: Date.now(),
},
payment: {
id: '',
description: '',
amount: '0',
amountEditable: false,
token: 'USDC',
chainId: 8453,
recipientAddress: '',
fiatAmount: null,
fiatCurrency: 'USD',
creatorDid: '',
creatorUsername: '',
status: 'pending',
paymentMethod: null,
txHash: null,
payerIdentity: null,
transakOrderId: null,
paymentType: 'single',
maxPayments: 0,
paymentCount: 0,
enabledMethods: { card: true, wallet: true, encryptid: true },
interval: null,
nextDueAt: 0,
subscriberEmail: null,
linkedCartId: null,
paymentHistory: [],
createdAt: Date.now(),
updatedAt: Date.now(),
paidAt: 0,
expiresAt: 0,
},
}),
};
export function paymentRequestDocId(space: string, paymentId: string) {
return `${space}:payments:${paymentId}` as const;
}
/** Legacy doc ID pattern (when payments lived inside rCart). Used by the one-time migration. */
export function legacyPaymentRequestDocId(space: string, paymentId: string) {
return `${space}:cart:payments:${paymentId}` as const;
}