Last active
July 5, 2023 15:25
-
-
Save matiasfha/071d01373cb9fbc7d90cb990a63a83bc 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
export actions = { | |
prepyment: async ({ request }) => { | |
const formData = await request.formData() | |
const price = formData.get('price') | |
const email = formData.get('email') | |
const url = await generateCheckout(price, email) | |
return { success: true, url } | |
} | |
} | |
async function generateCheckout(price: number, email: string) { | |
const res = await fetch('https://api.lemonsqueezy.com/v1/checkouts', { | |
method: 'POST', | |
headers: { | |
Accept: 'application/vnd.api+json', | |
'Content-Type': 'application/vnd.api+json', | |
Authorization: `Bearer ${env.LEMONSQUEEZY_KEY}` | |
}, | |
body: JSON.stringify({ | |
data: { | |
type: 'checkouts', | |
attributes: { | |
preview: false, | |
custom_price: price, | |
checkout_options: { | |
embed: true, | |
discount: false, | |
subscription_preview: false, | |
button_color: '#C148AC' | |
}, | |
}, | |
relationships: { | |
store: { | |
data: { | |
id: '30883', | |
type: 'stores' | |
} | |
}, | |
variant: { | |
data: { | |
id: '85474', | |
type: 'variants' | |
} | |
} | |
} | |
} | |
}) | |
}); | |
const json = await res.json(); | |
const { data } = LemonSqueezyCheckout.parse(json); | |
return data.attributes.url | |
} |
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
<script> | |
let paidForm | |
const onGetPaymentUrl = () => { | |
return async ({ update, result }) => { | |
if (result.type === 'success') { | |
console.log(result); | |
const url = result.data.url; | |
LemonSqueezy.Url.Open(url); | |
} | |
await update(); | |
}; | |
} | |
onMount(() => { | |
// Create the LemonSqueezy global object | |
window.createLemonSqueezy(); | |
LemonSqueezy.Setup({ | |
eventHandler: ({ event, data }) => { | |
// Listen for events, if payment was successfull | |
// submit the data | |
if (event === 'Checkout.Success') { | |
console.log('Will submit the form'); | |
// Since payment was made, send the audio to the backend | |
// append the order id to allow the BE to check the paid status | |
orderId = data.order.data.id; | |
LemonSqueezy.Url.Close(); | |
paidForm.submit(); | |
} | |
} | |
}); | |
}); | |
</script> | |
<form | |
method="POST" | |
action="/?/prepayment" | |
use:enhance={onGetPayment} | |
> | |
<button type="submit">Pay</button> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! Thanks @matiasfha 🙌