| 1 | import type { WebhookContext, WebhookPayload } from "../types"; |
| 2 | |
| 3 | export interface PayPalTopupOptions |
| 4 | userId: string; |
| 5 | amountCents: number; |
| 6 | currency: string; |
| 7 | successUrl: string; |
| 8 | cancelUrl: string; |
| 9 | gatewayId: string; |
| 10 | } |
| 11 | |
| 12 | function apiBase(sandbox? |
| 13 | return sandbox ? |
| 14 | } |
| 15 | |
| 16 | async function getAccessToken( |
| 17 | const res = |
| 18 | method: "POST", |
| 19 | headers: { |
| 20 | Authorization: `Basic ${Buffer |
| 21 | "Content-Type": "application/x-www-form-urlencoded", |
| 22 | }, |
| 23 | body: "grant_type=client_credentials", |
| 24 | }); |
| 25 | if (!res.ok |
| 26 | const data = |
| 27 | if (!data.access_token |
| 28 | return data.access_token |
| 29 | } |
| 30 | |
| 31 | export async function createPayPalTopupSession |
| 32 | config: { |
| 33 | opts: PayPalTopupOptions |
| 34 | ): Promise<{ |
| 35 | const token = |
| 36 | const res = |
| 37 | method: "POST", |
| 38 | headers: { |
| 39 | Authorization: `Bearer ${token |
| 40 | "Content-Type": "application/json", |
| 41 | }, |
| 42 | body: JSON.stringify |
| 43 | intent: "CAPTURE", |
| 44 | purchase_units: [ |
| 45 | { |
| 46 | amount: { |
| 47 | currency_code: opts.currency |
| 48 | value: (opts.amountCents |
| 49 | }, |
| 50 | custom_id: opts.userId |
| 51 | }, |
| 52 | ], |
| 53 | application_context: { |
| 54 | return_url: opts.successUrl |
| 55 | cancel_url: opts.cancelUrl |
| 56 | user_action: "PAY_NOW", |
| 57 | }, |
| 58 | }), |
| 59 | }); |
| 60 | if (!res.ok |
| 61 | const order = |
| 62 | id: string; |
| 63 | links?: { |
| 64 | }; |
| 65 | const approve = |
| 66 | if (!approve?.href |
| 67 | return { url: |
| 68 | } |
| 69 | |
| 70 | export async function handlePayPalWebhook |
| 71 | if (!ctx.config |
| 72 | |
| 73 | let event: |
| 74 | try { |
| 75 | event = JSON. |
| 76 | } catch { |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | const verifyRes = |
| 81 | method: "POST", |
| 82 | headers: { |
| 83 | Authorization: `Bearer ${await |
| 84 | clientId: ctx.config |
| 85 | secret: ctx.config |
| 86 | sandbox: ctx.config |
| 87 | })}`, |
| 88 | "Content-Type": "application/json", |
| 89 | }, |
| 90 | body: JSON.stringify |
| 91 | auth_algo: ctx.headers |
| 92 | cert_url: ctx.headers |
| 93 | transmission_id: ctx.headers |
| 94 | transmission_sig: ctx.headers |
| 95 | transmission_time: ctx.headers |
| 96 | webhook_id: ctx.config |
| 97 | webhook_event: event, |
| 98 | }), |
| 99 | }); |
| 100 | if (!verifyRes.ok |
| 101 | const verification = |
| 102 | if (verification.verification_status |
| 103 | |
| 104 | const e = |
| 105 | event_type?: string |
| 106 | resource?: { |
| 107 | }; |
| 108 | if (e.event_type |
| 109 | const orderId = |
| 110 | if (!/^[A-Za-z0-9_- |
| 111 | const encodedOrderId = |
| 112 | |
| 113 | const token = |
| 114 | clientId: ctx.config |
| 115 | secret: ctx.config |
| 116 | sandbox: ctx.config |
| 117 | }); |
| 118 | const captureRes = |
| 119 | method: "POST", |
| 120 | headers: { |
| 121 | Authorization: `Bearer ${token |
| 122 | "Content-Type": "application/json", |
| 123 | }, |
| 124 | }); |
| 125 | if (!captureRes.ok |
| 126 | const capture = |
| 127 | id?: string |
| 128 | status?: string |
| 129 | purchase_units?: { |
| 130 | payments?: { |
| 131 | custom_id?: string |
| 132 | amount?: { |
| 133 | }[]; |
| 134 | }; |
| 135 | if (capture.status |
| 136 | |
| 137 | const unit = |
| 138 | const capturedPayment = |
| 139 | const amount = |
| 140 | const userId = |
| 141 | if (!userId || |
| 142 | |
| 143 | const parsedAmount = |
| 144 | if (!Number.isFinite |
| 145 | |
| 146 | return { |
| 147 | type: "topup.succeeded", |
| 148 | userId, |
| 149 | amountCents: Math.round |
| 150 | currency: amount.currency_code |
| 151 | providerTransactionId: capturedPayment?.id |
| 152 | gatewayId: ctx.gatewayId |
| 153 | }; |
| 154 | } |