Payments and Checkouts made dead simple with Nuxt.
Choose your preferred package manager to install the module:
pnpm add @polar-sh/nuxt
Add the module to your nuxt.config.ts:
export default defineNuxtConfig({
modules: ["@polar-sh/nuxt"],
});
Create a Checkout handler which takes care of redirections.
// server/routes/api/checkout.post.ts
export default defineEventHandler((event) => {
const {
private: { polarAccessToken, polarCheckoutSuccessUrl, polarServer },
} = useRuntimeConfig();
const checkoutHandler = Checkout({
accessToken: polarAccessToken,
successUrl: polarCheckoutSuccessUrl,
returnUrl: "https://myapp.com", // Optional Return URL, which renders a Back-button in the Checkout
server: polarServer as "sandbox" | "production",
theme: "dark" // Enforces the theme - System-preferred theme will be set if left omitted
});
return checkoutHandler(event);
});
Pass query params to this route.
?products=123?products=123&customerId=xxx?products=123&customerExternalId=xxx?products=123&customerEmail=janedoe@gmail.com?products=123&customerName=JaneURL-Encoded JSON stringCreate a customer portal where your customer can view orders and subscriptions.
// server/routes/api/portal.get.ts
export default defineEventHandler((event) => {
const {
private: { polarAccessToken, polarCheckoutSuccessUrl, polarServer },
} = useRuntimeConfig();
const customerPortalHandler = CustomerPortal({
accessToken: polarAccessToken,
server: polarServer as "sandbox" | "production",
getCustomerId: (event) => {
return Promise.resolve("9d89909b-216d-475e-8005-053dba7cff07");
},
returnUrl: "https://myapp.com", // Optional Return URL, which renders a Back-button in the Customer Portal
});
return customerPortalHandler(event);
});
A simple utility which resolves incoming webhook payloads by signing the webhook secret properly.
// server/routes/webhook/polar.post.ts
export default defineEventHandler((event) => {
const {
private: { polarWebhookSecret },
} = useRuntimeConfig();
const webhooksHandler = Webhooks({
webhookSecret: polarWebhookSecret,
onPayload: async (payload: any) => {
// Handle the payload
// No need to return an acknowledge response
},
});
return webhooksHandler(event);
});
The Webhook handler also supports granular handlers for easy integration.