Klarna orders

This commit is contained in:
Sebastian Rindom
2020-07-10 16:30:32 +02:00
parent d930d00723
commit b45fcae6fe
3 changed files with 66 additions and 61 deletions

View File

@@ -45,7 +45,6 @@ class BaseModel {
}
/**
* @private
*/
startSession() {
return this.mongooseModel_.startSession()

View File

@@ -1,7 +1,10 @@
import { MedusaError } from "medusa-core-utils"
export default async (req, res) => {
const { klarna_order_id } = req.query
try {
const cartService = req.scope.resolve("cartService")
const orderService = req.scope.resolve("orderService")
const klarnaProviderService = req.scope.resolve("pp_klarna")
@@ -10,9 +13,21 @@ export default async (req, res) => {
)
const cartId = klarnaOrder.merchant_data
const order = await orderService.list({ cart_id: cartId })[0]
await klarnaProviderService.acknowledgeOrder(klarnaOrder.id, order._id)
try {
const order = await orderService.retrieveByCartId(cartId)
await klarnaProviderService.acknowledgeOrder(klarnaOrder.id, order._id)
} catch (err) {
if (err.type === MeudsaError.Types.NOT_FOUND) {
let cart = await cartService.retrieve(cartId)
const method = cart.payment_sessions.find(p => p.provider_id === "klarna")
cart = await cartService.setPaymentMethod(cart._id, method)
const order = await orderService.createFromCart(cart)
await klarnaProviderService.acknowledgeOrder(klarnaOrder.id, order._id)
}
}
res.sendStatus(200)
} catch (error) {
throw error

View File

@@ -209,69 +209,60 @@ class OrderService extends BaseService {
// Create DB session for transaction
const dbSession = await this.orderModel_.startSession()
try {
// Initialize DB transaction
await dbSession.withTransaction(async () => {
// Check if order from cart already exists
// If so, this function throws
const exists = await this.existsByCartId(cart._id)
if (exists) {
throw new MedusaError(
MedusaError.types.INVALID_ARGUMENT,
"Order from cart already exists"
)
}
// Throw if payment method does not exist
if (!cart.payment_method) {
throw new MedusaError(
MedusaError.types.INVALID_ARGUMENT,
"Cart does not contain a payment method"
)
}
const { payment_method } = cart
const paymentProvider = await this.paymentProviderService_.retrieveProvider(
payment_method.provider_id
// Initialize DB transaction
return dbSession.withTransaction(async () => {
// Check if order from cart already exists
// If so, this function throws
const exists = await this.existsByCartId(cart._id)
if (exists) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Order from cart already exists"
)
const paymentStatus = await paymentProvider.getStatus(
payment_method.data
}
// Throw if payment method does not exist
if (!cart.payment_method) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Cart does not contain a payment method"
)
}
// If payment status is not authorized, we throw
if (paymentStatus !== "authorized") {
throw new MedusaError(
MedusaError.types.INVALID_ARGUMENT,
"Payment method is not authorized"
)
}
const { payment_method } = cart
const o = {
payment_method: cart.payment_method,
shipping_methods: cart.shipping_methods,
items: cart.items,
shipping_address: cart.shipping_address,
billing_address: cart.shipping_address,
region_id: cart.region_id,
email: cart.email,
customer_id: cart.customer_id,
cart_id: cart._id,
}
const paymentProvider = await this.paymentProviderService_.retrieveProvider(
payment_method.provider_id
)
const paymentStatus = await paymentProvider.getStatus(payment_method.data)
const orderDocument = await this.orderModel_.create(o)
// Commit transaction
await dbSession.commitTransaction()
// Emit and return
this.eventBus_emit(OrderService.Events.PLACED, orderDocument)
return orderDocument
})
} catch (error) {
console.log(error)
await dbSession.abortTransaction()
} finally {
await dbSession.endSession()
}
// If payment status is not authorized, we throw
if (paymentStatus !== "authorized") {
throw new MedusaError(
MedusaError.types.INVALID_ARGUMENT,
"Payment method is not authorized"
)
}
const o = {
payment_method: cart.payment_method,
shipping_methods: cart.shipping_methods,
items: cart.items,
shipping_address: cart.shipping_address,
billing_address: cart.shipping_address,
region_id: cart.region_id,
email: cart.email,
customer_id: cart.customer_id,
cart_id: cart._id,
}
const orderDocument = await this.orderModel_.create(o)
// Commit transaction
await dbSession.commitTransaction()
// Emit and return
this.eventBus_emit(OrderService.Events.PLACED, orderDocument)
return orderDocument
})
}
/**