diff --git a/cotisations/models.py b/cotisations/models.py index d314f31d..17d8cc2a 100644 --- a/cotisations/models.py +++ b/cotisations/models.py @@ -51,6 +51,8 @@ from re2o.field_permissions import FieldPermissionModelMixin from re2o.mixins import AclMixin, RevMixin from preferences.models import OptionalUser +from cotisations.utils import find_payment_method + # TODO : change facture to invoice class Facture(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model): @@ -643,8 +645,9 @@ class Paiement(RevMixin, AclMixin, models.Model): :returns: An `HttpResponse`-like object. """ - if hasattr(self, 'payment_method') and use_payment_method: - return self.payment_method.end_payment(invoice, request) + payment_method = find_payment_method(self) + if payment_method is not None and use_payment_method: + return payment_method.end_payment(invoice, request) # In case a cotisation was bought, inform the user, the # cotisation time has been extended too diff --git a/cotisations/payment_methods/__init__.py b/cotisations/payment_methods/__init__.py index 36904411..a85fa49c 100644 --- a/cotisations/payment_methods/__init__.py +++ b/cotisations/payment_methods/__init__.py @@ -1,25 +1,6 @@ -from django.conf.urls import include, url -from django.utils.translation import ugettext_lazy as _l - -from . import comnpay, cheque - - -urlpatterns = [ - url(r'^comnpay/', include(comnpay.urls, namespace='comnpay')), - url(r'^cheque/', include(cheque.urls, namespace='cheque')), -] +from . import comnpay, cheque, urls PAYMENT_METHODS = [ comnpay, cheque, ] - - -def find_payment_method(payment): - for method in PAYMENT_METHODS: - try: - o = method.PaymentMethod.objects.get(payment=payment) - return o - except method.PaymentMethod.DoesNotExist: - pass - return None diff --git a/cotisations/payment_methods/comnpay/models.py b/cotisations/payment_methods/comnpay/models.py index 68f883d4..466f6d14 100644 --- a/cotisations/payment_methods/comnpay/models.py +++ b/cotisations/payment_methods/comnpay/models.py @@ -31,5 +31,5 @@ class ComnpayPayment(PaymentMethodMixin, models.Model): def end_payment(self, invoice, request): invoice.valid = False invoice.save() - content = comnpay(invoice, request) + content = comnpay(invoice, request, self) return render(request, 'cotisations/payment.html', content) diff --git a/cotisations/payment_methods/comnpay/views.py b/cotisations/payment_methods/comnpay/views.py index 094ebff4..85b237ff 100644 --- a/cotisations/payment_methods/comnpay/views.py +++ b/cotisations/payment_methods/comnpay/views.py @@ -114,7 +114,7 @@ def ipn(request): return HttpResponse("HTTP/1.0 200 OK") -def comnpay(facture, request): +def comnpay(facture, request, payment): """ Build a request to start the negociation with Comnpay by using a facture id, the price and the secret transaction data stored in @@ -122,14 +122,14 @@ def comnpay(facture, request): """ host = request.get_host() p = Transaction( - str(AssoOption.get_cached_value('payment_id')), - str(AssoOption.get_cached_value('payment_pass')), + str(payment.payment_credential), + str(payment.payment_pass), 'https://' + host + reverse( - 'cotisations:comnpay_accept_payment', + 'cotisations:comnpay:accept_payment', kwargs={'factureid': facture.id} ), - 'https://' + host + reverse('cotisations:comnpay_refuse_payment'), - 'https://' + host + reverse('cotisations:comnpay_ipn'), + 'https://' + host + reverse('cotisations:comnpay:refuse_payment'), + 'https://' + host + reverse('cotisations:comnpay:ipn'), "", "D" ) diff --git a/cotisations/payment_methods/forms.py b/cotisations/payment_methods/forms.py index 2acc80f0..f5bf4f76 100644 --- a/cotisations/payment_methods/forms.py +++ b/cotisations/payment_methods/forms.py @@ -2,8 +2,8 @@ from django import forms from django.utils.translation import ugettext as _ from django.utils.translation import ugettext_lazy as _l -from . import PAYMENT_METHODS, find_payment_method - +from . import PAYMENT_METHODS +from cotisations.utils import find_payment_method def payment_method_factory(payment, *args, **kwargs): payment_method = kwargs.pop('instance', find_payment_method(payment)) diff --git a/cotisations/payment_methods/urls.py b/cotisations/payment_methods/urls.py new file mode 100644 index 00000000..9d35846f --- /dev/null +++ b/cotisations/payment_methods/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import include, url +from . import comnpay, cheque + +urlpatterns = [ + url(r'^comnpay/', include(comnpay.urls, namespace='comnpay')), + url(r'^cheque/', include(cheque.urls, namespace='cheque')), +] diff --git a/cotisations/urls.py b/cotisations/urls.py index 0603c96e..9e318b53 100644 --- a/cotisations/urls.py +++ b/cotisations/urls.py @@ -144,5 +144,5 @@ urlpatterns = [ name='recharge' ), url(r'^$', views.index, name='index'), -] + payment_methods.urlpatterns +] + payment_methods.urls.urlpatterns diff --git a/cotisations/utils.py b/cotisations/utils.py new file mode 100644 index 00000000..01881af3 --- /dev/null +++ b/cotisations/utils.py @@ -0,0 +1,9 @@ +def find_payment_method(payment): + from cotisations.payment_methods import PAYMENT_METHODS + for method in PAYMENT_METHODS: + try: + o = method.PaymentMethod.objects.get(payment=payment) + return o + except method.PaymentMethod.DoesNotExist: + pass + return None