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

Direct reference between intent and checkouts #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
35 changes: 14 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,20 @@ fn compute_stripe_donors(
) -> Vec<Donor> {
let mut donors = HashMap::new();

let mut customer_id_to_checkouts = HashMap::new();
let mut checkouts = HashMap::new();
let now = Utc::now();
for checkout_session in checkout_sessions {
if checkout_session.status != Some(stripe::CheckoutSessionStatus::Complete) {
continue;
}

let customer = checkout_session.customer.as_ref().expect("Customer information was not included. Code currently assumes all complete checkouts have customers.");
let customer_id = customer.id();

let checkouts = customer_id_to_checkouts
.entry(customer_id)
.or_insert_with(Vec::new);

checkouts.push(checkout_session.clone());
}
let payment_intent = checkout_session.payment_intent.as_ref().expect("Payment intent was not included. Code currently assumes all CheckoutSessions have payment intents.");
let payment_intent_id = payment_intent.id();

// Ensure checkouts are sorted by the time they were created
for checkouts in customer_id_to_checkouts.values_mut() {
checkouts.sort_by_key(|c| c.created);
checkouts
.entry(payment_intent_id)
.or_insert_with(Vec::new)
.push(checkout_session.clone());
}

let mut payment_intents = payment_intents.to_vec();
Expand All @@ -191,16 +185,15 @@ fn compute_stripe_donors(
let customer = payment_intent.customer.as_ref().expect("Customer information was not included. Code currently assumes all succeeded PaymentIntents have customers.");
let customer_id = customer.id();

let checkouts = customer_id_to_checkouts.get(&customer_id).expect(
"Successful payment intents are expected to have at least one checkout session",
);

let checkout = checkouts
.iter()
.rev()
.find(|c| c.amount_total == Some(payment_intent.amount))
.get(&payment_intent.id)
.expect(
"Successful payment intents are expected to have at least one checkout session.",
)
.into_iter()
.max_by_key(|c| c.created)
.expect(
"Successful payment intent should have a matching checkout with the same amount",
"Successful payment intent should have a matching checkout with the same amount.",
);

let mut link = None;
Expand Down