Files
AluxPay/backend/services/woocommerce.js
matej a866801535 Added visualisation
Added return and cancel URL's
2025-09-28 02:29:07 +02:00

203 lines
6.0 KiB
JavaScript

const axios = require('axios');
const https = require('https');
class WooCommerceService {
constructor() {
this.baseURL = process.env.WOOCOMMERCE_URL;
this.consumerKey = process.env.WOOCOMMERCE_CONSUMER_KEY;
this.consumerSecret = process.env.WOOCOMMERCE_CONSUMER_SECRET;
if (!this.baseURL || !this.consumerKey || !this.consumerSecret) {
console.warn('WooCommerce credentials not fully configured');
}
// Create HTTPS agent that ignores SSL errors in development
const httpsAgent = new https.Agent({
rejectUnauthorized: process.env.NODE_ENV === 'production'
});
this.client = axios.create({
baseURL: `${this.baseURL}/wp-json/wc/v3`,
auth: {
username: this.consumerKey,
password: this.consumerSecret
},
timeout: 10000,
httpsAgent: httpsAgent,
// Additional options for SSL issues
headers: {
'User-Agent': 'PaymentWebsite/1.0'
}
});
}
// Get order details from WooCommerce
async getOrder(orderId) {
try {
const response = await this.client.get(`/orders/${orderId}`);
console.log('WooCommerce Order Retrieved:', {
id: response.data.id,
status: response.data.status,
total: response.data.total
});
return {
success: true,
order: response.data
};
} catch (error) {
console.error('WooCommerce Get Order Error:', {
orderId,
error: error.response?.data || error.message
});
return {
success: false,
error: error.response?.data?.message || error.message
};
}
}
// Update order status in WooCommerce
async updateOrderStatus(orderId, status, transactionId = null) {
try {
const updateData = {
status: status
};
// Add transaction ID if payment was successful
if (transactionId) {
updateData.transaction_id = transactionId;
updateData.meta_data = [
{
key: '_paypal_transaction_id',
value: transactionId
}
];
}
const response = await this.client.put(`/orders/${orderId}`, updateData);
console.log('WooCommerce Order Updated:', {
id: response.data.id,
status: response.data.status,
transaction_id: transactionId
});
return {
success: true,
order: response.data
};
} catch (error) {
console.error('WooCommerce Update Order Error:', {
orderId,
status,
error: error.response?.data || error.message
});
return {
success: false,
error: error.response?.data?.message || error.message
};
}
}
// Add order note
async addOrderNote(orderId, note, customerNote = false) {
try {
const response = await this.client.post(`/orders/${orderId}/notes`, {
note: note,
customer_note: customerNote
});
console.log('Order note added:', {
orderId,
noteId: response.data.id
});
return {
success: true,
note: response.data
};
} catch (error) {
console.error('Add Order Note Error:', {
orderId,
error: error.response?.data || error.message
});
return {
success: false,
error: error.response?.data?.message || error.message
};
}
}
// Process payment completion
async completePayment(orderId, paypalData) {
try {
// Update order to processing/completed
const updateResult = await this.updateOrderStatus(
orderId,
'processing', // or 'completed' based on your workflow
paypalData.transaction_id
);
if (!updateResult.success) {
throw new Error(updateResult.error);
}
// Add payment note
const noteText = `Payment completed via PayPal. Transaction ID: ${paypalData.transaction_id}. Payer Email: ${paypalData.payer?.email_address || 'N/A'}`;
await this.addOrderNote(orderId, noteText, false);
return {
success: true,
message: 'Payment completed and order updated'
};
} catch (error) {
console.error('Complete Payment Error:', {
orderId,
error: error.message
});
return {
success: false,
error: error.message
};
}
}
// Handle payment failure
async failPayment(orderId, reason) {
try {
// Update order to failed
const updateResult = await this.updateOrderStatus(orderId, 'failed');
if (!updateResult.success) {
throw new Error(updateResult.error);
}
// Add failure note
const noteText = `Payment failed: ${reason}`;
await this.addOrderNote(orderId, noteText, false);
return {
success: true,
message: 'Order marked as failed'
};
} catch (error) {
console.error('Fail Payment Error:', {
orderId,
error: error.message
});
return {
success: false,
error: error.message
};
}
}
}
module.exports = new WooCommerceService();