47 lines
1.8 KiB
JavaScript
47 lines
1.8 KiB
JavaScript
const { getCart, getRedirectUrl } = require("../lib/bigcommerceRestAPI")
|
|
const { bigcommerceStoreHash, bigcommerceBaseURL, bigCommerceCliendId, bigCommerceClientSecret } = require("../config")
|
|
const { getCustomerById, getLoginUrl } = require("../lib/bigcommerceRestAPI")
|
|
const { withAccount } = require("../lib/utils")
|
|
const { mapRestApiCartToGraphQL } = require("./helper")
|
|
;(function () {
|
|
if (context.user.auth()) return {}
|
|
const checkout = context.request().query("checkout")
|
|
const loggedInCheckout = context.request().query("loggedInCheckout")
|
|
if (checkout) {
|
|
if (loggedInCheckout) {
|
|
withAccount((loginClaims) => {
|
|
const cartId = context.request().param("id")
|
|
const cart = getCart(cartId)
|
|
const { checkoutURL } = getRedirectUrl(cartId)
|
|
const loginUrl = getLoginUrl(
|
|
loginClaims.bigCommerceId,
|
|
bigcommerceStoreHash,
|
|
bigcommerceBaseURL,
|
|
bigCommerceCliendId,
|
|
bigCommerceClientSecret,
|
|
checkoutURL.split(bigcommerceBaseURL).pop()
|
|
)
|
|
throw {
|
|
status: 200,
|
|
data: cart && {
|
|
cart: mapRestApiCartToGraphQL(cart),
|
|
checkoutURL: loginUrl,
|
|
},
|
|
}
|
|
})
|
|
}
|
|
}
|
|
const cartId = context.request().param("id")
|
|
const cart = getCart(cartId)
|
|
const { checkoutURL, embeddedCheckoutURL } = getRedirectUrl(cartId)
|
|
|
|
throw {
|
|
status: 200,
|
|
data: cart && {
|
|
cart: mapRestApiCartToGraphQL(cart),
|
|
checkoutURL: checkoutURL,
|
|
embeddedCheckoutURL: embeddedCheckoutURL,
|
|
},
|
|
}
|
|
})()
|