merges master

This commit is contained in:
Sebastian Rindom
2020-05-28 14:37:49 +02:00
2 changed files with 57 additions and 37 deletions
@@ -25,58 +25,58 @@ describe("ProductService", () => {
}) })
describe("createSession", () => { describe("createSession", () => {
const createSession = jest.fn().mockReturnValue(Promise.resolve()) const createPayment = jest.fn().mockReturnValue(Promise.resolve())
const container = { const container = {
totalsService: TotalsService,
pp_default_provider: { pp_default_provider: {
createSession createPayment,
} },
} }
const providerService = new PaymentProviderService(container) const providerService = new PaymentProviderService(container)
it("successfully creates session", () => { it("successfully creates session", async () => {
await providerService.createSession("default_provider", { await providerService.createSession("default_provider", {
total: 100 total: 100,
}) })
expect(createSession).toBeCalledTimes(1) expect(createPayment).toBeCalledTimes(1)
expect(createSession).toBeCalledWith({ expect(createPayment).toBeCalledWith({
total: 100 total: 100,
}) })
}) })
}) })
describe("updateSession", () => { describe("updateSession", () => {
const createSession = jest.fn().mockReturnValue(Promise.resolve()) const updatePayment = jest.fn().mockReturnValue(Promise.resolve())
const updateSession = jest.fn().mockReturnValue(Promise.resolve())
const container = { const container = {
totalsService: TotalsService,
pp_default_provider: { pp_default_provider: {
createSession, updatePayment,
updateSession },
}
} }
const providerService = new PaymentProviderService(container) const providerService = new PaymentProviderService(container)
it("successfully creates session", () => { it("successfully creates session", async () => {
await providerService.createSession({ await providerService.updateSession(
provider_id: "default_provider", {
data: { provider_id: "default_provider",
id: "1234" data: {
id: "1234",
},
},
{
total: 100,
} }
}, { )
total: 100
})
expect(TotalsService.getTotal).toBeCalledTimes(1) expect(updatePayment).toBeCalledTimes(1)
expect(TotalsService.getTotal).toBeCalledWith({ expect(updatePayment).toBeCalledWith(
total: 100 { id: "1234" },
}) {
total: 100,
expect }
)
}) })
}) })
}) })
@@ -10,21 +10,41 @@ class PaymentProviderService {
} }
/** /**
* Handles incoming jobs. * Creates a payment session with the given provider.
* @param job {{ eventName: (string), data: (any) }} * @param {string} providerId - the id of the provider to create payment with
* eventName - the name of the event to process * @param {Cart} cart - a cart object used to calculate the amount, etc. from
* data - data to send to the subscriber * @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 * @returns {PaymentService} the payment provider
*/ */
retrieveProvider(provider_id) { retrieveProvider(providerId) {
try { try {
const provider = this.container_[`pp_${provider_id}`] const provider = this.container_[`pp_${providerId}`]
return provider return provider
} catch (err) { } catch (err) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.NOT_FOUND, MedusaError.Types.NOT_FOUND,
`Could not find a payment provider with id: ${provider_id}` `Could not find a payment provider with id: ${providerId}`
) )
} }
} }