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]
|
|
||||||
|
|
||||||
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)
|
res.sendStatus(200)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
|
|||||||
@@ -209,69 +209,60 @@ 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
|
return dbSession.withTransaction(async () => {
|
||||||
await 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"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
)
|
)
|
||||||
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
|
const { payment_method } = cart
|
||||||
if (paymentStatus !== "authorized") {
|
|
||||||
throw new MedusaError(
|
|
||||||
MedusaError.types.INVALID_ARGUMENT,
|
|
||||||
"Payment method is not authorized"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const o = {
|
const paymentProvider = await this.paymentProviderService_.retrieveProvider(
|
||||||
payment_method: cart.payment_method,
|
payment_method.provider_id
|
||||||
shipping_methods: cart.shipping_methods,
|
)
|
||||||
items: cart.items,
|
const paymentStatus = await paymentProvider.getStatus(payment_method.data)
|
||||||
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)
|
// If payment status is not authorized, we throw
|
||||||
// Commit transaction
|
if (paymentStatus !== "authorized") {
|
||||||
await dbSession.commitTransaction()
|
throw new MedusaError(
|
||||||
// Emit and return
|
MedusaError.types.INVALID_ARGUMENT,
|
||||||
this.eventBus_emit(OrderService.Events.PLACED, orderDocument)
|
"Payment method is not authorized"
|
||||||
return orderDocument
|
)
|
||||||
})
|
}
|
||||||
} catch (error) {
|
|
||||||
console.log(error)
|
const o = {
|
||||||
await dbSession.abortTransaction()
|
payment_method: cart.payment_method,
|
||||||
} finally {
|
shipping_methods: cart.shipping_methods,
|
||||||
await dbSession.endSession()
|
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
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user