-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathPaymentProcessor.java
More file actions
39 lines (32 loc) · 1.5 KB
/
PaymentProcessor.java
File metadata and controls
39 lines (32 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.payment;
import java.util.Objects;
public class PaymentProcessor {
private final PaymentGateway paymentGateway;
private final PaymentRepository paymentRepository;
private final PaymentEmailSender emailSender;
private final String apiKey;
private final String confirmationEmailTo;
public PaymentProcessor(PaymentGateway paymentGateway,
PaymentRepository paymentRepository,
PaymentEmailSender emailSender,
String apiKey,
String confirmationEmailTo) {
this.paymentGateway = Objects.requireNonNull(paymentGateway, "paymentGateway");
this.paymentRepository = Objects.requireNonNull(paymentRepository, "paymentRepository");
this.emailSender = Objects.requireNonNull(emailSender, "emailSender");
this.apiKey = Objects.requireNonNull(apiKey, "apiKey");
this.confirmationEmailTo = Objects.requireNonNull(confirmationEmailTo, "confirmationEmailTo");
}
public boolean processPayment(double amount) {
if (amount <= 0.0) {
throw new IllegalArgumentException("amount måste vara > 0");
}
PaymentChargeResponse response = paymentGateway.charge(apiKey, amount);
if (response.isSuccess()) {
paymentRepository.saveSuccessfulPayment(amount);
emailSender.sendPaymentConfirmation(confirmationEmailTo, amount);
return true;
}
return false;
}
}