Send an email to lapsed patrons

This commit is contained in:
Alex Cabal 2022-07-02 16:40:50 -05:00
parent 0875e697b4
commit 3fa9592e6d
17 changed files with 185 additions and 42 deletions

View file

@ -195,7 +195,9 @@ try{
$log->Write('Sending thank you email to non-patron donor.');
$em = new Email();
$em->To = $payment->User->Email;
$em->ToName = $payment->User->Name;
$em->From = EDITOR_IN_CHIEF_EMAIL_ADDRESS;
$em->FromName = EDITOR_IN_CHIEF_NAME;
$em->Subject = 'Thank you for supporting Standard Ebooks!';
$em->Body = Template::EmailDonationThankYou();
$em->TextBody = Template::EmailDonationThankYouText();
@ -214,9 +216,7 @@ catch(Exception $ex){
$exceptionString = vds($ex);
$log->Write('Error: Uncaught exception: ' . $exceptionString);
$em = new Email();
$em->To = ADMIN_EMAIL_ADDRESS;
$em->From = NO_REPLY_EMAIL_ADDRESS;
$em = new Email(true);
$em->Subject = 'Donation processing failed';
$em->Body = Template::EmailDonationProcessingFailed(['exception' => preg_replace('/^/m', "\t", $exceptionString)]);
$em->TextBody = Template::EmailDonationProcessingFailedText(['exception' => preg_replace('/^/m', "\t", $exceptionString)]);

View file

@ -6,19 +6,74 @@ require_once('/standardebooks.org/web/lib/Core.php');
// who aren't in that list.
// We give a 15 day grace period to Patrons Circle members because sometimes FA can be delayed in charging.
Db::Query('
update Patrons
set Ended = utc_timestamp()
where UserId not in
$now = new DateTime();
$lastYear = new DateTime('-1 year');
$expiredPatrons = Db::Query('
select * from Patrons
where
Ended is null and
UserId not in
(
select distinct UserId from Payments where
UserId is not null
and
(
(IsRecurring = 1 and Amount >= 10 and Created > utc_timestamp() - interval 45 day)
(IsRecurring = 1 and Amount >= 10 and Created > ? - interval 45 day)
or
(IsRecurring = 0 and Amount >= 100 and Created > utc_timestamp() - interval 1 year)
(IsRecurring = 0 and Amount >= 100 and Created > ? - interval 1 year)
)
)
');
', [$now, $now], 'Patron');
if(sizeof($expiredPatrons) > 0){
$ebooksThisYear = 0;
// We can't use the Library class to get ebooks because this script is typically run via cron or CLI,
// which doesn't have access PHP-FMP's APCu cache.
foreach(explode("\n", trim(shell_exec('find ' . EBOOKS_DIST_PATH . ' -name "content.opf"') ?? '')) as $filename){
$metadata = file_get_contents($filename);
// Don't create a new Ebook object because that's very slow. Just do a regex match for speed.
preg_match_all('/<dc:date>(.+?)<\/dc:date>/iu', $metadata, $matches);
if(sizeof($matches) > 0){
$created = new DateTime($matches[1][0]);
if($created >= $lastYear){
$ebooksThisYear++;
}
}
}
foreach($expiredPatrons as $patron){
Db::Query('update Patrons set Ended = ? where UserId = ?', [$now, $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');
if(sizeof($lastPayment) > 0 && $patron->User->Email !== null){
$em = new Email();
$em->From = EDITOR_IN_CHIEF_EMAIL_ADDRESS;
$em->FromName = EDITOR_IN_CHIEF_NAME;
$em->To = $patron->User->Email;
$em->ToName = $patron->Name;
$em->Subject = 'Will you continue supporting beautiful digital literature?';
if($lastPayment[0]->IsRecurring){
// Email recurring donors who have lapsed
$em->Body = Template::EmailPatronsCircleRecurringCompleted();
$em->TextBody = Template::EmailPatronsCircleRecurringCompletedText();
}
else{
// Email one time donors who have expired after one year
$em->Body = Template::EmailPatronsCircleCompleted(['ebooksThisYear' => $ebooksThisYear]);
$em->TextBody = Template::EmailPatronsCircleCompletedText(['ebooksThisYear' => $ebooksThisYear]);
}
$em->Send();
}
}
}
?>