Skip to content

Commit

Permalink
Stripe subscription updates (amount/schedule)
Browse files Browse the repository at this point in the history
  • Loading branch information
VSydor committed Sep 10, 2022
1 parent 36b9869 commit 3a0c1ff
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.impactupgrade.nucleus.model.CrmAccount;
import com.impactupgrade.nucleus.model.CrmContact;
import com.impactupgrade.nucleus.model.CrmRecurringDonation;
import com.impactupgrade.nucleus.model.ManageDonationEvent;
import com.impactupgrade.nucleus.model.PaymentGatewayEvent;
import com.impactupgrade.nucleus.service.segment.CrmService;
import com.impactupgrade.nucleus.service.segment.StripePaymentGatewayService;
Expand Down Expand Up @@ -192,6 +193,37 @@ public void processEvent(String eventType, StripeObject stripeObject, Environmen
log.info("subscription is not trialing, so doing nothing; allowing the charge.succeeded event to create the recurring donation");
}
}
// TODO: combine these 2 types and process in the same way?
case "customer.subscription.updated" -> {
Subscription subscription = (Subscription) stripeObject;
log.info("found subscription {}", subscription.getId());

Customer updatedSubscriptionCustomer = env.stripeClient().getCustomer(subscription.getCustomer());
log.info("found customer {}", updatedSubscriptionCustomer.getId());

PaymentGatewayEvent paymentGatewayEvent = new PaymentGatewayEvent(env);
paymentGatewayEvent.initStripe(subscription, updatedSubscriptionCustomer);

ManageDonationEvent manageDonationEvent = new ManageDonationEvent(env);
manageDonationEvent.setSubscriptionId(paymentGatewayEvent.getSubscriptionId());
manageDonationEvent.setAmount(paymentGatewayEvent.getSubscriptionAmountInDollars());

// update the recurring donation.
env.donationService().updateRecurringDonation(manageDonationEvent);
}
case "subscription_schedule.updated" -> {
Subscription subscription = (Subscription) stripeObject;
log.info("found subscription {}", subscription.getId());

Customer updatedSubscriptionCustomer = env.stripeClient().getCustomer(subscription.getCustomer());
log.info("found customer {}", updatedSubscriptionCustomer.getId());

PaymentGatewayEvent paymentGatewayEvent = new PaymentGatewayEvent(env);
paymentGatewayEvent.initStripe(subscription, updatedSubscriptionCustomer);

// update the recurring donation.
env.donationService().processSubscription(paymentGatewayEvent);
}
case "customer.subscription.deleted" -> {
Subscription subscription = (Subscription) stripeObject;
log.info("found subscription {}", subscription.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ public class PaymentGatewayEvent {
protected List<String> products = new ArrayList<>();
protected String refundId;
protected Calendar refundDate;

protected Double subscriptionAmountInDollars;
protected String subscriptionCurrency;
protected String subscriptionDescription;
protected String subscriptionId;
protected String subscriptionInterval;
protected Calendar subscriptionNextDate;
protected Calendar subscriptionStartDate;

protected Double transactionAmountInDollars;
protected Double transactionNetAmountInDollars;
protected Calendar transactionDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public class DonationService {

private final Environment env;
private final CrmService crmService;
private final NotificationService notificationservice;
private final NotificationService notificationService;

public DonationService(Environment env) {
this.env = env;
crmService = env.donationsCrmService();
notificationservice = env.notificationService();
notificationService = env.notificationService();
}

public void createDonation(PaymentGatewayEvent paymentGatewayEvent) throws Exception {
Expand Down Expand Up @@ -104,8 +104,9 @@ public void processSubscription(PaymentGatewayEvent paymentGatewayEvent) throws
paymentGatewayEvent.getSubscriptionId());
crmService.insertRecurringDonation(paymentGatewayEvent);
} else {
log.info("found an existing CRM recurring donation using subscription {}",
log.info("found an existing CRM recurring donation using subscription {}; updating it...",
paymentGatewayEvent.getSubscriptionId());
crmService.updateRecurringDonation(paymentGatewayEvent);
}
}

Expand All @@ -127,7 +128,7 @@ public void closeRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) thro
targetId = recurringDonation.get().contact.id;
}

notificationservice.sendNotification(
notificationService.sendNotification(
"Recurring Donation Closed",
"Recurring donation " + recurringDonation.get().id + " has been closed.",
targetId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ default String insertRecurringDonation(PaymentGatewayEvent paymentGatewayEvent)
return null;
}

default void updateRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) throws Exception {
}

default Optional<CrmRecurringDonation> getRecurringDonationBySubscriptionId(String subscriptionId) throws Exception {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ default Optional<CrmRecurringDonation> getRecurringDonation(PaymentGatewayEvent
return getRecurringDonationBySubscriptionId(paymentGatewayEvent.getSubscriptionId());
}
String insertRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) throws Exception;
void updateRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) throws Exception;
void closeRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) throws Exception;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,23 @@ public String insertRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) t
}
}

@Override
public void updateRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) throws Exception {
Optional<CrmDonation> donation = getDonation(paymentGatewayEvent);

if (donation.isEmpty()) {
log.warn("unable to find HS donation using transaction {}", paymentGatewayEvent.getTransactionId());
return;
}

Deal deal = (Deal) donation.get().rawObject;
DealProperties dealProperties = deal.getProperties();
// TODO: update everything or only subset of fields?
setRecurringDonationFields(dealProperties, paymentGatewayEvent);

hsClient.deal().update(deal.getId(), dealProperties);
}

protected void setRecurringDonationFields(DealProperties deal, PaymentGatewayEvent paymentGatewayEvent) throws Exception {
// TODO: campaign

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,23 @@ public String insertRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) t
return sfdcClient.insert(recurringDonation).getId();
}

@Override
public void updateRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) throws Exception {
Optional<CrmDonation> crmDonation = getDonation(paymentGatewayEvent);
if (crmDonation.isEmpty()) {
log.warn("unable to find SFDC opportunity using transaction {}", paymentGatewayEvent.getTransactionId());
return;
}

SObject opportunity = (SObject) crmDonation.get().rawObject;
SObject opportunityUpdate = new SObject("Opportunity");
opportunityUpdate.setId(opportunity.getId());
// TODO: update everything or only subset of fields?
setRecurringDonationFields(opportunity, getCampaignOrDefault(paymentGatewayEvent), paymentGatewayEvent);

sfdcClient.update(opportunityUpdate);
}

/**
* Set any necessary fields on an RD before it's inserted.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ public String insertRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) t
return null;
}

@Override
public void updateRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) throws Exception {

}

@Override
public void closeRecurringDonation(PaymentGatewayEvent paymentGatewayEvent) throws Exception {

Expand Down

0 comments on commit 3a0c1ff

Please sign in to comment.