Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Test stripe utils for billing and shipping
Jakub Draganekcc59b4b4a83e
diff --git a/saleor/payment/gateways/stripe_new/forms.py b/saleor/payment/gateways/stripe_new/forms.pydeleted file mode 100644index 93f8ea0ac3..0000000000--- a/saleor/payment/gateways/stripe_new/forms.py+++ /dev/null@@ -1,85 +0,0 @@-from typing import Dict--from django import forms-from django.forms.utils import flatatt-from django.forms.widgets import HiddenInput-from django.utils.html import format_html-from django.utils.translation import pgettext_lazy--from ...interface import PaymentData-from .utils import get_amount_for_stripe--CHECKOUT_SCRIPT_URL = "https://checkout.stripe.com/checkout.js"-CHECKOUT_DESCRIPTION = pgettext_lazy(- "Stripe payment gateway description", "Total payment"-)---class StripeCheckoutWidget(HiddenInput):- def __init__(- self, payment_information: PaymentData, gateway_params: Dict, *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_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_information.currency,- "data-locale": gateway_params.get("locale"),- "data-allow-remember-me": "true"- if gateway_params.get("remember_me")- else "false",- "data-billing-address": "true"- if gateway_params.get("enable_billing_address")- else "false",- "data-zip-code": "true"- if gateway_params.get("enable_billing_address")- else "false",- "data-shipping-address": "true"- if gateway_params.get("enable_shipping_address")- else "false",- }-- if gateway_params.get("prefill"):- kwargs["attrs"].update({"data-email": payment_information.customer_email})-- kwargs["attrs"].update(attrs)- super(StripeCheckoutWidget, self).__init__(*args, **kwargs)-- def render(self, name=None, value=None, attrs=None, renderer=None):- attrs = attrs or {}- attrs.update(self.attrs)- attrs.pop("id", None)- return format_html("<script{0}></script>", flatatt(attrs))---class StripePaymentModalForm(forms.Form):- """The modal form of the Stripe payment provider.-- 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)-- def __init__(- self, payment_information: PaymentData, gateway_params: Dict, *args, **kwargs- ):- super().__init__(*args, **kwargs)-- self.fields["stripe"] = forms.CharField(- widget=StripeCheckoutWidget(- payment_information=payment_information, gateway_params=gateway_params- ),- required=False,- )-- def get_payment_token(self):- return self.cleaned_data["stripeToken"]diff --git a/tests/gateway/stripe/test_stripe.py b/tests/gateway/stripe/test_stripe.pyindex 6008f91971..459291a71c 100644--- a/tests/gateway/stripe/test_stripe.py+++ b/tests/gateway/stripe/test_stripe.py@@ -10,6 +10,8 @@ from saleor.payment.gateways.stripe_new import (authorize,capture,confirm,+ _get_client,+ get_client_token,list_client_sources,refund,void,@@ -342,3 +344,11 @@ def test_list_customer_sources(sandbox_gateway_config):)sources = list_client_sources(sandbox_gateway_config, CUSTOMER_ID)assert sources == [expected_customer_source]+++def test_get_client(gateway_config):+ assert _get_client(**gateway_config.connection_params).api_key == "secret"+++def test_get_client_token():+ assert get_client_token() is Nonediff --git a/tests/gateway/stripe/test_utils.py b/tests/gateway/stripe/test_utils.pynew file mode 100644index 0000000000..b20c4a3c8f--- /dev/null+++ b/tests/gateway/stripe/test_utils.py@@ -0,0 +1,78 @@+from decimal import Decimal+from math import isclose++from django_countries import countries++from saleor.payment.gateways.stripe_new.utils import (+ get_amount_for_stripe,+ get_amount_from_stripe,+ get_currency_from_stripe,+ get_currency_for_stripe,+ get_payment_billing_fullname,+ shipping_to_stripe_dict,+)+from saleor.payment.interface import AddressData+from saleor.payment.utils import create_payment_information+++def test_get_amount_for_stripe():+ assert get_amount_for_stripe(Decimal(1), "USD") == 100+ assert get_amount_for_stripe(Decimal(1), "usd") == 100++ assert get_amount_for_stripe(Decimal(0.01), "USD") == 1+ assert get_amount_for_stripe(Decimal(24.24), "USD") == 2424+ assert get_amount_for_stripe(Decimal(42.42), "USD") == 4242++ assert get_amount_for_stripe(Decimal(1), "JPY") == 1+ assert get_amount_for_stripe(Decimal(1), "jpy") == 1+++def test_get_amount_from_stripe():+ assert get_amount_from_stripe(100, "USD") == Decimal(1)+ assert get_amount_from_stripe(100, "usd") == Decimal(1)++ assert isclose(get_amount_from_stripe(1, "USD"), Decimal(0.01))+ assert isclose(get_amount_from_stripe(2424, "USD"), Decimal(24.24))+ assert isclose(get_amount_from_stripe(4242, "USD"), Decimal(42.42))++ assert get_amount_from_stripe(1, "JPY") == Decimal(1)+ assert get_amount_from_stripe(1, "jpy") == Decimal(1)+++def test_get_currency_for_stripe():+ assert get_currency_for_stripe("USD") == "usd"+ assert get_currency_for_stripe("usd") == "usd"+ assert get_currency_for_stripe("uSd") == "usd"+++def test_get_currency_from_stripe():+ assert get_currency_from_stripe("USD") == "USD"+ assert get_currency_from_stripe("usd") == "USD"+ assert get_currency_from_stripe("uSd") == "USD"+++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_info) == expected_fullname+++def test_shipping_address_to_stripe_dict(address):+ address_data = AddressData(**address.as_data())+ expected_address_dict = {+ "name": "John Doe",+ "phone": "+48713988102",+ "address": {+ "line1": address.street_address_1,+ "line2": address.street_address_2,+ "city": address.city,+ "state": address.country_area,+ "postal_code": address.postal_code,+ "country": dict(countries).get(address.country, ""),+ },+ }+ assert shipping_to_stripe_dict(address_data) == expected_address_dict