Created
September 8, 2020 14:42
-
-
Save gje4/d297ace208b34ec87526020d33de03dd 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 checkoutBC(cartData) { | |
// https://api.bigcommerce.com/stores/{{store_hash}}/v3/carts/{{cart_id}}/redirect_urls | |
const options = { | |
method: 'POST', | |
uri: `https://api.bigcommerce.com/stores/q5fk0ac2gk/v3/carts/${cartData.data.id}/redirect_urls`, | |
headers: { | |
accept: 'application/json', | |
'X-Auth-Client': '', | |
'X-Auth-Token': '', | |
}, | |
} | |
var cartData = await request(options) | |
console.log('BC cart urls', cartData) | |
return cartData | |
} | |
async function addToCart(cartData) { | |
const options = { | |
method: 'POST', | |
uri: 'https://api.bigcommerce.com/stores/q5fk0ac2gk/v3/carts', | |
headers: { | |
accept: 'application/json', | |
'X-Auth-Client': '', | |
'X-Auth-Token': '', | |
}, | |
body: { | |
channel_id: cartData.channel_id, | |
line_items: cartData.line_items, | |
}, | |
json: true, | |
} | |
var cartData = await request(options) | |
console.log('BC cart', cartData) | |
return cartData | |
} | |
module.exports.cart = async event => { | |
let cartData = JSON.parse(event.body) | |
//Have to add it to cart first | |
console.log('cart data', cartData) | |
const cartItems = await addToCart(cartData) | |
console.log('cart created', cartItems) | |
const checkoutUrls = await checkoutBC(cartItems) | |
return { | |
statusCode: 200, | |
headers: { | |
'Access-Control-Allow-Credentials': true, | |
'Access-Control-Allow-Origin': '*', | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(checkoutUrls), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment