Skip to content

Commit

Permalink
feat(contribution): added option to change the amount of the subscrip…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
ikjelle committed Dec 23, 2024
1 parent 047517e commit d0c44e6
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Controller/ContributionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ public function payContribution(Request $request, MollieApiClient $mollieApiClie
// Otherwise redirect to payment screen immediately
return $this->redirect($molliePayment->getCheckoutUrl(), 303);
}
/**
* @Route("/contributie-aanpassen", name="member_contribution_change_amount")
*/
public function changeContributionAmount(Request $request, MollieApiClient $mollieApiClient, bool $automaticCollection = false): Response
{
$member = $this->getUser();

$mollieCustomerId = $member->getMollieCustomerId();
$contributionAmount = $member->getContributionPerPeriodInEuros();
$subscriptionId = $member->getMollieSubscriptionId();
// update subscription amount
$updatedSubscription = $mollieApiClient->subscriptions->update($mollieCustomerId, $subscriptionId, [
'amount' => [
'currency' => 'EUR',
'value' => number_format($contributionAmount, 2, '.', '')
]
]);

return $this->redirectToRoute('member_details');
}

/**
* @Route("/api/webhook/mollie-contribution", name="member_contribution_mollie_webhook")
Expand Down Expand Up @@ -228,6 +248,37 @@ public function automaticCollection(Request $request, LoggerInterface $logger):
'contributionEnabled' => false,
]);
}
/**
* @Route("/automatische-incasso-aanpassen", name="member_contribution_automatic_collection_change_amount")
*/
public function automaticCollectionChangeAmount(Request $request, LoggerInterface $logger): Response
{
$projectRoot = $this->getParameter('kernel.project_dir');
$org_config = Yaml::parseFile($projectRoot . '/config/instances/' . $this->getParameter('app.organizationID') . '.yaml');

$form = $this->createForm(ContributionIncomeType::class, null, [
'contribution' => $org_config['contribution']
]);

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$member = $this->getUser();
$member->setContributionPerPeriodInCents($form->getData());
$member->setContributionPeriod(Member::PERIOD_QUARTERLY);
$em->flush();

return $this->redirectToRoute('member_contribution_change_amount');
}

return $this->render('user/contribution/automatic-collection-change.html.twig', [
'success' => false,
'form' => $form->createView(),
'contribution' => $org_config['contribution'],
// The user is currently setting up contribution, so setting this false disables the contribution nagbar
'contributionEnabled' => false,
]);
}

/**
* @Route("/automatische-incasso/inschakelen", name="member_contribution_automatic_collection_enable")
Expand Down
91 changes: 91 additions & 0 deletions templates/user/contribution/automatic-collection-change.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{% extends 'user/layout/members.html.twig' %}

{% block scripts %}
{{ parent() }}
<script>
function updateContribution() {
let other = document.querySelector('.other-amount');
if (document.querySelector('.choice :checked').value !== 0) {
other.value = '';
}
}
function updateOtherAmount() {
let other = document.querySelector('.other-amount');
if (other.value != '')
{
document.querySelector('.choice [value="0"]').checked = true;
}
}
document.addEventListener('change', e => {
if (e.target.closest) {
if (e.target.closest('.other-amount')) {
updateOtherAmount();
} else if (e.target.closest('.choices')) {
updateContribution();
}
}
})
</script>
{% endblock %}

{% block content %}
<div class="main-content">
<h1>Automatische incasso</h1>
<p>
Contributie zal elk kwartaal automatisch van je rekening afgeschreven worden via automatisch incasso.
Hoeveel je hierbij betaalt hangt af van je inkomen.
</p>

{% if contribution.tiers|length > 2 %}
<table class="table">
<thead>
<tr>
<th>Maandinkomen</th>
<th>Contributie</th>
</tr>
</thead>
<tbody>
{% for tier in contribution.tiers %}
{% if tier.amount is not same as(null) %}
<tr>
<td>{{ tier.description }}</td>
<td>€{{ (tier.amount / 100)|number_format(2, ',') }} per kwartaal (€{{ (tier.amount / 100 / 3)|number_format(2, ',') }} per maand)</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
{% endif %}

{% if form.vars.submitted and not form.vars.valid %}
<div class="error">
{{ form_errors(form) }}
</div>
{% endif %}

<p>
Selecteer wat er voor jou van toepassing is:
</p>
{{ form_start(form, { attr: { class: 'incomes' }}) }}
{{ form_row(form._token) }}

<div class="choices">
{% for option in form.contributionAmount %}
<div class="choice">
{{ form_widget(option) }}
<div class="choice-text">
{{ form_label(option, null, { required: false }) }}
{% if option.vars.value == 0 %}
<br />
{{ form_widget(form.otherAmount, { attr: { class: 'other-amount' } }) }} per kwartaal
{% endif %}
</div>
</div>
{% endfor %}
</div>

<button type="submit" class="button submit-button">Incasso bedrag aanpassen</button>
{{ form_end(form, {render_rest: false}) }}

</div>
{% endblock %}
1 change: 1 addition & 0 deletions templates/user/contribution/automatic-collection.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

<a href="{{ url('member_contribution_automatic_collection_disable') }}" class="button">Automatische incasso stopzetten</a>
<a href="{{ url('member_contribution_preferences_change_bank_account') }}" class="button">Instellen met andere bankrekening</a>
<a href="{{ url('member_contribution_automatic_collection_change_amount') }}" class="button">Incasso bedrag aanpassen</a>
{% else %}
<p>
Contributie zal elk kwartaal automatisch van je rekening afgeschreven worden via automatisch incasso.
Expand Down
1 change: 1 addition & 0 deletions templates/user/details.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@

<a class="action-link" href="{{ url('member_contribution_automatic_collection') }}">Automatische incasso stopzetten</a>
<a class="action-link" href="{{ url('member_contribution_preferences_change_bank_account') }}" class="button">Instellen met andere bankrekening</a>
<a class="action-link" href="{{ url('member_contribution_automatic_collection_change_amount') }}">Incasso bedrag aanpassen</a>
{% else %}
{% if app.user.contributionCompleted(date()) %}
<p>
Expand Down

0 comments on commit d0c44e6

Please sign in to comment.