diff --git a/README.md b/README.md index 83efd4b..4ef9523 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This is a custom Node.js Express server that listens for Shopify `orders/create` - Node.js (v14 or higher) - A Shopify Store -- An Email Account (e.g., Gmail, SendGrid) to send emails via SMTP. +- An Email Account (e.g ., Gmail, SendGrid) to send emails via SMTP. ## Setup diff --git a/mailer.js b/mailer.js index af8f7f6..85f1de9 100644 --- a/mailer.js +++ b/mailer.js @@ -74,6 +74,9 @@ const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer, shippingLa // --- 3. Billing Address Details HTML --- const billing = orderData.billing_address || {}; + const whatsappNumber = orderData.note_attributes?.find?.((attr) => attr.name === 'WhatsApp Number')?.value; + const contactNumber = billing.phone || orderData.phone; + let billingAddressHtml = ''; if (billing.first_name || billing.last_name) { billingAddressHtml += `${billing.first_name || ''} ${billing.last_name || ''}
`; @@ -84,8 +87,12 @@ const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer, shippingLa billingAddressHtml += `${billing.city || ''} ${billing.province || ''} ${billing.zip || ''}
`; } if (billing.country) billingAddressHtml += `${billing.country}
`; - if (billing.phone || orderData.phone) { - billingAddressHtml += `${billing.phone || orderData.phone}`; + if (whatsappNumber) { + billingAddressHtml += `WhatsApp: ${whatsappNumber}`; + } + if (contactNumber) { + if (whatsappNumber) billingAddressHtml += `
`; + billingAddressHtml += `Contact Number: ${contactNumber}`; } const paymentMethod = orderData.gateway || 'Request Quote'; diff --git a/pdfGenerator.js b/pdfGenerator.js index 555838a..69b9d7c 100644 --- a/pdfGenerator.js +++ b/pdfGenerator.js @@ -132,6 +132,8 @@ const generateInvoicePDF = async (orderData) => { // Bill To (Left) const billing = orderData.billing_address || {}; + const whatsappNumber = orderData.note_attributes?.find?.((attr) => attr.name === 'WhatsApp Number')?.value; + doc.fontSize(11) .font('Helvetica-Bold') .fillColor(primaryColor) @@ -166,8 +168,13 @@ const generateInvoicePDF = async (orderData) => { doc.text(orderData.email || orderData.contact_email, 40, billTextY); billTextY += 13; } + if (whatsappNumber) { + doc.text(`WhatsApp: ${whatsappNumber}`, 40, billTextY); + billTextY += 13; + } if (billing.phone || orderData.phone) { - doc.text(billing.phone || orderData.phone, 40, billTextY); + doc.text(`Contact Number: ${billing.phone || orderData.phone}`, 40, billTextY); + billTextY += 13; } // Order Details (Right) diff --git a/server.js b/server.js index 2c13c5d..2a93709 100644 --- a/server.js +++ b/server.js @@ -7,7 +7,11 @@ const { generateShippingLabelPDF } = require('./shippingLabelGenerator'); const { sendEmailWithAttachment } = require('./mailer'); const app = express(); -app.use(cors()); // Allow frontend Shopify store to call this server +app.use(cors({ + origin: '*', // Allow all origins (you can restrict this to 'https://rayaarishop.com' in production) + methods: ['GET', 'POST', 'OPTIONS', 'PUT', 'PATCH', 'DELETE'], + allowedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization', 'X-Shopify-Hmac-Sha256', 'X-Shopify-Access-Token'] +})); // Allow frontend Shopify store to call this server const PORT = process.env.PORT || 3000; // Middleware to capture raw body for Shopify webhook HMAC verification @@ -208,6 +212,16 @@ app.post('/create-order', async (req, res) => { } }; + const whatsappNumber = customerData.whatsapp_number || customerData.contact_phone || customerData.contactNumber; + if (whatsappNumber) { + orderPayload.order.note_attributes = [ + { + name: 'WhatsApp Number', + value: whatsappNumber + } + ]; + } + const shopifyResponse = await fetch(`https://${process.env.SHOPIFY_STORE_DOMAIN}/admin/api/2024-01/orders.json`, { method: 'POST', headers: {