Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add stripe payment form and widget
Unknowncb39e9c653fb
diff --git a/saleor/payment/gateways/stripe/__init__.py b/saleor/payment/gateways/stripe/__init__.pyindex bc2546e94d..6745e03d54 100644--- a/saleor/payment/gateways/stripe/__init__.py+++ b/saleor/payment/gateways/stripe/__init__.py@@ -8,11 +8,6 @@ from .forms import StripePaymentModalFormfrom .utils import get_amount_for_stripe, get_amount_from_stripe-# TODO: Add token to transactions and filter charge back from transactions-# TODO: Create a card and add address for fraud detection-# TODO: Add fraud details as https://github.com/mirumee/django-payments/blob/master/payments/stripe/forms.py--def get_client_token(**_):"""Not implemented for stripe gateway currently. The client token cangenerated by Stripe's checkout.js or stripe.js automatically.diff --git a/saleor/payment/gateways/stripe/forms.py b/saleor/payment/gateways/stripe/forms.pynew file mode 100644index 0000000000..bce61de9c6--- /dev/null+++ b/saleor/payment/gateways/stripe/forms.py@@ -0,0 +1,75 @@+from django import forms+from django.forms.utils import flatatt+from django.forms.widgets import HiddenInput+from django.utils.html import format_html+from django.utils.translation import pgettext_lazy++from ...forms import PaymentForm+from .utils import get_amount_for_stripe+++CHECKOUT_SCRIPT_URL = 'https://checkout.stripe.com/checkout.js'+CHECKOUT_DESCRIPTION = pgettext_lazy(+ 'Stripe payment gateway description', 'Total payment')+++class StripeCheckoutWidget(HiddenInput):++ def __init__(self, payment, gateway_params, *args, **kwargs):+ attrs = kwargs.get('attrs', {})+ kwargs['attrs'] = {+ 'class': 'stripe-button',+ 'src': CHECKOUT_SCRIPT_URL,+ 'data-key': gateway_params.get('public_key'),+ 'data-amount': get_amount_for_stripe(+ payment.total, payment.currency),+ 'data-name': gateway_params.get('store_name'),+ 'data-image': gateway_params.get('store_image'),+ 'data-description': CHECKOUT_DESCRIPTION,+ 'data-currency': payment.currency,+ 'data-locale': 'auto',+ 'data-allow-remember-me': gateway_params.get('remember_me'),+ 'data-billing-address': 'true' if gateway_params.get(+ 'enable_billing_address') else 'false',+ 'data-zip-code': 'true' if gateway_params.get(+ 'enable_billing_address') else 'false',+ 'data-shipping-address': 'true' if gateway_params.get(+ 'enable_shipping_address') else 'false',+ }++ if gateway_params.get('prefill'):+ kwargs['attrs'].update({+ 'data-email': payment.billing_email})++ kwargs['attrs'].update(attrs)+ super(StripeCheckoutWidget, self).__init__(*args, **kwargs)++ def render(self, **kwargs):+ attrs = kwargs.setdefault('attrs', {})+ attrs.update(self.attrs)+ del attrs['id']+ return format_html('<script{0}></script>', flatatt(attrs))+++class StripePaymentModalForm(PaymentForm):+ """+ At this moment partial-payment is not supported, but there is no need to+ validate amount, which may be manually adjusted in the template,+ since checkout.js can do that automatically.+ """+ stripeToken = forms.CharField(+ required=True, widget=HiddenInput)++ def __init__(self, *args, **kwargs):+ super(StripePaymentModalForm, self).__init__(*args, **kwargs)++ self.fields['stripe'] = forms.CharField(+ widget=StripeCheckoutWidget(+ payment=self.payment, gateway_params=self.gateway_params),+ required=False)++ def process_payment(self):+ cleaned_data = super(StripePaymentModalForm, self).clean()+ stripe_token = cleaned_data['stripeToken']+ self.payment.charge(payment_token=stripe_token)+ return self.payment