fix(payment): Idempotent cancellation and proper creationg fail handling (#10135)

RESOLVES SUP-188

**What**
Two changes are happening here
- In the stripe payment provider, idempotent cancellation action, if not id is provided then return the existing data unchanged
- Payment module should not try to cancel a session that have failed to be created in the first place
This commit is contained in:
Adrien de Peretti
2024-11-18 18:45:13 +01:00
committed by GitHub
parent 47ca1d4b54
commit b1b7a4abf1
4 changed files with 102 additions and 16 deletions

View File

@@ -14,6 +14,10 @@ moduleIntegrationTestRunner<IPaymentModuleService>({
moduleName: Modules.PAYMENT,
testSuite: ({ MikroOrmWrapper, service }) => {
describe("Payment Module Service", () => {
beforeEach(() => {
jest.clearAllMocks()
})
it(`should export the appropriate linkable configuration`, () => {
const linkable = Module(Modules.PAYMENT, {
service: PaymentModuleService,
@@ -395,7 +399,6 @@ moduleIntegrationTestRunner<IPaymentModuleService>({
customer: {},
billing_address: {},
email: "test@test.test.com",
resource_id: "cart_test",
},
})
@@ -422,6 +425,80 @@ moduleIntegrationTestRunner<IPaymentModuleService>({
})
)
})
it("should gracefully handle payment session creation fails from external provider", async () => {
jest
.spyOn((service as any).paymentProviderService_, "createSession")
.mockImplementationOnce(() => {
throw new Error("Create session error")
})
const deleteProviderSessionMock = jest.spyOn(
(service as any).paymentProviderService_,
"deleteSession"
)
const deletePaymentSessionMock = jest.spyOn(
(service as any).paymentSessionService_,
"delete"
)
const error = await service
.createPaymentSession("pay-col-id-1", {
provider_id: "pp_system_default",
amount: 200,
currency_code: "usd",
data: {},
context: {
extra: {},
customer: {},
billing_address: {},
email: "test@test.test.com",
},
})
.catch((e) => e)
expect(deleteProviderSessionMock).toHaveBeenCalledTimes(0)
expect(deletePaymentSessionMock).toHaveBeenCalledTimes(1)
expect(error.message).toEqual("Create session error")
})
it("should gracefully handle payment session creation fails from internal failure", async () => {
jest
.spyOn((service as any).paymentSessionService_, "update")
.mockImplementationOnce(() => {
throw new Error("Update session error")
})
const deleteProviderSessionMock = jest.spyOn(
(service as any).paymentProviderService_,
"deleteSession"
)
const deletePaymentSessionMock = jest.spyOn(
(service as any).paymentSessionService_,
"delete"
)
const error = await service
.createPaymentSession("pay-col-id-1", {
provider_id: "pp_system_default",
amount: 200,
currency_code: "usd",
data: {},
context: {
extra: {},
customer: {},
billing_address: {},
email: "test@test.test.com",
},
})
.catch((e) => e)
expect(deleteProviderSessionMock).toHaveBeenCalledTimes(1)
expect(deletePaymentSessionMock).toHaveBeenCalledTimes(1)
expect(error.message).toEqual("Update session error")
})
})
describe("update", () => {
@@ -436,7 +513,6 @@ moduleIntegrationTestRunner<IPaymentModuleService>({
customer: {},
billing_address: {},
email: "test@test.test.com",
resource_id: "cart_test",
},
})
@@ -446,7 +522,6 @@ moduleIntegrationTestRunner<IPaymentModuleService>({
currency_code: "eur",
data: {},
context: {
resource_id: "res_id",
extra: {},
customer: {},
billing_address: {},