feat(medusa): Update payment session management (#2937)
This commit is contained in:
@@ -1,28 +1,20 @@
|
||||
import EventBusService from "../services/event-bus"
|
||||
import { CartService, PaymentProviderService } from "../services"
|
||||
import { CartService } from "../services"
|
||||
import { EntityManager } from "typeorm"
|
||||
|
||||
type InjectedDependencies = {
|
||||
eventBusService: EventBusService
|
||||
cartService: CartService
|
||||
paymentProviderService: PaymentProviderService
|
||||
manager: EntityManager
|
||||
}
|
||||
|
||||
class CartSubscriber {
|
||||
protected readonly manager_: EntityManager
|
||||
protected readonly cartService_: CartService
|
||||
protected readonly paymentProviderService_: PaymentProviderService
|
||||
protected readonly eventBus_: EventBusService
|
||||
|
||||
constructor({
|
||||
manager,
|
||||
cartService,
|
||||
paymentProviderService,
|
||||
eventBusService,
|
||||
}: InjectedDependencies) {
|
||||
constructor({ manager, cartService, eventBusService }: InjectedDependencies) {
|
||||
this.cartService_ = cartService
|
||||
this.paymentProviderService_ = paymentProviderService
|
||||
this.eventBus_ = eventBusService
|
||||
this.manager_ = manager
|
||||
|
||||
@@ -38,30 +30,18 @@ class CartSubscriber {
|
||||
await this.manager_.transaction(
|
||||
"SERIALIZABLE",
|
||||
async (transactionManager) => {
|
||||
const cart = await this.cartService_
|
||||
.withTransaction(transactionManager)
|
||||
.retrieveWithTotals(cartId, {
|
||||
relations: [
|
||||
"billing_address",
|
||||
"region",
|
||||
"region.payment_providers",
|
||||
"payment_sessions",
|
||||
"customer",
|
||||
],
|
||||
})
|
||||
const cartServiceTx =
|
||||
this.cartService_.withTransaction(transactionManager)
|
||||
|
||||
const cart = await cartServiceTx.retrieve(cartId, {
|
||||
relations: ["payment_sessions"],
|
||||
})
|
||||
|
||||
if (!cart.payment_sessions?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const paymentProviderServiceTx =
|
||||
this.paymentProviderService_.withTransaction(transactionManager)
|
||||
|
||||
return await Promise.all(
|
||||
cart.payment_sessions.map(async (paymentSession) => {
|
||||
return paymentProviderServiceTx.updateSession(paymentSession, cart)
|
||||
})
|
||||
)
|
||||
return await cartServiceTx.setPaymentSessions(cart.id)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user