StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 2 · Configuration and Options

Payment Gateway Settings Module

easy100%1 of 1 solved

Problem statement

Use everything from this module in one app: layer the payment settings from files and environment variables, bind the nested gateway and retry sections to typed options, validate them at startup, and read them in a service.

Requirements

The PaymentsController, the DescribeGateway method and the settings files are already provided. Your task has four parts, in the files under PaymentGatewayApi: the configuration sources in Configuration/ConfigurationComposition.cs, validation attributes in Options/PaymentGatewayOptions.cs and Options/RetryPolicyOptions.cs, the registration in Configuration/PaymentGatewayOptionsRegistration.cs, and the constructor in Services/PaymentService.cs.

appsettings.json holds the section Payments:Gateway (ProviderName, BaseUrl, MerchantId, ApiKey, TimeoutSeconds), with the section Payments:Gateway:Retry (MaxAttempts, BackoffSeconds) inside it. appsettings.Sandbox.json sets only BaseUrl, to https://sandbox.northbridge-pay.com/v2.

ConfigurationComposition

Program removes every default source first. Add these, in this order:

  • appsettings.json
  • appsettings.{EnvironmentName}.json, marked optional
  • the environment variables, so that Payments__Gateway__ApiKey overrides the live API key from both files

PaymentGatewayOptions and RetryPolicyOptions

Add data annotations so that:

  • ProviderName and MerchantId are set
  • BaseUrl is a web address starting with http:// or https://
  • ApiKey is set and has at least 24 characters
  • TimeoutSeconds is from 5 to 120
  • MaxAttempts is from 1 to 5, and BackoffSeconds is from 1 to 60

PaymentGatewayOptionsRegistration

  • Register each of the two classes with AddOptions, bound to its own SectionName
  • Their data annotations are checked when the app starts, so an invalid setting stops the app with an error naming each invalid member

PaymentService

  • The constructor receives IOptions<PaymentGatewayOptions> and IOptions<RetryPolicyOptions> and keeps each Value in the gateway and retry fields
  • The service reads its settings only from the injected options, never from IConfiguration

PaymentsController (provided)

  • GET /payments/gateway answers 200 with the gateway profile. It reports apiKeyLength, never the key itself
Sample Requests & Responses

GET /payments/gateway when the environment is Development

Response (200 OK):

{
  "providerName": "Northbridge",
  "baseUrl": "https://api.northbridge-pay.com/v2",
  "merchantId": "MID-4471902",
  "apiKeyLength": 25,
  "timeoutSeconds": 30,
  "maxAttempts": 3,
  "backoffSeconds": 5
}

GET /payments/gateway when the environment is Sandbox and Payments__Gateway__ApiKey is set to a 28-character key

Response (200 OK), with baseUrl from the Sandbox file and apiKeyLength from the variable:

{
  "providerName": "Northbridge",
  "baseUrl": "https://sandbox.northbridge-pay.com/v2",
  "merchantId": "MID-4471902",
  "apiKeyLength": 28,
  "timeoutSeconds": 30,
  "maxAttempts": 3,
  "backoffSeconds": 5
}

Starting the app with Payments__Gateway__ApiKey set to "short" stops it before it serves any request:

Unhandled exception. Microsoft.Extensions.Options.OptionsValidationException: DataAnnotation validation failed for 'PaymentGatewayOptions' members: 'ApiKey' with the error: 'The field ApiKey must be a string or array type with a minimum length of '24'.'.

More in Configuration and Options

See the module
Typed Smtp SettingsThis problem covers binding a configuration section to a typed settings class with the options pattern, and receiving those settings in a service through IOptions.100%1 of 1 solvedeasyEnvironment Override PrecedenceThis problem covers layering configuration from a base file, an environment specific file and environment variables, so each setting comes from the highest source that defines it.100%1 of 1 solvedeasyFail-Fast Options ValidationThis problem covers adding validation attributes to an options class and checking them as the application starts, so a bad setting stops it with an error naming that setting.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.