From cf5f488cae343839879607a0c9c14925c8e240cd Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Fri, 13 Dec 2024 10:10:07 -0600 Subject: [PATCH] Improve error message for people trying to log in to the Patrons Circle --- lib/Enums/ExceptionMessageType.php | 7 +++++++ lib/Exceptions/AppException.php | 1 + lib/Formatter.php | 29 +++++++++++++++++++++++++++++ lib/Session.php | 18 +++++++++++++++++- lib/User.php | 16 ++++++++++++++++ templates/DonationCounter.php | 2 +- templates/Error.php | 14 +++++++++++++- www/donate/index.php | 6 +++--- www/users/get.php | 22 +++++++++++++++++----- 9 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 lib/Enums/ExceptionMessageType.php diff --git a/lib/Enums/ExceptionMessageType.php b/lib/Enums/ExceptionMessageType.php new file mode 100644 index 00000000..2ceed418 --- /dev/null +++ b/lib/Enums/ExceptionMessageType.php @@ -0,0 +1,7 @@ +formatCurrency($amount, 'USD'); + + if($output === false){ + $output = '$0.00'; + } + + if($trimZeroCents){ + $output = preg_replace('/\.00$/u', '', $output); + } + + return $output; + } } diff --git a/lib/Session.php b/lib/Session.php index 3c017e00..320be09c 100644 --- a/lib/Session.php +++ b/lib/Session.php @@ -1,4 +1,6 @@ SessionId); } catch(Exceptions\UserNotFoundException){ - throw new Exceptions\InvalidLoginException(); + // We couldn't find a *registered * `User`. But, often people make a small donation assuming it automatically adds them to the Patrons Circle. So, check if they made a donation less than 7 days ago, and if so, notify them about the requirements to join the Patrons Circle. + $ex = new Exceptions\InvalidLoginException(); + try{ + $user = User::GetByIdentifier($identifier); + /** @throws void */ + if($user->LastPayment !== null && $user->LastPayment->Created > new DateTimeImmutable('7 days ago')){ + $ex = new InvalidLoginException('

We couldn’t find you in the Patrons Circle, but you recently ' . ($user->LastPayment->IsRecurring ? 'started a recurring' : 'made a one-time') . ' donation of ' . Formatter::FormatCurrency($user->LastPayment->Amount) . '.

To join the Patrons Circle, supporters must start a recurring donation of ' . Formatter::FormatCurrency(PATRONS_CIRCLE_MONTHLY_COST, true) . '/month or more, or make a one-time donation of ' . Formatter::FormatCurrency(PATRONS_CIRCLE_YEARLY_COST, true) . ' or more to join for one year.

Once you join the Patrons Circle, you’ll be able to log in and access member benefits.

'); + $ex->MessageType = Enums\ExceptionMessageType::Html; + } + } + catch(Exceptions\UserNotFoundException){ + // Pass. + } + + throw $ex; } } diff --git a/lib/User.php b/lib/User.php index 6edf2447..02fda675 100644 --- a/lib/User.php +++ b/lib/User.php @@ -11,6 +11,7 @@ use function Safe\preg_match; * @property string $Url * @property ?Patron $Patron * @property ?NewsletterSubscription $NewsletterSubscription + * @property ?Payment $LastPayment */ class User{ use Traits\Accessor; @@ -27,6 +28,7 @@ class User{ protected bool $_IsRegistered; /** @var array $_Payments */ protected array $_Payments; + protected ?Payment $_LastPayment; protected Benefits $_Benefits; protected string $_Url; protected ?Patron $_Patron; @@ -87,6 +89,20 @@ class User{ return $this->_Payments; } + protected function GetLastPayment(): ?Payment{ + if(!isset($this->_LastPayment)){ + $this->_LastPayment = Db::Query(' + SELECT * + from Payments + where UserId = ? + order by Created desc + limit 1 + ', [$this->UserId], Payment::class)[0] ?? null; + } + + return $this->_LastPayment; + } + protected function GetBenefits(): Benefits{ if(!isset($this->_Benefits)){ if(isset($this->UserId)){ diff --git a/templates/DonationCounter.php b/templates/DonationCounter.php index df33d275..704349fa 100644 --- a/templates/DonationCounter.php +++ b/templates/DonationCounter.php @@ -56,7 +56,7 @@ $digits = str_split(str_pad((string)$current, 3, "0", STR_PAD_LEFT))

Our fiscal sponsor, Fractured Atlas, is celebrating the twenty-year anniversary of their fiscal sponsorship program by distributing $1,000 to twenty different projects.

Each one-time donation of any amount to Standard Ebooks through gives us one entry in this $1,000 giveaway. The more donations we receive through , the more chances we have to win!

-

This is a great time to join our Patrons Circle with a donation of $. Not only will your donation support us directly, but it’ll give us one more entry in this big giveaway.

+

This is a great time to join our Patrons Circle with a donation of . Not only will your donation support us directly, but it’ll give us one more entry in this big giveaway.

Will you show your support for free, beautiful digital literature?

diff --git a/www/donate/index.php b/www/donate/index.php index 2541bcec..d7d8441b 100644 --- a/www/donate/index.php +++ b/www/donate/index.php @@ -34,10 +34,10 @@ $newsletterSubscriberCount = floor(Db::QueryInt('

Membership in the Patrons Circle is limited to individuals only. Organizations, please see corporate sponsorship instead.

Join now

-

Join the Patrons Circle by starting a recurring donation of $/month or more, or join for one year with a one-time donation of $ or more.

+

Join the Patrons Circle by starting a recurring donation of /month or more, or join for one year with a one-time donation of or more.

- Donate $/month or more - Donate $ or more + Donate /month or more + Donate or more

Important: We need to know your email address to be able to log you in to the Patrons Circle. Make sure to select either “List my name publicly” or “Don’t list publicly, but reveal to project” during checkout to be able to log in to the Patrons Circle.

diff --git a/www/users/get.php b/www/users/get.php index fa8a2e77..482ab46d 100644 --- a/www/users/get.php +++ b/www/users/get.php @@ -87,11 +87,23 @@ catch(Exceptions\SeeOtherException $ex){ Cycle type: - Patron->CycleType->value) ?> + + Patron->CycleType !== null){ ?> + Patron->CycleType->value) ?> + + Not set + + Base cost: - $Patron->BaseCost) ?> + + Patron->BaseCost !== null){ ?> + Patron->BaseCost) ?> + + Not set + + Is anonymous: @@ -193,13 +205,13 @@ catch(Exceptions\SeeOtherException $ex){ - $Amount, 2) ?> + Amount) ?> - $Fee, 2) ?> + Fee) ?> - $Amount - $payment->Fee, 2) ?> + Amount - $payment->Fee) ?> TransactionId) ?>