@stylusapparel/opv3-merchant-api-nodejs
Version:
This is the official NodeJs wrapper for connecting to the StylusOP API V3
148 lines (135 loc) • 4.83 kB
JavaScript
const stylusWrapper = require('../index.js');
const { MERCHANT_ID, MERCHANT_NAME, MERCHANT_SECRET, LIMIT_ORDERS } = require('./constants');
// Create the client
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(`Access token: ${accessToken}`);
console.log('Client is authenticated:', stylusClient.isAuthenticated());
})
.catch(error => {
console.error(`Error getting 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() )
.catch(error => {
console.error(`Error verifying the access token: ${error}`);
handleError(error)
})
.then( tokenVerified => {
console.log('Token is valid! \nToken verification response:', tokenVerified);
console.log('Client is authenticated:', stylusClient.isAuthenticated());
// Handle your token valid logic here
// In this example, we'll list the merchant's orders
console.log(`Proceed to list the last ${LIMIT_ORDERS} orders...`);
return listOrders();
})
.catch(error => {
console.error(`Error getting the orders: ${error}`);
handleError(error)
})
.then(orders => {
console.log('Getting details of the first order...')
if (orders.length > 0) {
return getOrderDetails(orders.pop().orderKey); // You can also use order.orderId
} else {
console.error('No orders found');
return null;
}
})
.catch(error => {
console.error(`Error getting the order status: ${error}`);
handleError(error)
})
// .then(orderDetails => {
// const orderId = orderDetails.orderKey;
// console.log('Getting activities of the first order...')
// return getOrderActivity(orderId);
// })
// .catch(error => {
// console.error(`Error getting the order activities: ${error}`);
// handleError(error)
// })
.then( () => {
console.log('*** Sleeping for 10 seconds before listing the products ***');
return sleep(10);
})
.then( listProducts )
.then(products => {
console.log('Getting the product details...')
return getProductDetails(products.pop().productId);
})
.then(productDetails => {
console.log('Getting the product pricing...')
return getProductPricing(productDetails.productId);
})
// 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)
}
// Sleep for a given number of seconds //
function sleep(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
// List orders... //
function listOrders() {
return stylusClient.orders.list({ limit: LIMIT_ORDERS }).then(({orders}) => {
console.log('Retrieved orders: ', orders);
return orders;
});
}
// Get an order's details //
function getOrderDetails(orderId) {
return stylusClient.orders.get(orderId).then((order) => {
const { status, ...otherDetails } = order;
console.log("Order's status: ", status);
console.log("Order's other details: ", otherDetails);
return order;
});
}
// Get an order's activity log //
function getOrderActivity(orderId) {
return stylusClient.orders.activities(orderId).then((activity) => {
console.log("Order's activty: ", activity);
return activity;
});
}
// List products //
function listProducts() {
return stylusClient.products.list().then((products) => {
console.log('Found ', products.length, ' products');
console.log('Retrieved products: ', products);
return products;
});
}
// Get Product Details //
function getProductDetails(productId) {
return stylusClient.products.get(productId).then((productDetails) => {
console.log('Retrieved product details: ', productDetails);
return productDetails;
});
}
// Get Product Pricing //
function getProductPricing(productId) {
return stylusClient.products.pricing(productId).then((productPricing) => {
console.log('Retrieved product pricing: ', productPricing);
return productPricing;
});
}