Improve: plans, passer par checkouts à la place de la création unique et d'une boucle
Closed this issue · 4 comments
CinquinAndy commented
On boucle sur tout les variants pour créer des url de checkout
Pour améliorer ça & éviter de spam lemonsqueezy et avoir des temps de réponses énorme ( 60+s )
j'essaye de passer par :
(listCheckouts())
-> sauf que je reçois des données archi chimique, c'est incompréhensible...
Comment ça se fait que ça renvoie 1700 items ?? j'arrive pas à comprendre comment c'est possible
CinquinAndy commented
@LightInn aucune idée ;(
CinquinAndy commented
j'ai trouvé un moyen en faisant des chimistery, je pense qu'on est pas mal :
export async function getCheckouts(plans) {
await initLemonSqueezy()
const STORE_ID = getStoreId()
const user = await currentUser()
if (!user) {
console.error('User is not authenticated.')
throw new Error('User is not authenticated.')
}
const check = await listCheckouts({
product_options: {
redirectUrl: `${process.env.NEXT_PUBLIC_URL}/app/playground`,
},
checkoutData: {
custom: {
user_id: user.id,
},
},
filter: {
storeId: STORE_ID,
},
page: {
size: 100,
},
})
const checkoutUrls = {}
for (const plan of plans) {
const existingCheckout = check.data.data.find(
checkout =>
checkout.attributes.variant_id.toString() === plan.variantId.toString()
)
if (existingCheckout) {
checkoutUrls[plan.variantId] = existingCheckout.attributes.url
} else {
const newCheckout = await createCheckout(STORE_ID, plan.variantId, {
productOptions: {
redirectUrl: `https://forvoyez.com/app/billing/`,
receiptButtonText: 'Go to Dashboard',
enabledVariants: [plan.variantId], //
},
checkoutData: {
custom: {
user_id: user.id,
},
},
})
checkoutUrls[plan.variantId] = newCheckout.data.data.attributes.url
}
}
return checkoutUrls
}
je récupère tout les checkout url, et j'en crée uniquement si les liens n'existent pas déjà
LightInn commented
CinquinAndy commented