Problem statement
Complete the implementation of ServiceCollectionExtensions and CheckoutService so that a checkout charges through the payment provider that belongs to the method it was handed.
Sample Data
Three implementations of IPaymentProvider ship with the repository, one per payment method. Each one names itself, carries an id fixed when it is built, and charges a fee of its own:
- CardPaymentProvider, named Card Network, charges 2.9% of the amount plus 0.30.
- WalletPaymentProvider, named Wallet Balance, charges 1.5% of the amount.
- BankTransferPaymentProvider, named Bank Transfer, charges a flat 1.25.
A fee is rounded to two decimal places, and the receipt a provider answers carries that provider's name and id, the amount, the fee and their sum.
Service
ServiceCollectionExtensions
The application calls AddPaymentProviders on its service collection at startup, before anything is resolved. Register all three implementations against IPaymentProvider, each one under its own method key:
- card reaches CardPaymentProvider, wallet reaches WalletPaymentProvider, and bank-transfer reaches BankTransferPaymentProvider.
- Register each of them once for the whole life of the application, so a provider is built once and every charge for its method runs through that one instance.
- A provider is reachable only under its own key. Asking for an IPaymentProvider without naming a key finds nothing, and asking for every IPaymentProvider at once finds nothing either.
CheckoutService
Checkout is handed a CheckoutRequest carrying a method and an amount, and answers a CheckoutResult holding either a receipt or an error:
- Treat the request's method as a key, take the provider registered under it, and charge it the request's amount. Return a result whose Receipt is the receipt that provider answered.
- Take that provider from the registrations the application already holds rather than building one, so the providerInstanceId on the receipt is the id of the registered provider.
- The key decides, not a list of names written into the lookup: whatever key the running application holds a provider under charges through that provider, including a key the three registrations above do not create.
- Match a key exactly, so CARD is not card and Bank-Transfer is not bank-transfer.
- A method that matches no key produces a result with no Receipt and an Error of Payment method '{method}' is not supported. Supported methods are bank-transfer, card, wallet., where {method} stands for the method that was asked for. The three supported methods are always named in that order, whatever was asked for.
POST /checkout is provided. It answers 200 with the receipt when the result carries one, and 400 with the error under a message key when it does not.
Sample Requests & Responses
POST /checkout
Request body:
{ "method": "card", "amount": 100.00 }
Response code: 200
{
"provider": "Card Network",
"providerInstanceId": "e7b70d3c-5e54-48ae-89e2-0a7df332eaa9",
"amount": 100.00,
"fee": 3.20,
"total": 103.20
}
The providerInstanceId is the id its provider was built with, so it is a different value from one run to the next.
The same amount through the other two methods also answers 200:
- wallet answers Wallet Balance, a fee of 1.50 and a total of 101.50.
- bank-transfer answers Bank Transfer, a fee of 1.25 and a total of 101.25.
Each method answers a providerInstanceId of its own, and answers the same one every time:
- Charging card again, for a different amount, reports the providerInstanceId shown above.
- Charging wallet reports a different one.
POST /checkout with a method nothing is registered under:
Request body:
{ "method": "crypto", "amount": 100.00 }
Response code: 400
{
"message": "Payment method 'crypto' is not supported. Supported methods are bank-transfer, card, wallet."
}
CARD, Wallet and Bank-Transfer are answered the same way, each message naming the method it was handed.
