@stylusapparel/opv3-merchant-api-nodejs
Version:
This is the official NodeJs wrapper for connecting to the StylusOP API V3
134 lines (125 loc) • 3.67 kB
JavaScript
const stylusWrapper = require('../index.js');
const { MERCHANT_ID, MERCHANT_NAME, MERCHANT_SECRET, LIMIT_ORDERS } = require('./constants');
const stylusClient = stylusWrapper.createClient(MERCHANT_ID, MERCHANT_SECRET, {
merchantName: MERCHANT_NAME, // Replace with your merchant name / ID
sandbox: true,
apiVersion: 'v3',
tokenType: 'basic',
});
stylusClient
.oauthToken()
.then((accessToken) => {
console.log(`Authenticated with token: ${accessToken}`);
})
.catch((error) => {
console.error('Error getting the access token: ', error);
handleError(error);
})
.then( () => {
console.log('*** Sleeping for 10 seconds and then verifying the token - The Oauth access token is valid for 1 hour ***');
return sleep(10);
})
.then( () => stylusClient.verifyToken() )
.then(listOrders)
.then((orders) => {
const order = orders.pop();
const { orderKey, priority, shippingInfo: { shipTo } } = order;
console.log('*** Order details: ***');
console.log(`Order: ${orderKey} - Priority: ${priority}`);
console.log('Ship To: ');
console.table(shipTo);
return orderKey;
})
.then( async orderKey => {
console.log('Update order priority...');
await updateOrderPriority(orderKey, 'qa');
console.log('Successfully updated order priority!');
return orderKey;
})
.then(async (orderKey) => {
console.log('Update order shipping info...');
const newShippingInfo = {
shipTo: {
name: 'Samuel',
street1: '4564 Steet 12',
street2: 'Apt 345',
city: 'Dallas',
state: 'TX',
country: 'US',
postcode: '75201',
phone: '214-433-234',
email: 'test@stylus.com',
},
returnTo: {
name: 'John Doe',
company: 'Stylus',
street1: '1234 Main St',
street2: 'Apt 101',
city: 'Dallas',
state: 'TX',
country: 'US',
postcode: '75201',
phone: '214-555-1234',
email: 'admin@stylus.com',
},
}
await updateOrderShippingInfo(orderKey, newShippingInfo);
console.log('Successfully updated order shipping info!');
return orderKey;
})
.catch((error) => {
console.error('Error updating order: ', error);
handleError(error);
})
.then((orderKey) => {
console.log('Getting order details...');
return getOrder(orderKey);
})
.catch((error) => {
console.error('Error getting order details: ', error);
handleError(error);
})
.then((order) => {
const { orderKey, priority, shippingInfo: { shipTo } } = order;
console.log('*** Updated order details: ***');
console.log(`Order: ${orderKey} - Priority: ${priority}`);
console.log('Ship To: ');
console.table(shipTo);
})
// Sleep for a given number of seconds //
function sleep(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
// Get order... //
function getOrder(orderKey) {
return stylusClient.orders.get(orderKey)
}
// List orders... //
function listOrders() {
return stylusClient.orders.list({ limit: LIMIT_ORDERS }).then(({ orders }) => {
return orders;
});
}
// update order
function updateOrderPriority(orderKey, newPriority) {
return stylusClient.orders.update(orderKey, {
priority: newPriority,
});
}
function updateOrderShippingInfo(orderKey, newShippingInfo) {
return stylusClient.orders.update(orderKey, {
shippingInfo: newShippingInfo,
});
}
// Error handler //
function handleError(err) {
// Handle error //
if (err.status === 403 || err.status === 401 || err.errorCode === 'TOKEN_INVALID') {
// Unauthorized
console.error(`Error!\n(${err.errorCode}) ${err.message} ${JSON.stringify(err.rawError)}`);
} else {
// Other error
console.error(`Error!\n(${err.errorCode}) ${err.message}`);
}
process.exit(1);
}