37 lines
1.6 KiB
Java
37 lines
1.6 KiB
Java
package adapter;
|
|
/**
|
|
* The Adapter — bridges StripeClient to PaymentGateway.
|
|
*
|
|
* Object Adapter variant: holds a StripeClient instance via composition.
|
|
* This means it can wrap any StripeClient including subclasses.
|
|
*/
|
|
public class StripePaymentAdapter implements PaymentGateway {
|
|
private final StripeClient stripe;
|
|
public StripePaymentAdapter(StripeClient stripe) {
|
|
this.stripe = stripe;
|
|
}
|
|
@Override
|
|
public boolean charge(String customerId, double amount, String currency) {
|
|
// Translation 1: dollars → cents
|
|
long amountInCents = Math.round(amount * 100);
|
|
// Translation 2: charge() → createCharge(), lowercase currency
|
|
StripeClient.StripeChargeResult result =
|
|
stripe.createCharge(customerId, amountInCents, currency.toLowerCase());
|
|
// Translation 3: StripeChargeResult → boolean
|
|
return result.success;
|
|
}
|
|
@Override
|
|
public boolean refund(String transactionId, double amount) {
|
|
long amountInCents = Math.round(amount * 100);
|
|
// Translation: refund() → issueRefund(), your "transactionId" is Stripe's "chargeId"
|
|
return stripe.issueRefund(transactionId, amountInCents);
|
|
}
|
|
@Override
|
|
public String getStatus(String transactionId) {
|
|
// Translation: getStatus() → retrieveCharge(), StripeChargeResult → String
|
|
StripeClient.StripeChargeResult result = stripe.retrieveCharge(transactionId);
|
|
if (!result.success) return "FAILED";
|
|
return result.status != null ? result.status.toUpperCase() : "UNKNOWN";
|
|
}
|
|
}
|