Last active
April 6, 2022 22:17
-
-
Save gje4/28ec093b1abdf848125a6e0d2bad72c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const request = require("request-promise"); | |
async function getOrderData(orderDataId) { | |
const options = { | |
method: "GET", | |
uri: `https://api.bigcommerce.com/stores/XXXX/v2/orders/${orderDataId}`, | |
headers: { | |
accept: "application/json", | |
"X-Auth-Client": "XXXX", | |
"X-Auth-Token": "XXXX" | |
} | |
}; | |
var orderData = await request(options); | |
console.log("order data", orderData); | |
return orderData; | |
} | |
async function sendOrderData(order) { | |
//Do not have access to the docs for Microsoft | |
// https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/dynamics-open-api | |
const options = { | |
method: "POST", | |
uri: `https://api.bigcommerce.com/stores/XXXX/v2/orders/${order.id}`, | |
headers: { | |
accept: "application/json", | |
"X-Auth-Client": "XXXXX", | |
"X-Auth-Token": "XXXX" | |
}, | |
body: [ | |
{ | |
//data fields | |
order_id: 4, | |
customer_id: 1231, | |
value: 99.99, | |
} | |
], | |
json: true | |
}; | |
var orderDataSent = await request(options); | |
return orderDataSent; | |
} | |
module.exports.OrderWebhooksHandler = async event => { | |
let orderData = JSON.parse(event.body); | |
//The webhook will have the order id, you can use that get all the data for the order | |
const allOrderData = await getOrderData(orderData); | |
//Once you have the data look to send to Order dyanmic. | |
//This is where you could have a function that does any data translation that needs to happen | |
const orderSent = await sendOrderData(allOrderData); | |
if (orderSent) | |
{ | |
return { | |
statusCode: 200, | |
headers: { | |
"Access-Control-Allow-Credentials": true, | |
"Access-Control-Allow-Origin": "*", | |
"Content-Type": "application/json" | |
}, | |
body: JSON.stringify("order data sent") | |
}; | |
} | |
else | |
{ | |
return { | |
statusCode: 400, | |
headers: { | |
"Access-Control-Allow-Credentials": true, | |
"Access-Control-Allow-Origin": "*", | |
"Content-Type": "application/json" | |
}, | |
body: JSON.stringify("issue with connector") | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment