Skip to content

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

Adjust Stripe to new payments core

Michał Jelonekcca02d5016b5
diff --git a/saleor/payment/gateways/stripe/__init__.py b/saleor/payment/gateways/stripe/__init__.py
index 6f98221e1c..6b3d758fe9 100644
--- a/saleor/payment/gateways/stripe/__init__.py
+++ b/saleor/payment/gateways/stripe/__init__.py
@@ -1,16 +1,14 @@
from decimal import Decimal
+from typing import Dict
import stripe
from . import errors
-from ... import TransactionKind
-from ....payment.models import Payment
-from ...utils import create_transaction
from .forms import StripePaymentModalForm
from .utils import (
get_amount_for_stripe, get_amount_from_stripe, get_currency_for_stripe,
get_currency_from_stripe, get_payment_billing_fullname,
- shipping_address_to_stripe_dict)
+ shipping_to_stripe_dict)
def get_client_token(**_):
@@ -20,148 +18,112 @@ def get_client_token(**_):
return
-def authorize(payment, payment_token, **connection_params):
- client, error = _get_client(**connection_params), ''
+def process_payment(payment_information, **connection_params):
+ return charge(payment_information, **connection_params)
+
+
+def authorize(payment_information, **connection_params):
+ client, error = _get_client(**connection_params), None
# Get amount from payment
- amount = payment.total
+ amount = payment_information['amount']
try:
# Authorize without capture
response = _create_stripe_charge(
- client=client, payment=payment, amount=amount,
- payment_token=payment_token, should_capture=False)
+ client=client, payment_information=payment_information,
+ should_capture=False)
except stripe.error.StripeError as exc:
response = _get_error_response_from_exc(exc)
error = exc.user_message
- # Create transaction
- txn = _create_transaction(
- payment=payment,
- amount=amount,
- kind=TransactionKind.AUTH,
- response=response)
-
- return txn, error
+ # Create response
+ return _create_response(
+ payment_information=payment_information,
+ kind='auth', response=response, error=error)
-def capture(payment, amount, **connection_params):
- client, error = _get_client(**connection_params), ''
+def capture(payment_information, **connection_params):
+ client, error = _get_client(**connection_params), None
# Get amount from argument or payment, and convert to stripe's amount
- amount = amount or payment.total
- stripe_amount = get_amount_for_stripe(amount, payment.currency)
-
- capture_txn = payment.transactions.filter(
- kind=TransactionKind.AUTH, is_success=True).first()
-
- if capture_txn is not None:
- try:
- # Retrieve stripe charge and capture specific amount
- stripe_charge = client.Charge.retrieve(capture_txn.token)
- response = stripe_charge.capture(amount=stripe_amount)
- except stripe.error.StripeError as exc:
- response = _get_error_response_from_exc(exc)
- error = exc.user_message
- else:
- error = errors.ORDER_NOT_AUTHORIZED
- response = dict()
+ amount = payment_information['amount']
+ stripe_amount = get_amount_for_stripe(
+ amount, payment_information['currency'])
- # Create transaction
- txn = _create_transaction(
- payment=payment,
- amount=amount,
- kind=TransactionKind.CAPTURE,
- response=response)
+ try:
+ # Retrieve stripe charge and capture specific amount
+ stripe_charge = client.Charge.retrieve(payment_information['token'])
+ response = stripe_charge.capture(amount=stripe_amount)
+ except stripe.error.StripeError as exc:
+ response = _get_error_response_from_exc(exc)
+ error = exc.user_message
- return txn, error
+ # Create response
+ return _create_response(
+ payment_information=payment_information,
+ kind='capture', response=response, error=error)
-def charge(payment, payment_token, amount, **connection_params):
- client, error = _get_client(**connection_params), ''
+def charge(payment_information, **connection_params):
+ client, error = _get_client(**connection_params), None
# Get amount from argument or payment
- amount = amount or payment.total
+ amount = payment_information['amount']
try:
# Charge without pre-authorize
response = _create_stripe_charge(
- client=client, payment=payment, amount=amount,
- payment_token=payment_token, should_capture=True)
+ client=client, payment_information=payment_information,
+ should_capture=True)
except stripe.error.StripeError as exc:
response = _get_error_response_from_exc(exc)
error = exc.user_message
- # Create transaction
- txn = _create_transaction(
- payment=payment,
- amount=amount,
- kind=TransactionKind.CHARGE,
- response=response)
-
- return txn, error
+ # Create response
+ return _create_response(
+ payment_information=payment_information,
+ kind='charge', response=response, error=error)
-def refund(payment, amount, **connection_params):
- client, error = _get_client(**connection_params), ''
-
- # Get amount from argument or payment, and convert to stripe's amount
- amount = amount or payment.total
- stripe_amount = get_amount_for_stripe(amount, payment.currency)
-
- capture_txn = payment.transactions.filter(
- kind__in=[TransactionKind.CAPTURE, TransactionKind.CHARGE],
- is_success=True).first()
-
- if capture_txn is not None:
- try:
- # Retrieve stripe charge and refund specific amount
- stripe_charge = client.Charge.retrieve(capture_txn.token)
- response = client.Refund.create(
- charge=stripe_charge.id, amount=stripe_amount)
- except stripe.error.StripeError as exc:
- response = _get_error_response_from_exc(exc)
- error = exc.user_message
- else:
- error = errors.ORDER_NOT_CHARGED
- response = dict()
+def refund(payment_information, **connection_params):
+ client, error = _get_client(**connection_params), None
- # Create transaction
- txn = _create_transaction(
- payment=payment,
- amount=amount,
- kind=TransactionKind.REFUND,
- response=response)
-
- return txn, error
+ # Get amount from payment, and convert to stripe's amount
+ amount = payment_information['amount']
+ stripe_amount = get_amount_for_stripe(amount,
+ payment_information['currency'])
+ try:
+ # Retrieve stripe charge and refund specific amount
+ stripe_charge = client.Charge.retrieve(payment_information['token'])
+ response = client.Refund.create(
+ charge=stripe_charge.id, amount=stripe_amount)
+ except stripe.error.StripeError as exc:
+ response = _get_error_response_from_exc(exc)
+ error = exc.user_message
-def void(payment, **connection_params):
- client, error = _get_client(**connection_params), ''
+ # Create response
+ return _create_response(
+ payment_information=payment_information,
+ kind='refund', response=response, error=error)
- capture_txn = payment.transactions.filter(
- kind=TransactionKind.AUTH, is_success=True).first()
- if capture_txn is not None:
- try:
- # Retrieve stripe charge and refund all
- stripe_charge = client.Charge.retrieve(capture_txn.token)
- response = client.Refund.create(charge=stripe_charge.id)
- except stripe.error.StripeError as exc:
- response = _get_error_response_from_exc(exc)
- error = exc.user_message
- else:
- error = errors.ORDER_NOT_AUTHORIZED
- response = dict()
+def void(payment_information, **connection_params):
+ client, error = _get_client(**connection_params), None
- # Create transaction
- txn = _create_transaction(
- payment=payment,
- amount=payment.total,
- kind=TransactionKind.VOID,
- response=response)
+ try:
+ # Retrieve stripe charge and refund all
+ stripe_charge = client.Charge.retrieve(payment_information['token'])
+ response = client.Refund.create(charge=stripe_charge.id)
+ except stripe.error.StripeError as exc:
+ response = _get_error_response_from_exc(exc)
+ error = exc.user_message
- return txn, error
+ # Create response
+ return _create_response(
+ payment_information=payment_information,
+ kind='void', response=response, error=error)
def get_form_class():
@@ -173,51 +135,49 @@ def _get_client(**connection_params):
return stripe
-def _get_stripe_charge_payload(
- payment: Payment, amount: Decimal, payment_token: str,
- should_capture: bool):
- shipping_address = payment.order.shipping_address
+def _get_stripe_charge_payload(payment_information: Dict, should_capture: bool):
+ shipping = payment_information['shipping']
# Get currency
- currency = get_currency_for_stripe(payment.currency)
+ currency = get_currency_for_stripe(payment_information['currency'])
# Get appropriate amount for stripe
- stripe_amount = get_amount_for_stripe(amount, currency)
+ stripe_amount = get_amount_for_stripe(
+ payment_information['amount'], currency)
# Get billing name from payment
- name = get_payment_billing_fullname(payment)
+ name = get_payment_billing_fullname(payment_information)
# Construct the charge payload from the data
charge_payload = {
'capture': should_capture,
'amount': stripe_amount,
'currency': currency,
- 'source': payment_token,
+ 'source': payment_information['token'],
'description': name}
- if shipping_address:
+ if shipping:
# Update shipping address to prevent fraud in Stripe
charge_payload['shipping'] = {
'name': name,
- 'address': shipping_address_to_stripe_dict(shipping_address)}
+ 'address': shipping_to_stripe_dict(shipping)}
return charge_payload
-def _create_stripe_charge(
- client, payment, amount: Decimal, payment_token: str,
- should_capture: bool):
+def _create_stripe_charge(client, payment_information, should_capture: bool):
"""Create a charge with specific amount, ignoring payment's total."""
charge_payload = _get_stripe_charge_payload(
- payment, amount, payment_token, should_capture)
+ payment_information, should_capture)
return client.Charge.create(**charge_payload)
-def _create_transaction(payment, amount, kind, response):
+def _create_response(payment_information, kind, response, error):
# Get currency from response or payment
currency = get_currency_from_stripe(
- response.get('currency', payment.currency))
+ response.get('currency', payment_information['currency']))
+ amount = payment_information.get('amount')
# Get amount from response or payment
if 'amount' in response:
stripe_amount = response.get('amount')
@@ -233,17 +193,15 @@ def _create_transaction(payment, amount, kind, response):
# Check if the response's status is flagged as succeeded
is_success = (response.get('status') == 'succeeded')
- # Create transaction
- txn = create_transaction(
- payment=payment,
- token=token,
- kind=kind,
- is_success=is_success,
- amount=amount,
- currency=currency,
- gateway_response=response)
-
- return txn
+ return {
+ 'is_success': is_success,
+ 'transaction_id': token,
+ 'kind': kind,
+ 'amount': amount,
+ 'currency': currency,
+ 'error': error,
+ 'raw_response': response,
+ }
def _get_error_response_from_exc(exc):
diff --git a/saleor/payment/gateways/stripe/forms.py b/saleor/payment/gateways/stripe/forms.py
index 5285533da5..44f343986d 100644
--- a/saleor/payment/gateways/stripe/forms.py
+++ b/saleor/payment/gateways/stripe/forms.py
@@ -4,7 +4,6 @@ from django.forms.widgets import HiddenInput
from django.utils.html import format_html
from django.utils.translation import pgettext_lazy
-from ...forms import PaymentForm
from .utils import get_amount_for_stripe
CHECKOUT_SCRIPT_URL = 'https://checkout.stripe.com/checkout.js'
@@ -14,18 +13,18 @@ CHECKOUT_DESCRIPTION = pgettext_lazy(
class StripeCheckoutWidget(HiddenInput):
- def __init__(self, payment, gateway_params, *args, **kwargs):
+ def __init__(self, payment_information, gateway_params, *args, **kwargs):
attrs = kwargs.get('attrs', {})
kwargs['attrs'] = {
'class': 'stripe-button',
'src': CHECKOUT_SCRIPT_URL,
'data-key': gateway_params.get('public_key'),
'data-amount': get_amount_for_stripe(
- payment.total, payment.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,
- 'data-currency': payment.currency,
+ 'data-currency': payment_information['currency'],
'data-locale': gateway_params.get('locale'),
'data-allow-remember-me': 'true' if gateway_params.get(
'remember_me') else 'false',
@@ -39,7 +38,7 @@ class StripeCheckoutWidget(HiddenInput):
if gateway_params.get('prefill'):
kwargs['attrs'].update({
- 'data-email': payment.billing_email})
+ 'data-email': payment_information['customer_email']})
kwargs['attrs'].update(attrs)
super(StripeCheckoutWidget, self).__init__(*args, **kwargs)
@@ -51,25 +50,22 @@ class StripeCheckoutWidget(HiddenInput):
return format_html('<script{0}></script>', flatatt(attrs))
-class StripePaymentModalForm(PaymentForm):
+class StripePaymentModalForm(forms.Form):
"""
At this moment partial-payment is not supported, but there is no need to
validate amount, which may be manually adjusted in the template,
since checkout.js can do that automatically.
"""
- stripeToken = forms.CharField(
- required=True, widget=HiddenInput)
+ stripeToken = forms.CharField(required=True, widget=HiddenInput)
- def __init__(self, *args, **kwargs):
- super(StripePaymentModalForm, self).__init__(*args, **kwargs)
+ def __init__(self, payment_information, gateway_params, *args, **kwargs):
+ super().__init__(*args, **kwargs)
self.fields['stripe'] = forms.CharField(
widget=StripeCheckoutWidget(
- payment=self.payment, gateway_params=self.gateway_params),
+ payment_information=payment_information,
+ gateway_params=gateway_params),
required=False)
- def process_payment(self):
- cleaned_data = super(StripePaymentModalForm, self).clean()
- stripe_token = cleaned_data['stripeToken']
- self.payment.charge(payment_token=stripe_token)
- return self.payment
+ def get_payment_token(self):
+ return self.cleaned_data['stripeToken']
diff --git a/saleor/payment/gateways/stripe/utils.py b/saleor/payment/gateways/stripe/utils.py
index 3e0ad8a787..aa6557f71a 100644
--- a/saleor/payment/gateways/stripe/utils.py
+++ b/saleor/payment/gateways/stripe/utils.py
@@ -1,4 +1,5 @@
from decimal import Decimal
+from typing import Dict
from django_countries import countries
@@ -57,17 +58,18 @@ def get_currency_from_stripe(currency):
return currency.upper()
-def get_payment_billing_fullname(payment: Payment):
+def get_payment_billing_fullname(payment_information: Dict):
# Get billing name from payment
return '%s %s' % (
- payment.billing_last_name, payment.billing_first_name)
+ payment_information['billing']['last_name'],
+ payment_information['billing']['first_name'])
-def shipping_address_to_stripe_dict(shipping_address: Address):
+def shipping_to_stripe_dict(shipping: Dict):
return {
- 'line1': shipping_address.street_address_1,
- 'line2': shipping_address.street_address_2,
- 'city': shipping_address.city,
- 'state': shipping_address.country_area,
- 'postal_code': shipping_address.postal_code,
- 'country': dict(countries).get(shipping_address.country, '')}
+ 'line1': shipping['street_address_1'],
+ 'line2': shipping['street_address_2'],
+ 'city': shipping['city'],
+ 'state': shipping['country_area'],
+ 'postal_code': shipping['postal_code'],
+ 'country': dict(countries).get(shipping['country'], '')}
diff --git a/tests/gateway/test_stripe.py b/tests/gateway/test_stripe.py
index f8ac6bc990..bd5711674e 100644
--- a/tests/gateway/test_stripe.py
+++ b/tests/gateway/test_stripe.py
@@ -8,7 +8,7 @@ from django_countries import countries
from saleor.payment import ChargeStatus, TransactionKind
from saleor.payment.gateways.stripe import (
- _create_transaction, _get_client, _get_error_response_from_exc,
+ _create_response, _get_client, _get_error_response_from_exc,
_get_stripe_charge_payload, authorize, capture, charge,
get_amount_for_stripe, get_amount_from_stripe, get_client_token,
get_currency_for_stripe, get_currency_from_stripe, get_form_class, refund,
@@ -18,7 +18,8 @@ from saleor.payment.gateways.stripe.errors import (
from saleor.payment.gateways.stripe.forms import (
StripeCheckoutWidget, StripePaymentModalForm)
from saleor.payment.gateways.stripe.utils import (
- get_payment_billing_fullname, shipping_address_to_stripe_dict)
+ get_payment_billing_fullname, shipping_to_stripe_dict)
+from saleor.payment.utils import create_payment_information
TRANSACTION_AMOUNT = Decimal(42.42)
TRANSACTION_REFUND_AMOUNT = Decimal(24.24)
@@ -59,11 +60,6 @@ def stripe_authorized_payment(stripe_payment):
stripe_payment.charge_status = ChargeStatus.NOT_CHARGED
stripe_payment.save(update_fields=['charge_status'])
- stripe_payment.transactions.create(
- amount=stripe_payment.total,
- kind=TransactionKind.AUTH,
- gateway_response={},
- is_success=True)
return stripe_payment
@@ -73,11 +69,6 @@ def stripe_captured_payment(stripe_payment):
stripe_payment.charge_status = ChargeStatus.CHARGED
stripe_payment.save(update_fields=['captured_amount', 'charge_status'])
- stripe_payment.transactions.create(
- amount=stripe_payment.total,
- kind=TransactionKind.CAPTURE,
- gateway_response={},
- is_success=True)
return stripe_payment
@@ -163,9 +154,11 @@ def test_get_currency_from_stripe():
def test_get_payment_billing_fullname(payment_dummy):
+ payment_info = create_payment_information(payment_dummy)
+
expected_fullname = '%s %s' % (
payment_dummy.billing_last_name, payment_dummy.billing_first_name)
- assert get_payment_billing_fullname(payment_dummy) == expected_fullname
+ assert get_payment_billing_fullname(payment_info) == expected_fullname
def test_shipping_address_to_stripe_dict(address):
@@ -176,11 +169,12 @@ def test_shipping_address_to_stripe_dict(address):
'state': address.country_area,
'postal_code': address.postal_code,
'country': dict(countries).get(address.country, '')}
- assert shipping_address_to_stripe_dict(address) == expected_address_dict
+ assert shipping_to_stripe_dict(address.as_data()) == expected_address_dict
def test_widget_with_default_options(stripe_payment, gateway_params):
- widget = StripeCheckoutWidget(stripe_payment, gateway_params)
+ payment_info = create_payment_information(stripe_payment)
+ widget = StripeCheckoutWidget(payment_info, gateway_params)
assert widget.render() == (
'<script class="stripe-button" data-allow-remember-me="true" '
'data-amount="4242" data-billing-address="false" data-currency="USD" '
@@ -192,65 +186,73 @@ def test_widget_with_default_options(stripe_payment, gateway_params):
def test_widget_with_additional_attr(stripe_payment, gateway_params):
+ payment_info = create_payment_information(stripe_payment)
+
widget = StripeCheckoutWidget(
- stripe_payment, gateway_params, attrs={'data-custom': 'custom-data'})
+ payment_info, gateway_params, attrs={'data-custom': 'custom-data'})
assert 'data-custom="custom-data"' in widget.render()
def test_widget_with_prefill_option(stripe_payment, gateway_params):
+ payment_info = create_payment_information(stripe_payment)
+
gateway_params['prefill'] = True
- widget = StripeCheckoutWidget(stripe_payment, gateway_params)
+ widget = StripeCheckoutWidget(payment_info, gateway_params)
assert 'data-email="[email protected]"' in widget.render()
gateway_params['prefill'] = False
- widget = StripeCheckoutWidget(stripe_payment, gateway_params)
+ widget = StripeCheckoutWidget(payment_info, gateway_params)
assert 'data-email="[email protected]"' not in widget.render()
def test_widget_with_remember_me_option(stripe_payment, gateway_params):
+ payment_info = create_payment_information(stripe_payment)
+
gateway_params['remember_me'] = True
- widget = StripeCheckoutWidget(stripe_payment, gateway_params)
+ widget = StripeCheckoutWidget(payment_info, gateway_params)
assert 'data-allow-remember-me="true"' in widget.render()
gateway_params['remember_me'] = False
- widget = StripeCheckoutWidget(stripe_payment, gateway_params)
+ widget = StripeCheckoutWidget(payment_info, gateway_params)
assert 'data-allow-remember-me="false"' in widget.render()
def test_widget_with_enable_billing_address_option(stripe_payment, gateway_params):
+ payment_info = create_payment_information(stripe_payment, FAKE_TOKEN)
+
gateway_params['enable_billing_address'] = True
- widget = StripeCheckoutWidget(stripe_payment, gateway_params)
+ widget = StripeCheckoutWidget(payment_info, gateway_params)
assert 'data-billing-address="true"' in widget.render()
assert 'data-zip-code="true"' in widget.render()
gateway_params['enable_billing_address'] = False
- widget = StripeCheckoutWidget(stripe_payment, gateway_params)
+ widget = StripeCheckoutWidget(payment_info, gateway_params)
assert 'data-billing-address="false"' in widget.render()
assert 'data-zip-code="false"' in widget.render()
def test_widget_with_enable_shipping_address_option(stripe_payment, gateway_params):
+ payment_info = create_payment_information(stripe_payment, FAKE_TOKEN)
+
gateway_params['enable_shipping_address'] = True
- widget = StripeCheckoutWidget(stripe_payment, gateway_params)
+ widget = StripeCheckoutWidget(payment_info, gateway_params)
assert 'data-shipping-address="true"' in widget.render()
gateway_params['enable_shipping_address'] = False
- widget = StripeCheckoutWidget(stripe_payment, gateway_params)
+ widget = StripeCheckoutWidget(payment_info, gateway_params)
assert 'data-shipping-address="false"' in widget.render()
def test_stripe_payment_form(stripe_payment, gateway_params):
+ payment_info = create_payment_information(stripe_payment, FAKE_TOKEN)
form = StripePaymentModalForm(
- payment=stripe_payment, gateway=None, gateway_params=gateway_params)
+ payment_information=payment_info, gateway_params=gateway_params)
assert not form.is_valid()
form = StripePaymentModalForm(
- data={'stripeToken': FAKE_TOKEN},
- payment=stripe_payment, gateway=None, gateway_params=gateway_params)
+ data={'stripeToken': FAKE_TOKEN}, payment_information=payment_info,
+ gateway_params=gateway_params)
assert form.is_valid()
- with patch.object(stripe_payment, 'charge') as mocked_charge:
- assert form.process_payment() == stripe_payment
- mocked_charge.assert_called_once_with(payment_token=FAKE_TOKEN)
def test_get_form_class():
@@ -277,7 +279,8 @@ def test_get_error_response_from_exc():
def test_get_stripe_charge_payload_with_shipping(stripe_payment):
- billing_name = get_payment_billing_fullname(stripe_payment)
+ payment_info = create_payment_information(stripe_payment, FAKE_TOKEN)
+ billing_name = get_payment_billing_fullname(payment_info)
expected_payload = {
'capture': True,
'amount': get_amount_for_stripe(
@@ -287,18 +290,17 @@ def test_get_stripe_charge_payload_with_shipping(stripe_payment):
'description': billing_name,
'shipping': {
'name': billing_name,
- 'address': shipping_address_to_stripe_dict(
- stripe_payment.order.shipping_address)}}
+ 'address': shipping_to_stripe_dict(payment_info['shipping'])}}
- charge_payload = _get_stripe_charge_payload(
- stripe_payment, TRANSACTION_AMOUNT, FAKE_TOKEN, True)
+ charge_payload = _get_stripe_charge_payload(payment_info, True)
assert charge_payload == expected_payload
def test_get_stripe_charge_payload_without_shipping(stripe_payment):
stripe_payment.order.shipping_address = None
- billing_name = get_payment_billing_fullname(stripe_payment)
+ payment_info = create_payment_information(stripe_payment, FAKE_TOKEN)
+ billing_name = get_payment_billing_fullname(payment_info)
expected_payload = {
'capture': True,
'amount': get_amount_for_stripe(
@@ -307,8 +309,7 @@ def test_get_stripe_charge_payload_without_shipping(stripe_payment):
'source': FAKE_TOKEN,
'description': billing_name}
- charge_payload = _get_stripe_charge_payload(
- stripe_payment, TRANSACTION_AMOUNT, FAKE_TOKEN, True)
+ charge_payload = _get_stripe_charge_payload(payment_info, True)
assert charge_payload == expected_payload
@@ -316,50 +317,62 @@ def test_get_stripe_charge_payload_without_shipping(stripe_payment):
def test_create_transaction_with_charge_success_response(
stripe_payment,
stripe_charge_success_response):
- txn = _create_transaction(
- payment=stripe_payment, amount=None, kind='ANYKIND',
- response=stripe_charge_success_response)
- assert txn.token == TRANSACTION_TOKEN
- assert txn.is_success is True
- assert isclose(txn.amount, TRANSACTION_AMOUNT)
- assert txn.currency == TRANSACTION_CURRENCY
+ payment_info = create_payment_information(stripe_payment)
+
+ response = _create_response(
+ payment_information=payment_info, kind='ANYKIND',
+ response=stripe_charge_success_response, error=None)
+
+ assert response['transaction_id'] == TRANSACTION_TOKEN
+ assert response['is_success'] is True
+ assert isclose(response['amount'], TRANSACTION_AMOUNT)
+ assert response['currency'] == TRANSACTION_CURRENCY
def test_create_transaction_with_partial_charge_success_response(
stripe_payment,
stripe_partial_charge_success_response):
- txn = _create_transaction(
- payment=stripe_payment, amount=None, kind='ANYKIND',
- response=stripe_partial_charge_success_response)
- assert txn.token == TRANSACTION_TOKEN
- assert txn.is_success is True
+ payment_info = create_payment_information(stripe_payment)
+
+ response = _create_response(
+ payment_information=payment_info, kind='ANYKIND',
+ response=stripe_partial_charge_success_response, error=None)
+
+ assert response['transaction_id'] == TRANSACTION_TOKEN
+ assert response['is_success'] is True
assert isclose(
- txn.amount, TRANSACTION_AMOUNT - TRANSACTION_REFUND_AMOUNT)
- assert txn.currency == TRANSACTION_CURRENCY
+ response['amount'], TRANSACTION_AMOUNT - TRANSACTION_REFUND_AMOUNT)
+ assert response['currency'] == TRANSACTION_CURRENCY
def test_create_transaction_with_refund_success_response(
stripe_payment,
stripe_refund_success_response):
- txn = _create_transaction(
- payment=stripe_payment, amount=None, kind='ANYKIND',
- response=stripe_refund_success_response)
- assert txn.token == TRANSACTION_TOKEN
- assert txn.is_success is True
- assert isclose(txn.amount, TRANSACTION_REFUND_AMOUNT)
- assert txn.currency == TRANSACTION_CURRENCY
+ payment_info = create_payment_information(stripe_payment)
+ response = _create_response(
+ payment_information=payment_info, kind='ANYKIND',
+ response=stripe_refund_success_response, error=None)
-def test_create_transaction_with_error_response(stripe_payment):
+ assert response['transaction_id'] == TRANSACTION_TOKEN
+ assert response['is_success'] is True
+ assert isclose(response['amount'], TRANSACTION_REFUND_AMOUNT)
+ assert response['currency'] == TRANSACTION_CURRENCY
+
+
+def test_create_response_with_error_response(stripe_payment):
payment = stripe_payment
+ payment_info = create_payment_information(payment)
stripe_error_response = {}
- txn = _create_transaction(
- payment=payment, amount=payment.total, kind='ANYKIND',
- response=stripe_error_response)
- assert txn.token == ''
- assert txn.is_success is False
- assert txn.amount == payment.total
- assert txn.currency == payment.currency
+
+ response = _create_response(
+ payment_information=payment_info, kind='ANYKIND',
+ response=stripe_error_response, error=None)
+
+ assert response['transaction_id'] == ''
+ assert response['is_success'] is False
+ assert response['amount'] == payment.total
+ assert response['currency'] == payment.currency
@pytest.mark.integration
@@ -370,19 +383,19 @@ def test_authorize(
gateway_params,
stripe_charge_success_response):
payment = stripe_payment
+ payment_info = create_payment_information(payment, FAKE_TOKEN)
response = stripe_charge_success_response
mock_charge_create.return_value = response
- txn, error = authorize(payment, FAKE_TOKEN, **gateway_params)
+ response = authorize(payment_info, **gateway_params)
- assert not error
- assert txn.payment == payment
- assert txn.token == TRANSACTION_TOKEN
- assert txn.kind == TransactionKind.AUTH
- assert txn.is_success
- assert isclose(txn.amount, TRANSACTION_AMOUNT)
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == response
+ assert not response['error']
+ assert response['transaction_id'] == TRANSACTION_TOKEN
+ assert response['kind'] == TransactionKind.AUTH
+ assert response['is_success']
+ assert isclose(response['amount'], TRANSACTION_AMOUNT)
+ assert response['currency'] == TRANSACTION_CURRENCY
+ assert response['raw_response'] == stripe_charge_success_response
@pytest.mark.integration
@@ -392,20 +405,21 @@ def test_authorize_error_response(
stripe_payment,
gateway_params):
payment = stripe_payment
+ payment_info = create_payment_information(payment, FAKE_TOKEN)
stripe_error = stripe.error.InvalidRequestError(
message=ERROR_MESSAGE, param=None)
mock_charge_create.side_effect = stripe_error
- txn, error = authorize(payment, FAKE_TOKEN, **gateway_params)
+ response = authorize(payment_info, **gateway_params)
- assert error == ERROR_MESSAGE
- assert txn.payment == payment
- assert txn.token == ''
- assert txn.kind == TransactionKind.AUTH
- assert not txn.is_success
- assert txn.amount == payment.total
- assert txn.currency == payment.currency
- assert txn.gateway_response == _get_error_response_from_exc(stripe_error)
+ assert response['error'] == ERROR_MESSAGE
+ assert response['transaction_id'] == ''
+ assert response['kind'] == TransactionKind.AUTH
+ assert not response['is_success']
+ assert response['amount'] == payment.total
+ assert response['currency'] == payment.currency
+ assert response['raw_response'] == _get_error_response_from_exc(
+ stripe_error)
@pytest.mark.integration
@@ -416,20 +430,21 @@ def test_capture(
gateway_params,
stripe_charge_success_response):
payment = stripe_authorized_payment
+ payment_info = create_payment_information(
+ payment, amount=TRANSACTION_AMOUNT)
response = stripe_charge_success_response
mock_charge_retrieve.return_value = Mock(
capture=Mock(return_value=response))
- txn, error = capture(payment, TRANSACTION_AMOUNT, **gateway_params)
+ response = capture(payment_info, **gateway_params)
- assert not error
- assert txn.payment == payment
- assert txn.token == TRANSACTION_TOKEN
- assert txn.kind == TransactionKind.CAPTURE
- assert txn.is_success
- assert isclose(txn.amount, TRANSACTION_AMOUNT)
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == response
+ assert not response['error']
+ assert response['transaction_id'] == TRANSACTION_TOKEN
+ assert response['kind'] == TransactionKind.CAPTURE
+ assert response['is_success']
+ assert isclose(response['amount'], TRANSACTION_AMOUNT)
+ assert response['currency'] == TRANSACTION_CURRENCY
+ assert response['raw_response'] == stripe_charge_success_response
@pytest.mark.integration
@@ -440,20 +455,21 @@ def test_partial_capture(
gateway_params,
stripe_partial_charge_success_response):
payment = stripe_authorized_payment
+ payment_info = create_payment_information(
+ payment, amount=TRANSACTION_AMOUNT)
response = stripe_partial_charge_success_response
mock_charge_retrieve.return_value = Mock(
capture=Mock(return_value=response))
- txn, error = capture(payment, TRANSACTION_AMOUNT, **gateway_params)
+ response = capture(payment_info, **gateway_params)
- assert not error
- assert txn.payment == payment
- assert txn.token == TRANSACTION_TOKEN
- assert txn.kind == TransactionKind.CAPTURE
- assert txn.is_success
- assert isclose(txn.amount, TRANSACTION_AMOUNT - TRANSACTION_REFUND_AMOUNT)
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == response
+ assert not response['error']
+ assert response['transaction_id'] == TRANSACTION_TOKEN
+ assert response['kind'] == TransactionKind.CAPTURE
+ assert response['is_success']
+ assert isclose(response['amount'], TRANSACTION_AMOUNT - TRANSACTION_REFUND_AMOUNT)
+ assert response['currency'] == TRANSACTION_CURRENCY
+ assert response['raw_response'] == stripe_partial_charge_success_response
@pytest.mark.integration
@@ -463,38 +479,22 @@ def test_capture_error_response(
stripe_authorized_payment,
gateway_params):
payment = stripe_authorized_payment
+ payment_info = create_payment_information(
+ payment, amount=TRANSACTION_AMOUNT)
stripe_error = stripe.error.InvalidRequestError(
message=ERROR_MESSAGE, param=None)
mock_charge_retrieve.side_effect = stripe_error
- txn, error = capture(payment, TRANSACTION_AMOUNT, **gateway_params)
-
- assert error == ERROR_MESSAGE
- assert txn.payment == payment
- assert txn.token == ''
- assert txn.kind == TransactionKind.CAPTURE
- assert not txn.is_success
- assert txn.amount == payment.total
- assert txn.currency == payment.currency
- assert txn.gateway_response == _get_error_response_from_exc(stripe_error)
+ response = capture(payment_info, **gateway_params)
-
-def test_capture_not_authorized(
- stripe_payment,
- gateway_params):
- payment = stripe_payment
-
- txn, error = capture(payment, TRANSACTION_AMOUNT, **gateway_params)
-
- assert error == ORDER_NOT_AUTHORIZED
- assert txn.payment == payment
- assert txn.token == ''
- assert txn.kind == TransactionKind.CAPTURE
- assert not txn.is_success
- assert txn.amount == payment.total
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == {}
+ assert response['error'] == ERROR_MESSAGE
+ assert response['transaction_id'] == ''
+ assert response['kind'] == TransactionKind.CAPTURE
+ assert not response['is_success']
+ assert response['amount'] == payment.total
+ assert response['currency'] == payment.currency
+ assert response['raw_response'] == _get_error_response_from_exc(
+ stripe_error)
@pytest.mark.integration
@@ -505,20 +505,20 @@ def test_charge(
gateway_params,
stripe_charge_success_response):
payment = stripe_payment
+ payment_info = create_payment_information(
+ payment, FAKE_TOKEN, TRANSACTION_AMOUNT)
response = stripe_charge_success_response
mock_charge_create.return_value = response
- txn, error = charge(
- payment, FAKE_TOKEN, TRANSACTION_AMOUNT, **gateway_params)
+ response = charge(payment_info, **gateway_params)
- assert not error
- assert txn.payment == payment
- assert txn.token == TRANSACTION_TOKEN
- assert txn.kind == TransactionKind.CHARGE
- assert txn.is_success
- assert isclose(txn.amount, TRANSACTION_AMOUNT)
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == response
+ assert not response['error']
+ assert response['transaction_id'] == TRANSACTION_TOKEN
+ assert response['kind'] == TransactionKind.CHARGE
+ assert response['is_success']
+ assert isclose(response['amount'], TRANSACTION_AMOUNT)
+ assert response['currency'] == TRANSACTION_CURRENCY
+ assert response['raw_response'] == stripe_charge_success_response
@pytest.mark.integration
@@ -528,21 +528,22 @@ def test_charge_error_response(
stripe_payment,
gateway_params):
payment = stripe_payment
+ payment_info = create_payment_information(
+ payment, FAKE_TOKEN, TRANSACTION_AMOUNT)
stripe_error = stripe.error.InvalidRequestError(
message=ERROR_MESSAGE, param=None)
mock_charge_create.side_effect = stripe_error
- txn, error = charge(
- payment, FAKE_TOKEN, TRANSACTION_AMOUNT, **gateway_params)
+ response = charge(payment_info, **gateway_params)
- assert error == ERROR_MESSAGE
- assert txn.payment == payment
- assert txn.token == ''
- assert txn.kind == TransactionKind.CHARGE
- assert not txn.is_success
- assert txn.amount == payment.total
- assert txn.currency == payment.currency
- assert txn.gateway_response == _get_error_response_from_exc(stripe_error)
+ assert response['error'] == ERROR_MESSAGE
+ assert response['transaction_id'] == ''
+ assert response['kind'] == TransactionKind.CHARGE
+ assert not response['is_success']
+ assert response['amount'] == payment.total
+ assert response['currency'] == payment.currency
+ assert response['raw_response'] == _get_error_response_from_exc(
+ stripe_error)
@pytest.mark.integration
@@ -555,20 +556,21 @@ def test_refund_charged(
gateway_params,
stripe_refund_success_response):
payment = stripe_charged_payment
+ payment_info = create_payment_information(
+ payment, amount=TRANSACTION_AMOUNT)
response = stripe_refund_success_response
mock_charge_retrieve.return_value = Mock(id='')
mock_refund_create.return_value = response
- txn, error = refund(payment, TRANSACTION_AMOUNT, **gateway_params)
+ response = refund(payment_info, **gateway_params)
- assert not error
- assert txn.payment == payment
- assert txn.token == TRANSACTION_TOKEN
- assert txn.kind == TransactionKind.REFUND
- assert txn.is_success
- assert isclose(txn.amount, TRANSACTION_REFUND_AMOUNT)
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == response
+ assert not response['error']
+ assert response['transaction_id'] == TRANSACTION_TOKEN
+ assert response['kind'] == TransactionKind.REFUND
+ assert response['is_success']
+ assert isclose(response['amount'], TRANSACTION_REFUND_AMOUNT)
+ assert response['currency'] == TRANSACTION_CURRENCY
+ assert response['raw_response'] == stripe_refund_success_response
@pytest.mark.integration
@@ -581,20 +583,21 @@ def test_refund_captured(
gateway_params,
stripe_refund_success_response):
payment = stripe_captured_payment
+ payment_info = create_payment_information(
+ payment, amount=TRANSACTION_AMOUNT)
response = stripe_refund_success_response
mock_charge_retrieve.return_value = Mock(id='')
mock_refund_create.return_value = response
- txn, error = refund(payment, TRANSACTION_AMOUNT, **gateway_params)
+ response = refund(payment_info, **gateway_params)
- assert not error
- assert txn.payment == payment
- assert txn.token == TRANSACTION_TOKEN
- assert txn.kind == TransactionKind.REFUND
- assert txn.is_success
- assert isclose(txn.amount, TRANSACTION_REFUND_AMOUNT)
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == response
+ assert not response['error']
+ assert response['transaction_id'] == TRANSACTION_TOKEN
+ assert response['kind'] == TransactionKind.REFUND
+ assert response['is_success']
+ assert isclose(response['amount'], TRANSACTION_REFUND_AMOUNT)
+ assert response['currency'] == TRANSACTION_CURRENCY
+ assert response['raw_response'] == stripe_refund_success_response
@pytest.mark.integration
@@ -606,39 +609,23 @@ def test_refund_error_response(
stripe_charged_payment,
gateway_params):
payment = stripe_charged_payment
+ payment_info = create_payment_information(
+ payment, amount=TRANSACTION_AMOUNT)
mock_charge_retrieve.return_value = Mock(id='')
stripe_error = stripe.error.InvalidRequestError(
message=ERROR_MESSAGE, param=None)
mock_refund_create.side_effect = stripe_error
- txn, error = refund(payment, TRANSACTION_AMOUNT, **gateway_params)
+ response = refund(payment_info, **gateway_params)
- assert error == ERROR_MESSAGE
- assert txn.payment == payment
- assert txn.token == ''
- assert txn.kind == TransactionKind.REFUND
- assert not txn.is_success
- assert txn.amount == payment.total
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == _get_error_response_from_exc(stripe_error)
-
-
-def test_refund_not_charged_or_captured(
- stripe_payment,
- gateway_params):
- payment = stripe_payment
-
- txn, error = refund(payment, TRANSACTION_AMOUNT, **gateway_params)
-
- assert error == ORDER_NOT_CHARGED
- assert txn.payment == payment
- assert txn.token == ''
- assert txn.kind == TransactionKind.REFUND
- assert not txn.is_success
- assert txn.amount == payment.total
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == {}
+ assert response['error'] == ERROR_MESSAGE
+ assert response['transaction_id'] == ''
+ assert response['kind'] == TransactionKind.REFUND
+ assert not response['is_success']
+ assert response['amount'] == payment.total
+ assert response['currency'] == TRANSACTION_CURRENCY
+ assert response['raw_response'] == _get_error_response_from_exc(
+ stripe_error)
@pytest.mark.integration
@@ -651,20 +638,20 @@ def test_void(
gateway_params,
stripe_refund_success_response):
payment = stripe_authorized_payment
+ payment_info = create_payment_information(payment)
response = stripe_refund_success_response
mock_charge_retrieve.return_value = Mock(id='')
mock_refund_create.return_value = response
- txn, error = void(payment, **gateway_params)
+ response = void(payment_info, **gateway_params)
- assert not error
- assert txn.payment == payment
- assert txn.token == TRANSACTION_TOKEN
- assert txn.kind == TransactionKind.VOID
- assert txn.is_success
- assert isclose(txn.amount, TRANSACTION_REFUND_AMOUNT)
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == response
+ assert not response['error']
+ assert response['transaction_id'] == TRANSACTION_TOKEN
+ assert response['kind'] == TransactionKind.VOID
+ assert response['is_success']
+ assert isclose(response['amount'], TRANSACTION_REFUND_AMOUNT)
+ assert response['currency'] == TRANSACTION_CURRENCY
+ assert response['raw_response'] == stripe_refund_success_response
@pytest.mark.integration
@@ -676,36 +663,18 @@ def test_void_error_response(
stripe_authorized_payment,
gateway_params):
payment = stripe_authorized_payment
+ payment_info = create_payment_information(payment)
mock_charge_retrieve.return_value = Mock(id='')
stripe_error = stripe.error.InvalidRequestError(
message=ERROR_MESSAGE, param=None)
mock_refund_create.side_effect = stripe_error
- txn, error = void(payment, **gateway_params)
-
- assert error == ERROR_MESSAGE
- assert txn.payment == payment
- assert txn.token == ''
- assert txn.kind == TransactionKind.VOID
- assert not txn.is_success
- assert txn.amount == payment.total
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == _get_error_response_from_exc(stripe_error)
-
-
-def test_void_not_authorized(
- stripe_payment,
- gateway_params):
- payment = stripe_payment
-
- txn, error = void(payment, **gateway_params)
+ response = void(payment_info, **gateway_params)
- assert error == ORDER_NOT_AUTHORIZED
- assert txn.payment == payment
- assert txn.token == ''
- assert txn.kind == TransactionKind.VOID
- assert not txn.is_success
- assert txn.amount == payment.total
- assert txn.currency == TRANSACTION_CURRENCY
- assert txn.gateway_response == {}
+ assert response['error'] == ERROR_MESSAGE
+ assert response['transaction_id'] == ''
+ assert response['kind'] == TransactionKind.VOID
+ assert not response['is_success']
+ assert response['amount'] == payment.total
+ assert response['currency'] == TRANSACTION_CURRENCY
+ assert response['raw_response'] == {}