Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add can_confirm payment permission method
Jakub Draganek99ca6ea0c97b
diff --git a/saleor/payment/gateways/stripe/__init__.py b/saleor/payment/gateways/stripe/__init__.pyindex 9f017568fe..e7bfaf6057 100644--- a/saleor/payment/gateways/stripe/__init__.py+++ b/saleor/payment/gateways/stripe/__init__.py@@ -222,14 +222,12 @@ def _success_response(def fill_card_details(intent: stripe.PaymentIntent, response: GatewayResponse):charges = intent.charges["data"]- if not charges:- return response-- card = intent.charges["data"][-1]["payment_method_details"]["card"]- response.card_info = CreditCardInfo(- last_4=card["last4"],- exp_year=card["exp_year"],- exp_month=card["exp_month"],- brand=card["brand"],- )+ if charges:+ card = intent.charges["data"][-1]["payment_method_details"]["card"]+ response.card_info = CreditCardInfo(+ last_4=card["last4"],+ exp_year=card["exp_year"],+ exp_month=card["exp_month"],+ brand=card["brand"],+ )return responsediff --git a/saleor/payment/models.py b/saleor/payment/models.pyindex 840ac96660..b36c19c635 100644--- a/saleor/payment/models.py+++ b/saleor/payment/models.py@@ -184,6 +184,9 @@ class Payment(models.Model):and self.gateway != CustomPaymentChoices.MANUAL)+ def can_confirm(self):+ return self.is_active and self.not_charged+class Transaction(models.Model):"""Represents a single payment operation.diff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex 139b787cd0..8adeea044f 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -468,39 +468,25 @@ def gateway_void(payment) -> Transaction:if not payment.can_void():raise PaymentError("Only pre-authorized transactions can be voided.")- auth_transaction = payment.transactions.filter(- kind=TransactionKind.AUTH, is_success=True- ).first()- if auth_transaction is None:- raise PaymentError("Cannot void unauthorized transaction")- payment_token = auth_transaction.token-+ payment_token = get_payment_token(payment)transaction = call_gateway(operation_type=OperationType.VOID, payment=payment, payment_token=payment_token)-_gateway_postprocess(transaction, payment)return transaction@require_active_paymentdef gateway_confirm(payment) -> Transaction:- if not payment.can_void():- raise PaymentError("Only pre-authorized transactions can be voided.")-- auth_transaction = payment.transactions.filter(- kind=TransactionKind.AUTH, is_success=True- ).first()- if auth_transaction is None:- raise PaymentError("Cannot void unauthorized transaction")- payment_token = auth_transaction.token+ if not payment.can_confirm():+ raise PaymentError("Only active and not paid payments can be confirmed.")+ payment_token = get_payment_token(payment)transaction = call_gateway(operation_type=OperationType.CONFIRM,payment=payment,payment_token=payment_token,)-_gateway_postprocess(transaction, payment)return transaction@@ -578,3 +564,12 @@ def update_card_details(payment, gateway_response):payment.save(update_fields=["cc_brand", "cc_last_digits", "cc_exp_year", "cc_exp_month"])+++def get_payment_token(payment: Payment):+ auth_transaction = payment.transactions.filter(+ kind=TransactionKind.AUTH, is_success=True+ ).first()+ if auth_transaction is None:+ raise PaymentError("Cannot process unauthorized transaction")+ return auth_transaction.token