mirror of
https://github.com/lucaspalomodevelop/indico-plugins.git
synced 2026-03-12 23:27:22 +00:00
Payment/Paypal: Adapt to new registration module
This commit is contained in:
parent
d2b05cd2c3
commit
5c124740c0
@ -21,7 +21,7 @@ from indico_payment_paypal.controllers import RHPaypalIPN, RHPaypalCancel, RHPay
|
||||
|
||||
|
||||
blueprint = IndicoPluginBlueprint('payment_paypal', __name__,
|
||||
url_prefix='/event/<confId>/registration/payment/response/paypal')
|
||||
url_prefix='/event/<confId>/registrations/<int:reg_form_id>/payment/response/cern')
|
||||
|
||||
blueprint.add_url_rule('/cancel', 'cancel', RHPaypalCancel, methods=('GET', 'POST'))
|
||||
blueprint.add_url_rule('/success', 'success', RHPaypalSuccess, methods=('GET', 'POST'))
|
||||
|
||||
@ -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))
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -2,9 +2,9 @@ Clicking on the <strong>Pay now</strong> button you will get redirected to the P
|
||||
|
||||
<dl class="i-data-list">
|
||||
<dt>{% trans %}First name{% endtrans %}</dt>
|
||||
<dd>{{ registrant.getFirstName() }}</dd>
|
||||
<dd>{{ registration.first_name }}</dd>
|
||||
<dt>{% trans %}Last name{% endtrans %}</dt>
|
||||
<dd>{{ registrant.getFamilyName() }}</dd>
|
||||
<dd>{{ registration.last_name }}</dd>
|
||||
<dt>{% trans %}Total amount{% endtrans %}</dt>
|
||||
<dd>{{ format_currency(amount, currency, locale=session.lang) }}</dd>
|
||||
<dt></dt>
|
||||
@ -16,7 +16,7 @@ Clicking on the <strong>Pay now</strong> button you will get redirected to the P
|
||||
<input type="hidden" name="amount" value="{{ amount }}">
|
||||
<input type="hidden" name="currency_code" value="{{ currency }}">
|
||||
<input type="hidden" name="charset" value="utf-8">
|
||||
<input type="hidden" name="invoice" value="{{ event.getId() }}_{{ registrant.getId() }}">
|
||||
<input type="hidden" name="invoice" value="{{ registration.id }}">
|
||||
<input type="hidden" name="return" value="{{ return_url }}">
|
||||
<input type="hidden" name="cancel_return" value="{{ cancel_url }}">
|
||||
<input type="hidden" name="notify_url" value="{{ notify_url }}">
|
||||
|
||||
@ -13,11 +13,19 @@
|
||||
{% endblock %}
|
||||
|
||||
{% block warning_box %}
|
||||
{% if transaction.amount != registrant.getTotal() %}
|
||||
{% if transaction.amount != transaction.registration.price %}
|
||||
{% call warning_message() %}
|
||||
<p>{% trans %}The paid amount does not match the required amount. Please contact the registrant to solve the issue.{% endtrans %}</p>
|
||||
<p>{% trans %}Paid: {% endtrans %}{{ format_currency(transaction.amount, transaction.currency, locale=session.lang) }}</p>
|
||||
<p>{% trans %}Required: {% endtrans %}{{ format_currency(registrant.getTotal(), transaction.currency, locale=session.lang) }}</p>
|
||||
<p>
|
||||
{%- trans %}The paid amount does not match the required amount. Please contact the registrant to solve the issue.{% endtrans -%}
|
||||
</p>
|
||||
<p>
|
||||
{%- trans %}Paid: {% endtrans -%}
|
||||
{{- format_currency(transaction.amount, transaction.currency, locale=session.lang) -}}
|
||||
</p>
|
||||
<p>
|
||||
{%- trans %}Required: {% endtrans -%}
|
||||
{{- format_currency(transaction.registration.price, transaction.currency, locale=session.lang) -}}
|
||||
</p>
|
||||
{% endcall %}
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
@ -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}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user