Topics covering webhook creation & management, event handling, Pub/Sub, and Eventbridge, in Shopify apps.
So this is the code i get when i first create an app with shopify cli (with some small variations)
export const action = async ({ request }) => {
console.log("ejecutando request webhook");
/* Verificamos que sea un webhook válido de Shopify
if (!verifyShopifyWebhook(request)) {
return json({ message: "Unauthorized" }, { status: 401 });
}
*/
const payload = await request.json(); // Obtenemos el payload del webhook
console.log(payload);
if (payload.topic === "app/uninstalled") {
console.log("App desinstalada", payload);
// Lógica para manejar la desinstalación
} else if (payload.topic === "app_purchases_one_time/update") {
console.log("Compra de app actualizada", payload);
// Lógica para manejar la actualización de compras
}
console.log("pinto", payload);
// Siempre devolver una respuesta
return json({ message: "Webhook recibido correctamente" });
const { topic, shop, session, admin } = await authenticate.webhook(request);
console.log("esto nunca pasa", topic);
if (!admin && topic !== "SHOP_REDACT") {
// The admin context isn't returned if the webhook fired after a shop was uninstalled.
// The SHOP_REDACT webhook will be fired up to 48 hours after a shop uninstalls the app.
// Because of this, no admin context is available.
throw new Response();
}
// The topics handled here should be declared in the shopify.app.toml.
// More info: https://shopify.dev/docs/apps/build/cli-for-apps/app-configuration
switch (topic) {
case "APP_UNINSTALLED":
if (session) {
await db.session.deleteMany({ where: { shop } });
}
break;
case "APP_PURCHASES_ONE_TIME_UPDATE":
console.log("COMPRARON UNA VEZ");
break;
case "CUSTOMERS_DATA_REQUEST":
case "CUSTOMERS_REDACT":
case "SHOP_REDACT":
default:
throw new Response("Unhandled webhook topic", { status: 404 });
}
throw new Response();
};
{
app_purchase_one_time: {
admin_graphql_api_id: 'gid://shopify/AppPurchaseOneTime/3120824564',
name: '4 AI Videos',
status: 'ACTIVE',
admin_graphql_api_shop_id: 'gid://shopify/Shop/71627636980',
created_at: '2024-09-12T12:14:16-04:00',
updated_at: '2024-09-12T12:14:23-04:00'
}
}