-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic webhooks implementation + mail styles
- Loading branch information
Showing
27 changed files
with
530 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
body { | ||
color: #111; | ||
font-family: Arial, Tahoma, Sans-serif, serif; | ||
background: #ebebeb; | ||
font-size: 15px; | ||
} | ||
|
||
h1 { | ||
font-weight: normal; | ||
font-size: 26px; | ||
} | ||
|
||
h2 { | ||
font-weight: normal; | ||
font-size: 22px; | ||
} | ||
|
||
h3 { | ||
font-weight: normal; | ||
font-size: 18px; | ||
} | ||
|
||
p { | ||
margin: 1em 0; | ||
} | ||
|
||
a { | ||
color: #f34770; | ||
text-decoration: underline; | ||
} | ||
|
||
hr { | ||
border-bottom: 1px solid #ccc; | ||
margin: 15px 0; | ||
padding: 0; | ||
height: 1px; | ||
} | ||
|
||
|
||
h1.newsletter { | ||
color: #f34770; | ||
font-size: 24px; | ||
font-weight: normal; | ||
} | ||
|
||
.admin-h1 { | ||
font-size: 24px; | ||
font-weight: normal; | ||
} | ||
|
||
.logo { | ||
width: 80px; | ||
} | ||
|
||
.newsletterContainer { | ||
width: 100%; | ||
max-width: 600px; | ||
margin: 15px auto; | ||
border: 1px solid #f34770; | ||
padding: 15px; | ||
background: #fff; | ||
} | ||
|
||
.border-bottom { | ||
border-bottom: 1px solid #333; | ||
padding-bottom: 15px; | ||
margin-bottom: 15px; | ||
} | ||
|
||
.text-center { | ||
text-align: center; | ||
} | ||
|
||
.text-end { | ||
text-align: right; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\Message; | ||
|
||
readonly final class CreateMembershipSubscription | ||
{ | ||
public function __construct( | ||
public string $stripeSubscriptionId, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\Message; | ||
|
||
readonly final class NotifyAboutFailedPayment | ||
{ | ||
public function __construct( | ||
public string $stripeSubscriptionId, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\Message; | ||
|
||
readonly final class UpdateMembershipSubscription | ||
{ | ||
public function __construct( | ||
public string $stripeSubscriptionId, | ||
) { | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/MessageHandler/CreateMembershipSubscriptionHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\MessageHandler; | ||
|
||
use DateTimeImmutable; | ||
use Psr\Clock\ClockInterface; | ||
use Ramsey\Uuid\Uuid; | ||
use SpeedPuzzling\Web\Entity\Membership; | ||
use SpeedPuzzling\Web\Exceptions\MembershipNotFound; | ||
use SpeedPuzzling\Web\Exceptions\PlayerNotFound; | ||
use SpeedPuzzling\Web\Message\CreateMembershipSubscription; | ||
use SpeedPuzzling\Web\Message\UpdateMembershipSubscription; | ||
use SpeedPuzzling\Web\Repository\MembershipRepository; | ||
use SpeedPuzzling\Web\Repository\PlayerRepository; | ||
use Stripe\StripeClient; | ||
use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
#[AsMessageHandler] | ||
readonly final class CreateMembershipSubscriptionHandler | ||
{ | ||
public function __construct( | ||
private StripeClient $stripeClient, | ||
private PlayerRepository $playerRepository, | ||
private MembershipRepository $membershipRepository, | ||
private ClockInterface $clock, | ||
private MessageBusInterface $messageBus, | ||
) { | ||
} | ||
|
||
/** | ||
* @throws PlayerNotFound | ||
*/ | ||
public function __invoke(CreateMembershipSubscription $message): void | ||
{ | ||
$subscriptionId = $message->stripeSubscriptionId; | ||
$subscription = $this->stripeClient->subscriptions->retrieve($subscriptionId); | ||
$billingPeriodEnd = DateTimeImmutable::createFromFormat('U', (string) $subscription->current_period_end); | ||
assert($billingPeriodEnd instanceof DateTimeImmutable); | ||
|
||
$customerId = $subscription->customer; | ||
assert(is_string($customerId)); | ||
|
||
$customer = $this->stripeClient->customers->retrieve($customerId); | ||
$playerId = $customer->metadata->player_id ?? null; | ||
|
||
if (!is_string($playerId)) { | ||
// Can not continue without playerId | ||
return; | ||
} | ||
|
||
try { | ||
$this->membershipRepository->getByPlayerId($playerId); | ||
$this->messageBus->dispatch( | ||
new UpdateMembershipSubscription($subscriptionId), | ||
); | ||
} catch (MembershipNotFound) { | ||
$player = $this->playerRepository->get($playerId); | ||
$membership = new Membership( | ||
Uuid::uuid7(), | ||
$player, | ||
$this->clock->now(), | ||
$subscriptionId, | ||
$billingPeriodEnd, | ||
); | ||
|
||
$this->membershipRepository->save($membership); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\MessageHandler; | ||
|
||
use SpeedPuzzling\Web\Events\MembershipStarted; | ||
use SpeedPuzzling\Web\Exceptions\MembershipNotFound; | ||
use SpeedPuzzling\Web\Repository\MembershipRepository; | ||
use Symfony\Bridge\Twig\Mime\TemplatedEmail; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
|
||
#[AsMessageHandler] | ||
readonly final class NotifyWhenMembershipStarted | ||
{ | ||
public function __construct( | ||
private MembershipRepository $membershipRepository, | ||
private MailerInterface $mailer, | ||
) { | ||
} | ||
|
||
/** | ||
* @throws MembershipNotFound | ||
*/ | ||
public function __invoke(MembershipStarted $message): void | ||
{ | ||
$membership = $this->membershipRepository->get($message->membershipId->toString()); | ||
$player = $membership->player; | ||
|
||
if ($player->email === null) { | ||
return; | ||
} | ||
|
||
if ($membership->billingPeriodEndsAt === null) { | ||
$email = (new TemplatedEmail()) | ||
->to($player->email) | ||
->locale('en') // TODO: take locale from user object | ||
->subject('Congratulations to your MySpeedPuzzling membership!') | ||
->htmlTemplate('emails/membership_granted.html.twig') | ||
->context([ | ||
'membershipExpiresAt' => $membership->endsAt?->format('d.m.Y'), | ||
]); | ||
|
||
$this->mailer->send($email); | ||
} | ||
|
||
if ($membership->billingPeriodEndsAt !== null) { | ||
$email = (new TemplatedEmail()) | ||
->to($player->email) | ||
->locale('en') // TODO: take locale from user object | ||
->subject('Congratulations to your MySpeedPuzzling membership!') | ||
->htmlTemplate('emails/membership_subscribed.html.twig') | ||
->context([ | ||
'nextBillingPeriod' => $membership->billingPeriodEndsAt->format('d.m.Y H:i'), | ||
]); | ||
|
||
$this->mailer->send($email); | ||
} | ||
} | ||
} |
Oops, something went wrong.