Klarna orders

This commit is contained in:
Sebastian Rindom
2020-07-10 15:41:42 +02:00
parent 85601ca428
commit d930d00723
6 changed files with 45 additions and 36 deletions

View File

@@ -3,30 +3,29 @@ export default async (req, res) => {
const { shipping_address, merchant_data } = req.body
try {
const cartService = req.resolve("cartService")
const klarnaProviderService = req.resolve("pp_klarna")
const cartService = req.scope.resolve("cartService")
const klarnaProviderService = req.scope.resolve("pp_klarna")
const cart = await cartService.retrieve(merchant_data)
if (shipping_address) {
const updatedAddress = {
email: shipping_address.email,
street_address: shipping_address.address_1,
street_address2: shipping_address.address_2,
postal_code: shipping_address.postal_code,
first_name: shipping_address.given_name,
last_name: shipping_address.family_name,
address_1: shipping_address.street_address,
address_2: shipping_address.street_address2,
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, {
email: shipping_address.email,
shipping_address: updatedAddress,
billing_address: updatedAddress,
})
await cartService.updateShippingAddress(cart._id, updatedAddress)
await cartService.updateBillingAddress(cart._id, updatedAddress)
await cartService.updateEmail(cart._id, shipping_address.email)
// Fetch and return updated Klarna order
const updatedCart = await cartService.retrieve(cart._id)
const order = klarnaProviderService.cartToKlarnaOrder(updatedCart)
const order = await klarnaProviderService.cartToKlarnaOrder(updatedCart)
res.json(order)
return
} else {

View File

@@ -4,10 +4,10 @@ import middlewares from "../../middlewares"
const route = Router()
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("/shipping", middlewares.wrap(require("./shipping").default))
route.post("/push", middlewares.wrap(require("./push").default))
return app

View File

@@ -2,8 +2,8 @@ export default async (req, res) => {
const { klarna_order_id } = req.query
try {
const orderService = req.resolve("orderService")
const klarnaProviderService = req.resolve("pp_klarna")
const orderService = req.scope.resolve("orderService")
const klarnaProviderService = req.scope.resolve("pp_klarna")
const klarnaOrder = await klarnaProviderService.retrieveCompletedOrder(
klarna_order_id

View File

@@ -3,27 +3,24 @@ export default async (req, res) => {
const { merchant_data, selected_shipping_option } = req.body
try {
const cartService = req.resolve("cartService")
const klarnaProviderService = req.resolve("pp_klarna")
const cartService = req.scope.resolve("cartService")
const klarnaProviderService = req.scope.resolve("pp_klarna")
const shippingProfileService = req.scope.resolve("shippingProfileService")
const cart = await cartService.retrieve(merchant_data)
const updatedMethod = cart.shipping_options.find(
(so) => so._id === selected_shipping_option.id
)
const shippingOptions = await shippingProfileService.fetchCartOptions(cart)
if (updatedMethod) {
await cartService.update(cart._id, {
shipping_methods: [updatedMethod],
})
const option = shippingOptions.find(({ _id }) => _id === selected_shipping_option.id)
if (option) {
await cartService.addShippingMethod(cart._id, option._id, option.data)
// Fetch and return updated Klarna order
const updatedCart = await cartService.retrieve(cart._id)
const order = klarnaProviderService.cartToKlarnaOrder(updatedCart)
res.json(order)
return
} else {
res.sendStatus(400)
return
}
} catch (error) {
throw error

View File

@@ -92,6 +92,19 @@ class KlarnaProviderService extends PaymentService {
}
})
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
}
@@ -137,9 +150,9 @@ class KlarnaProviderService extends PaymentService {
terms: this.options_.merchant_urls.terms,
checkout: this.options_.merchant_urls.checkout,
confirmation: this.options_.merchant_urls.confirmation,
push: `${this.backendUrl_}/klarna/hooks/push`,
shipping_option_update: `${this.backendUrl_}/klarna/hooks/shipping`,
address_update: `${this.backendUrl_}/klarna/hooks/address`,
push: `${this.backendUrl_}/klarna/push`,
shipping_option_update: `${this.backendUrl_}/klarna/shipping`,
address_update: `${this.backendUrl_}/klarna/address`,
}
@@ -151,8 +164,7 @@ class KlarnaProviderService extends PaymentService {
const shipping_method = shippingOptions[0]
order.selected_shipping_option = {
id: shipping_method._id,
// TODO: Add shipping method name
name: shipping_method.provider_id,
name: shipping_method.name,
price: shipping_method.price * (1 + 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
@@ -162,7 +174,7 @@ class KlarnaProviderService extends PaymentService {
const shipping_method = cart.shipping_methods[0]
order.selected_shipping_option = {
id: shipping_method._id,
name: shipping_method.provider_id,
name: shipping_method.name,
price: shipping_method.price * (1 + tax_rate) * 100,
tax_amount: shipping_method.price * tax_rate * 100,
tax_rate: tax_rate * 10000,
@@ -173,7 +185,7 @@ class KlarnaProviderService extends PaymentService {
order.shipping_options = shippingOptions.map((so) => ({
id: so._id,
name: so.provider_id,
name: so.name,
price: so.price * (1 + tax_rate) * 100,
tax_amount: so.price * tax_rate * 100,
tax_rate: tax_rate * 10000,

View File

@@ -1,6 +1,7 @@
import mongoose from "mongoose"
export default new mongoose.Schema({
name: { type: String, default: "" },
provider_id: { type: String, required: true },
profile_id: { type: String, required: true },
price: { type: Number, required: true },