| 1 | import { createHash, randomUUID } from "crypto"; |
| 2 | import type { WebhookContext, |
| 3 | |
| 4 | export interface P24TopupOptions |
| 5 | userId: string; |
| 6 | amountCents: number; |
| 7 | currency: string; |
| 8 | successUrl: string; |
| 9 | cancelUrl: string; |
| 10 | statusUrl: string; |
| 11 | gatewayId: string; |
| 12 | customerEmail: string; |
| 13 | } |
| 14 | |
| 15 | function apiBase(sandbox? |
| 16 | return sandbox ? |
| 17 | } |
| 18 | |
| 19 | function basicAuth(posId: |
| 20 | return `Basic ${Buffer |
| 21 | } |
| 22 | |
| 23 | export async function createP24TopupSession |
| 24 | config: { |
| 25 | opts: P24TopupOptions |
| 26 | ): Promise<{ |
| 27 | const sessionId = |
| 28 | const res = |
| 29 | method: "POST", |
| 30 | headers: { |
| 31 | Authorization: basicAuth(config |
| 32 | "Content-Type": "application/json", |
| 33 | }, |
| 34 | body: JSON.stringify |
| 35 | merchantId: config.merchantId |
| 36 | posId: config.posId |
| 37 | sessionId, |
| 38 | amount: opts.amountCents |
| 39 | currency: opts.currency |
| 40 | description: "Wallet Top-Up", |
| 41 | email: opts.customerEmail |
| 42 | country: "PL", |
| 43 | urlReturn: opts.successUrl |
| 44 | urlStatus: opts.statusUrl |
| 45 | }), |
| 46 | }); |
| 47 | if (!res.ok |
| 48 | const data = |
| 49 | if (!data.token |
| 50 | return { url: |
| 51 | } |
| 52 | |
| 53 | export async function handleP24Webhook |
| 54 | const crc = |
| 55 | if (!crc) |
| 56 | |
| 57 | let n: |
| 58 | sessionId?: string |
| 59 | orderId?: number |
| 60 | amount?: number |
| 61 | currency?: string |
| 62 | sign?: string |
| 63 | }; |
| 64 | try { |
| 65 | n = JSON. |
| 66 | } catch { |
| 67 | return null; |
| 68 | } |
| 69 | if (!n.sessionId |
| 70 | |
| 71 | const expected = |
| 72 | .update(`${n |
| 73 | .digest("hex") |
| 74 | if (expected !== |
| 75 | |
| 76 | const userId = |
| 77 | if (!userId) |
| 78 | |
| 79 | const verifyRes = |
| 80 | method: "GET", |
| 81 | headers: { Authorization: basicAuth |
| 82 | }); |
| 83 | if (!verifyRes.ok |
| 84 | const verify = |
| 85 | const data = |
| 86 | if (data?.status |
| 87 | |
| 88 | return { |
| 89 | type: "topup.succeeded", |
| 90 | userId, |
| 91 | amountCents: data.amount |
| 92 | currency: (data.currency |
| 93 | providerTransactionId: String(n |
| 94 | gatewayId: ctx.gatewayId |
| 95 | }; |
| 96 | } |