Creates updateSession and createSession in PaymentProviderService (#70)
This commit is contained in:
1 parent
706ca8ac45
commit
3c7864dfca
2 files changed
+84
-8
No files matched your search
@@ -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.
|
* 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_.resolve(`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}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user