Adds gift card support
This commit is contained in:
@@ -10,15 +10,18 @@ export default async (req, res) => {
|
||||
const cart = await cartService.retrieve(merchant_data)
|
||||
const shippingOptions = await shippingProfileService.fetchCartOptions(cart)
|
||||
|
||||
const option = shippingOptions.find(({ _id }) => _id.equals(selected_shipping_option.id))
|
||||
const ids = selected_shipping_option.id.split(".")
|
||||
await Promise.all(ids.map(async id => {
|
||||
const option = shippingOptions.find(({ _id }) => _id.equals(id))
|
||||
|
||||
if (option) {
|
||||
const newCart = await cartService.addShippingMethod(cart._id, option._id, option.data)
|
||||
const order = await klarnaProviderService.cartToKlarnaOrder(newCart)
|
||||
res.json(order)
|
||||
} else {
|
||||
res.sendStatus(400)
|
||||
}
|
||||
if (option) {
|
||||
await cartService.addShippingMethod(cart._id, option._id, option.data)
|
||||
}
|
||||
}))
|
||||
|
||||
const newCart = await cartService.retrieve(cart._id)
|
||||
const order = await klarnaProviderService.cartToKlarnaOrder(newCart)
|
||||
res.json(order)
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ class KlarnaProviderService extends PaymentService {
|
||||
this.klarnaOrderManagementUrl_ = "/ordermanagement/v1/orders"
|
||||
|
||||
this.backendUrl_ =
|
||||
process.env.BACKEND_URL || "https://7e9a5bc2a2eb.ngrok.io"
|
||||
process.env.BACKEND_URL || "https://c8e1abe7d8b3.ngrok.io"
|
||||
|
||||
this.totalsService_ = totalsService
|
||||
|
||||
@@ -73,10 +73,14 @@ class KlarnaProviderService extends PaymentService {
|
||||
})
|
||||
|
||||
if (cart.shipping_methods.length) {
|
||||
const shippingMethod = cart.shipping_methods[0]
|
||||
const price = shippingMethod.price
|
||||
const { name, price } = cart.shipping_methods.reduce((acc, next) => {
|
||||
acc.name = [...acc.name, next.name]
|
||||
acc.price += next.price
|
||||
return acc
|
||||
}, { name: [], price: 0 })
|
||||
|
||||
order_lines.push({
|
||||
name: `${shippingMethod.name}`,
|
||||
name: name.join(" + "),
|
||||
quantity: 1,
|
||||
type: "shipping_fee",
|
||||
unit_price: price * (1 + taxRate) * 100,
|
||||
@@ -167,18 +171,42 @@ class KlarnaProviderService extends PaymentService {
|
||||
}
|
||||
}
|
||||
|
||||
// If the cart does have shipping methods, set the selected shipping method
|
||||
order.shipping_options = shippingOptions.map((so) => ({
|
||||
id: so._id,
|
||||
name: so.name,
|
||||
price: so.price * (1 + tax_rate) * 100,
|
||||
tax_amount: so.price * tax_rate * 100,
|
||||
tax_rate: tax_rate * 10000,
|
||||
preselected: shippingOptions.length === 1
|
||||
}))
|
||||
const partitioned = shippingOptions.reduce((acc, next) => {
|
||||
if (acc[next.profile_id]) {
|
||||
acc[next.profile_id] = [...acc[next.profile_id], next]
|
||||
} else {
|
||||
acc[next.profile_id] = [next]
|
||||
}
|
||||
return acc
|
||||
}, {})
|
||||
|
||||
let f = (a, b) => [].concat(...a.map(a => b.map(b => [].concat(a, b))))
|
||||
let cartesian = (a, b, ...c) => b ? cartesian(f(a, b), ...c) : a
|
||||
|
||||
const methods = Object.keys(partitioned).map(k => partitioned[k])
|
||||
const combinations = cartesian(...methods)
|
||||
|
||||
|
||||
order.shipping_options = combinations.map((combination) => {
|
||||
combination = Array.isArray(combination) ? combination : [combination]
|
||||
const details = combination.reduce((acc, next) => {
|
||||
acc.id = [...acc.id, next._id]
|
||||
acc.name = [...acc.name, next.name]
|
||||
acc.price += next.price
|
||||
return acc
|
||||
}, { id: [], name: [], price: 0 })
|
||||
|
||||
return {
|
||||
id: details.id.join("."),
|
||||
name: details.name.join(" + "),
|
||||
price: details.price * (1 + tax_rate) * 100,
|
||||
tax_amount: details.price * tax_rate * 100,
|
||||
tax_rate: tax_rate * 10000,
|
||||
preselected: combinations.length === 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
console.log(order)
|
||||
return order
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user