Created
May 24, 2021 16:34
-
-
Save m5r/52021a6bf565bfdd590d338a86099d91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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