Skip to main content

We are attempting to renovate our online reverse logistics process by creating an intuitive returns form which responds to customer inputs.

The form is hosted on Jotform and our ecommerce platform is Bigcommerce. Both pass their respective connection tests.

I have successfully pulled information from the Jotform forms fields, so that step works. I have also successfully ran related Bigcommerce tests for creating new blog posts.

However, when I run my test to retrieve specific order information based on customer-generated inputs, it throws a 403 Authentication Required error.

What have I overlooked?

export async function getOrderDetails({
storeHash,
clientId,
accessToken,
searchBy,
orderNumber,
postCode,
emailAddress
}: {
storeHash: string;
clientId: string;
accessToken: string;
searchBy: 'Order Number' | 'Email Address';
orderNumber?: number;
postCode: string;
emailAddress?: string;
}): Promise<any> {

// Helper function to fetch all orders
async function fetchAllOrders(storeHash: string, clientId: string, accessToken: string) {
const url = `https://api.bigcommerce.com/stores/${storeHash}/v3/orders`;
const response = await fetchWithZapier(url, {
headers: {
'Content-Type': 'application/json',
'X-Auth-Client': clientId,
'X-Auth-Token': accessToken
}
});
await response.throwErrorIfNotOk();
return response.json();
}

// Helper function to fetch order details by order ID
async function fetchOrderDetails(storeHash: string, orderId: number, clientId: string, accessToken: string) {
const url = `https://api.bigcommerce.com/stores/${storeHash}/v3/orders/${orderId}`;
const response = await fetchWithZapier(url, {
headers: {
'Content-Type': 'application/json',
'X-Auth-Client': clientId,
'X-Auth-Token': accessToken
}
});
await response.throwErrorIfNotOk();
return response.json();
}

// Helper function to fetch products of an order
async function fetchOrderProducts(storeHash: string, orderId: number, clientId: string, accessToken: string) {
const url = `https://api.bigcommerce.com/stores/${storeHash}/v3/orders/${orderId}/products`;
const response = await fetchWithZapier(url, {
headers: {
'Content-Type': 'application/json',
'X-Auth-Client': clientId,
'X-Auth-Token': accessToken
}
});
await response.throwErrorIfNotOk();
return response.json();
}


// Fetch all orders
const orders = await fetchAllOrders(storeHash, clientId, accessToken);

if (searchBy === 'Order Number' && orderNumber) {
// Find the order by order number
const order = orders.find((o: any) => o.order_number === orderNumber);
if (!order) {
throw new Error(`Order with number ${orderNumber} not found`);
}

// Fetch order details
const orderDetails = await fetchOrderDetails(storeHash, order.id, clientId, accessToken);

// Fetch order products
const orderProducts = await fetchOrderProducts(storeHash, order.id, clientId, accessToken);

// Calculate total quantity and subtotal
const totalQuantity = orderProducts.reduce((sum: number, product: any) => sum + product.quantity, 0);
const subtotal = orderProducts.reduce((sum: number, product: any) => sum + product.total_inc_tax, 0);

return {
orderId: order.id,
productDetails: orderProducts,
totalQuantity,
subtotal,
shippingCost: orderDetails.shippingCostTotal,
orderTotal: orderDetails.orderAmount,
dateShipped: orderDetails.isComplete ? new Date().toISOString() : null
};
} else if (searchBy === 'Email Address' && emailAddress) {
// Find orders by email address
const customerOrders = orders.filter((o: any) => o.customer_id === emailAddress);
if (customerOrders.length === 0) {
throw new Error(`No orders found for email address ${emailAddress}`);
}

// Return list of order IDs
return {
orderIds: customerOrders.map((o: any) => o.id)
};
} else {
throw new Error('Invalid search criteria');
}
}

 

Be the first to reply!

Reply