Siirry sisältöön

Webhook-todentaja

@feitpay/webhooks todentaa toimitukset täsmälleen sivulla Webhook-allekirjoitusten todentaminen kuvatulla tavalla ja palauttaa tyypitetyn tapahtuman.

Terminal window
pnpm add @feitpay/webhooks

WebCrypto (mikä tahansa moderni ajoympäristö)

Osio nimeltä “WebCrypto (mikä tahansa moderni ajoympäristö)”
import { verifyWebhook, WebhookVerificationError, type WebhookEvent } from '@feitpay/webhooks';
const event = await verifyWebhook({
payload: rawBody, // string or Uint8Array — the raw body
header: request.headers.get('feitpay-signature'),
secrets: [process.env.FEITPAY_WEBHOOK_SECRET!, process.env.FEITPAY_WEBHOOK_SECRET_PREVIOUS!].filter(Boolean),
toleranceSeconds: 300, // default
});
if (event.event_type === 'payment.confirmed' && event.data.fulfillable) {
// typed: event.data.quote?.asset_amount, event.data.totals?.confirmed_base_units …
}
import { verifyWebhookSync } from '@feitpay/webhooks/node';
const event = verifyWebhookSync({ payload: rawBodyBuffer, header, secret });

WebhookVerificationError.reason on jokin seuraavista: malformed_header, no_signatures, signature_mismatch, timestamp_outside_tolerance tai invalid_payload. Vastaa 400. Puuttuva salaisuus tai muu kuin positiivinen toleranssi heittää TypeError-poikkeuksen (kyse on konfiguraatiovirheestä, ei virheellisestä pyynnöstä).

import { generateTestHeader } from '@feitpay/webhooks';
const header = await generateTestHeader({ payload: body, secret: 'whsec_test_only' });

Paketin oma testisarja ajaa jokaisen tapauksen kaikille kielille yhteisestä vektoritiedostosta.