From 3c7864dfcac81a4a1622cfd360aac4324a27c7d1 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Thu, 28 May 2020 14:36:13 +0200 Subject: [PATCH] Creates updateSession and createSession in PaymentProviderService (#70) --- .../services/__tests__/payment-provider.js | 56 +++++++++++++++++++ .../medusa/src/services/payment-provider.js | 36 +++++++++--- 2 files changed, 84 insertions(+), 8 deletions(-) diff --git a/packages/medusa/src/services/__tests__/payment-provider.js b/packages/medusa/src/services/__tests__/payment-provider.js index e1fb0a6bac..bf5d118fa7 100644 --- a/packages/medusa/src/services/__tests__/payment-provider.js +++ b/packages/medusa/src/services/__tests__/payment-provider.js @@ -26,4 +26,60 @@ describe("ProductService", () => { } }) }) + + describe("createSession", () => { + const createPayment = jest.fn().mockReturnValue(Promise.resolve()) + const container = { + pp_default_provider: { + createPayment, + }, + } + + const providerService = new PaymentProviderService(container) + + it("successfully creates session", async () => { + await providerService.createSession("default_provider", { + total: 100, + }) + + expect(createPayment).toBeCalledTimes(1) + expect(createPayment).toBeCalledWith({ + total: 100, + }) + }) + }) + + describe("updateSession", () => { + const updatePayment = jest.fn().mockReturnValue(Promise.resolve()) + + const container = { + pp_default_provider: { + updatePayment, + }, + } + + const providerService = new PaymentProviderService(container) + + it("successfully creates session", async () => { + await providerService.updateSession( + { + provider_id: "default_provider", + data: { + id: "1234", + }, + }, + { + total: 100, + } + ) + + 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 5e58221dbf..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_.resolve(`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}` ) } }