// backend/paypalHandler.jsw import { fetch } from 'wix-fetch'; const WEBHOOK_URL = "https://manage.wix.com/_api/webhook-trigger/report/553f0605-46ec-4906-aa26-542c081de012/b1f62d7f-efb2-4b59-afc5-c3250e559410"; /** * notifyWebhook - called from client after PayPal onApprove * @param {Object} payload - { email, orderId, status, rawOrder } */ export async function notifyWebhook(payload) { if (!payload || !payload.orderId) { throw new Error('Missing payload or orderId'); } const body = { email: payload.email || null, orderId: payload.orderId, status: payload.status || 'PAYPAL_PREORDER_COMPLETE', timestamp: new Date().toISOString(), rawOrder: payload.rawOrder || null }; try { const response = await fetch(WEBHOOK_URL, { method: 'post', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }); // Include response text for debugging in logs const text = await response.text(); // If Wix returns non-2xx this will throw if (!response.ok) { throw new Error(`Webhook POST failed: ${response.status} ${text}`); } return { success: true, status: response.status, bodyText: text }; } catch (err) { // surface helpful error to client and to site logs console.error('notifyWebhook error', err); throw new Error(`notifyWebhook error: ${err.message}`); } } // page code (e.g., in page's code file) import { notifyWebhook } from 'backend/paypalHandler'; $w.onReady(function () { // Render the hosted PayPal button using the SDK (if you are using an HTML embed for the button, // put this script in the embed OR adapt to your embed code). Example client-side: if (window.paypal && paypal.HostedButtons) { paypal.HostedButtons({ hostedButtonId: "G9MB6VP7XGJTE", onApprove: (data, actions) => { return actions.order.get().then(async (orderDetails) => { // Try to extract payer email — may be undefined if buyer used guest checkout const buyerEmail = orderDetails?.payer?.email_address || null; const payload = { email: buyerEmail, orderId: data.orderID, status: 'PAYPAL_PREORDER_COMPLETE', rawOrder: orderDetails }; try { const result = await notifyWebhook(payload); console.log('notifyWebhook result', result); } catch (err) { console.error('notifyWebhook failed', err); } }); } }).render("#paypal-container-G9MB6VP7XGJTE"); } else { console.error('PayPal SDK not loaded or HostedButtons not available.'); } });
top of page

New Book!

Very Blessed Events: Wedding Planning Guide

Created for couples who want a simple, organized, and stress-free roadmap from engagement to “I do.”

bottom of page