145 lines
4.3 KiB
JavaScript
145 lines
4.3 KiB
JavaScript
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',
|
|
purchase_units: [{
|
|
reference_id: orderData.reference_id,
|
|
amount: {
|
|
currency_code: orderData.currency || 'USD',
|
|
value: orderData.total
|
|
},
|
|
description: orderData.description || 'Payment from WooCommerce',
|
|
custom_id: orderData.wc_order_id,
|
|
items: orderData.items || []
|
|
}],
|
|
application_context: {
|
|
brand_name: orderData.brand_name || 'Your 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
|
|
}; |