Skip to content

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

Implement payment information

Michał Jelonekf61199d2980d
diff --git a/saleor/payment/gateways/dummy/__init__.py b/saleor/payment/gateways/dummy/__init__.py
index f8cb707a1a..274a36ef96 100644
--- a/saleor/payment/gateways/dummy/__init__.py
+++ b/saleor/payment/gateways/dummy/__init__.py
@@ -24,16 +24,15 @@ def get_form_class():
return DummyPaymentForm
-def process_payment(
- payment: Dict, payment_token: str, amount: Decimal,
- **connection_params):
- auth_resp = authorize(payment, payment_token)
+def process_payment(payment_information: Dict, **connection_params) -> Dict:
+ """Process the whole payment, by calling authorize and capture."""
+ auth_resp = authorize(payment_information, **connection_params)
if auth_resp['error']:
return auth_resp
- return [auth_resp, capture(payment, payment_token, amount)]
+ return [auth_resp, capture(payment_information, **connection_params)]
-def authorize(payment: Dict, payment_token: str, **connection_params):
+def authorize(payment_information: Dict, **connection_params) -> Dict:
success = dummy_success()
error = None
if not success:
@@ -41,13 +40,13 @@ def authorize(payment: Dict, payment_token: str, **connection_params):
return {
'is_success': success,
'kind': 'auth',
- 'amount': payment['total'],
- 'currency': payment['currency'],
- 'transaction_id': payment_token,
+ 'amount': payment_information['amount'],
+ 'currency': payment_information['currency'],
+ 'transaction_id': payment_information['token'],
'error': error}
-def void(payment: Dict, payment_token: str, **connection_params: Dict):
+def void(payment_information: Dict, **connection_params) -> Dict:
error = None
success = dummy_success()
if not success:
@@ -55,15 +54,13 @@ def void(payment: Dict, payment_token: str, **connection_params: Dict):
return {
'is_success': success,
'kind': 'void',
- 'amount': payment['total'],
- 'currency': payment['currency'],
- 'transaction_id': payment_token,
+ 'amount': payment_information['amount'],
+ 'currency': payment_information['currency'],
+ 'transaction_id': payment_information['token'],
'error': error}
-def capture(
- payment: Dict, payment_token: str, amount: Decimal,
- **connection_params: Dict):
+def capture(payment_information: Dict, **connection_params: Dict) -> Dict:
error = None
success = dummy_success()
if not success:
@@ -71,15 +68,13 @@ def capture(
return {
'is_success': success,
'kind': 'capture',
- 'amount': amount,
- 'currency': payment['currency'],
- 'transaction_id': payment_token,
+ 'amount': payment_information['amount'],
+ 'currency': payment_information['currency'],
+ 'transaction_id': payment_information['token'],
'error': error}
-def refund(
- payment: Dict, payment_token: str, amount: Decimal,
- **connection_params: Dict):
+def refund(payment_information: Dict, **connection_params: Dict):
error = None
success = dummy_success()
if not success:
@@ -87,17 +82,15 @@ def refund(
return {
'is_success': success,
'kind': 'refund',
- 'amount': amount,
- 'currency': payment['currency'],
- 'transaction_id': payment_token,
+ 'amount': payment_information['amount'],
+ 'currency': payment_information['currency'],
+ 'transaction_id': payment_information['token'],
'error': error}
-def charge(
- payment: Dict, payment_token: str, amount: Decimal,
- **connection_params):
+def charge(payment_information: Dict, **connection_params):
"""Performs Authorize and Capture transactions in a single run."""
- auth_resp = authorize(payment, payment_token)
+ auth_resp = authorize(payment_information, **connection_params)
if auth_resp['error']:
return auth_resp
- return [auth_resp, capture(payment, payment_token, amount)]
+ return [auth_resp, capture(payment_information, **connection_params)]
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 5841ef86e3..7cf3bb804c 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -39,6 +39,26 @@ def get_billing_data(order):
return data
+def create_payment_information(
+ payment: Payment, payment_token: str, amount: Decimal=None) -> Dict:
+ """Extracts order information along with payment details.
+
+ Returns information required to process payment and additional
+ billing/shipping addresses for optional fraud-prevention mechanisms.
+ """
+ return {
+ 'token': payment_token,
+ 'amount': amount or payment.total,
+ 'currency': payment.currency,
+ 'billing': (
+ payment.order.billing_address.as_data()
+ if payment.order.billing_address else None),
+ 'shipping': (
+ payment.order.shipping_address.as_data()
+ if payment.order.shipping_address else None)
+ }
+
+
def handle_fully_paid_order(order):
order.events.create(type=OrderEvents.ORDER_FULLY_PAID.value)
if order.get_user_current_email():
@@ -136,24 +156,24 @@ def call_gateway(
gateway_response = None
error_msg = None
- #TODO: improve so only one argument is sent (dict?) instead of passing
- # multiple kwargs, eventually keep connection params separate
- #TODO: add additional information to payment data (e.g. shipping/billing)
+ payment_information = create_payment_information(
+ payment, payment_token, **extra_params
+ )
+
try:
gateway_response = getattr(gateway, func_name)(
- payment=model_to_dict(payment), payment_token=payment_token,
- **extra_params, **gateway_params)
+ payment_information=payment_information, **gateway_params)
validate_gateway_response(gateway_response)
except AttributeError:
error_msg = 'Gateway doesn\'t implement {}'.format(func_name)
- logger.exception(msg)
+ logger.exception(error_msg)
except GatewayError:
error_msg = 'Gateway response validation failed'
- logger.exception()
+ logger.exception(error_msg)
gateway_response = None # set response empty as the validation failed
except Exception:
error_msg = 'Gateway encountered an error'
- logger.exception('Gateway encountered an error')
+ logger.exception(error_msg)
finally:
if not isinstance(gateway_response, list):
gateway_response = [gateway_response]
diff --git a/tests/test_payment.py b/tests/test_payment.py
index f813a3aab7..c433cb5d9d 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -16,7 +16,8 @@ from saleor.payment.utils import (
clean_authorize, clean_capture, clean_charge, create_payment,
create_transaction, gateway_authorize, gateway_capture, gateway_charge,
gateway_get_client_token, gateway_refund, gateway_void, get_billing_data,
- handle_fully_paid_order, validate_payment)
+ handle_fully_paid_order, validate_payment, create_payment_information,
+ gateway_process_payment)
NOT_ACTIVE_PAYMENT_ERROR = 'This payment is no longer active.'
EXAMPLE_ERROR = 'Example dummy error'
@@ -205,28 +206,47 @@ def test_payment_needs_to_be_active_for_any_action(func, payment_dummy):
@patch('saleor.payment.utils.get_payment_gateway')
-def test_gateway_authorize(
+def test_gateway_process_payment(
mock_get_payment_gateway, payment_txn_preauth, gateway_params,
transaction_token, dummy_response):
payment_token = transaction_token
- txn = payment_txn_preauth.transactions.first()
payment = payment_txn_preauth
+ mock_process_payment = Mock(return_value=[dummy_response, dummy_response])
+ mock_get_payment_gateway.return_value = (
+ Mock(process_payment=mock_process_payment), gateway_params)
+
+ payment_info = create_payment_information(payment, payment_token)
+ gateway_process_payment(payment, payment_token)
+
+ mock_get_payment_gateway.assert_called_with(payment.gateway)
+ mock_process_payment.assert_called_once_with(
+ payment_information=payment_info, **gateway_params)
+
+
+@patch('saleor.payment.utils.get_payment_gateway')
+def test_gateway_authorize(
+ mock_get_payment_gateway, payment_txn_preauth, gateway_params,
+ transaction_token, dummy_response):
+ payment = payment_txn_preauth
+ payment_token = transaction_token
mock_authorize = Mock(return_value=dummy_response)
mock_get_payment_gateway.return_value = (
Mock(authorize=mock_authorize), gateway_params)
- payment_info = model_to_dict(payment)
+ payment_info = create_payment_information(payment, payment_token)
gateway_authorize(payment, payment_token)
+
mock_get_payment_gateway.assert_called_once_with(payment.gateway)
mock_authorize.assert_called_once_with(
- payment=payment_info, payment_token=payment_token, **gateway_params)
+ payment_information=payment_info, **gateway_params)
@patch('saleor.payment.utils.get_payment_gateway')
def test_gateway_authorize_failed(
mock_get_payment_gateway, payment_txn_preauth, gateway_params,
transaction_token, dummy_response):
+ payment = payment_txn_preauth
payment_token = transaction_token
txn = payment_txn_preauth.transactions.first()
txn.is_success = False
@@ -263,11 +283,12 @@ def test_gateway_capture(
mock_get_payment_gateway.return_value = (
Mock(capture=mock_capture), gateway_params)
- payment_info = model_to_dict(payment)
+ payment_info = create_payment_information(payment, '', amount)
gateway_capture(payment, amount)
+
mock_get_payment_gateway.assert_called_once_with(payment.gateway)
mock_capture.assert_called_once_with(
- payment=payment_info, payment_token='', amount=amount, **gateway_params)
+ payment_information=payment_info, **gateway_params)
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.CHARGED
@@ -359,12 +380,12 @@ def test_gateway_charge(
mock_get_payment_gateway.return_value = (
Mock(charge=mock_charge), gateway_params)
- payment_info = model_to_dict(payment)
+ payment_info = create_payment_information(payment, payment_token, amount)
gateway_charge(payment, payment_token, amount)
+
mock_get_payment_gateway.assert_called_once_with(payment.gateway)
mock_charge.assert_called_once_with(
- payment=payment_info, payment_token=payment_token, amount=amount,
- **gateway_params)
+ payment_information=payment_info, **gateway_params)
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.CHARGED
@@ -457,11 +478,12 @@ def test_gateway_void(
mock_void = Mock(return_value=dummy_response)
mock_get_payment_gateway.return_value = (Mock(void=mock_void), gateway_params)
- payment_info = model_to_dict(payment)
+ payment_info = create_payment_information(payment, txn.token)
gateway_void(payment)
+
mock_get_payment_gateway.assert_called_once_with(payment.gateway)
mock_void.assert_called_once_with(
- payment=payment_info, payment_token=txn.token, **gateway_params)
+ payment_information=payment_info, **gateway_params)
payment.refresh_from_db()
assert payment.is_active == False
@@ -507,12 +529,12 @@ def test_gateway_refund(
mock_get_payment_gateway.return_value = (
Mock(refund=mock_refund), gateway_params)
- payment_info = model_to_dict(payment)
+ payment_info = create_payment_information(payment, txn.token, amount)
gateway_refund(payment, amount)
+
mock_get_payment_gateway.assert_called_once_with(payment.gateway)
mock_refund.assert_called_once_with(
- payment=payment_info, payment_token='',
- amount=amount, **gateway_params)
+ payment_information=payment_info, **gateway_params)
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.FULLY_REFUNDED