| 1 | import { SimPayClient } from "@simpay/typescript"; |
| 2 | import type { PaymentIpnNotification |
| 3 | import type { WebhookContext, |
| 4 | |
| 5 | export interface SimPayTopupOptions |
| 6 | userId: string; |
| 7 | amountCents: number; |
| 8 | currency: string; |
| 9 | successUrl: string; |
| 10 | cancelUrl: string; |
| 11 | gatewayId: string; |
| 12 | customerEmail: string; |
| 13 | customerName?: string |
| 14 | } |
| 15 | |
| 16 | export async function createSimPayTopupSession |
| 17 | config: { |
| 18 | opts: SimPayTopupOptions |
| 19 | ): Promise<{ |
| 20 | const simpay = |
| 21 | api: { password: config |
| 22 | service: { id: config |
| 23 | ipn: { signatureKey: "" |
| 24 | }); |
| 25 | const tx = |
| 26 | amount: opts.amountCents |
| 27 | currency: opts.currency |
| 28 | description: "Wallet Top-Up", |
| 29 | control: opts.userId |
| 30 | customer: { email: opts |
| 31 | returns: { success: opts |
| 32 | }); |
| 33 | return { url: |
| 34 | } |
| 35 | |
| 36 | export async function handleSimPayWebhook |
| 37 | const simpay = |
| 38 | api: { password: ctx |
| 39 | service: { id: ctx |
| 40 | ipn: { signatureKey: ctx |
| 41 | }); |
| 42 | |
| 43 | let payload: |
| 44 | try { |
| 45 | payload = JSON. |
| 46 | } catch { |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | try { |
| 51 | await simpay.notifications |
| 52 | } catch { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | const n = |
| 57 | if (n.type |
| 58 | const data = |
| 59 | if (!data.control |
| 60 | const parsedAmount = |
| 61 | if (!Number.isFinite |
| 62 | return { |
| 63 | type: "topup.succeeded", |
| 64 | userId: data.control |
| 65 | amountCents: Math.round |
| 66 | currency: data.amount |
| 67 | providerTransactionId: data.payer_transaction_id |
| 68 | gatewayId: ctx.gatewayId |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | return null; |
| 73 | } |