From 5c124740c0aa018b88f2e2f560601a160f13cdfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Avil=C3=A9s?= Date: Wed, 21 Oct 2015 15:36:12 +0200 Subject: [PATCH] Payment/Paypal: Adapt to new registration module --- .../indico_payment_paypal/blueprint.py | 2 +- .../indico_payment_paypal/controllers.py | 32 +++++++++---------- .../indico_payment_paypal/plugin.py | 13 +++----- .../templates/event_payment_form.html | 6 ++-- .../templates/transaction_details.html | 16 +++++++--- payment_paypal/tests/controllers_test.py | 16 +++++----- 6 files changed, 44 insertions(+), 41 deletions(-) diff --git a/payment_paypal/indico_payment_paypal/blueprint.py b/payment_paypal/indico_payment_paypal/blueprint.py index 114f862..37b6faf 100644 --- a/payment_paypal/indico_payment_paypal/blueprint.py +++ b/payment_paypal/indico_payment_paypal/blueprint.py @@ -21,7 +21,7 @@ from indico_payment_paypal.controllers import RHPaypalIPN, RHPaypalCancel, RHPay blueprint = IndicoPluginBlueprint('payment_paypal', __name__, - url_prefix='/event//registration/payment/response/paypal') + url_prefix='/event//registrations//payment/response/cern') blueprint.add_url_rule('/cancel', 'cancel', RHPaypalCancel, methods=('GET', 'POST')) blueprint.add_url_rule('/success', 'success', RHPaypalSuccess, methods=('GET', 'POST')) diff --git a/payment_paypal/indico_payment_paypal/controllers.py b/payment_paypal/indico_payment_paypal/controllers.py index e4e57f0..86589d0 100644 --- a/payment_paypal/indico_payment_paypal/controllers.py +++ b/payment_paypal/indico_payment_paypal/controllers.py @@ -23,13 +23,13 @@ from flask import request, flash, redirect from flask_pluginengine import current_plugin from werkzeug.exceptions import BadRequest -from indico.modules.payment.models.transactions import PaymentTransaction, TransactionAction +from indico.modules.events.registration.models.registrations import Registration +from indico.modules.events.registration.controllers.display import RHRegistrationFormRegistrationBase +from indico.modules.payment.models.transactions import TransactionAction from indico.modules.payment.notifications import notify_amount_inconsistency -from indico.modules.payment.util import register_transaction, get_registrant_params +from indico.modules.payment.util import register_transaction from indico.web.flask.util import url_for -from MaKaC.conference import ConferenceHolder from MaKaC.webinterface.rh.base import RH -from MaKaC.webinterface.rh.registrationFormDisplay import RHRegistrationFormRegistrantBase from indico_payment_paypal import _ @@ -46,11 +46,9 @@ class RHPaypalIPN(RH): """Process the notification sent by the PayPal""" def _checkParams(self): - self.event = ConferenceHolder().getById(request.view_args['confId']) - self.registrant = self.event.getRegistrantById(request.args['registrantId']) - if self.registrant is None: - raise BadRequest - if self.registrant.getRandomId() != request.args['authkey']: + self.token = request.args['token'] + self.registration = Registration.find_first(uuid=self.token) + if not self.registration: raise BadRequest def _process(self): @@ -78,7 +76,7 @@ class RHPaypalIPN(RH): "Data received: {}".format(payment_status, request.form)) return self._verify_amount() - register_transaction(registrant=self.registrant, + register_transaction(registration=self.registration, amount=float(request.form['mc_gross']), currency=request.form['mc_currency'], action=paypal_transaction_action_mapping[payment_status], @@ -95,33 +93,33 @@ class RHPaypalIPN(RH): return False def _verify_amount(self): - expected = self.registrant.getTotal() + expected = self.registration.price amount = float(request.form['mc_gross']) if expected == amount: return True current_plugin.logger.warning("Paid amount doesn't match event's fee: {} != {}".format(amount, expected)) - notify_amount_inconsistency(self.registrant, amount) + notify_amount_inconsistency(self.registration, amount) return False def _is_transaction_duplicated(self): - transaction = PaymentTransaction.find_latest_for_registrant(self.registrant) + transaction = self.registration.transaction if not transaction or transaction.provider != 'paypal': return False return (transaction.data['payment_status'] == request.form.get('payment_status') and transaction.data['txn_id'] == request.form.get('txn_id')) -class RHPaypalSuccess(RHRegistrationFormRegistrantBase): +class RHPaypalSuccess(RHRegistrationFormRegistrationBase): """Confirmation message after successful payment""" def _process(self): flash(_('Your payment request has been processed.'), 'success') - return redirect(url_for('event.confRegistrationFormDisplay', self._conf, **get_registrant_params())) + return redirect(url_for('event_registration.display_regform', self.registration.locator.registrant)) -class RHPaypalCancel(RHRegistrationFormRegistrantBase): +class RHPaypalCancel(RHRegistrationFormRegistrationBase): """Cancellation message""" def _process(self): flash(_('You cancelled the payment process.'), 'info') - return redirect(url_for('event.confRegistrationFormDisplay', self._conf, **get_registrant_params())) + return redirect(url_for('event_registration.display_regform', self.registration.locator.registrant)) diff --git a/payment_paypal/indico_payment_paypal/plugin.py b/payment_paypal/indico_payment_paypal/plugin.py index 4ffa94e..811af1d 100644 --- a/payment_paypal/indico_payment_paypal/plugin.py +++ b/payment_paypal/indico_payment_paypal/plugin.py @@ -22,7 +22,6 @@ from wtforms.validators import DataRequired, Optional from indico.core.plugins import IndicoPlugin, url_for_plugin from indico.modules.payment import PaymentPluginMixin, PaymentPluginSettingsFormBase, PaymentEventSettingsFormBase -from indico.modules.payment.util import get_registrant_params from indico.util.string import remove_accents from indico_payment_paypal import _ @@ -62,11 +61,9 @@ class PaypalPaymentPlugin(PaymentPluginMixin, IndicoPlugin): def adjust_payment_form_data(self, data): event = data['event'] - registrant = data['registrant'] - params = get_registrant_params() - data['item_name'] = '{}: registration for {}'.format(remove_accents(registrant.getFullName()), + registration = data['registration'] + data['item_name'] = '{}: registration for {}'.format(remove_accents(registration.full_name), remove_accents(event.getTitle())) - data['return_url'] = url_for_plugin('payment_paypal.success', event, _external=True, **params) - data['cancel_url'] = url_for_plugin('payment_paypal.cancel', event, _external=True, **params) - data['notify_url'] = url_for_plugin('payment_paypal.notify', registrant, authkey=registrant.getRandomId(), - _external=True) + data['return_url'] = url_for_plugin('payment_paypal.success', registration.locator.uuid, _external=True) + data['cancel_url'] = url_for_plugin('payment_paypal.cancel', registration.locator.uuid, _external=True) + data['notify_url'] = url_for_plugin('payment_paypal.notify', registration.locator.uuid, _external=True) diff --git a/payment_paypal/indico_payment_paypal/templates/event_payment_form.html b/payment_paypal/indico_payment_paypal/templates/event_payment_form.html index e0acce2..7329138 100644 --- a/payment_paypal/indico_payment_paypal/templates/event_payment_form.html +++ b/payment_paypal/indico_payment_paypal/templates/event_payment_form.html @@ -2,9 +2,9 @@ Clicking on the Pay now button you will get redirected to the P
{% trans %}First name{% endtrans %}
-
{{ registrant.getFirstName() }}
+
{{ registration.first_name }}
{% trans %}Last name{% endtrans %}
-
{{ registrant.getFamilyName() }}
+
{{ registration.last_name }}
{% trans %}Total amount{% endtrans %}
{{ format_currency(amount, currency, locale=session.lang) }}
@@ -16,7 +16,7 @@ Clicking on the Pay now button you will get redirected to the P - + diff --git a/payment_paypal/indico_payment_paypal/templates/transaction_details.html b/payment_paypal/indico_payment_paypal/templates/transaction_details.html index 3e55cbf..fc9c3a4 100644 --- a/payment_paypal/indico_payment_paypal/templates/transaction_details.html +++ b/payment_paypal/indico_payment_paypal/templates/transaction_details.html @@ -13,11 +13,19 @@ {% endblock %} {% block warning_box %} - {% if transaction.amount != registrant.getTotal() %} + {% if transaction.amount != transaction.registration.price %} {% call warning_message() %} -

{% trans %}The paid amount does not match the required amount. Please contact the registrant to solve the issue.{% endtrans %}

-

{% trans %}Paid: {% endtrans %}{{ format_currency(transaction.amount, transaction.currency, locale=session.lang) }}

-

{% trans %}Required: {% endtrans %}{{ format_currency(registrant.getTotal(), transaction.currency, locale=session.lang) }}

+

+ {%- trans %}The paid amount does not match the required amount. Please contact the registrant to solve the issue.{% endtrans -%} +

+

+ {%- trans %}Paid: {% endtrans -%} + {{- format_currency(transaction.amount, transaction.currency, locale=session.lang) -}} +

+

+ {%- trans %}Required: {% endtrans -%} + {{- format_currency(transaction.registration.price, transaction.currency, locale=session.lang) -}} +

{% endcall %} {% endif %} {% endblock %} diff --git a/payment_paypal/tests/controllers_test.py b/payment_paypal/tests/controllers_test.py index 3451058..361a411 100644 --- a/payment_paypal/tests/controllers_test.py +++ b/payment_paypal/tests/controllers_test.py @@ -47,8 +47,8 @@ def test_ipn_verify_amount(mocker, amount, expected): nai = mocker.patch('indico_payment_paypal.controllers.notify_amount_inconsistency') rh = RHPaypalIPN() rh.event = MagicMock(id=1) - rh.registrant = MagicMock() - rh.registrant.getTotal.return_value = 13.37 + rh.registration = MagicMock() + rh.registration.price = 13.37 request.form = {'mc_gross': amount} with PaypalPaymentPlugin.instance.plugin_context(): assert rh._verify_amount() == expected @@ -63,13 +63,13 @@ def test_ipn_verify_amount(mocker, amount, expected): ('123456', 'Pending', False), )) def test_ipn_is_transaction_duplicated(mocker, txn_id, payment_status, expected): - flfr = mocker.patch('indico_payment_paypal.controllers.PaymentTransaction.find_latest_for_registrant') request.form = {'payment_status': 'Completed', 'txn_id': '12345'} rh = RHPaypalIPN() - rh.registrant = MagicMock() - flfr.return_value = None + rh.registration = MagicMock() + rh.registration.transaction = None assert not rh._is_transaction_duplicated() - flfr.return_value = PaymentTransaction(provider='paypal', data={'payment_status': payment_status, 'txn_id': txn_id}) + transaction = PaymentTransaction(provider='paypal', data={'payment_status': payment_status, 'txn_id': txn_id}) + rh.registration.transaction = transaction assert rh._is_transaction_duplicated() == expected @@ -90,8 +90,8 @@ def test_ipn_process(mocker, fail): rh = RHPaypalIPN() rh._is_transaction_duplicated = lambda: fail == 'dup_txn' rh.event = MagicMock(id=1) - rh.registrant = MagicMock() - rh.registrant.getTotal.return_value = 10.00 + rh.registration = MagicMock() + rh.registration.getTotal.return_value = 10.00 payment_status = {'fail': 'Failed', 'refund': 'Refunded', 'status': 'Foobar'}.get(fail, 'Completed') amount = '-10.00' if fail == 'negative' else '10.00' request.view_args = {'confId': rh.event.id}