diff --git a/packages/medusa-payment-stripe/package.json b/packages/medusa-payment-stripe/package.json index 2b56b01d41..63e05ba197 100644 --- a/packages/medusa-payment-stripe/package.json +++ b/packages/medusa-payment-stripe/package.json @@ -23,7 +23,8 @@ "client-sessions": "^0.8.0", "cross-env": "^5.2.1", "eslint": "^6.8.0", - "jest": "^25.5.2" + "jest": "^25.5.2", + "medusa-test-utils": "^0.3.0" }, "scripts": { "build": "babel src -d dist", @@ -41,4 +42,4 @@ "body-parser": "^1.19.0" }, "gitHead": "35e0930650d5f4aedf2610749cd131ae8b7e17cc" -} \ No newline at end of file +} diff --git a/packages/medusa-payment-stripe/src/services/__tests__/stripe-provider.js b/packages/medusa-payment-stripe/src/services/__tests__/stripe-provider.js index 0aedc1abf3..e7853d33aa 100644 --- a/packages/medusa-payment-stripe/src/services/__tests__/stripe-provider.js +++ b/packages/medusa-payment-stripe/src/services/__tests__/stripe-provider.js @@ -120,14 +120,10 @@ describe("StripeProviderService", () => { result = await stripeProviderService.updatePayment( { - payment_method: { - data: { - id: "pi_lebron", - }, - }, + id: "pi_lebron", }, { - amount: 1000, + total: 1000, } ) }) diff --git a/packages/medusa-payment-stripe/src/services/stripe-provider.js b/packages/medusa-payment-stripe/src/services/stripe-provider.js index 1b6b0455b1..330f306b6d 100644 --- a/packages/medusa-payment-stripe/src/services/stripe-provider.js +++ b/packages/medusa-payment-stripe/src/services/stripe-provider.js @@ -122,14 +122,17 @@ class StripeProviderService extends PaymentService { /** * Updates Stripe PaymentIntent. - * @param {string} cart - the cart to update payment intent for - * @param {Object} data - the update object for the payment intent + * @param {object} data - The payment session data. + * @param {Object} cart - the current cart value * @returns {Object} Stripe PaymentIntent */ - async updatePayment(cart, update) { + async updatePayment(data, cart) { try { - const { data } = cart.payment_method - return this.stripe_.paymentIntents.update(data.id, update) + const { id } = data + const amount = this.totalsService_.getTotal(cart) + return this.stripe_.paymentIntents.update(id, { + amount + }) } catch (error) { throw error } diff --git a/packages/medusa/src/services/__mocks__/totals.js b/packages/medusa/src/services/__mocks__/totals.js index ef7e7ca99e..f7a90dd474 100644 --- a/packages/medusa/src/services/__mocks__/totals.js +++ b/packages/medusa/src/services/__mocks__/totals.js @@ -1,6 +1,12 @@ import { IdMap } from "medusa-test-utils" export const TotalsServiceMock = { + getTotal: jest.fn().mockImplementation(cart => { + if (cart.total) { + return cart.total + } + return 0 + }), getSubtotal: jest.fn().mockImplementation(cart => { if (cart.subtotal) { return cart.subtotal diff --git a/packages/medusa/src/services/__tests__/payment-provider.js b/packages/medusa/src/services/__tests__/payment-provider.js index 9fbfcf374d..283009fad2 100644 --- a/packages/medusa/src/services/__tests__/payment-provider.js +++ b/packages/medusa/src/services/__tests__/payment-provider.js @@ -23,4 +23,60 @@ describe("ProductService", () => { } }) }) + + describe("createSession", () => { + const createSession = jest.fn().mockReturnValue(Promise.resolve()) + const container = { + totalsService: TotalsService, + pp_default_provider: { + createSession + } + } + + const providerService = new PaymentProviderService(container) + + it("successfully creates session", () => { + await providerService.createSession("default_provider", { + total: 100 + }) + + expect(createSession).toBeCalledTimes(1) + expect(createSession).toBeCalledWith({ + total: 100 + }) + }) + }) + + describe("updateSession", () => { + const createSession = jest.fn().mockReturnValue(Promise.resolve()) + const updateSession = jest.fn().mockReturnValue(Promise.resolve()) + + const container = { + totalsService: TotalsService, + pp_default_provider: { + createSession, + updateSession + } + } + + const providerService = new PaymentProviderService(container) + + it("successfully creates session", () => { + await providerService.createSession({ + provider_id: "default_provider", + data: { + id: "1234" + } + }, { + total: 100 + }) + + expect(TotalsService.getTotal).toBeCalledTimes(1) + expect(TotalsService.getTotal).toBeCalledWith({ + total: 100 + }) + + expect + }) + }) })