Creates updateSession and createSession in PaymentProviderService (#70)
This commit is contained in:
@@ -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,
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user