From 9f87133cb903ab37dc4573afe338df54dc050fb0 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Sun, 11 Dec 2022 12:10:27 -0600 Subject: [PATCH] Notify admins of new fully-anonymous patrons --- scripts/process-pending-payments | 36 ++++++++++++++++++++++---------- scripts/update-patrons-circle | 12 +++++------ templates/DonationProgress.php | 4 ++-- www/about/index.php | 4 ++-- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/scripts/process-pending-payments b/scripts/process-pending-payments index a5a02301..b693e75a 100755 --- a/scripts/process-pending-payments +++ b/scripts/process-pending-payments @@ -10,7 +10,6 @@ use Facebook\WebDriver\Firefox\FirefoxDriver; use Facebook\WebDriver\Firefox\FirefoxOptions; use Safe\DateTime; -use function Safe\file_get_contents; use function Safe\preg_match; use function Safe\preg_replace; use function Safe\putenv; @@ -60,12 +59,13 @@ try{ foreach($pendingPayments as $pendingPayment){ if($pendingPayment->ChannelId == PAYMENT_CHANNEL_FA){ - if(Db::QueryInt('SELECT count(*) from Payments where TransactionId = ? limit 1', [$pendingPayment->TransactionId]) > 0){ + $log->Write('Processing donation ' . $pendingPayment->TransactionId . ' ...'); + + if(Db::QueryInt('SELECT count(*) from Payments where TransactionId = ?', [$pendingPayment->TransactionId]) > 0){ $log->Write('Donation already exists in database.'); continue; } - $log->Write('Processing donation ' . $pendingPayment->TransactionId . ' ...'); $driver->get('https://fundraising.fracturedatlas.org/admin/donations?query=' . $pendingPayment->TransactionId); // Check if we need to log in to FA. @@ -152,12 +152,12 @@ try{ continue; } - // If this payment isn't anonymous, does it put us in the Patrons Circle? - if($payment->User !== null){ - if(($payment->IsRecurring && $payment->Amount >= 10 && $payment->Created >= $lastMonth) || ($payment->Amount >= 100 && $payment->Created >= $lastYear)){ - // This payment is eligible for the Patrons Circle. + // Does this payment put us in the Patrons Circle? + if(($payment->IsRecurring && $payment->Amount >= 10 && $payment->Created >= $lastMonth) || (!$payment->IsRecurring && $payment->Amount >= 100 && $payment->Created >= $lastYear)){ + // This payment is eligible for the Patrons Circle! + if($payment->User !== null){ // Are we already a patron? - if(!Db::QueryInt('SELECT count(*) from Patrons where UserId = ? and Ended is null', [$payment->UserId])){ + if(Db::QueryInt('SELECT count(*) from Patrons where UserId = ? and Ended is null', [$payment->UserId]) == 0){ // Not a patron yet, add them to the Patrons Circle $patron = new Patron(); @@ -177,9 +177,23 @@ try{ $patron->Create(); } } - else{ - // Not eligible to be a patron; send a thank you email anyway, but only if this is a non-recurring donation, - // or if it's their very first recurring donation + elseif(!$payment->IsRecurring){ + // Fully-anonymous, non-recurring donation eligible for the Patrons Circle. We can't notify them, but do notify the admins. + $patron = new Patron(); + $patron->User = new User(); + + $em = new Email(); + $em->To = ADMIN_EMAIL_ADDRESS; + $em->From = ADMIN_EMAIL_ADDRESS; + $em->Subject = 'New Patrons Circle member'; + $em->Body = Template::EmailAdminNewPatron(['patron' => $patron, 'payment' => $payment]); + $em->TextBody = Template::EmailAdminNewPatronText(['patron' => $patron, 'payment' => $payment]);; + $em->Send(); + } + } + else{ + // Not eligible to be a patron; send a thank you email anyway, but only if this is a non-recurring donation, or if it's their very first recurring donation + if($payment->User !== null){ $previousPaymentCount = Db::QueryInt('SELECT count(*) from Payments where UserId = ? and IsRecurring = true', [$payment->UserId]); // We just added a payment to the system, so if this is their very first recurring payment, we expect the count to be exactly 1 diff --git a/scripts/update-patrons-circle b/scripts/update-patrons-circle index f0610614..a7376303 100755 --- a/scripts/update-patrons-circle +++ b/scripts/update-patrons-circle @@ -10,7 +10,7 @@ $now = new DateTime(); $lastYear = new DateTime('-1 year'); $expiredPatrons = Db::Query(' - select * from Patrons + SELECT * from Patrons where Ended is null and UserId not in @@ -19,9 +19,9 @@ $expiredPatrons = Db::Query(' UserId is not null and ( - (IsRecurring = 1 and Amount >= 10 and Created > ? - interval 45 day) + (IsRecurring = true and Amount >= 10 and Created > ? - interval 45 day) or - (IsRecurring = 0 and Amount >= 100 and Created > ? - interval 1 year) + (IsRecurring = false and Amount >= 100 and Created > ? - interval 1 year) ) ) ', [$now, $now], 'Patron'); @@ -46,12 +46,12 @@ if(sizeof($expiredPatrons) > 0){ } foreach($expiredPatrons as $patron){ - Db::Query('update Patrons set Ended = ? where UserId = ?', [$now, $patron->UserId]); - Db::Query('update Benefits set CanAccessFeeds = false, CanVote = false, CanBulkDownload = false where UserId = ?', [$patron->UserId]); + Db::Query('UPDATE Patrons set Ended = ? where UserId = ?', [$now, $patron->UserId]); + Db::Query('UPDATE Benefits set CanAccessFeeds = false, CanVote = false, CanBulkDownload = false where UserId = ?', [$patron->UserId]); // Email the patron to notify them their term has ended // Is the patron a recurring subscriber? - $lastPayment = Db::Query('select * from Payments where UserId = ? order by Created desc limit 1;', [$patron->UserId], 'Payment'); + $lastPayment = Db::Query('SELECT * from Payments where UserId = ? order by Created desc limit 1;', [$patron->UserId], 'Payment'); if(sizeof($lastPayment) > 0 && $patron->User->Email !== null){ $em = new Email(); diff --git a/templates/DonationProgress.php b/templates/DonationProgress.php index 4d1e8824..c1d3c90f 100644 --- a/templates/DonationProgress.php +++ b/templates/DonationProgress.php @@ -21,9 +21,9 @@ $current = Db::QueryInt(' UserId is null and ( - (Amount >= 100 and IsRecurring = false and Created >= ?) + (IsRecurring = true and Amount >= 10 and Created >= ?) or - (Amount >= 10 and IsRecurring = true and Created >= ?) + (IsRecurring = false and Amount >= 100 and Created >= ?) ) ) union all diff --git a/www/about/index.php b/www/about/index.php index 90a429e2..750ec4ea 100644 --- a/www/about/index.php +++ b/www/about/index.php @@ -25,9 +25,9 @@ $anonymousPatronCount = Db::QueryInt('SELECT sum(cnt) UserId is null and ( - (Amount >= 100 and IsRecurring = false and Created >= utc_timestamp() - interval 1 year) + (IsRecurring = true and Amount >= 10 and Created >= utc_timestamp() - interval 30 day) or - (Amount >= 10 and IsRecurring = true and Created >= utc_timestamp() - interval 30 day) + (IsRecurring = false and Amount >= 100 and Created >= utc_timestamp() - interval 1 year) ) ) union all