307. Payment Gateway Strategy Selected at Runtime
Asked in
Payment Gateway Strategy Selected at Runtime
Design a payment gateway that selects a payment strategy at runtime. The gateway must support UPI and bank-transfer payments.
Use the Strategy design pattern so that each payment method has its own processing logic. The gateway must select the requested strategy and delegate the payment to it.

Supported Payment Methods

  • UPI: paymentDetails contains a UPI ID such as "riya@okbank".
  • BANK_TRANSFER: paymentDetails contains an account number and IFSC code separated by a comma, such as "123456789012,HDFC0001234".

Method Signature

String processPayment(String paymentMethod, long amountInCents, String paymentDetails)
  • paymentMethod identifies the payment strategy to select.
  • amountInCents is the payment amount in cents.
  • paymentDetails contains the information required to process the payment.
  • Return the result produced by the selected payment strategy.

Output Format

For a successful payment, return SUCCESS,<paymentMethod>,<amountInCents>,<paymentDetails>.
If the payment method is unsupported, return FAILED,UNSUPPORTED_PAYMENT_METHOD,<paymentMethod>.
If any other payment details are invalid, return FAILED,INVALID_PAYMENT_DETAILS,<paymentMethod>.

Design Requirements

  • Use separate strategies for UPI and bank-transfer payments.
  • Select the required strategy using paymentMethod.
  • Keep payment-specific processing logic outside processPayment.
  • New payment methods should be addable without changing the existing payment strategies or the payment-processing flow.
  • The gateway does not need to contact a real payment provider.

Constraints

  • 1 ≤ amountInCents ≤ 1,000,000,000
  • 1 ≤ paymentMethod.length() ≤ 50
  • 1 ≤ paymentDetails.length() ≤ 200
  • paymentMethod and paymentDetails are never empty.
  • A valid UPI ID contains exactly one @ character, with at least one character before and after it.
  • Valid bank-transfer details contain a numeric account number followed by a comma and an 11-character IFSC code.

Examples

Example 1

Input: processPayment(paymentMethod = "UPI", amountInCents = 12500, paymentDetails = "riya@okbank")
Output: "SUCCESS,UPI,12500,riya@okbank"
Explanation: The gateway selects the UPI strategy to process the payment.

Example 2

Input: processPayment(paymentMethod = "BANK_TRANSFER", amountInCents = 84500, paymentDetails = "123456789012,HDFC0001234")
Output: "SUCCESS,BANK_TRANSFER,84500,123456789012,HDFC0001234"
Explanation: The gateway selects the bank-transfer strategy to process the payment.

Example 3

Input: processPayment(paymentMethod = "CARD", amountInCents = 25990, paymentDetails = "4111111111111111")
Output: "FAILED,UNSUPPORTED_PAYMENT_METHOD,CARD"
Explanation: No payment strategy is available for the CARD payment method.


Please use Laptop/Desktop or any other large screen to add/edit code.