Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Payment improvements and tests

Michał Jelonekdd4a1196d5fa
diff --git a/saleor/order/views.py b/saleor/order/views.py
index 2fd7c6797f..bc868ef5dd 100644
--- a/saleor/order/views.py
+++ b/saleor/order/views.py
@@ -18,7 +18,8 @@ from ..core.utils import get_client_ip
from ..payment import (
ChargeStatus, PaymentError, TransactionKind, get_payment_gateway)
from ..payment.models import Payment
-from ..payment.utils import gateway_process_payment, get_billing_data
+from ..payment.utils import (
+ create_payment_information, gateway_process_payment, get_billing_data)
from .forms import (
CustomerNoteForm, PasswordForm, PaymentDeleteForm, PaymentsForm)
from .models import Order
@@ -110,9 +111,10 @@ def start_payment(request, order, gateway):
payment_gateway, gateway_params = get_payment_gateway(payment.gateway)
client_token = payment_gateway.get_client_token(**gateway_params)
+ payment_info = create_payment_information(payment)
form = payment_gateway.get_form_class()(
- data=request.POST or None, amount=payment.total,
- currency=payment.currency, gateway_params=gateway_params)
+ data=request.POST or None, payment_information=payment_info,
+ gateway_params=gateway_params)
if form.is_valid():
try:
gateway_process_payment(payment=payment,
diff --git a/saleor/payment/gateways/dummy/forms.py b/saleor/payment/gateways/dummy/forms.py
index 8c34489465..c2062b0be1 100644
--- a/saleor/payment/gateways/dummy/forms.py
+++ b/saleor/payment/gateways/dummy/forms.py
@@ -12,7 +12,7 @@ class DummyPaymentForm(forms.Form):
choices=ChargeStatus.CHOICES, initial=ChargeStatus.NOT_CHARGED,
widget=forms.RadioSelect)
- def __init__(self, amount, currency, gateway_params, *args, **kwargs):
+ def __init__(self, payment_information, gateway_params, *args, **kwargs):
super().__init__(*args, **kwargs)
def get_payment_token(self):
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 7cf3bb804c..e6bdf8c714 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -40,7 +40,7 @@ def get_billing_data(order):
def create_payment_information(
- payment: Payment, payment_token: str, 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
@@ -55,8 +55,10 @@ def create_payment_information(
if payment.order.billing_address else None),
'shipping': (
payment.order.shipping_address.as_data()
- if payment.order.shipping_address else None)
- }
+ if payment.order.shipping_address else None),
+ 'order_id': payment.order.id,
+ 'customer_ip_address': payment.customer_ip_address,
+ 'customer_email': payment.billing_email}
def handle_fully_paid_order(order):
@@ -93,8 +95,8 @@ def create_payment(**payment_data):
return payment
-def create_transaction(payment: Payment, kind: str, payment_token: str,
- gateway_response: Dict=None, error_msg=None, **extra_params) -> 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 = {}
@@ -102,10 +104,12 @@ def create_transaction(payment: Payment, kind: str, payment_token: str,
txn, _ = Transaction.objects.get_or_create(
payment=payment,
kind=gateway_response.get('kind', kind),
- token=gateway_response.get('transaction_id', payment_token),
+ token=gateway_response.get(
+ 'transaction_id', payment_information['token']),
is_success=gateway_response.get('is_success', False),
- amount=gateway_response.get('amount', payment.total),
- currency=gateway_response.get('currency', payment.currency),
+ amount=gateway_response.get('amount', payment_information['amount']),
+ currency=gateway_response.get(
+ 'currency', payment_information['currency']),
error=gateway_response.get('error', error_msg),
gateway_response=gateway_response)
return txn # returns last created transaction
@@ -181,7 +185,7 @@ def call_gateway(
transaction = create_transaction(
payment=payment,
kind=transaction_kind,
- payment_token=payment_token,
+ payment_information=payment_information,
error_msg=error_msg,
gateway_response=response)
@@ -196,6 +200,7 @@ def validate_gateway_response(responses):
"""Validates response to be a correct format for Saleor to process."""
if not isinstance(responses, (dict, list)):
raise GatewayError('Gateway needs to return a dictionary or a list')
+
if not isinstance(responses, list):
responses = [responses]
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index 3ae7587e65..7e94385a43 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -1,4 +1,4 @@
-from unittest.mock import MagicMock, Mock
+from unittest.mock import MagicMock, Mock, patch
import graphene
import pytest
@@ -1095,6 +1095,15 @@ def test_clean_order_void_payment():
assert errors[0].field == 'payment'
assert errors[0].message == 'Only pre-authorized payments can be voided'
+ payment.is_active = True
+ error_msg = 'error has happened.'
+ with patch(
+ 'saleor.graphql.order.mutations.orders.gateway_void',
+ side_effect=ValueError(error_msg)):
+ errors = clean_void_payment(payment, [])
+ assert errors[0].field == 'payment'
+ assert errors[0].message == error_msg
+
def test_clean_order_refund_payment():
payment = MagicMock(spec=Payment)
diff --git a/tests/test_payment.py b/tests/test_payment.py
index c433cb5d9d..2ea36d3dc3 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -27,7 +27,8 @@ EXAMPLE_ERROR = 'Example dummy error'
def transaction_data(payment_dummy, settings):
return {
'payment': payment_dummy,
- 'payment_token': 'token',
+ 'payment_information': create_payment_information(
+ payment_dummy, 'token'),
'kind': TransactionKind.CAPTURE,
'gateway_response': {
'is_success': True,
@@ -35,6 +36,7 @@ def transaction_data(payment_dummy, settings):
'errors': None,
'amount': Decimal(14.50),
'currency': 'USD',
+ 'kind': TransactionKind.CAPTURE,
'raw_response': {
'credit_card': '4321',
'transaction': 'token'}}}
@@ -156,7 +158,7 @@ def test_create_transaction(transaction_data):
txn = create_transaction(**transaction_data)
assert txn.payment == transaction_data['payment']
- assert txn.kind == transaction_data['kind']
+ assert txn.kind == transaction_data['gateway_response']['kind']
assert txn.amount == transaction_data['gateway_response']['amount']
assert txn.currency == transaction_data['gateway_response']['currency']
assert txn.token == transaction_data['gateway_response']['transaction_id']