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(
|
const order = await this.klarna_.get(
|
||||||
`${this.klarnaOrderUrl_}/${order_id}`
|
`${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"
|
let status = "initial"
|
||||||
|
if (order.status === "AUTHORIZED") {
|
||||||
|
status = "authorized"
|
||||||
|
}
|
||||||
return status
|
return status
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error
|
throw error
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ class EconomicService extends BaseService {
|
|||||||
* customer_number_dk: 012
|
* customer_number_dk: 012
|
||||||
* customer_number_eu: 345
|
* customer_number_eu: 345
|
||||||
* customer_number_world: 678,
|
* customer_number_world: 678,
|
||||||
* vat_number: 42,
|
|
||||||
* unit_number: 42,
|
* unit_number: 42,
|
||||||
* payment_terms_number: 42,
|
* payment_terms_number: 42,
|
||||||
* shipping_product_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.
|
// At this point we can register the payment method.
|
||||||
return this.cartModel_
|
return this.cartModel_
|
||||||
.updateOne(
|
.updateOne(
|
||||||
|
|||||||
@@ -245,69 +245,76 @@ 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 () => {
|
||||||
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 = {
|
let paymentSession = cart.payment_sessions.find(
|
||||||
payment_method: cart.payment_method,
|
ps => ps.provider_id === payment_method.provider_id
|
||||||
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)
|
// Throw if payment method does not exist
|
||||||
// Commit transaction
|
if (!paymentSession) {
|
||||||
await dbSession.commitTransaction()
|
throw new MedusaError(
|
||||||
// Emit and return
|
MedusaError.Types.INVALID_ARGUMENT,
|
||||||
this.eventBus_emit(OrderService.Events.PLACED, orderDocument)
|
"Cart does not have an authorized payment session"
|
||||||
return orderDocument
|
)
|
||||||
})
|
}
|
||||||
} catch (error) {
|
|
||||||
console.log(error)
|
const paymentProvider = this.paymentProviderService_.retrieveProvider(
|
||||||
await dbSession.abortTransaction()
|
paymentSession.provider_id
|
||||||
} finally {
|
)
|
||||||
await dbSession.endSession()
|
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