Fixes the setting of a payment method

This commit is contained in:
olivermrbl
2020-07-10 16:29:51 +02:00
parent e9bf18fc47
commit 9f17ab5861
4 changed files with 69 additions and 75 deletions
@@ -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,
-14
View File
@@ -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(
+21 -14
View File
@@ -245,7 +245,6 @@ 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
@@ -253,7 +252,7 @@ class OrderService extends BaseService {
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"
) )
} }
@@ -261,19 +260,29 @@ 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"
) )
} }
const { payment_method } = cart const { payment_method } = cart
const paymentProvider = await this.paymentProviderService_.retrieveProvider( let paymentSession = cart.payment_sessions.find(
payment_method.provider_id ps => ps.provider_id === payment_method.provider_id
) )
const paymentStatus = await paymentProvider.getStatus(
payment_method.data // 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 payment status is not authorized, we throw
if (paymentStatus !== "authorized") { if (paymentStatus !== "authorized") {
@@ -283,8 +292,12 @@ class OrderService extends BaseService {
) )
} }
paymentSession = this.paymentProviderService_.retrieveProvider(
paymentSession.provider_id
)
const o = { const o = {
payment_method: cart.payment_method, payment_method: paymentSession,
shipping_methods: cart.shipping_methods, shipping_methods: cart.shipping_methods,
items: cart.items, items: cart.items,
shipping_address: cart.shipping_address, shipping_address: cart.shipping_address,
@@ -302,12 +315,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()
}
} }
/** /**