Files
design-patterns/02-structural/adapter/StripePaymentAdapter.java

45 lines
1.6 KiB
Java

package adapter;
/**
* The Adapter — bridges StripeClient (Adaptee) to PaymentGateway (Target).
*
* This is the Object Adapter variant: it holds a StripeClient instance
* via composition (not inheritance), so it can adapt any StripeClient
* including subclasses.
*
* The key responsibility: translate YOUR interface's methods into
* calls that Stripe understands — data conversion included.
*/
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: your code uses decimal dollars; Stripe wants integer cents
long amountInCents = Math.round(amount * 100);
StripeClient.StripeChargeResult result =
stripe.createCharge(customerId, amountInCents, currency.toLowerCase());
return result.success;
}
@Override
public boolean refund(String transactionId, double amount) {
// Translation: your "transactionId" is Stripe's "chargeId"
long amountInCents = Math.round(amount * 100);
return stripe.issueRefund(transactionId, amountInCents);
}
@Override
public String getStatus(String transactionId) {
// Translation: map Stripe's object to your simple status string
StripeClient.StripeChargeResult result = stripe.retrieveCharge(transactionId);
if (!result.success) return "FAILED";
return result.status != null ? result.status.toUpperCase() : "UNKNOWN";
}
}