Fixed merge conflics
This commit is contained in:
commit
e9bf18fc47
8 files changed
+120
-73
No files matched your search
@@ -6,5 +6,11 @@ node_modules
|
|||||||
!index.js
|
!index.js
|
||||||
!jest.config.js
|
!jest.config.js
|
||||||
|
|
||||||
dist
|
/dist
|
||||||
|
/api
|
||||||
|
/services
|
||||||
|
/models
|
||||||
|
/subscribers
|
||||||
|
/__mocks__
|
||||||
|
|
||||||
|
|
||||||
@@ -3,30 +3,29 @@ export default async (req, res) => {
|
|||||||
const { shipping_address, merchant_data } = req.body
|
const { shipping_address, merchant_data } = req.body
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const cartService = req.resolve("cartService")
|
const cartService = req.scope.resolve("cartService")
|
||||||
const klarnaProviderService = req.resolve("pp_klarna")
|
const klarnaProviderService = req.scope.resolve("pp_klarna")
|
||||||
|
|
||||||
const cart = await cartService.retrieve(merchant_data)
|
const cart = await cartService.retrieve(merchant_data)
|
||||||
|
|
||||||
if (shipping_address) {
|
if (shipping_address) {
|
||||||
const updatedAddress = {
|
const updatedAddress = {
|
||||||
email: shipping_address.email,
|
first_name: shipping_address.given_name,
|
||||||
street_address: shipping_address.address_1,
|
last_name: shipping_address.family_name,
|
||||||
street_address2: shipping_address.address_2,
|
address_1: shipping_address.street_address,
|
||||||
postal_code: shipping_address.postal_code,
|
address_2: shipping_address.street_address2,
|
||||||
city: shipping_address.city,
|
city: shipping_address.city,
|
||||||
country: shipping_address.country_code,
|
country_code: shipping_address.country,
|
||||||
|
postal_code: shipping_address.postal_code,
|
||||||
}
|
}
|
||||||
|
|
||||||
await cartService.update(cart._id, {
|
await cartService.updateShippingAddress(cart._id, updatedAddress)
|
||||||
email: shipping_address.email,
|
await cartService.updateBillingAddress(cart._id, updatedAddress)
|
||||||
shipping_address: updatedAddress,
|
await cartService.updateEmail(cart._id, shipping_address.email)
|
||||||
billing_address: updatedAddress,
|
|
||||||
})
|
|
||||||
|
|
||||||
// Fetch and return updated Klarna order
|
// Fetch and return updated Klarna order
|
||||||
const updatedCart = await cartService.retrieve(cart._id)
|
const updatedCart = await cartService.retrieve(cart._id)
|
||||||
const order = klarnaProviderService.cartToKlarnaOrder(updatedCart)
|
const order = await klarnaProviderService.cartToKlarnaOrder(updatedCart)
|
||||||
res.json(order)
|
res.json(order)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import middlewares from "../../middlewares"
|
|||||||
const route = Router()
|
const route = Router()
|
||||||
|
|
||||||
export default (app) => {
|
export default (app) => {
|
||||||
app.use("/hooks", route)
|
app.use("/klarna", route)
|
||||||
|
|
||||||
route.post("/shipping", middlewares.wrap(require("./shippping").default))
|
|
||||||
route.post("/address", middlewares.wrap(require("./address").default))
|
route.post("/address", middlewares.wrap(require("./address").default))
|
||||||
|
route.post("/shipping", middlewares.wrap(require("./shipping").default))
|
||||||
route.post("/push", middlewares.wrap(require("./push").default))
|
route.post("/push", middlewares.wrap(require("./push").default))
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ export default async (req, res) => {
|
|||||||
const { klarna_order_id } = req.query
|
const { klarna_order_id } = req.query
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const orderService = req.resolve("orderService")
|
const orderService = req.scope.resolve("orderService")
|
||||||
const klarnaProviderService = req.resolve("pp_klarna")
|
const klarnaProviderService = req.scope.resolve("pp_klarna")
|
||||||
|
|
||||||
const klarnaOrder = await klarnaProviderService.retrieveCompletedOrder(
|
const klarnaOrder = await klarnaProviderService.retrieveCompletedOrder(
|
||||||
klarna_order_id
|
klarna_order_id
|
||||||
|
|||||||
@@ -3,27 +3,24 @@ export default async (req, res) => {
|
|||||||
const { merchant_data, selected_shipping_option } = req.body
|
const { merchant_data, selected_shipping_option } = req.body
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const cartService = req.resolve("cartService")
|
const cartService = req.scope.resolve("cartService")
|
||||||
const klarnaProviderService = req.resolve("pp_klarna")
|
const klarnaProviderService = req.scope.resolve("pp_klarna")
|
||||||
|
const shippingProfileService = req.scope.resolve("shippingProfileService")
|
||||||
|
|
||||||
const cart = await cartService.retrieve(merchant_data)
|
const cart = await cartService.retrieve(merchant_data)
|
||||||
const updatedMethod = cart.shipping_options.find(
|
const shippingOptions = await shippingProfileService.fetchCartOptions(cart)
|
||||||
(so) => so._id === selected_shipping_option.id
|
|
||||||
)
|
|
||||||
|
|
||||||
if (updatedMethod) {
|
const option = shippingOptions.find(({ _id }) => _id === selected_shipping_option.id)
|
||||||
await cartService.update(cart._id, {
|
|
||||||
shipping_methods: [updatedMethod],
|
if (option) {
|
||||||
})
|
await cartService.addShippingMethod(cart._id, option._id, option.data)
|
||||||
|
|
||||||
// Fetch and return updated Klarna order
|
// Fetch and return updated Klarna order
|
||||||
const updatedCart = await cartService.retrieve(cart._id)
|
const updatedCart = await cartService.retrieve(cart._id)
|
||||||
const order = klarnaProviderService.cartToKlarnaOrder(updatedCart)
|
const order = klarnaProviderService.cartToKlarnaOrder(updatedCart)
|
||||||
res.json(order)
|
res.json(order)
|
||||||
return
|
|
||||||
} else {
|
} else {
|
||||||
res.sendStatus(400)
|
res.sendStatus(400)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ import { PaymentService } from "medusa-interfaces"
|
|||||||
class KlarnaProviderService extends PaymentService {
|
class KlarnaProviderService extends PaymentService {
|
||||||
static identifier = "klarna"
|
static identifier = "klarna"
|
||||||
|
|
||||||
constructor({ totalsService, regionService }, options) {
|
constructor(
|
||||||
|
{ shippingProfileService, totalsService, regionService },
|
||||||
|
options
|
||||||
|
) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
this.options_ = options
|
this.options_ = options
|
||||||
@@ -22,11 +25,14 @@ class KlarnaProviderService extends PaymentService {
|
|||||||
|
|
||||||
this.klarnaOrderManagementUrl_ = "/ordermanagement/v1/orders"
|
this.klarnaOrderManagementUrl_ = "/ordermanagement/v1/orders"
|
||||||
|
|
||||||
this.backendUrl_ = process.env.BACKEND_URL || ""
|
this.backendUrl_ =
|
||||||
|
process.env.BACKEND_URL || "https://58721b1f44d9.ngrok.io"
|
||||||
|
|
||||||
this.totalsService_ = totalsService
|
this.totalsService_ = totalsService
|
||||||
|
|
||||||
this.regionService_ = regionService
|
this.regionService_ = regionService
|
||||||
|
|
||||||
|
this.shippingProfileService_ = shippingProfileService
|
||||||
}
|
}
|
||||||
|
|
||||||
async lineItemsToOrderLines_(cart, taxRate) {
|
async lineItemsToOrderLines_(cart, taxRate) {
|
||||||
@@ -35,12 +41,16 @@ class KlarnaProviderService extends PaymentService {
|
|||||||
const discount = cart.discounts.find(
|
const discount = cart.discounts.find(
|
||||||
({ discount_rule }) => discount_rule.type !== "free_shipping"
|
({ discount_rule }) => discount_rule.type !== "free_shipping"
|
||||||
)
|
)
|
||||||
// If the discount has an item specific allocation method,
|
|
||||||
// we need to fetch the discount for each item
|
let itemDiscounts = []
|
||||||
const itemDiscounts = await this.totalsService_.getAllocationItemDiscounts(
|
if (discount) {
|
||||||
discount,
|
// If the discount has an item specific allocation method,
|
||||||
cart
|
// we need to fetch the discount for each item
|
||||||
)
|
itemDiscounts = await this.totalsService_.getAllocationItemDiscounts(
|
||||||
|
discount,
|
||||||
|
cart
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
cart.items.forEach((item) => {
|
cart.items.forEach((item) => {
|
||||||
// For bundles, we create an order line for each item in the bundle
|
// For bundles, we create an order line for each item in the bundle
|
||||||
@@ -50,20 +60,17 @@ class KlarnaProviderService extends PaymentService {
|
|||||||
const total_tax_amount = total_amount * taxRate
|
const total_tax_amount = total_amount * taxRate
|
||||||
|
|
||||||
order_lines.push({
|
order_lines.push({
|
||||||
|
name: item.title,
|
||||||
unit_price: c.unit_price,
|
unit_price: c.unit_price,
|
||||||
// Medusa does not allow discount on bundles
|
// Medusa does not allow discount on bundles
|
||||||
total_discount_amount: 0,
|
total_discount_amount: 0,
|
||||||
quantity: c.quantity,
|
quantity: c.quantity,
|
||||||
tax_rate: taxRate,
|
tax_rate: taxRate * 10000,
|
||||||
total_amount,
|
total_amount,
|
||||||
total_tax_amount,
|
total_tax_amount,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
const total_amount =
|
|
||||||
item.content.unit_price * item.content.quantity * (taxRate + 1)
|
|
||||||
const total_tax_amount = total_amount * taxRate
|
|
||||||
|
|
||||||
// Find the discount for current item and default to 0
|
// Find the discount for current item and default to 0
|
||||||
const itemDiscount =
|
const itemDiscount =
|
||||||
(itemDiscounts &&
|
(itemDiscounts &&
|
||||||
@@ -71,19 +78,38 @@ class KlarnaProviderService extends PaymentService {
|
|||||||
0
|
0
|
||||||
|
|
||||||
// Withdraw discount from the total item amount
|
// Withdraw discount from the total item amount
|
||||||
const total_discount_amount =
|
const quantity = item.content.quantity
|
||||||
total_amount - itemDiscount * (taxRate + 1)
|
const unit_price = item.content.unit_price * 100 * (taxRate + 1)
|
||||||
|
const total_discount_amount = itemDiscount * (taxRate + 1) * 100
|
||||||
|
const total_amount = unit_price * quantity - total_discount_amount
|
||||||
|
const total_tax_amount = total_amount * (taxRate / (1 + taxRate))
|
||||||
|
|
||||||
order_lines.push({
|
order_lines.push({
|
||||||
unit_price: item.content.unit_price,
|
name: item.title,
|
||||||
quantity: item.content.quantity,
|
tax_rate: taxRate * 10000,
|
||||||
|
quantity,
|
||||||
|
unit_price,
|
||||||
total_discount_amount,
|
total_discount_amount,
|
||||||
tax_rate: taxRate,
|
|
||||||
total_amount,
|
total_amount,
|
||||||
total_tax_amount,
|
total_tax_amount,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (cart.shipping_methods.length) {
|
||||||
|
const shippingMethod = cart.shipping_methods[0]
|
||||||
|
const price = shippingMethod.price
|
||||||
|
order_lines.push({
|
||||||
|
name: `${shippingMethod.name}`,
|
||||||
|
quantity: 1,
|
||||||
|
unit_price: price * (1 + taxRate) * 100,
|
||||||
|
tax_rate: taxRate * 10000,
|
||||||
|
total_amount: price * (1 + taxRate) * 100,
|
||||||
|
total_tax_amount: price * taxRate * 100,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return order_lines
|
||||||
}
|
}
|
||||||
|
|
||||||
async cartToKlarnaOrder(cart) {
|
async cartToKlarnaOrder(cart) {
|
||||||
@@ -98,9 +124,9 @@ class KlarnaProviderService extends PaymentService {
|
|||||||
cart.region_id
|
cart.region_id
|
||||||
)
|
)
|
||||||
|
|
||||||
order.order_lines = this.lineItemsToOrderLines_(cart, tax_rate)
|
order.order_lines = await this.lineItemsToOrderLines_(cart, tax_rate)
|
||||||
|
|
||||||
if (cart.billing_address) {
|
if (!_.isEmpty(cart.billing_address)) {
|
||||||
order.billing_address = {
|
order.billing_address = {
|
||||||
email: cart.email,
|
email: cart.email,
|
||||||
street_address: cart.billing_address.address_1,
|
street_address: cart.billing_address.address_1,
|
||||||
@@ -112,15 +138,15 @@ class KlarnaProviderService extends PaymentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Check if country matches ISO
|
// TODO: Check if country matches ISO
|
||||||
if (cart.billing_address.country) {
|
if (!_.isEmpty(cart.billing_address) && cart.billing_address.country) {
|
||||||
order.purchase_country = cart.billing_address.country
|
order.purchase_country = cart.billing_address.country
|
||||||
} else {
|
} else {
|
||||||
// Defaults to Sweden
|
// Defaults to Sweden
|
||||||
order.purchase_country = "SE"
|
order.purchase_country = "SE"
|
||||||
}
|
}
|
||||||
|
|
||||||
order.order_amount = this.totalsService_.getTotal(cart)
|
order.order_amount = (await this.totalsService_.getTotal(cart)) * 100
|
||||||
order.order_tax_amount = this.totalsService_.getTaxTotal(cart)
|
order.order_tax_amount = (await this.totalsService_.getTaxTotal(cart)) * 100
|
||||||
// TODO: Check if currency matches ISO
|
// TODO: Check if currency matches ISO
|
||||||
order.purchase_currency = currency_code
|
order.purchase_currency = currency_code
|
||||||
|
|
||||||
@@ -128,41 +154,43 @@ class KlarnaProviderService extends PaymentService {
|
|||||||
terms: this.options_.merchant_urls.terms,
|
terms: this.options_.merchant_urls.terms,
|
||||||
checkout: this.options_.merchant_urls.checkout,
|
checkout: this.options_.merchant_urls.checkout,
|
||||||
confirmation: this.options_.merchant_urls.confirmation,
|
confirmation: this.options_.merchant_urls.confirmation,
|
||||||
push: `${this.backendUrl_}/klarna/hooks/push`,
|
push: `${this.backendUrl_}/klarna/push`,
|
||||||
shipping_option_update: `${this.backendUrl_}/klarna/hooks/shipping`,
|
shipping_option_update: `${this.backendUrl_}/klarna/shipping`,
|
||||||
address_update: `${this.backendUrl_}/klarna/hooks/address`,
|
address_update: `${this.backendUrl_}/klarna/address`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const shippingOptions = await this.shippingProfileService_.fetchCartOptions(
|
||||||
|
cart
|
||||||
|
)
|
||||||
|
|
||||||
// If the cart does not have shipping methods yet, preselect one from
|
// If the cart does not have shipping methods yet, preselect one from
|
||||||
// shipping_options and set the selected shipping method
|
// shipping_options and set the selected shipping method
|
||||||
if (!cart.shipping_methods) {
|
if (!cart.shipping_methods.length) {
|
||||||
const shipping_method = cart.shipping_options[0]
|
const shipping_method = shippingOptions[0]
|
||||||
order.selected_shipping_option = {
|
order.selected_shipping_option = {
|
||||||
id: shipping_method._id,
|
id: shipping_method._id,
|
||||||
// TODO: Add shipping method name
|
name: shipping_method.name,
|
||||||
name: shipping_method.provider_id,
|
|
||||||
price: shipping_method.price * (1 + tax_rate) * 100,
|
price: shipping_method.price * (1 + tax_rate) * 100,
|
||||||
tax_amount: shipping_method.price * tax_rate * 100,
|
tax_amount: shipping_method.price * tax_rate * 100,
|
||||||
// Medusa tax rate of e.g. 0.25 (25%) needs to be 2500 in Klarna
|
// Medusa tax rate of e.g. 0.25 (25%) needs to be 2500 in Klarna
|
||||||
tax_rate: tax_rate * 10000,
|
tax_rate: tax_rate * 10000,
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
|
||||||
// If the cart does have shipping methods, set the selected shipping method
|
|
||||||
if (cart.shipping_methods) {
|
|
||||||
const shipping_method = cart.shipping_methods[0]
|
const shipping_method = cart.shipping_methods[0]
|
||||||
order.selected_shipping_option = {
|
order.selected_shipping_option = {
|
||||||
id: shipping_method._id,
|
id: shipping_method._id,
|
||||||
name: shipping_method.provider_id,
|
name: shipping_method.name,
|
||||||
price: shipping_method.price * (1 + tax_rate) * 100,
|
price: shipping_method.price * (1 + tax_rate) * 100,
|
||||||
tax_amount: shipping_method.price * tax_rate * 100,
|
tax_amount: shipping_method.price * tax_rate * 100,
|
||||||
tax_rate: tax_rate * 10000,
|
tax_rate: tax_rate * 10000,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
order.shipping_options = cart.shipping_options.map((so) => ({
|
// If the cart does have shipping methods, set the selected shipping method
|
||||||
|
|
||||||
|
order.shipping_options = shippingOptions.map((so) => ({
|
||||||
id: so._id,
|
id: so._id,
|
||||||
name: so.provider_id,
|
name: so.name,
|
||||||
price: so.price * (1 + tax_rate) * 100,
|
price: so.price * (1 + tax_rate) * 100,
|
||||||
tax_amount: so.price * tax_rate * 100,
|
tax_amount: so.price * tax_rate * 100,
|
||||||
tax_rate: tax_rate * 10000,
|
tax_rate: tax_rate * 10000,
|
||||||
@@ -203,7 +231,9 @@ class KlarnaProviderService extends PaymentService {
|
|||||||
async createPayment(cart) {
|
async createPayment(cart) {
|
||||||
try {
|
try {
|
||||||
const order = await this.cartToKlarnaOrder(cart)
|
const order = await this.cartToKlarnaOrder(cart)
|
||||||
return this.klarna_.post(this.klarnaOrderUrl__, order)
|
return this.klarna_
|
||||||
|
.post(this.klarnaOrderUrl_, order)
|
||||||
|
.then(({ data }) => data)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
@@ -282,13 +312,12 @@ class KlarnaProviderService extends PaymentService {
|
|||||||
* @param {Object} data - the update object
|
* @param {Object} data - the update object
|
||||||
* @returns {Object} updated order
|
* @returns {Object} updated order
|
||||||
*/
|
*/
|
||||||
async updatePayment(order, update) {
|
async updatePayment(paymentData, cart) {
|
||||||
try {
|
try {
|
||||||
const { data } = order.payment_method
|
const order = await this.cartToKlarnaOrder(cart)
|
||||||
return this.klarna_.post(
|
return this.klarna_
|
||||||
`${this.klarnaOrderUrl_}/${data.order_id}`,
|
.post(`${this.klarnaOrderUrl_}/${paymentData.order_id}`, order)
|
||||||
update
|
.then(({ data }) => data)
|
||||||
)
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import mongoose from "mongoose"
|
import mongoose from "mongoose"
|
||||||
|
|
||||||
export default new mongoose.Schema({
|
export default new mongoose.Schema({
|
||||||
|
name: { type: String, default: "" },
|
||||||
provider_id: { type: String, required: true },
|
provider_id: { type: String, required: true },
|
||||||
profile_id: { type: String, required: true },
|
profile_id: { type: String, required: true },
|
||||||
price: { type: Number, required: true },
|
price: { type: Number, required: true },
|
||||||
|
|||||||
@@ -968,6 +968,21 @@ class CartService extends BaseService {
|
|||||||
update.shipping_methods = []
|
update.shipping_methods = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//if (cart.items.length && cart.payment_sessions.length) {
|
||||||
|
// update.payment_sessions = await Promise.all(
|
||||||
|
// region.payment_providers.map(async pId => {
|
||||||
|
// const data = await this.paymentProviderService_.createSession(pId, {
|
||||||
|
// ...cart,
|
||||||
|
// ...update,
|
||||||
|
// })
|
||||||
|
// return {
|
||||||
|
// provider_id: pId,
|
||||||
|
// data,
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
// )
|
||||||
|
//}
|
||||||
|
|
||||||
// Payment methods are region specific so the user needs to find a
|
// Payment methods are region specific so the user needs to find a
|
||||||
// new payment method
|
// new payment method
|
||||||
if (!_.isEmpty(cart.payment_method)) {
|
if (!_.isEmpty(cart.payment_method)) {
|
||||||
|
|||||||
Reference in new issue
Block a user