Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Docstrings, small code clean up
Pacu296816744d3c5
diff --git a/saleor/payment/models.py b/saleor/payment/models.pyindex 4e463bc894..4d568b12e0 100644--- a/saleor/payment/models.py+++ b/saleor/payment/models.py@@ -10,6 +10,17 @@ from ..order.models import Orderclass PaymentMethod(models.Model):+ """Represents transactable payment information+ such as credit card details, gift card information or a customer's+ authorization to charge their PayPal account.++ All payment process related pieces of information are stored+ at the gateway level, we are operating on the reusable token+ which is a unique identifier of the customer for given gateway.++ Payment methods belong to a customer, one can use several payments method+ within a single order.+ """variant = models.CharField(max_length=255)is_active = models.BooleanField(default=True)#: Creation date and time@@ -93,6 +104,10 @@ class PaymentMethod(models.Model):class Transaction(models.Model):+ """Transactions represent attempts to transfer money between your store+ and your customers, with a choosen payment method.+ """+ created = models.DateTimeField(auto_now_add=True, editable=False)payment_method = models.ForeignKey(PaymentMethod, related_name='transactions', on_delete=models.PROTECT)token = models.CharField(max_length=64, blank=True, default='')@@ -101,7 +116,9 @@ class Transaction(models.Model):is_success = models.BooleanField(default=False)amount = models.DecimalField(max_digits=9, decimal_places=2, default=Decimal('0.0'))+ currency = models.CharField(max_length=10, editable=False)+gateway_response = JSONField()def get_amount(self):- return Money(amount=self.amount, currency=self.payment_method.currency)+ return Money(amount=self.amount, currency=self.currency)diff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex c3da4775c1..4f580488ef 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -1,4 +1,5 @@from decimal import Decimal+from functools import wrapsfrom typing import Dict, Optionalfrom django.db import transaction@@ -7,9 +8,20 @@ from . import PaymentError, PaymentMethodChargeStatus, get_providerfrom .models import PaymentMethod, Transaction-def create_payment_method(**payment_method_kwargs):- payment_method, dummy_created = PaymentMethod.objects.get_or_create(- **payment_method_kwargs)+def validate_payment_method(view):+ """Decorate a view to check if payment method is active, so any actions+ can be performed on it.+ """+ @wraps(view)+ def func(payment_method, *args, **kwargs):+ if not payment_method.is_active:+ raise PaymentError('This payment method is no longer active.')+ return view(payment_method, *args, **kwargs)+ return func+++def create_payment_method(**payment_data):+ payment_method, _ = PaymentMethod.objects.get_or_create(**payment_data)return payment_method@@ -17,11 +29,10 @@ def create_transaction(payment_method: PaymentMethod, token: str, transaction_type: str,is_success: bool, amount: Decimal,gateway_response: Optional[Dict] = None) -> Transaction:-if not gateway_response:gateway_response = {}- txn, dummy_created = Transaction.objects.get_or_create(+ txn, _ = Transaction.objects.get_or_create(payment_method=payment_method, token=token,transaction_type=transaction_type, is_success=is_success,amount=amount, gateway_response=gateway_response)@@ -33,11 +44,10 @@ def gateway_get_client_token(provider_name):return provider.get_client_token(**provider_params)+@validate_payment_methoddef gateway_authorize(payment_method, transaction_token) -> Transaction:- if not payment_method.is_active:- raise PaymentError('This payment method is no longer active')if not payment_method.charge_status == PaymentMethodChargeStatus.NOT_CHARGED:- raise PaymentError('Charged transactions cannot be authorized again')+ raise PaymentError('Charged transactions cannot be authorized again.')provider, provider_params = get_provider(payment_method.variant)with transaction.atomic():@@ -52,16 +62,18 @@ def gateway_authorize(payment_method, transaction_token) -> Transaction:return txn+@validate_payment_methoddef gateway_charge(payment_method, amount) -> Transaction:- if not payment_method.is_active:- raise PaymentError('This payment method is no longer active')if payment_method.charge_status not in {PaymentMethodChargeStatus.CHARGED,PaymentMethodChargeStatus.NOT_CHARGED}:- raise PaymentError('This payment method cannot be charged')+ raise PaymentError('This payment method cannot be charged.')if amount > payment_method.total or amount > (payment_method.total - payment_method.captured_amount):- raise PaymentError('Unable to charge more than authorized amount')+ raise PaymentError('Unable to charge more than authorized amount.')+ if amount <= 0:+ raise PaymentError('Amount should be a positive number.')+provider, provider_params = get_provider(payment_method.variant)with transaction.atomic():txn, error = provider.charge(@@ -78,11 +90,10 @@ def gateway_charge(payment_method, amount) -> Transaction:return txn+@validate_payment_methoddef gateway_void(payment_method) -> Transaction:- if not payment_method.is_active:- raise PaymentError('This payment method is no longer active')if not payment_method.charge_status == PaymentMethodChargeStatus.NOT_CHARGED:- raise PaymentError('Only pre-authorized transactions can be void')+ raise PaymentError('Only pre-authorized transactions can be void.')provider, provider_params = get_provider(payment_method.variant)with transaction.atomic():txn, error = provider.void(payment_method, **provider_params)@@ -94,14 +105,15 @@ def gateway_void(payment_method) -> Transaction:return txn+@validate_payment_methoddef gateway_refund(payment_method, amount) -> Transaction:- if not payment_method.is_active:- raise PaymentError('This payment method is no longer active')if amount > payment_method.captured_amount:raise PaymentError('Cannot refund more than captured')+ if amount <= 0:+ raise PaymentError('Amount should be a positive number.')if not payment_method.charge_status == PaymentMethodChargeStatus.CHARGED:raise PaymentError(- 'Refund is possible only when transaction is charged')+ 'Refund is possible only when transaction is charged.')provider, provider_params = get_provider(payment_method.variant)with transaction.atomic():