Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Apply changes after review
Maciej Korycinski20d7d3ca5b0e
diff --git a/saleor/payment/gateways/braintree/__init__.py b/saleor/payment/gateways/braintree/__init__.pyindex 3db0cb3484..0844300348 100644--- a/saleor/payment/gateways/braintree/__init__.py+++ b/saleor/payment/gateways/braintree/__init__.py@@ -5,7 +5,7 @@ import braintree as braintree_sdkfrom django.core.exceptions import ImproperlyConfiguredfrom django.utils.translation import pgettext_lazy-from ...interface import GatewayResponse, PaymentInformation+from ...interface import GatewayResponse, PaymentDatafrom .errors import DEFAULT_ERROR_MESSAGE, BraintreeExceptionfrom .forms import BraintreePaymentForm@@ -39,20 +39,20 @@ class TransactionKind:VOID = 'void'-def get_customer_data(payment_information: PaymentInformation) -> Dict:+def get_customer_data(payment_information: PaymentData) -> Dict:billing = payment_information.billingreturn {'order_id': payment_information.order_id,'billing': {- 'first_name': billing['first_name'],- 'last_name': billing['last_name'],- 'company': billing['company_name'],- 'postal_code': billing['postal_code'],- 'street_address': billing['street_address_1'],- 'extended_address': billing['street_address_2'],- 'locality': billing['city'],- 'region': billing['country_area'],- 'country_code_alpha2': billing['country']},+ 'first_name': billing.first_name,+ 'last_name': billing.last_name,+ 'company': billing.company_name,+ 'postal_code': billing.postal_code,+ 'street_address': billing.street_address_1,+ 'extended_address': billing.street_address_2,+ 'locality': billing.city,+ 'region': billing.country_area,+ 'country_code_alpha2': billing.country},'risk_data': {'customer_ip': payment_information.customer_ip_address or ''},'customer': {'email': payment_information.customer_email}}@@ -121,7 +121,7 @@ def get_client_token(connection_params: Dict) -> str:def authorize(- payment_information: PaymentInformation, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:gateway = get_braintree_gateway(**connection_params)@@ -154,7 +154,7 @@ def authorize(def capture(- payment_information: PaymentInformation, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:gateway = get_braintree_gateway(**connection_params)@@ -183,7 +183,7 @@ def capture(def void(- payment_information: PaymentInformation, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:gateway = get_braintree_gateway(**connection_params)@@ -240,7 +240,7 @@ def refund(def process_payment(- payment_information: PaymentInformation, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:auth_resp = authorize(payment_information, connection_params)if auth_resp.is_success:diff --git a/saleor/payment/gateways/braintree/forms.py b/saleor/payment/gateways/braintree/forms.pyindex 7620a88975..132795d972 100644--- a/saleor/payment/gateways/braintree/forms.py+++ b/saleor/payment/gateways/braintree/forms.py@@ -2,7 +2,7 @@ from django import formsfrom django.core.exceptions import ValidationErrorfrom django.utils.translation import pgettext_lazy-from ...interface import PaymentInformation+from ...interface import PaymentDataclass BraintreePaymentForm(forms.Form):@@ -16,7 +16,7 @@ class BraintreePaymentForm(forms.Form):payment_method_nonce = forms.CharField()def __init__(- self, payment_information: PaymentInformation, *args, **kwargs):+ self, payment_information: PaymentData, *args, **kwargs):super().__init__(*args, **kwargs)self.payment_information = payment_informationself.fields['amount'].initial = payment_information.amountdiff --git a/saleor/payment/gateways/dummy/__init__.py b/saleor/payment/gateways/dummy/__init__.pyindex e86c5f7563..563144a033 100644--- a/saleor/payment/gateways/dummy/__init__.py+++ b/saleor/payment/gateways/dummy/__init__.py@@ -2,7 +2,7 @@ import uuidfrom typing import Dict, Listfrom ... import ChargeStatus-from ...interface import GatewayResponse+from ...interface import GatewayResponse, PaymentDatafrom .forms import DummyPaymentFormTEMPLATE_PATH = 'order/payment/dummy.html'@@ -29,7 +29,7 @@ def create_form(data, payment_information, connection_params):def authorize(- payment_information: Dict, connection_params) -> GatewayResponse:+ payment_information: PaymentData, connection_params) -> GatewayResponse:success = dummy_success()error = Noneif not success:@@ -45,7 +45,7 @@ def authorize(def void(- payment_information: Dict, connection_params) -> GatewayResponse:+ payment_information: PaymentData, connection_params) -> GatewayResponse:error = Nonesuccess = dummy_success()if not success:@@ -61,7 +61,7 @@ def void(def capture(- payment_information: Dict, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:error = Nonesuccess = dummy_success()@@ -79,7 +79,8 @@ def capture(def refund(- payment_information: Dict, connection_params: Dict) -> GatewayResponse:+ payment_information: PaymentData, connection_params: Dict+) -> GatewayResponse:error = Nonesuccess = dummy_success()if not success:@@ -95,7 +96,8 @@ def refund(def charge(- payment_information: Dict, connection_params) -> GatewayResponse:+ payment_information: PaymentData, connection_params+) -> GatewayResponse:"""Performs Authorize and Capture transactions in a single run."""auth_resp = authorize(payment_information, connection_params)if not auth_resp.is_success:@@ -104,7 +106,8 @@ def charge(def process_payment(- payment_information: Dict, connection_params) -> GatewayResponse:+ payment_information: PaymentData, connection_params+) -> GatewayResponse:"""Process the payment."""token = payment_information.tokendiff --git a/saleor/payment/gateways/razorpay/__init__.py b/saleor/payment/gateways/razorpay/__init__.pyindex ef6ba04075..500a60c206 100644--- a/saleor/payment/gateways/razorpay/__init__.py+++ b/saleor/payment/gateways/razorpay/__init__.py@@ -6,8 +6,8 @@ from typing import Dictimport razorpayimport razorpay.errors+from ...interface import GatewayResponse, PaymentDatafrom . import errors-from ...interface import GatewayResponse, PaymentInformationfrom .forms import RazorPaymentFormfrom .utils import get_amount_for_razorpay, get_error_response@@ -37,7 +37,7 @@ class TransactionKind:def _generate_response(- payment_information: PaymentInformation, kind: str, data: Dict+ payment_information: PaymentData, kind: str, data: Dict) -> GatewayResponse:"""Generate Saleor transaction information fromRazorpay's success payload or from passed data."""@@ -52,7 +52,7 @@ def _generate_response()-def check_payment_supported(payment_information: PaymentInformation):+def check_payment_supported(payment_information: PaymentData):"""Checks that a given payment is supported."""if payment_information.currency not in SUPPORTED_CURRENCIES:return errors.UNSUPPORTED_CURRENCY % {@@ -95,7 +95,7 @@ def get_client_token(**_):def charge(- payment_information: PaymentInformation, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:"""Charge a authorized payment using the razorpay client.@@ -131,7 +131,7 @@ def charge(def refund(- payment_information: PaymentInformation, connection_params+ payment_information: PaymentData, connection_params) -> GatewayResponse:"""Refund a payment using the razorpay client.@@ -166,7 +166,7 @@ def refund(def process_payment(- payment_information: PaymentInformation, connection_params+ payment_information: PaymentData, connection_params) -> GatewayResponse:return charge(payment_information=payment_information,diff --git a/saleor/payment/gateways/razorpay/forms.py b/saleor/payment/gateways/razorpay/forms.pyindex 79f9d0ac2b..90f9220841 100644--- a/saleor/payment/gateways/razorpay/forms.py+++ b/saleor/payment/gateways/razorpay/forms.py@@ -4,7 +4,7 @@ from django.forms.widgets import HiddenInputfrom django.utils.html import format_htmlfrom django.utils.translation import pgettext_lazy-from ...interface import PaymentInformation+from ...interface import PaymentDatafrom .utils import get_amount_for_razorpayCHECKOUT_SCRIPT_URL = 'https://checkout.razorpay.com/v1/checkout.js'@@ -19,7 +19,7 @@ class RazorPayCheckoutWidget(HiddenInput):def __init__(self, *,- payment_information: PaymentInformation,+ payment_information: PaymentData,public_key: str,prefill: bool,store_name: str,@@ -39,8 +39,8 @@ class RazorPayCheckoutWidget(HiddenInput):if prefill:customer_name = '%s %s' % (- payment_information.billing['last_name'],- payment_information.billing['first_name'])+ payment_information.billing.last_name,+ payment_information.billing.first_name)base_attrs.update({'data-prefill.name': customer_name,'data-prefill.email': payment_information.customer_email})diff --git a/saleor/payment/gateways/stripe/__init__.py b/saleor/payment/gateways/stripe/__init__.pyindex 63c6475885..0fcb680c3e 100644--- a/saleor/payment/gateways/stripe/__init__.py+++ b/saleor/payment/gateways/stripe/__init__.py@@ -2,7 +2,7 @@ from typing import Dictimport stripe-from ...interface import GatewayResponse, PaymentInformation+from ...interface import GatewayResponse, PaymentDatafrom .forms import StripePaymentModalFormfrom .utils import (get_amount_for_stripe, get_amount_from_stripe, get_currency_for_stripe,@@ -28,7 +28,7 @@ def get_client_token(**_):def authorize(- payment_information: PaymentInformation, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:client, error = _get_client(**connection_params), None@@ -48,7 +48,7 @@ def authorize(def capture(- payment_information: PaymentInformation, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:client, error = _get_client(**connection_params), None@@ -72,7 +72,7 @@ def capture(def charge(- payment_information: PaymentInformation, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:client, error = _get_client(**connection_params), None@@ -92,7 +92,7 @@ def charge(def refund(- payment_information: PaymentInformation, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:client, error = _get_client(**connection_params), None@@ -117,7 +117,7 @@ def refund(def void(- payment_information: PaymentInformation, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:client, error = _get_client(**connection_params), None@@ -136,13 +136,13 @@ def void(def process_payment(- payment_information: PaymentInformation, connection_params: Dict+ payment_information: PaymentData, connection_params: Dict) -> GatewayResponse:return charge(payment_information, connection_params)def create_form(- data: Dict, payment_information: PaymentInformation,+ data: Dict, payment_information: PaymentData,connection_params: Dict) -> StripePaymentModalForm:return StripePaymentModalForm(data=data, payment_information=payment_information,@@ -155,7 +155,7 @@ def _get_client(**connection_params):def _get_stripe_charge_payload(- payment_information: PaymentInformation, should_capture: bool) -> Dict:+ payment_information: PaymentData, should_capture: bool) -> Dict:shipping = payment_information.shipping# Get currency@@ -193,7 +193,7 @@ def _create_stripe_charge(client, payment_information, should_capture: bool):def _create_response(- payment_information: PaymentInformation, kind: str, response: Dict,+ payment_information: PaymentData, kind: str, response: Dict,error: str) -> GatewayResponse:# Get currency from response or paymentcurrency = get_currency_from_stripe(diff --git a/saleor/payment/gateways/stripe/forms.py b/saleor/payment/gateways/stripe/forms.pyindex 87bf7bba05..091ec2863a 100644--- a/saleor/payment/gateways/stripe/forms.py+++ b/saleor/payment/gateways/stripe/forms.py@@ -6,7 +6,7 @@ from django.forms.widgets import HiddenInputfrom django.utils.html import format_htmlfrom django.utils.translation import pgettext_lazy-from ...interface import PaymentInformation+from ...interface import PaymentDatafrom .utils import get_amount_for_stripeCHECKOUT_SCRIPT_URL = 'https://checkout.stripe.com/checkout.js'@@ -17,7 +17,7 @@ CHECKOUT_DESCRIPTION = pgettext_lazy(class StripeCheckoutWidget(HiddenInput):def __init__(- self, payment_information: PaymentInformation,+ self, payment_information: PaymentData,gateway_params: Dict, *args, **kwargs):attrs = kwargs.get('attrs', {})kwargs['attrs'] = {@@ -65,7 +65,7 @@ class StripePaymentModalForm(forms.Form):stripeToken = forms.CharField(required=True, widget=HiddenInput)def __init__(- self, payment_information: PaymentInformation,+ self, payment_information: PaymentData,gateway_params: Dict, *args, **kwargs):super().__init__(*args, **kwargs)diff --git a/saleor/payment/gateways/stripe/utils.py b/saleor/payment/gateways/stripe/utils.pyindex 88b9d5a847..a28b448bd0 100644--- a/saleor/payment/gateways/stripe/utils.py+++ b/saleor/payment/gateways/stripe/utils.py@@ -7,7 +7,7 @@ from django_countries import countries# Since there is no public API in Stripe backend or helper function# in Stripe's Python library, this list is straight out of Stripe's docs# https://stripe.com/docs/currencies#zero-decimal-from ...interface import PaymentInformation+from ...interface import PaymentDataZERO_DECIMAL_CURRENCIES = ['BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA',@@ -58,18 +58,18 @@ def get_currency_from_stripe(currency):def get_payment_billing_fullname(- payment_information: PaymentInformation) -> str:+ payment_information: PaymentData) -> str:# Get billing name from paymentreturn '%s %s' % (- payment_information.billing['last_name'],- payment_information.billing['first_name'])+ payment_information.billing.last_name,+ payment_information.billing.first_name)def shipping_to_stripe_dict(shipping: Dict):return {- 'line1': shipping['street_address_1'],- 'line2': shipping['street_address_2'],- 'city': shipping['city'],- 'state': shipping['country_area'],- 'postal_code': shipping['postal_code'],- 'country': dict(countries).get(shipping['country'], '')}+ 'line1': shipping.street_address_1,+ 'line2': shipping.street_address_2,+ 'city': shipping.city,+ 'state': shipping.country_area,+ 'postal_code': shipping.postal_code,+ 'country': dict(countries).get(shipping.country, '')}diff --git a/saleor/payment/interface.py b/saleor/payment/interface.pyindex 1586e739f0..2374464259 100644--- a/saleor/payment/interface.py+++ b/saleor/payment/interface.py@@ -5,7 +5,9 @@ from typing import Dict, Optional@dataclassclass GatewayResponse:- """Dataclass for storing gateway response"""+ """Dataclass for storing gateway response. Used for unifying the+ representation of gateway response. It is required to communicate between+ Saleor and given payment gateway."""is_success: boolkind: str@@ -17,14 +19,31 @@ class GatewayResponse:@dataclass-class PaymentInformation:- """Dataclass for storing all payment information"""+class AddressData:+ first_name: str+ last_name: str+ company_name: str+ street_address_1: str+ street_address_2: str+ city: str+ city_area: str+ postal_code: str+ country: str+ country_area: str+ phone: str+++@dataclass+class PaymentData:+ """Dataclass for storing all payment information. Used for unifying the+ representation of data. It is required to communicate between Saleor and+ given payment gateway."""token: stramount: Decimalcurrency: str- billing: Optional[Dict[str, str]]- shipping: Optional[Dict[str, str]]+ billing: Optional[AddressData]+ shipping: Optional[AddressData]order_id: intcustomer_ip_address: strcustomer_email: strdiff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex cdf5bc03e3..231412783a 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -18,7 +18,7 @@ from ..order.models import Orderfrom . import (ChargeStatus, CustomPaymentChoices, GatewayError, OperationType,PaymentError, TransactionKind, get_payment_gateway)-from .interface import GatewayResponse, PaymentInformation+from .interface import AddressData, GatewayResponse, PaymentDatafrom .models import Payment, Transactionlogger = logging.getLogger(__name__)@@ -47,7 +47,7 @@ def get_gateway_operation_func(gateway, operation_type):def create_payment_information(payment: Payment, payment_token: str = None,- amount: Decimal = None) -> PaymentInformation:+ amount: Decimal = None) -> PaymentData:"""Extracts order information along with payment details.Returns information required to process payment and additional@@ -56,12 +56,12 @@ def create_payment_information(billing, shipping = None, Noneif payment.order.billing_address:- billing = payment.order.billing_address.as_data()+ billing = AddressData(**payment.order.billing_address.as_data())if payment.order.shipping_address:- shipping = payment.order.shipping_address.as_data()+ shipping = AddressData(**payment.order.shipping_address.as_data())- return PaymentInformation(+ return PaymentData(token=payment_token,amount=amount or payment.total,currency=payment.currency,diff --git a/tests/gateway/test_stripe.py b/tests/gateway/test_stripe.pyindex b89babe2f8..a3a69ac03f 100644--- a/tests/gateway/test_stripe.py+++ b/tests/gateway/test_stripe.py@@ -19,6 +19,7 @@ from saleor.payment.gateways.stripe.forms import (StripeCheckoutWidget, StripePaymentModalForm)from saleor.payment.gateways.stripe.utils import (get_payment_billing_fullname, shipping_to_stripe_dict)+from saleor.payment.interface import AddressDatafrom saleor.payment.utils import create_payment_informationTRANSACTION_AMOUNT = Decimal(42.42)@@ -162,6 +163,7 @@ def test_get_payment_billing_fullname(payment_dummy):def test_shipping_address_to_stripe_dict(address):+ address_data = AddressData(**address.as_data())expected_address_dict = {'line1': address.street_address_1,'line2': address.street_address_2,@@ -169,7 +171,7 @@ def test_shipping_address_to_stripe_dict(address):'state': address.country_area,'postal_code': address.postal_code,'country': dict(countries).get(address.country, '')}- assert shipping_to_stripe_dict(address.as_data()) == expected_address_dict+ assert shipping_to_stripe_dict(address_data) == expected_address_dictdef test_widget_with_default_options(stripe_payment, gateway_params):