Sync Adapter to Java 25: add StripePaymentClassAdapter, README mapping
This commit is contained in:
@@ -1,42 +1,34 @@
|
||||
package adapter;
|
||||
|
||||
/**
|
||||
* The Adapter — bridges StripeClient (Adaptee) to PaymentGateway (Target).
|
||||
* The Adapter — bridges StripeClient to PaymentGateway.
|
||||
*
|
||||
* 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.
|
||||
* 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: your code uses decimal dollars; Stripe wants integer cents
|
||||
// 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) {
|
||||
// Translation: your "transactionId" is Stripe's "chargeId"
|
||||
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: map Stripe's object to your simple status string
|
||||
// Translation: getStatus() → retrieveCharge(), StripeChargeResult → String
|
||||
StripeClient.StripeChargeResult result = stripe.retrieveCharge(transactionId);
|
||||
if (!result.success) return "FAILED";
|
||||
return result.status != null ? result.status.toUpperCase() : "UNKNOWN";
|
||||
|
||||
Reference in New Issue
Block a user