Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Stripe API upgrade #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<artifactId>azure-identity</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.stripe</groupId>
<artifactId>stripe-java</artifactId>
<version>22.1.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ public void processEvent(String eventType, StripeObject stripeObject, Environmen
PaymentGatewayEvent paymentGatewayEvent = new PaymentGatewayEvent(env);
paymentGatewayEvent.initStripe(refund);
env.donationService().refundDonation(paymentGatewayEvent);
}
case "charge.updated" -> {

}
case "customer.subscription.created" -> {
Subscription subscription = (Subscription) stripeObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.stripe.model.Customer;
import com.stripe.model.Invoice;
import com.stripe.model.PaymentIntent;
import com.stripe.model.PaymentMethod;
import com.stripe.model.Refund;
import com.stripe.model.Subscription;
import com.stripe.model.SubscriptionItem;
Expand Down Expand Up @@ -163,7 +162,7 @@ public void initStripe(Charge stripeCharge, Optional<Customer> stripeCustomer,
public void initStripe(PaymentIntent stripePaymentIntent, Optional<Customer> stripeCustomer,
Optional<Invoice> stripeInvoice, Optional<BalanceTransaction> stripeBalanceTransaction) {
gatewayName = "Stripe";
String stripePaymentMethod = stripePaymentIntent.getCharges().getData().stream().findFirst().map(c -> c.getPaymentMethodDetails().getType()).orElse("");
String stripePaymentMethod = stripePaymentIntent.getLatestChargeObject().getPaymentMethodDetails().getType();
if (stripePaymentMethod.toLowerCase(Locale.ROOT).contains("ach")) {
paymentMethod = "ACH";
} else {
Expand Down Expand Up @@ -193,7 +192,7 @@ public void initStripe(PaymentIntent stripePaymentIntent, Optional<Customer> str

transactionDescription = stripePaymentIntent.getDescription();
transactionId = stripePaymentIntent.getId();
transactionSecondaryId = stripePaymentIntent.getCharges().getData().stream().findFirst().map(Charge::getId).orElse(null);
transactionSecondaryId = stripePaymentIntent.getLatestCharge();
// note this is different than a charge, which uses !"failed" -- intents have multiple phases of "didn't work",
// so explicitly search for succeeded
transactionSuccess = "succeeded".equalsIgnoreCase(stripePaymentIntent.getStatus());
Expand Down Expand Up @@ -260,7 +259,7 @@ public void initStripe(Subscription stripeSubscription, Customer stripeCustomer)
initStripeCustomer(Optional.of(stripeCustomer), Optional.empty());
}

protected void initStripeCustomer(Optional<Customer> __stripeCustomer, Optional<PaymentMethod.BillingDetails> billingDetails) {
protected void initStripeCustomer(Optional<Customer> __stripeCustomer, Optional<Charge.BillingDetails> billingDetails) {
Map<String, String> metadata = getAllMetadata();

if (__stripeCustomer.isPresent()) {
Expand Down Expand Up @@ -294,7 +293,7 @@ protected void initStripeCustomer(Optional<Customer> __stripeCustomer, Optional<
// What happens in this method seems ridiculous, but we're trying to resiliently deal with a variety of situations.
// Some donation forms and vendors use true Customer names, others use metadata on Customer, other still only put
// names in metadata on the Charge or Subscription. Madness. But let's be helpful...
protected void initStripeCustomerName(Optional<Customer> stripeCustomer, Optional<PaymentMethod.BillingDetails> billingDetails) {
protected void initStripeCustomerName(Optional<Customer> stripeCustomer, Optional<Charge.BillingDetails> billingDetails) {
Map<String, String> metadata = getAllMetadata();

// For the full name, start with Customer name. Generally this is populated, but a few vendors don't always do it.
Expand Down Expand Up @@ -371,7 +370,7 @@ protected void initStripeCustomerName(Optional<Customer> stripeCustomer, Optiona
}
}

protected void initStripeAddress(Optional<Customer> __stripeCustomer, Optional<PaymentMethod.BillingDetails> billingDetails) {
protected void initStripeAddress(Optional<Customer> __stripeCustomer, Optional<Charge.BillingDetails> billingDetails) {
CrmAddress crmAddress = new CrmAddress();

if (__stripeCustomer.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,18 @@ public PaymentGatewayEvent chargeToPaymentGatewayEvent(Charge charge, Optional<B
}

public PaymentGatewayEvent paymentIntentToPaymentGatewayEvent(PaymentIntent paymentIntent, boolean fullObjects) throws StripeException {
// TODO: 2022 versions of the Stripe API introduced breaking changes to paymentIntent.getCharges(). Until we upgrade
// all clients' webhooks (or introduce version-specific dependencies and code), we'll instead retrieve
// the full intent using our own version.
paymentIntent = stripeClient.getPaymentIntent(paymentIntent.getId());

Optional<BalanceTransaction> chargeBalanceTransaction = Optional.empty();
if (paymentIntent.getCharges() != null && !paymentIntent.getCharges().getData().isEmpty()) {
if (paymentIntent.getCharges().getData().size() == 1) {
String balanceTransactionId = paymentIntent.getCharges().getData().get(0).getBalanceTransaction();
if (fullObjects && !Strings.isNullOrEmpty(balanceTransactionId)) {
chargeBalanceTransaction = Optional.of(stripeClient.getBalanceTransaction(balanceTransactionId));
log.info("found balance transaction {}", chargeBalanceTransaction.get().getId());
}
// TODO: Does latest_charge need expanded?
if (paymentIntent.getLatestChargeObject() != null) {
String balanceTransactionId = paymentIntent.getLatestChargeObject().getBalanceTransaction();
if (fullObjects && !Strings.isNullOrEmpty(balanceTransactionId)) {
chargeBalanceTransaction = Optional.of(stripeClient.getBalanceTransaction(balanceTransactionId));
log.info("found balance transaction {}", chargeBalanceTransaction.get().getId());
}
}

Expand Down