Accepting Payments with Waffo Pancake
Esta página aún no está disponible en tu idioma.
Browse the source code here.
LaunchFast ships with a built-in Waffo Pancake integration. Waffo Pancake is a merchant of record for software businesses: it handles payments, sales tax, compliance, and payouts worldwide, and sells one-time products and subscriptions through a hosted checkout that accepts cards, Apple Pay, and Google Pay.
The starter kit uses the official @waffo/pancake-ts SDK, which signs every API request with your private key and verifies webhooks with Waffo’s built-in public keys.
Here’s how you would set up Waffo Pancake to use it with LaunchFast:
-
Create a Waffo Pancake account.
-
Create an API key in the dashboard under API & Development, and copy your merchant ID and private key. Test and production keys are separate.
-
Create a product, publish it, and copy its product ID.
-
Add a webhook pointing to
https://<your-domain>/api/waffo/webhook. -
Make sure to update the environment variable(s) of your application per the following instruction:
## Waffo Pancake Environment VariablesWAFFO_MERCHANT_ID="MER_..."WAFFO_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n" # PEM or base64 encoded PEMWAFFO_PRODUCT_ID="PROD_..." # Default product used by the Buttons/Waffo componentWAFFO_CURRENCY="USD" # Optional, defaults to USDThe private key is read as a server-side secret and never reaches the browser. It can be stored as a PEM with real or escaped \n newlines, or as a base64 encoded PEM, whichever your hosting provider handles best.
Adding a Checkout Button
Import the Waffo checkout button anywhere in your app. Clicking it posts to the /api/waffo/checkout route, which creates a Waffo checkout session and redirects the user to it:
---import ButtonWaffoCheckout from '@/components/Buttons/Waffo.astro'---
<ButtonWaffoCheckout brand_name="ACME" />
<!-- or checkout a specific product --><ButtonWaffoCheckout brand_name="ACME Pro" product_id="PROD_..." />Leave out product_id to sell the product set in WAFFO_PRODUCT_ID. Pass it to sell several products, such as different plans on a pricing page, from the same route.
Here is what the checkout route does under the hood:
const waffo = getWaffoClient()// Use the product picked by the button, else fall back to WAFFO_PRODUCT_IDconst productId = await getProductId(request, getSecret('WAFFO_PRODUCT_ID'))// Create a Waffo Pancake checkout sessionconst session = await waffo.checkout.createSession({ productId, metadata: getReflioMetadata(cookies), currency: getSecret('WAFFO_CURRENCY') || 'USD', // Mentioning the URL where user would land up when they complete the transaction succesfully (must be https) successUrl: new URL('/api/waffo/success', site).toString(),})// Redirect the user to the Waffo Pancake hosted checkout created aboveif (session.checkoutUrl) return webRedirect(session.checkoutUrl)Handling the Webhook
Once a payment goes through, Waffo calls your webhook route. The starter kit verifies the x-waffo-signature header against the raw request body and gives the customer access in the database:
// Obtain the raw body of the request to be used for verificationconst rawBody = await request.text()// Verify the RSA-SHA256 signature using the Waffo public keys baked into the SDK// (test vs prod is detected automatically)const event = verifyWebhook(rawBody, sig)// A one-time order being completed or a subscription being activated point to a succesful paymentif (event.eventType === WebhookEventType.OrderCompleted || event.eventType === WebhookEventType.SubscriptionActivated) { const customerEmail = event.data.buyerEmail if (customerEmail) { // Add the 'email' to the access list in the database await setAccess(customerEmail, '1') return webResponse('approved user access', 200) }}// Acknowledge the other events so that Waffo does not retry themreturn webJson({ received: event.eventType }, 200)order.completed fires for one-time products and subscription.activated for subscriptions. The SDK exposes every other event too, such as subscription.canceled and refund.succeeded, through the WebhookEventType enum, so you can revoke access or send emails from the same route.
Testing
In test mode, pay with the card number 4576 7500 0000 0110, any future expiry date, and any CVC.