Skip to content

Instantly share code, notes, and snippets.

@m5r
Created May 24, 2021 16:34
Show Gist options
  • Save m5r/52021a6bf565bfdd590d338a86099d91 to your computer and use it in GitHub Desktop.
Save m5r/52021a6bf565bfdd590d338a86099d91 to your computer and use it in GitHub Desktop.
declare global {
interface Window { Paddle: any; }
}
const VENDOR_ID = 111;
const PRODUCT_ID = 222;
type BuyParams = {
coupon?: string | null;
meta?: Record<string, string>;
onLoadCallback?: VoidFunction;
}
export function buy(params: BuyParams = {}) {
const { coupon, meta, onLoadCallback } = params;
const checkoutOpenParams = {
product: PRODUCT_ID,
allowQuantity: false,
passthrough: JSON.stringify(meta),
coupon: "",
};
if (coupon) {
checkoutOpenParams.coupon = coupon;
}
if (!window.Paddle) {
const script = document.createElement("script");
script.onload = () => {
window.Paddle.Setup({ vendor: VENDOR_ID });
window.Paddle.Checkout.open(checkoutOpenParams);
onLoadCallback?.();
};
script.src = "https://cdn.paddle.com/paddle/paddle.js";
document.head.appendChild(script);
return;
}
window.Paddle.Setup({ vendor: VENDOR_ID });
window.Paddle.Checkout.open(checkoutOpenParams);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment