Last active
January 18, 2025 10:10
-
-
Save harmlessprince/234b54831b788e2e00391acd7c44532c to your computer and use it in GitHub Desktop.
MockPaymentGateway mockup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
public class PaymentProcessingService { | |
// Enum for Transaction Types | |
public enum TransactionType { | |
CREDIT_CARD, | |
BANK_TRANSFER, | |
DIGITAL_WALLET | |
} | |
// Process Payment Method | |
public String processPayment(TransactionType type, String accountId, double amount) { | |
if (amount <= 0) { | |
return "Invalid payment amount."; | |
} | |
// Basic Authentication Check | |
if (!authenticateUser(accountId)) { | |
return "Authentication failed."; | |
} | |
switch (type) { | |
case CREDIT_CARD: | |
return handleCreditCardPayment(accountId, amount); | |
case BANK_TRANSFER: | |
return handleBankTransfer(accountId, amount); | |
case DIGITAL_WALLET: | |
return handleDigitalWalletPayment(accountId, amount); | |
default: | |
return "Unsupported transaction type."; | |
} | |
} | |
// Mock Authentication | |
private boolean authenticateUser(String accountId) { | |
// Simulated check for account validity | |
return accountId != null && accountId.matches("\\w+"); | |
} | |
// Handle Credit Card Payment | |
private String handleCreditCardPayment(String accountId, double amount) { | |
// Mock API integration | |
String response = MockPaymentGateway.process("CREDIT_CARD", accountId, amount); | |
return response != null ? "Credit card payment successful: " + response : "Payment failed."; | |
} | |
// Handle Bank Transfer | |
private String handleBankTransfer(String accountId, double amount) { | |
// Mock API integration | |
String response = MockPaymentGateway.process("BANK_TRANSFER", accountId, amount); | |
return response != null ? "Bank transfer successful: " + response : "Payment failed."; | |
} | |
// Handle Digital Wallet Payment | |
private String handleDigitalWalletPayment(String accountId, double amount) { | |
// Mock API integration | |
String response = MockPaymentGateway.process("DIGITAL_WALLET", accountId, amount); | |
return response != null ? "Digital wallet payment successful: " + response : "Payment failed."; | |
} | |
public static void main(String[] args) { | |
PaymentProcessingService service = new PaymentProcessingService(); | |
System.out.println(service.processPayment(TransactionType.CREDIT_CARD, "user123", 150.00)); | |
System.out.println(service.processPayment(TransactionType.BANK_TRANSFER, "user456", 250.00)); | |
System.out.println(service.processPayment(TransactionType.DIGITAL_WALLET, "user789", 75.50)); | |
} | |
} | |
// Mock Payment Gateway | |
class MockPaymentGateway { | |
public static String process(String paymentType, String accountId, double amount) { | |
// Simulate successful response from payment gateway | |
return "Transaction ID: TXN-" + Math.random() * 10000; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test class for PaymentProcessingService | |
import org.junit.jupiter.api.Test; | |
import static org.junit.jupiter.api.Assertions.*; | |
public class PaymentProcessingServiceTest { | |
@Test | |
public void testCreditCardPayment() { | |
PaymentProcessor service = new PaymentProcessingService(); | |
String result = service.process(PaymentProcessingService.TransactionType.CREDIT_CARD, "user123", 150.00); | |
assertTrue(result.contains("Credit card payment successful")); | |
} | |
@Test | |
public void testBankTransferPayment() { | |
PaymentProcessor service = new PaymentProcessingService(); | |
String result = service.process(PaymentProcessingService.TransactionType.BANK_TRANSFER, "user456", 250.00); | |
assertTrue(result.contains("Bank transfer successful")); | |
} | |
@Test | |
public void testDigitalWalletPayment() { | |
PaymentProcessor service = new PaymentProcessingService(); | |
String result = service.process(PaymentProcessingService.TransactionType.DIGITAL_WALLET, "user789", 75.50); | |
assertTrue(result.contains("Digital wallet payment successful")); | |
} | |
@Test | |
public void testInvalidAmount() { | |
PaymentProcessor service = new PaymentProcessingService(); | |
String result = service.process(PaymentProcessingService.TransactionType.CREDIT_CARD, "user123", -50.00); | |
assertEquals("Invalid payment amount.", result); | |
} | |
@Test | |
public void testAuthenticationFailure() { | |
PaymentProcessor service = new PaymentProcessingService(); | |
String result = service.process(PaymentProcessingService.TransactionType.CREDIT_CARD, "", 150.00); | |
assertEquals("Authentication failed.", result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment