diff --git a/packages/medusa/src/services/__tests__/payment-provider.js b/packages/medusa/src/services/__tests__/payment-provider.js index 283009fad2..13b3d9d10c 100644 --- a/packages/medusa/src/services/__tests__/payment-provider.js +++ b/packages/medusa/src/services/__tests__/payment-provider.js @@ -25,58 +25,58 @@ describe("ProductService", () => { }) describe("createSession", () => { - const createSession = jest.fn().mockReturnValue(Promise.resolve()) + const createPayment = jest.fn().mockReturnValue(Promise.resolve()) const container = { - totalsService: TotalsService, pp_default_provider: { - createSession - } + createPayment, + }, } const providerService = new PaymentProviderService(container) - it("successfully creates session", () => { + it("successfully creates session", async () => { await providerService.createSession("default_provider", { - total: 100 + total: 100, }) - expect(createSession).toBeCalledTimes(1) - expect(createSession).toBeCalledWith({ - total: 100 + expect(createPayment).toBeCalledTimes(1) + expect(createPayment).toBeCalledWith({ + total: 100, }) }) }) describe("updateSession", () => { - const createSession = jest.fn().mockReturnValue(Promise.resolve()) - const updateSession = jest.fn().mockReturnValue(Promise.resolve()) + const updatePayment = jest.fn().mockReturnValue(Promise.resolve()) const container = { - totalsService: TotalsService, pp_default_provider: { - createSession, - updateSession - } + updatePayment, + }, } const providerService = new PaymentProviderService(container) - it("successfully creates session", () => { - await providerService.createSession({ - provider_id: "default_provider", - data: { - id: "1234" + it("successfully creates session", async () => { + await providerService.updateSession( + { + provider_id: "default_provider", + data: { + id: "1234", + }, + }, + { + total: 100, } - }, { - total: 100 - }) + ) - expect(TotalsService.getTotal).toBeCalledTimes(1) - expect(TotalsService.getTotal).toBeCalledWith({ - total: 100 - }) - - expect + expect(updatePayment).toBeCalledTimes(1) + expect(updatePayment).toBeCalledWith( + { id: "1234" }, + { + total: 100, + } + ) }) }) }) diff --git a/packages/medusa/src/services/payment-provider.js b/packages/medusa/src/services/payment-provider.js index a539b75d4e..d23fea7798 100644 --- a/packages/medusa/src/services/payment-provider.js +++ b/packages/medusa/src/services/payment-provider.js @@ -10,21 +10,41 @@ class PaymentProviderService { } /** - * Handles incoming jobs. - * @param job {{ eventName: (string), data: (any) }} - * eventName - the name of the event to process - * data - data to send to the subscriber - * + * Creates a payment session with the given provider. + * @param {string} providerId - the id of the provider to create payment with + * @param {Cart} cart - a cart object used to calculate the amount, etc. from + * @return {Promise} the payment session + */ + createSession(providerId, cart) { + const provider = this.retrieveProvider(providerId) + return provider.createPayment(cart) + } + + /** + * Updates an existing payment session. + * @param {PaymentSession} paymentSession - the payment session object to + * update + * @param {Cart} cart - the cart object to update for + * @return {Promise} the updated payment session + */ + updateSession(paymentSession, cart) { + const provider = this.retrieveProvider(paymentSession.provider_id) + return provider.updatePayment(paymentSession.data, cart) + } + + /** + * Finds a provider given an id + * @param {string} providerId - the id of the provider to get * @returns {PaymentService} the payment provider */ - retrieveProvider(provider_id) { + retrieveProvider(providerId) { try { - const provider = this.container_[`pp_${provider_id}`] + const provider = this.container_[`pp_${providerId}`] return provider } catch (err) { throw new MedusaError( MedusaError.Types.NOT_FOUND, - `Could not find a payment provider with id: ${provider_id}` + `Could not find a payment provider with id: ${providerId}` ) } }