const paypal = require('@paypal/checkout-server-sdk'); // PayPal environment setup function environment() { const clientId = process.env.PAYPAL_CLIENT_ID; const clientSecret = process.env.PAYPAL_CLIENT_SECRET; const mode = process.env.PAYPAL_MODE || 'sandbox'; if (!clientId || !clientSecret) { throw new Error('PayPal credentials not found in environment variables'); } return mode === 'live' ? new paypal.core.LiveEnvironment(clientId, clientSecret) : new paypal.core.SandboxEnvironment(clientId, clientSecret); } // PayPal client function client() { return new paypal.core.PayPalHttpClient(environment()); } // Create PayPal order async function createOrder(orderData) { try { const request = new paypal.orders.OrdersCreateRequest(); request.prefer("return=representation"); request.requestBody({ intent: 'CAPTURE', // ADD PAYER INFORMATION payer: orderData.payer ? { email_address: orderData.payer.email, name: { given_name: orderData.payer.first_name, surname: orderData.payer.last_name }, phone: orderData.payer.phone ? { phone_type: "MOBILE", phone_number: { national_number: orderData.payer.phone } } : undefined, } : undefined, purchase_units: [{ reference_id: orderData.reference_id, amount: { currency_code: orderData.currency || 'USD', value: orderData.total, breakdown: { item_total: { currency_code: orderData.currency || 'USD', value: orderData.items_total || orderData.total }, shipping: orderData.shipping.total ? { currency_code: orderData.currency_code || 'USD', value: orderData.shipping.total } : undefined, tax_total: orderData.tax_total ? { currency_code: orderData.currency || 'USD', value: orderData.tax_total } : undefined, } }, description: orderData.description || 'Payment from WooCommerce', custom_id: orderData.wc_order_id, items: orderData.items || [], // ADD SHIPPING INFORMATION shipping: orderData.shipping ? { name: { full_name: `${orderData.shipping.first_name} ${orderData.shipping.last_name}` }, address: { address_line_1: orderData.shipping.address_1, address_line_2: orderData.shipping.address_2 || undefined, admin_area_2: orderData.shipping.city, admin_area_1: orderData.shipping.state, postal_code: orderData.shipping.postcode, country_code: orderData.shipping.country } } : undefined }], application_context: { brand_name: orderData.brand_name || 'Fashion Store', landing_page: 'BILLING', user_action: 'PAY_NOW', return_url: `${process.env.FRONTEND_URL}/success`, cancel_url: `${process.env.FRONTEND_URL}/cancel` } }); const order = await client().execute(request); console.log('PayPal Order Created:', { id: order.result.id, status: order.result.status, amount: orderData.total }); return { success: true, order_id: order.result.id, status: order.result.status, links: order.result.links }; } catch (error) { console.error('PayPal Create Order Error:', error); return { success: false, error: error.message, details: error.details || [] }; } } // Capture PayPal payment async function captureOrder(orderId) { try { const request = new paypal.orders.OrdersCaptureRequest(orderId); request.requestBody({}); const capture = await client().execute(request); console.log('PayPal Order Captured:', { id: capture.result.id, status: capture.result.status, payer_email: capture.result.payer?.email_address }); return { success: true, capture_id: capture.result.id, status: capture.result.status, payer: capture.result.payer, purchase_units: capture.result.purchase_units, transaction_id: capture.result.purchase_units[0]?.payments?.captures[0]?.id }; } catch (error) { console.error('PayPal Capture Order Error:', error); return { success: false, error: error.message, details: error.details || [] }; } } // Get order details async function getOrderDetails(orderId) { try { const request = new paypal.orders.OrdersGetRequest(orderId); const order = await client().execute(request); return { success: true, order: order.result }; } catch (error) { console.error('PayPal Get Order Error:', error); return { success: false, error: error.message }; } } // Verify webhook signature function verifyWebhookSignature(headers, body, webhookId) { // This is a simplified version - in production, use PayPal's webhook verification // For now, we'll do basic validation const webhookSecret = process.env.WEBHOOK_SECRET; if (!webhookSecret) { console.warn('WEBHOOK_SECRET not set - skipping signature verification'); return true; } // Add proper webhook signature verification here // For development, we'll return true return true; } module.exports = { createOrder, captureOrder, getOrderDetails, verifyWebhookSignature };