-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from roodjong/subscription-message-member-id
Allow usage of member number in subscription message
- Loading branch information
Showing
7 changed files
with
198 additions
and
62 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
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,87 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace App\Command; | ||
|
||
use App\Entity\Member; | ||
use App\Service\SubscriptionSetupService; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Mollie\Api\Exceptions\ApiException; | ||
use Mollie\Api\MollieApiClient; | ||
use Mollie\Api\Resources\Subscription; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
use Symfony\Component\Console\Question\Question; | ||
|
||
class MigrateSubscriptionToNewMessage extends Command | ||
{ | ||
protected static $defaultName = 'app:migrate-subscription-to-new-message'; | ||
protected static $defaultDescription = 'Update all Mollie subscriptions to use the currently configured payment description'; | ||
|
||
public function __construct( | ||
private readonly MollieApiClient $mollieApiClient, | ||
private readonly EntityManagerInterface $entityManager, | ||
private readonly SubscriptionSetupService $subscriptionService, | ||
) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$output->writeln('Updating Mollie subscription to new message'); | ||
|
||
try { | ||
$this->confirmChangeCount($input, $output); | ||
} catch (\Exception $e) { | ||
$output->writeln($e->getMessage()); | ||
return Command::FAILURE; | ||
} | ||
|
||
$memberRepository = $this->entityManager->getRepository(Member::class); | ||
|
||
$allSubscriptions = $this->mollieApiClient->subscriptions->iterator(); | ||
foreach ($allSubscriptions as $subscription) { | ||
/** @var Subscription $subscription */ | ||
if (!$subscription->isActive()) { | ||
continue; | ||
} | ||
$output->writeln('Updating subscription ' . $subscription->description); | ||
/** @var Member $member */ | ||
$members = $memberRepository->findBy(['mollieSubscriptionId' => $subscription->id]); | ||
if (count($members) === 0) { | ||
$output->writeln('<comment>Could not find member for subscription ' . $subscription->id . '! (skipping)</comment>'); | ||
continue; | ||
} | ||
$member = $members[0]; | ||
$newDescription = $this->subscriptionService->generateDescription($member); | ||
$subscription->description = $newDescription; | ||
try { | ||
$subscription->update(); | ||
} catch (ApiException $exception) { | ||
$output->writeln('<error>' . $exception->getMessage() . '</error>'); | ||
} | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
private function confirmChangeCount(InputInterface $input, OutputInterface $output): void | ||
{ | ||
$allSubscriptions = $this->mollieApiClient->subscriptions->iterator(); | ||
$amount = $allSubscriptions->count(); | ||
$helper = $this->getHelper('question'); | ||
$question = new Question("You are about to change $amount descriptions, to continue confirm the amount: ", false); | ||
$answer = (int)$helper->ask($input, $output, $question); | ||
if ($answer === $amount) { | ||
return; | ||
} | ||
throw new \Exception('Did not confirm'); | ||
} | ||
} |
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
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,73 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace App\Service; | ||
|
||
use App\Entity\Member; | ||
use DateTime; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Mollie\Api\Exceptions\ApiException; | ||
use Mollie\Api\Resources\Customer; | ||
use Mollie\Api\Resources\Subscription; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class SubscriptionSetupService | ||
{ | ||
public function __construct( | ||
private readonly ManagerRegistry $doctrine, | ||
private readonly ParameterBagInterface $params, | ||
private readonly UrlGeneratorInterface $router, | ||
) | ||
{ | ||
} | ||
|
||
public function generateDescription(Member $member): string | ||
{ | ||
$projectRoot = $this->params->get('kernel.project_dir'); | ||
$org_config = Yaml::parseFile($projectRoot . '/config/instances/' . $this->params->get('app.organizationID') . '.yaml'); | ||
|
||
$description = $org_config['mollie_payment_description']; | ||
$description = str_replace('{{organisation_name}}', $this->params->get("app.organizationName"), $description); | ||
return str_replace('{{member_number}}', (string)$member->getId(), $description); | ||
} | ||
|
||
/** | ||
* @throws ApiException | ||
*/ | ||
public function createSubscription(Member $member, Customer $customer): Subscription | ||
{ | ||
$mollieIntervals = [ | ||
Member::PERIOD_MONTHLY => '1 month', | ||
Member::PERIOD_QUARTERLY => '3 months', | ||
Member::PERIOD_ANNUALLY => '1 year' | ||
]; | ||
$dateTimeIntervals = [ | ||
Member::PERIOD_MONTHLY => 'P1M', | ||
Member::PERIOD_QUARTERLY => 'P3M', | ||
Member::PERIOD_ANNUALLY => 'P1Y' | ||
]; | ||
|
||
$startDate = new DateTime(); | ||
$startDate->setDate((int)date('Y'), (int)floor(date('m') / 3) * 3, 1); | ||
$startDate->add(new \DateInterval($dateTimeIntervals[$member->getContributionPeriod()])); | ||
|
||
$description = $this->generateDescription($member); | ||
|
||
$subscription = $customer->createSubscription([ | ||
'amount' => [ | ||
'currency' => 'EUR', | ||
'value' => number_format($member->getContributionPerPeriodInEuros(), 2, '.', '') | ||
], | ||
'interval' => $mollieIntervals[$member->getContributionPeriod()], | ||
'description' => $description, | ||
'startDate' => $startDate->format('Y-m-d'), | ||
'webhookUrl' => $this->router->generate('member_contribution_mollie_webhook', [], UrlGeneratorInterface::ABSOLUTE_URL) | ||
]); | ||
$member->setMollieSubscriptionId($subscription->id); | ||
$this->doctrine->getManager()->flush(); | ||
return $subscription; | ||
} | ||
} |