Fixes the setting of a payment method
This commit is contained in:
@@ -213,9 +213,11 @@ class KlarnaProviderService extends PaymentService {
|
||||
const order = await this.klarna_.get(
|
||||
`${this.klarnaOrderUrl_}/${order_id}`
|
||||
)
|
||||
// TODO: Klarna docs does not provide a list of statues, so we need to
|
||||
// play around our selves to figure it out
|
||||
|
||||
let status = "initial"
|
||||
if (order.status === "AUTHORIZED") {
|
||||
status = "authorized"
|
||||
}
|
||||
return status
|
||||
} catch (error) {
|
||||
throw error
|
||||
|
||||
@@ -14,7 +14,6 @@ class EconomicService extends BaseService {
|
||||
* customer_number_dk: 012
|
||||
* customer_number_eu: 345
|
||||
* customer_number_world: 678,
|
||||
* vat_number: 42,
|
||||
* unit_number: 42,
|
||||
* payment_terms_number: 42,
|
||||
* shipping_product_number: 42,
|
||||
|
||||
@@ -736,20 +736,6 @@ class CartService extends BaseService {
|
||||
)
|
||||
}
|
||||
|
||||
// The provider service will be able to perform operations on the
|
||||
// session we are trying to set as the payment method.
|
||||
const provider = this.paymentProviderService_.retrieveProvider(
|
||||
paymentMethod.provider_id
|
||||
)
|
||||
|
||||
const status = await provider.getStatus(paymentMethod.data)
|
||||
if (!(status === "authorized" || status === "succeeded")) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
`The payment method was not authorized`
|
||||
)
|
||||
}
|
||||
|
||||
// At this point we can register the payment method.
|
||||
return this.cartModel_
|
||||
.updateOne(
|
||||
|
||||
@@ -245,69 +245,76 @@ 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
|
||||
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"
|
||||
)
|
||||
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,
|
||||
}
|
||||
let paymentSession = cart.payment_sessions.find(
|
||||
ps => ps.provider_id === payment_method.provider_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
|
||||
})
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
await dbSession.abortTransaction()
|
||||
} finally {
|
||||
await dbSession.endSession()
|
||||
}
|
||||
// Throw if payment method does not exist
|
||||
if (!paymentSession) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
"Cart does not have an authorized payment session"
|
||||
)
|
||||
}
|
||||
|
||||
const paymentProvider = this.paymentProviderService_.retrieveProvider(
|
||||
paymentSession.provider_id
|
||||
)
|
||||
const paymentStatus = await paymentProvider.getStatus(paymentSession.data)
|
||||
|
||||
// If payment status is not authorized, we throw
|
||||
if (paymentStatus !== "authorized") {
|
||||
throw new MedusaError(
|
||||
MedusaError.types.INVALID_ARGUMENT,
|
||||
"Payment method is not authorized"
|
||||
)
|
||||
}
|
||||
|
||||
paymentSession = this.paymentProviderService_.retrieveProvider(
|
||||
paymentSession.provider_id
|
||||
)
|
||||
|
||||
const o = {
|
||||
payment_method: paymentSession,
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user