chore(payment): Payment module DML (#10553)
* chore(payment): Payment module DML * rm log * migration
This commit is contained in:
committed by
GitHub
parent
91cd9aad47
commit
0264294ab5
@@ -11,6 +11,7 @@ import {
|
||||
FilterablePaymentProviderProps,
|
||||
FilterablePaymentSessionProps,
|
||||
FindConfig,
|
||||
InferEntityType,
|
||||
InternalModuleDeclaration,
|
||||
IPaymentModuleService,
|
||||
Logger,
|
||||
@@ -87,11 +88,21 @@ export default class PaymentModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
|
||||
protected paymentService_: ModulesSdkTypes.IMedusaInternalService<Payment>
|
||||
protected captureService_: ModulesSdkTypes.IMedusaInternalService<Capture>
|
||||
protected refundService_: ModulesSdkTypes.IMedusaInternalService<Refund>
|
||||
protected paymentSessionService_: ModulesSdkTypes.IMedusaInternalService<PaymentSession>
|
||||
protected paymentCollectionService_: ModulesSdkTypes.IMedusaInternalService<PaymentCollection>
|
||||
protected paymentService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
typeof Payment
|
||||
>
|
||||
protected captureService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
typeof Capture
|
||||
>
|
||||
protected refundService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
typeof Refund
|
||||
>
|
||||
protected paymentSessionService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
typeof PaymentSession
|
||||
>
|
||||
protected paymentCollectionService_: ModulesSdkTypes.IMedusaInternalService<
|
||||
typeof PaymentCollection
|
||||
>
|
||||
protected paymentProviderService_: PaymentProviderService
|
||||
|
||||
constructor(
|
||||
@@ -157,7 +168,7 @@ export default class PaymentModuleService
|
||||
async createPaymentCollections_(
|
||||
data: CreatePaymentCollectionDTO[],
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<PaymentCollection[]> {
|
||||
): Promise<InferEntityType<typeof PaymentCollection>[]> {
|
||||
return await this.paymentCollectionService_.create(data, sharedContext)
|
||||
}
|
||||
|
||||
@@ -218,7 +229,7 @@ export default class PaymentModuleService
|
||||
async updatePaymentCollections_(
|
||||
data: UpdatePaymentCollectionDTO[],
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<PaymentCollection[]> {
|
||||
): Promise<InferEntityType<typeof PaymentCollection>[]> {
|
||||
return await this.paymentCollectionService_.update(data, sharedContext)
|
||||
}
|
||||
|
||||
@@ -244,7 +255,8 @@ export default class PaymentModuleService
|
||||
(collection): collection is CreatePaymentCollectionDTO => !collection.id
|
||||
)
|
||||
|
||||
const operations: Promise<PaymentCollection[]>[] = []
|
||||
const operations: Promise<InferEntityType<typeof PaymentCollection>[]>[] =
|
||||
[]
|
||||
|
||||
if (forCreate.length) {
|
||||
operations.push(this.createPaymentCollections_(forCreate, sharedContext))
|
||||
@@ -300,7 +312,7 @@ export default class PaymentModuleService
|
||||
input: CreatePaymentSessionDTO,
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<PaymentSessionDTO> {
|
||||
let paymentSession: PaymentSession | undefined
|
||||
let paymentSession: InferEntityType<typeof PaymentSession> | undefined
|
||||
let providerPaymentSession: Record<string, unknown> | undefined
|
||||
|
||||
try {
|
||||
@@ -313,7 +325,7 @@ export default class PaymentModuleService
|
||||
providerPaymentSession = await this.paymentProviderService_.createSession(
|
||||
input.provider_id,
|
||||
{
|
||||
context: { ...input.context, session_id: paymentSession.id },
|
||||
context: { ...input.context, session_id: paymentSession!.id },
|
||||
amount: input.amount,
|
||||
currency_code: input.currency_code,
|
||||
}
|
||||
@@ -322,7 +334,7 @@ export default class PaymentModuleService
|
||||
paymentSession = (
|
||||
await this.paymentSessionService_.update(
|
||||
{
|
||||
id: paymentSession.id,
|
||||
id: paymentSession!.id,
|
||||
data: { ...input.data, ...providerPaymentSession },
|
||||
},
|
||||
sharedContext
|
||||
@@ -354,7 +366,7 @@ export default class PaymentModuleService
|
||||
paymentCollectionId: string,
|
||||
data: CreatePaymentSessionDTO,
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<PaymentSession> {
|
||||
): Promise<InferEntityType<typeof PaymentSession>> {
|
||||
const paymentSession = await this.paymentSessionService_.create(
|
||||
{
|
||||
payment_collection_id: paymentCollectionId,
|
||||
@@ -493,11 +505,11 @@ export default class PaymentModuleService
|
||||
|
||||
@InjectTransactionManager()
|
||||
async authorizePaymentSession_(
|
||||
session: PaymentSession,
|
||||
session: InferEntityType<typeof PaymentSession>,
|
||||
data: Record<string, unknown>,
|
||||
status: PaymentSessionStatus,
|
||||
@MedusaContext() sharedContext?: Context
|
||||
): Promise<Payment> {
|
||||
): Promise<InferEntityType<typeof Payment>> {
|
||||
let autoCapture = false
|
||||
if (status === PaymentSessionStatus.CAPTURED) {
|
||||
status = PaymentSessionStatus.AUTHORIZED
|
||||
@@ -620,9 +632,9 @@ export default class PaymentModuleService
|
||||
data: CreateCaptureDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<{
|
||||
payment: Payment
|
||||
payment: InferEntityType<typeof Payment>
|
||||
isFullyCaptured: boolean
|
||||
capture?: Capture
|
||||
capture?: InferEntityType<typeof Capture>
|
||||
}> {
|
||||
const payment = await this.paymentService_.retrieve(
|
||||
data.payment_id,
|
||||
@@ -659,10 +671,10 @@ export default class PaymentModuleService
|
||||
}
|
||||
|
||||
const capturedAmount = payment.captures.reduce((captureAmount, next) => {
|
||||
return MathBN.add(captureAmount, next.raw_amount)
|
||||
return MathBN.add(captureAmount, next.raw_amount as BigNumberInput)
|
||||
}, MathBN.convert(0))
|
||||
|
||||
const authorizedAmount = new BigNumber(payment.raw_amount)
|
||||
const authorizedAmount = new BigNumber(payment.raw_amount as BigNumberInput)
|
||||
const newCaptureAmount = new BigNumber(data.amount)
|
||||
const remainingToCapture = MathBN.sub(authorizedAmount, capturedAmount)
|
||||
|
||||
@@ -692,7 +704,7 @@ export default class PaymentModuleService
|
||||
}
|
||||
@InjectManager()
|
||||
private async capturePaymentFromProvider_(
|
||||
payment: Payment,
|
||||
payment: InferEntityType<typeof Payment>,
|
||||
isFullyCaptured: boolean,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
@@ -756,20 +768,20 @@ export default class PaymentModuleService
|
||||
|
||||
@InjectTransactionManager()
|
||||
private async refundPayment_(
|
||||
payment: Payment,
|
||||
payment: InferEntityType<typeof Payment>,
|
||||
data: CreateRefundDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<Refund> {
|
||||
): Promise<InferEntityType<typeof Refund>> {
|
||||
if (!data.amount) {
|
||||
data.amount = payment.amount as BigNumberInput
|
||||
}
|
||||
|
||||
const capturedAmount = payment.captures.reduce((captureAmount, next) => {
|
||||
const amountAsBigNumber = new BigNumber(next.raw_amount)
|
||||
const amountAsBigNumber = new BigNumber(next.raw_amount as BigNumberInput)
|
||||
return MathBN.add(captureAmount, amountAsBigNumber)
|
||||
}, MathBN.convert(0))
|
||||
const refundedAmount = payment.refunds.reduce((refundedAmount, next) => {
|
||||
return MathBN.add(refundedAmount, next.raw_amount)
|
||||
return MathBN.add(refundedAmount, next.raw_amount as BigNumberInput)
|
||||
}, MathBN.convert(0))
|
||||
|
||||
const totalRefundedAmount = MathBN.add(refundedAmount, data.amount)
|
||||
@@ -797,8 +809,8 @@ export default class PaymentModuleService
|
||||
|
||||
@InjectManager()
|
||||
private async refundPaymentFromProvider_(
|
||||
payment: Payment,
|
||||
refund: Refund,
|
||||
payment: InferEntityType<typeof Payment>,
|
||||
refund: InferEntityType<typeof Refund>,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
) {
|
||||
const paymentData = await this.paymentProviderService_.refundPayment(
|
||||
@@ -806,7 +818,7 @@ export default class PaymentModuleService
|
||||
data: payment.data!,
|
||||
provider_id: payment.provider_id,
|
||||
},
|
||||
refund.raw_amount
|
||||
refund.raw_amount as BigNumberInput
|
||||
)
|
||||
|
||||
await this.paymentService_.update(
|
||||
@@ -828,14 +840,6 @@ export default class PaymentModuleService
|
||||
sharedContext
|
||||
)
|
||||
|
||||
// TODO: revisit when totals are implemented
|
||||
// if (payment.captured_amount !== 0) {
|
||||
// throw new MedusaError(
|
||||
// MedusaError.Types.INVALID_DATA,
|
||||
// `Cannot cancel a payment: ${payment.id} that has been captured.`
|
||||
// )
|
||||
// }
|
||||
|
||||
await this.paymentProviderService_.cancelPayment({
|
||||
data: payment.data!,
|
||||
provider_id: payment.provider_id,
|
||||
|
||||
Reference in New Issue
Block a user