Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add forms and templates for payments
Pacu25fc37d67288e
diff --git a/saleor/payment/gateways/braintree/forms.py b/saleor/payment/gateways/braintree/forms.pynew file mode 100644index 0000000000..6efa3402ef--- /dev/null+++ b/saleor/payment/gateways/braintree/forms.py@@ -0,0 +1,52 @@+from django import forms+from django.core.exceptions import ValidationError+from django.utils.translation import pgettext_lazy++from ... import PaymentError+from ...forms import PaymentForm+++class BraintreePaymentForm(PaymentForm):+ amount = forms.DecimalField()++ # Unique transaction identifier returned by Braintree+ # for testing in the sandbox mode please refer to+ # https://developers.braintreepayments.com/reference/general/testing/python#nonces-representing-cards+ # as it's values should be hardcoded to simulate each payment gateway+ # response+ payment_method_nonce = forms.CharField()++ def __init__(self, *args, **kwargs):+ super().__init__(*args, **kwargs)+ self.fields['amount'].initial = self.payment.total+ # FIXME if environment is Sandbox, we could provide couple of predefined+ # nounces for easier testing++ def clean(self):+ cleaned_data = super().clean()+ # Amount is sent client-side+ # authorizing different amount than payments' total could happen only+ # when manually adjusting the template value as we do not allow+ # partial-payments at this moment, error is returned instead.+ amount = cleaned_data.get('amount')+ if amount and amount != self.payment.total:+ msg = pgettext_lazy(+ 'payment error',+ 'Unable to process transaction. Please try again in a moment')+ raise ValidationError(msg)+ cleaned_data['payment_method_nonce'] = 'fake-valid-nonce' # FIXME remove after testing+ return cleaned_data++ def process_payment(self):+ # FIXME add tests+ payment_token = self.cleaned_data['payment_method_nonce']+ self.payment.token = payment_token+ self.payment.save(update_fields=['token'])+ self.payment.authorize(payment_token)+ try:+ self.payment.capture(self.payment.total)+ except PaymentError as exc:+ # Void authorization if the capture failed+ self.payment.void()+ raise exc+ return self.paymentdiff --git a/saleor/payment/gateways/dummy/forms.py b/saleor/payment/gateways/dummy/forms.pynew file mode 100644index 0000000000..6b2d5e22fe--- /dev/null+++ b/saleor/payment/gateways/dummy/forms.py@@ -0,0 +1,26 @@+from ...forms import PaymentForm+from django import forms+from django.utils.translation import pgettext_lazy++from ... import ChargeStatus+++class DummyPaymentForm(PaymentForm):+ charge_status = forms.ChoiceField(+ label=pgettext_lazy('Payment status form field', 'Payment status'),+ choices=ChargeStatus.CHOICES, initial=ChargeStatus.NOT_CHARGED,+ widget=forms.RadioSelect)+++ def process_payment(self):+ # FIXME add tests+ # Dummy provider requires no real token+ fake_token = self.gateway.get_client_token(**self.gateway_params)+ self.payment.authorize(fake_token)+ charge_status = self.cleaned_data['charge_status']+ if charge_status == ChargeStatus.NOT_CHARGED:+ return+ self.payment.capture()+ if charge_status == ChargeStatus.FULLY_REFUNDED:+ self.payment.refund()+ return self.paymentdiff --git a/saleor/payment/migrations/0001_initial.py b/saleor/payment/migrations/0001_initial.pyindex f01fef5fda..70f3966f61 100644--- a/saleor/payment/migrations/0001_initial.py+++ b/saleor/payment/migrations/0001_initial.py@@ -7,6 +7,7 @@ import django_prices.modelsimport saleor.corefrom decimal import Decimalimport django.core.validators+import saleor.paymentclass Migration(migrations.Migration):@@ -47,6 +48,7 @@ class Migration(migrations.Migration):('cc_last_digits', models.CharField(blank=True, default='', max_length=4)),('extra_data', models.TextField(blank=True, default='')),('token', models.CharField(blank=True, default='', max_length=128)),+ ('error', models.CharField(choices=[(saleor.payment.TransactionError('incorrect_number'), 'incorrect_number'), (saleor.payment.TransactionError('invalid_number'), 'invalid_number'), (saleor.payment.TransactionError('incorrect_cvv'), 'incorrect_cvv'), (saleor.payment.TransactionError('invalid_cvv'), 'invalid_cvv'), (saleor.payment.TransactionError('incorrect_zip'), 'incorrect_zip'), (saleor.payment.TransactionError('incorrect_address'), 'incorrect_address'), (saleor.payment.TransactionError('invalid_expiry_date'), 'invalid_expiry_date'), (saleor.payment.TransactionError('expired'), 'expired'), (saleor.payment.TransactionError('processing_error'), 'processing_error'), (saleor.payment.TransactionError('declined'), 'declined')], max_length=256, null=True)),('currency', models.CharField(max_length=10)),('total', models.DecimalField(decimal_places=2, default=Decimal('0.0'), max_digits=12)),('captured_amount', models.DecimalField(decimal_places=2, default=Decimal('0.0'), max_digits=12)),diff --git a/templates/order/payment/dummy.html b/templates/order/payment/dummy.htmlnew file mode 100644index 0000000000..0c9a1a3ec4--- /dev/null+++ b/templates/order/payment/dummy.html@@ -0,0 +1,26 @@+{% extends "order/payment/details.html" %}+{% load bootstrap_form from bootstrap4 %}+{% load i18n %}++{% block forms %}+<form method="POST" id="payment-form" class="form-horizontal"{% if form.action %} action="{{ form.action }}"{% endif %} novalidate>+ {% csrf_token %}+ {{ form.media }}+ {% bootstrap_form form layout="vertical" %}+ {% if form.errors %}+ <a class="btn" href="{% url "order:details" token=payment.order.token %}">+ {% trans "Change payment" context "Payment form (Stripe) secondary action" %}+ </a>+ {% endif %}+ {% if not form.autosubmit %}+ <div class="form-group">+ <div class="col-sm-2"></div>+ <div class="col-sm-10">+ <button type="submit" class="btn primary">+ {% trans "Make payment" context "Payment form primary action" %}+ </button>+ </div>+ </div>+ {% endif %}+</form>+{% endblock forms %}