beerbay-math
Version:
87 lines (72 loc) • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.cartToLineItems = exports.fromDecimalToInteger = void 0;
var _coupon = require("./coupon");
var _products = require("./products");
var _cart = require("./cart");
var _shipping = require("./shipping");
/**
* Given a decimal amount return the version in cents
* @param {*} amount
*/
var fromDecimalToInteger = function fromDecimalToInteger(amount) {
return parseInt(amount * 100);
};
/**
* Given the array of products return the highest priced one.
* @param {*} arr
*/
exports.fromDecimalToInteger = fromDecimalToInteger;
var getHighestPricedItem = function getHighestPricedItem(arr) {
var highestPrice = Math.max.apply(Math, arr.map(function (product) {
return product.price_data.unit_amount;
}));
return arr.find(function (product) {
return product.price_data.unit_amount === highestPrice;
});
};
/**
* Serialize the Cart so Stripe can parse it
* Also adds shipping as Cart item so people know (and total is proper)
* @param {*} cart
* @param {*} selectedCountry
*/
var cartToLineItems = function cartToLineItems(cart, selectedCountry, coupon, user) {
var freeShipping = (0, _coupon.isFreeShipping)(cart, selectedCountry, coupon);
var couponValue = (0, _coupon.getCouponValue)(cart, selectedCountry, coupon);
var lineItems = cart.map(function (product) {
return {
price_data: {
currency: "eur",
product_data: {
name: product.name
},
unit_amount: fromDecimalToInteger((0, _products.getProductPrice)(product))
},
quantity: (0, _products.getProductQuantity)(product)
};
});
if (couponValue && !freeShipping) {
lineItems.forEach(function (product) {
var splitValue = Math.round(couponValue / (0, _cart.getCartPrice)(cart) * product.price_data.unit_amount);
product.price_data.unit_amount -= splitValue;
});
}
if (!freeShipping) {
var shipping = (0, _shipping.getShippingFinal)(cart, selectedCountry);
lineItems.push({
price_data: {
currency: "eur",
product_data: {
name: "Shipping Fees"
},
unit_amount: fromDecimalToInteger(shipping)
},
quantity: 1
});
}
return lineItems;
};
exports.cartToLineItems = cartToLineItems;