第三方在使用 Mizu 服务时,使用 Mizu SDK 可以大大减少开发成本。也会提升客户使用 Mizu 服务的意愿。所以我们应该开发并维护一套好用的 SDK。
npm install @mizufinancial/sdk
# or
pnpm add @mizufinancial/sdk
# or
yarn add @mizufinancial/sdkimport Mizu from '@mizufinancial/sdk';
const mizu = new Mizu('your_api_key', {
webhookSecret: 'whsec_...'
});Verify the webhook signature and construct the event object.
Note: You must provide the raw request body as a string to constructEventAsync, not the parsed JSON object.
// Assuming you are using Express
app.post('/webhook', async (req, res) => {
const signature = req.headers['mizu-signature'];
try {
const event = await mizu.webhooks.constructEventAsync(
req.body, // This must be the raw string body
signature
);
// Handle the event
switch (event.type) {
case 'payment.succeeded':
const payment = event.data;
// ... handle payment success
break;
default:
console.log(`Unhandled event type ${event.type}`);
}
res.json({received: true});
} catch (err) {
console.log(`Webhook Error: ${err.message}`);
res.status(400).send(`Webhook Error: ${err.message}`);
}
});/packages/core- Main entry point (@mizufinancial/sdk)/packages/webhook- Webhook verification logic (@mizufinancial/sdk-webhook)/packages/signature- Cryptographic signature utilities (@mizufinancial/sdk-signature)