Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Style fixes
Michał Jelonek0c44901e7b6a
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex 7a6bfe1f91..6c2874510d 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -32,8 +32,8 @@ from ...order.utils import update_order_statusfrom ...page.models import Pagefrom ...payment.models import Paymentfrom ...payment.utils import (- get_billing_data, gateway_authorize, gateway_capture, gateway_refund,- gateway_void)+ gateway_authorize, gateway_capture, gateway_refund, gateway_void,+ get_billing_data)from ...product.models import (Attribute, AttributeValue, Category, Collection, Product, ProductImage,ProductType, ProductVariant)diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 90e8c600d1..62b13288ac 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -21,7 +21,7 @@ from ...order.utils import (from ...payment import ChargeStatus, CustomPaymentChoices, PaymentErrorfrom ...payment.models import Paymentfrom ...payment.utils import (- get_billing_data, gateway_capture, gateway_refund, gateway_void)+ gateway_capture, gateway_refund, gateway_void, get_billing_data)from ...product.models import Product, ProductVariantfrom ...product.utils import allocate_stock, deallocate_stockfrom ...shipping.models import ShippingMethoddiff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.pyindex b36c85c07b..43b85c099a 100644--- a/saleor/graphql/order/mutations/orders.py+++ b/saleor/graphql/order/mutations/orders.py@@ -8,7 +8,7 @@ from ....order.utils import cancel_orderfrom ....payment import ChargeStatus, CustomPaymentChoices, PaymentErrorfrom ....payment.models import Paymentfrom ....payment.utils import (- gateway_capture, gateway_void, gateway_refund, get_billing_data)+ gateway_capture, gateway_refund, gateway_void, get_billing_data)from ....shipping.models import ShippingMethod as ShippingMethodModelfrom ...account.types import AddressInputfrom ...core.mutations import BaseMutationdiff --git a/saleor/order/views.py b/saleor/order/views.pyindex bc868ef5dd..99172986e0 100644--- a/saleor/order/views.py+++ b/saleor/order/views.py@@ -15,8 +15,7 @@ from . import FulfillmentStatusfrom ..account.forms import LoginFormfrom ..account.models import Userfrom ..core.utils import get_client_ip-from ..payment import (- ChargeStatus, PaymentError, TransactionKind, get_payment_gateway)+from ..payment import ChargeStatus, TransactionKind, get_payment_gatewayfrom ..payment.models import Paymentfrom ..payment.utils import (create_payment_information, gateway_process_payment, get_billing_data)@@ -117,8 +116,8 @@ def start_payment(request, order, gateway):gateway_params=gateway_params)if form.is_valid():try:- gateway_process_payment(payment=payment,- payment_token=form.get_payment_token())+ gateway_process_payment(+ payment=payment, payment_token=form.get_payment_token())except Exception as exc:form.add_error(None, str(exc))else:diff --git a/saleor/payment/forms.py b/saleor/payment/forms.pydeleted file mode 100644index 5898d3859b..0000000000--- a/saleor/payment/forms.py+++ /dev/null@@ -1,24 +0,0 @@-from django import forms--from .models import Payment---class PaymentForm(forms.Form):- """Abstract Payment form that should be used as a base for every- payment gateway implemented.- """-- def __init__(- self,- payment: Payment,- gateway: object, gateway_params: dict,- *args: tuple, **kwargs: dict):-- self.payment = payment- self.gateway = gateway- self.gateway_params = gateway_params-- super().__init__(*args, **kwargs)-- def process_payment(self):- raise NotImplementedError()diff --git a/saleor/payment/gateways/braintree/__init__.py b/saleor/payment/gateways/braintree/__init__.pyindex e4dbc74a40..328af92cc7 100644--- a/saleor/payment/gateways/braintree/__init__.py+++ b/saleor/payment/gateways/braintree/__init__.py@@ -122,7 +122,7 @@ def process_payment(try:return [auth_resp, capture(payment_information, **connection_params)]- except Exception as e:+ except Exception:return [auth_resp, void(payment_information, **connection_params)]diff --git a/saleor/payment/gateways/braintree/forms.py b/saleor/payment/gateways/braintree/forms.pyindex a46667e39b..9749a24b11 100644--- a/saleor/payment/gateways/braintree/forms.py+++ b/saleor/payment/gateways/braintree/forms.py@@ -15,6 +15,7 @@ class BraintreePaymentForm(forms.Form):def __init__(self, payment_information, gateway_params, *args, **kwargs):super().__init__(*args, **kwargs)+ self.gateway_params = gateway_paramsself.payment_information = payment_informationself.fields['amount'].initial = payment_information['amount']diff --git a/saleor/payment/gateways/dummy/forms.py b/saleor/payment/gateways/dummy/forms.pyindex c2062b0be1..ac4658a6ff 100644--- a/saleor/payment/gateways/dummy/forms.py+++ b/saleor/payment/gateways/dummy/forms.py@@ -13,6 +13,8 @@ class DummyPaymentForm(forms.Form):widget=forms.RadioSelect)def __init__(self, payment_information, gateway_params, *args, **kwargs):+ self.payment_information = payment_information+ self.gateway_params = gateway_paramssuper().__init__(*args, **kwargs)def get_payment_token(self):diff --git a/saleor/payment/gateways/razorpay/__init__.py b/saleor/payment/gateways/razorpay/__init__.pyindex c5d58afcdb..273f9af084 100644--- a/saleor/payment/gateways/razorpay/__init__.py+++ b/saleor/payment/gateways/razorpay/__init__.py@@ -141,7 +141,8 @@ def refund(payment_information: Dict, **connection_params) -> Dict:payment_information['amount'], error=error)else:razorpay_client = get_client(**connection_params)- razorpay_amount = get_amount_for_razorpay(payment_information['amount'])+ razorpay_amount = get_amount_for_razorpay(+ payment_information['amount'])try:response = razorpay_client.payment.refund(payment_information['token'], razorpay_amount)diff --git a/saleor/payment/gateways/razorpay/forms.py b/saleor/payment/gateways/razorpay/forms.pyindex e561101fbc..21f45ee065 100644--- a/saleor/payment/gateways/razorpay/forms.py+++ b/saleor/payment/gateways/razorpay/forms.py@@ -34,7 +34,8 @@ class RazorPayCheckoutWidget(HiddenInput):'data-image': store_image,'data-name': store_name,'data-description': SECONDARY_TITLE,- 'data-amount': get_amount_for_razorpay(payment_information['amount']),+ 'data-amount': get_amount_for_razorpay(+ payment_information['amount']),'data-currency': payment_information['currency']}if prefill:diff --git a/saleor/payment/gateways/stripe/__init__.py b/saleor/payment/gateways/stripe/__init__.pyindex 6b3d758fe9..7d48543f37 100644--- a/saleor/payment/gateways/stripe/__init__.py+++ b/saleor/payment/gateways/stripe/__init__.py@@ -25,9 +25,6 @@ def process_payment(payment_information, **connection_params):def authorize(payment_information, **connection_params):client, error = _get_client(**connection_params), None- # Get amount from payment- amount = payment_information['amount']-try:# Authorize without captureresponse = _create_stripe_charge(@@ -68,9 +65,6 @@ def capture(payment_information, **connection_params):def charge(payment_information, **connection_params):client, error = _get_client(**connection_params), None- # Get amount from argument or payment- amount = payment_information['amount']-try:# Charge without pre-authorizeresponse = _create_stripe_charge(@@ -91,8 +85,8 @@ def refund(payment_information, **connection_params):# Get amount from payment, and convert to stripe's amountamount = payment_information['amount']- stripe_amount = get_amount_for_stripe(amount,- payment_information['currency'])+ stripe_amount = get_amount_for_stripe(+ amount, payment_information['currency'])try:# Retrieve stripe charge and refund specific amount@@ -135,7 +129,8 @@ def _get_client(**connection_params):return stripe-def _get_stripe_charge_payload(payment_information: Dict, should_capture: bool):+def _get_stripe_charge_payload(+ payment_information: Dict, should_capture: bool):shipping = payment_information['shipping']# Get currencydiff --git a/saleor/payment/gateways/stripe/forms.py b/saleor/payment/gateways/stripe/forms.pyindex 44f343986d..e600e1658a 100644--- a/saleor/payment/gateways/stripe/forms.py+++ b/saleor/payment/gateways/stripe/forms.py@@ -20,7 +20,8 @@ class StripeCheckoutWidget(HiddenInput):'src': CHECKOUT_SCRIPT_URL,'data-key': gateway_params.get('public_key'),'data-amount': get_amount_for_stripe(- payment_information['amount'], payment_information['currency']),+ payment_information['amount'],+ payment_information['currency']),'data-name': gateway_params.get('store_name'),'data-image': gateway_params.get('store_image'),'data-description': CHECKOUT_DESCRIPTION,diff --git a/saleor/payment/gateways/stripe/utils.py b/saleor/payment/gateways/stripe/utils.pyindex aa6557f71a..fcf1faf751 100644--- a/saleor/payment/gateways/stripe/utils.py+++ b/saleor/payment/gateways/stripe/utils.py@@ -3,9 +3,6 @@ from typing import Dictfrom django_countries import countries-from ....account.models import Address-from ....payment.models import Payment-# List of zero-decimal currencies# 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 docsdiff --git a/saleor/payment/models.py b/saleor/payment/models.pyindex 33da282189..b85a281b2b 100644--- a/saleor/payment/models.py+++ b/saleor/payment/models.py@@ -3,8 +3,8 @@ from operator import attrgetterfrom django.conf import settingsfrom django.contrib.postgres.fields import JSONField-from django.core.validators import MaxValueValidator, MinValueValidatorfrom django.core.serializers.json import DjangoJSONEncoder+from django.core.validators import MaxValueValidator, MinValueValidatorfrom django.db import modelsfrom prices import Moneydiff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex 538efd4f69..60982e9005 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -11,8 +11,8 @@ from django.forms.models import model_to_dictfrom prices import Moneyfrom . import (- ChargeStatus, GatewayError, PaymentError, get_payment_gateway,- TransactionKind)+ ChargeStatus, GatewayError, PaymentError, TransactionKind,+ get_payment_gateway)from ..core import analyticsfrom ..order import OrderEvents, OrderEventsEmailsfrom ..order.emails import send_payment_confirmation@@ -45,7 +45,8 @@ def get_billing_data(order):def create_payment_information(- payment: Payment, payment_token: str=None, amount: Decimal=None) -> Dict:+ payment: Payment, payment_token: str = None,+ amount: Decimal = None) -> Dict:"""Extracts order information along with payment details.Returns information required to process payment and additional@@ -100,8 +101,9 @@ def create_payment(**payment_data):return payment-def create_transaction(payment: Payment, kind: str, payment_information: Dict,- gateway_response: Dict=None, error_msg=None) -> Transaction:+def create_transaction(+ payment: Payment, kind: str, payment_information: Dict,+ gateway_response: Dict = None, error_msg=None) -> Transaction:"""Creates a Transaction based on transaction kind and gateway response."""if gateway_response is None:gateway_response = {}@@ -186,19 +188,21 @@ def call_gateway(finally:if not isinstance(gateway_response, list):gateway_response = [gateway_response]+ transactions = []for response in gateway_response:- transaction = create_transaction(+ transactions.append(create_transaction(payment=payment,kind=transaction_kind,payment_information=payment_information,error_msg=error_msg,- gateway_response=response)+ gateway_response=response))- if not transaction.is_success:- # attempt to get errors from response, if none raise a generic one- raise PaymentError(transaction.error or GENERIC_TRANSACTION_ERROR)+ for transaction in transactions:+ if not transaction.is_success:+ # attempt to get errors from response, if none raise a generic one+ raise PaymentError(transaction.error or GENERIC_TRANSACTION_ERROR)- return transaction+ return transactions[-1]def validate_gateway_response(responses):@@ -223,7 +227,8 @@ def validate_gateway_response(responses):try:json.dumps(response, cls=DjangoJSONEncoder)except (TypeError, ValueError):- raise GatewayError('Gateway response needs to be json serializable')+ raise GatewayError(+ 'Gateway response needs to be json serializable')@validate_payment@@ -246,8 +251,9 @@ def gateway_process_payment(@validate_payment-def gateway_charge(payment: Payment, payment_token: str,- amount: Decimal=None) -> Transaction:+def gateway_charge(+ payment: Payment, payment_token: str,+ amount: Decimal = None) -> Transaction:"""Performs authorization and capture in a single run.For gateways not supporting the authorization it should be a@@ -261,9 +267,9 @@ def gateway_charge(payment: Payment, payment_token: str,amount = payment.get_charge_amount()clean_charge(payment, amount)- transaction = call_gateway('charge',- transaction_kind=TransactionKind.CHARGE, payment=payment,- payment_token=payment_token, amount=amount)+ transaction = call_gateway(+ func_name='charge', transaction_kind=TransactionKind.CHARGE,+ payment=payment, payment_token=payment_token, amount=amount)if transaction.is_success:payment.charge_status = ChargeStatus.CHARGED@@ -285,13 +291,13 @@ def gateway_authorize(payment: Payment, payment_token: str) -> Transaction:"""clean_authorize(payment)- return call_gateway('authorize',- transaction_kind=TransactionKind.AUTH, payment=payment,- payment_token=payment_token)+ return call_gateway(+ func_name='authorize', transaction_kind=TransactionKind.AUTH,+ payment=payment, payment_token=payment_token)@validate_payment-def gateway_capture(payment: Payment, amount: Decimal=None) -> Transaction:+def gateway_capture(payment: Payment, amount: Decimal = None) -> Transaction:"""Captures the money that was reserved during the authorization stage."""if amount is None:amount = payment.get_charge_amount()@@ -303,9 +309,9 @@ def gateway_capture(payment: Payment, amount: Decimal=None) -> Transaction:raise PaymentError('Cannot capture unauthorized transaction')payment_token = auth_transaction.token- transaction = call_gateway('capture',- transaction_kind=TransactionKind.CAPTURE, payment=payment,- payment_token=payment_token, amount=amount)+ transaction = call_gateway(+ func_name='capture', transaction_kind=TransactionKind.CAPTURE,+ payment=payment, payment_token=payment_token, amount=amount)if transaction.is_success:payment.charge_status = ChargeStatus.CHARGED@@ -329,7 +335,8 @@ def gateway_void(payment) -> Transaction:raise PaymentError('Cannot void unauthorized transaction')payment_token = auth_transaction.token- transaction = call_gateway('void', transaction_kind=TransactionKind.VOID,+ transaction = call_gateway(+ func_name='void', transaction_kind=TransactionKind.VOID,payment=payment, payment_token=payment_token)if transaction.is_success:@@ -340,7 +347,7 @@ def gateway_void(payment) -> Transaction:@validate_payment-def gateway_refund(payment, amount: Decimal=None) -> Transaction:+def gateway_refund(payment, amount: Decimal = None) -> Transaction:"""Refunds the charged funds back to the customer.Refunds can be total or partial."""@@ -363,9 +370,9 @@ def gateway_refund(payment, amount: Decimal=None) -> Transaction:raise PaymentError('Cannot refund uncaptured/uncharged transaction')payment_token = transaction.token- transaction = call_gateway('refund',- transaction_kind=TransactionKind.REFUND, payment=payment,- payment_token=payment_token, amount=amount)+ transaction = call_gateway(+ func_name='refund', transaction_kind=TransactionKind.REFUND,+ payment=payment, payment_token=payment_token, amount=amount)if transaction.is_success:changed_fields = ['captured_amount']