Klarna orders
This commit is contained in:
@@ -45,7 +45,6 @@ class BaseModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
|
||||||
*/
|
*/
|
||||||
startSession() {
|
startSession() {
|
||||||
return this.mongooseModel_.startSession()
|
return this.mongooseModel_.startSession()
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
|
import { MedusaError } from "medusa-core-utils"
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const { klarna_order_id } = req.query
|
const { klarna_order_id } = req.query
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const cartService = req.scope.resolve("cartService")
|
||||||
const orderService = req.scope.resolve("orderService")
|
const orderService = req.scope.resolve("orderService")
|
||||||
const klarnaProviderService = req.scope.resolve("pp_klarna")
|
const klarnaProviderService = req.scope.resolve("pp_klarna")
|
||||||
|
|
||||||
@@ -10,9 +13,21 @@ export default async (req, res) => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
const cartId = klarnaOrder.merchant_data
|
const cartId = klarnaOrder.merchant_data
|
||||||
const order = await orderService.list({ cart_id: cartId })[0]
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
const order = await orderService.retrieveByCartId(cartId)
|
||||||
await klarnaProviderService.acknowledgeOrder(klarnaOrder.id, order._id)
|
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)
|
res.sendStatus(200)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
|
|||||||
@@ -209,15 +209,14 @@ class OrderService extends BaseService {
|
|||||||
// Create DB session for transaction
|
// Create DB session for transaction
|
||||||
const dbSession = await this.orderModel_.startSession()
|
const dbSession = await this.orderModel_.startSession()
|
||||||
|
|
||||||
try {
|
|
||||||
// Initialize DB transaction
|
// Initialize DB transaction
|
||||||
await dbSession.withTransaction(async () => {
|
return dbSession.withTransaction(async () => {
|
||||||
// Check if order from cart already exists
|
// Check if order from cart already exists
|
||||||
// If so, this function throws
|
// If so, this function throws
|
||||||
const exists = await this.existsByCartId(cart._id)
|
const exists = await this.existsByCartId(cart._id)
|
||||||
if (exists) {
|
if (exists) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.types.INVALID_ARGUMENT,
|
MedusaError.Types.INVALID_ARGUMENT,
|
||||||
"Order from cart already exists"
|
"Order from cart already exists"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -225,7 +224,7 @@ class OrderService extends BaseService {
|
|||||||
// Throw if payment method does not exist
|
// Throw if payment method does not exist
|
||||||
if (!cart.payment_method) {
|
if (!cart.payment_method) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.types.INVALID_ARGUMENT,
|
MedusaError.Types.INVALID_ARGUMENT,
|
||||||
"Cart does not contain a payment method"
|
"Cart does not contain a payment method"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -235,9 +234,7 @@ class OrderService extends BaseService {
|
|||||||
const paymentProvider = await this.paymentProviderService_.retrieveProvider(
|
const paymentProvider = await this.paymentProviderService_.retrieveProvider(
|
||||||
payment_method.provider_id
|
payment_method.provider_id
|
||||||
)
|
)
|
||||||
const paymentStatus = await paymentProvider.getStatus(
|
const paymentStatus = await paymentProvider.getStatus(payment_method.data)
|
||||||
payment_method.data
|
|
||||||
)
|
|
||||||
|
|
||||||
// If payment status is not authorized, we throw
|
// If payment status is not authorized, we throw
|
||||||
if (paymentStatus !== "authorized") {
|
if (paymentStatus !== "authorized") {
|
||||||
@@ -266,12 +263,6 @@ class OrderService extends BaseService {
|
|||||||
this.eventBus_emit(OrderService.Events.PLACED, orderDocument)
|
this.eventBus_emit(OrderService.Events.PLACED, orderDocument)
|
||||||
return orderDocument
|
return orderDocument
|
||||||
})
|
})
|
||||||
} catch (error) {
|
|
||||||
console.log(error)
|
|
||||||
await dbSession.abortTransaction()
|
|
||||||
} finally {
|
|
||||||
await dbSession.endSession()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user