|
| 1 | +import type { PaymentStatus, WalletAddressInfo } from 'publisher-tools-api' |
| 2 | +import type { PendingGrant } from '@interledger/open-payments' |
| 3 | +import { createDefaultPaywallProfile } from '@shared/default-data' |
| 4 | +import type { PaywallProfile } from '@shared/types' |
| 5 | + |
| 6 | +type WalletAddressUrl = string |
| 7 | +/** The amount sender wants to send (like "1.05"), does not include fees */ |
| 8 | +type UserAmount = number | PaymentCurrencyAmount['value'] |
| 9 | + |
| 10 | +interface QuoteInput { |
| 11 | + sender: WalletAddressInfo |
| 12 | + receiver: WalletAddressInfo |
| 13 | + amount: UserAmount |
| 14 | +} |
| 15 | +type QuoteResult = |
| 16 | + | { debitAmount: PaymentCurrencyAmount; receiveAmount: PaymentCurrencyAmount } |
| 17 | + | { error: string; minSendAmount?: PaymentCurrencyAmount } |
| 18 | + |
| 19 | +interface InitiatePaymentInput { |
| 20 | + sender: WalletAddressInfo |
| 21 | + receiver: WalletAddressInfo |
| 22 | + amount: UserAmount |
| 23 | + note: string |
| 24 | +} |
| 25 | +interface InitiatePaymentResult { |
| 26 | + paymentId: string |
| 27 | + grantRedirectUrl: PendingGrant['interact']['redirect'] |
| 28 | +} |
| 29 | + |
| 30 | +type Entitlement = 'no-access' | 'auth-required' | 'has-access' |
| 31 | + |
| 32 | +export interface Controller { |
| 33 | + receiverWalletAddressUrl: string |
| 34 | + |
| 35 | + fetchConfig(): Promise<PaywallProfile> |
| 36 | + |
| 37 | + /** Check if given wallet address is entitled to access */ |
| 38 | + checkEntitlement(walletAddressUrl: WalletAddressUrl): Promise<Entitlement> |
| 39 | + /** Store the entitlement after a successful payment in some backend */ |
| 40 | + saveEntitlement( |
| 41 | + walletAddressUrl: WalletAddressUrl, |
| 42 | + details: { |
| 43 | + outgoingPaymentId: string |
| 44 | + incomingPaymentId: string |
| 45 | + paymentId: string |
| 46 | + }, |
| 47 | + ): Promise<void> |
| 48 | + |
| 49 | + getWallet(walletAddressUrl: WalletAddressUrl): Promise<WalletAddressInfo> |
| 50 | + fetchQuote(request: QuoteInput): Promise<QuoteResult> |
| 51 | + initiatePayment(request: InitiatePaymentInput): Promise<InitiatePaymentResult> |
| 52 | + getStatus( |
| 53 | + paymentId: string, |
| 54 | + signal?: AbortSignal, |
| 55 | + ): AsyncGenerator<PaymentStatus> |
| 56 | + |
| 57 | + isPreviewMode?: boolean |
| 58 | +} |
| 59 | + |
| 60 | +export const NO_OP_CONTROLLER: Controller = { |
| 61 | + receiverWalletAddressUrl: 'https://example.com/pay', |
| 62 | + fetchConfig: () => Promise.resolve(createDefaultPaywallProfile('')), |
| 63 | + checkEntitlement: () => Promise.resolve('no-access'), |
| 64 | + saveEntitlement: () => Promise.resolve(), |
| 65 | + getWallet(walletAddressUrl) { |
| 66 | + return Promise.resolve({ |
| 67 | + $url: walletAddressUrl, |
| 68 | + id: walletAddressUrl, |
| 69 | + assetCode: 'USD', |
| 70 | + assetScale: 2, |
| 71 | + authServer: 'https://auth.example.com', |
| 72 | + resourceServer: 'https://resource.example.com', |
| 73 | + publicName: 'Wallet (Preview)', |
| 74 | + }) |
| 75 | + }, |
| 76 | + fetchQuote({ amount, sender, receiver }) { |
| 77 | + amount = String(amount) |
| 78 | + const debitAmount = { value: amount, currency: sender.assetCode } |
| 79 | + const receiveAmount = { value: amount, currency: receiver.assetCode } |
| 80 | + return Promise.resolve({ debitAmount, receiveAmount }) |
| 81 | + }, |
| 82 | + initiatePayment() { |
| 83 | + return Promise.resolve({ |
| 84 | + paymentId: 'payment-id', |
| 85 | + grantRedirectUrl: 'https://example.com/redirect', |
| 86 | + }) |
| 87 | + }, |
| 88 | + async *getStatus() { |
| 89 | + yield { |
| 90 | + type: 'OUTGOING_PAYMENT_DONE', |
| 91 | + outgoingPaymentId: '', |
| 92 | + result: 'success', |
| 93 | + } |
| 94 | + }, |
| 95 | +} |
0 commit comments