From 6b04fe7e231a4dcb2c157478286f07118f15d954 Mon Sep 17 00:00:00 2001 From: Pablo Moreno Date: Sun, 24 Mar 2024 20:59:23 +0100 Subject: [PATCH] chore: refactor settings --- config/settings/develop.py | 4 ---- config/settings/production.py | 1 - shop/tests/test_shop_models.py | 16 ++++++++-------- shop/utils.py | 28 ++++++++++++---------------- tpv/redsys.py | 29 ++++++++++++++++++++++------- tpv/tests/test_redsys.py | 4 +++- tpv/utils.py | 4 +++- tpv/views.py | 12 +++++++++--- 8 files changed, 57 insertions(+), 41 deletions(-) delete mode 100644 config/settings/develop.py delete mode 100644 config/settings/production.py diff --git a/config/settings/develop.py b/config/settings/develop.py deleted file mode 100644 index 2844fbe..0000000 --- a/config/settings/develop.py +++ /dev/null @@ -1,4 +0,0 @@ -from .base import * - -DEBUG = True -ALLOWED_HOSTS = ["*"] diff --git a/config/settings/production.py b/config/settings/production.py deleted file mode 100644 index 9b5ed21..0000000 --- a/config/settings/production.py +++ /dev/null @@ -1 +0,0 @@ -from .base import * diff --git a/shop/tests/test_shop_models.py b/shop/tests/test_shop_models.py index c27a260..88b5229 100644 --- a/shop/tests/test_shop_models.py +++ b/shop/tests/test_shop_models.py @@ -35,14 +35,14 @@ class ShopModelsTest(TestCase): def test_create_order(self): self.customer = Customer.objects.create( - first_name='Luke', - last_name='Skywalker', - vat_id='11111111H', - address='Farm Skywalker', - city='Desert', - state='Mos-Eisley', - zip='00001', - country='Tatooine', + first_name="Luke", + last_name="Skywalker", + vat_id="11111111H", + address="Farm Skywalker", + city="Desert", + state="Mos-Eisley", + zip="00001", + country="Tatooine", ) l1 = create_order_line_for_product( diff --git a/shop/utils.py b/shop/utils.py index e629ffc..1d9a545 100644 --- a/shop/utils.py +++ b/shop/utils.py @@ -22,20 +22,18 @@ def create_order_line_for_product(product: Product, quantity: Decimal, tax: Tax) def create_order( - customer: Customer, - lines: Iterable, - - billing_address: str, - billing_city: str, - billing_state: str, - billing_country: str, - billing_zip: str, - - shipping_address: str = '', - shipping_city: str = '', - shipping_state: str = '', - shipping_country: str = '', - shipping_zip: str = '', + customer: Customer, + lines: Iterable, + billing_address: str, + billing_city: str, + billing_state: str, + billing_country: str, + billing_zip: str, + shipping_address: str = "", + shipping_city: str = "", + shipping_state: str = "", + shipping_country: str = "", + shipping_zip: str = "", ) -> Order: billing_address = billing_address if billing_address else customer.address billing_city = billing_city if billing_city else customer.city @@ -51,13 +49,11 @@ def create_order( order = Order.objects.create( customer=customer, - billing_address=billing_address, billing_city=billing_city, billing_state=billing_state, billing_country=billing_country, billing_zip=billing_zip, - shipping_address=shipping_address, shipping_city=shipping_city, shipping_state=shipping_state, diff --git a/tpv/redsys.py b/tpv/redsys.py index ed7f7f7..6a1df64 100644 --- a/tpv/redsys.py +++ b/tpv/redsys.py @@ -43,11 +43,15 @@ class RedsysClient: def get_currency_code(self) -> str: return settings.REDSYS_CURRENCY_CODE - def get_merchant_url_ok_for_transaction(self, transaction: PaymentTransaction) -> str: + def get_merchant_url_ok_for_transaction( + self, transaction: PaymentTransaction + ) -> str: path = reverse("tpv:ok", kwargs={"transaction": transaction.hash}) return f"{settings.REDSYS_TPV_DOMAIN}{path}" - def get_merchant_url_ko_for_transaction(self, transaction: PaymentTransaction) -> str: + def get_merchant_url_ko_for_transaction( + self, transaction: PaymentTransaction + ) -> str: path = reverse("tpv:ko", kwargs={"transaction": transaction.hash}) return f"{settings.REDSYS_TPV_DOMAIN}{path}" @@ -63,7 +67,9 @@ class RedsysClient: return compute_signature(str(hash), payload, key).decode() def get_merchant_parameters_for_transaction( - self, transaction: PaymentTransaction, transaction_type: int = TransactionTypes.AUTHORIZATION + self, + transaction: PaymentTransaction, + transaction_type: int = TransactionTypes.AUTHORIZATION, ) -> dict: merchant_code = self.get_merchant_code() @@ -71,7 +77,10 @@ class RedsysClient: "DS_MERCHANT_AMOUNT": str(transaction.amount_integer), "DS_MERCHANT_CURRENCY": self.get_currency_code(), "DS_MERCHANT_MERCHANTCODE": merchant_code, - "DS_MERCHANT_MERCHANTURL": self.get_webhook_url_for_transaction(transaction), # Webhook + # Webhook + "DS_MERCHANT_MERCHANTURL": self.get_webhook_url_for_transaction( + transaction + ), "DS_MERCHANT_ORDER": transaction.hash.hex, "DS_MERCHANT_TERMINAL": self.get_terminal(), "DS_MERCHANT_TRANSACTIONTYPE": transaction_type, @@ -82,14 +91,20 @@ class RedsysClient: } def get_encoded_merchant_parameters_for_transaction( - self, transaction: PaymentTransaction, transaction_type: int = TransactionTypes.AUTHORIZATION + self, + transaction: PaymentTransaction, + transaction_type: int = TransactionTypes.AUTHORIZATION, ) -> str: - body = self.get_merchant_parameters_for_transaction(transaction, transaction_type) + body = self.get_merchant_parameters_for_transaction( + transaction, transaction_type + ) stringified_body = json.dumps(body) return base64.b64encode(stringified_body.encode()).decode("utf-8") def get_body_for_transaction( - self, transaction: PaymentTransaction, transaction_type: int = TransactionTypes.AUTHORIZATION + self, + transaction: PaymentTransaction, + transaction_type: int = TransactionTypes.AUTHORIZATION, ) -> dict: merchant_parameters = self.get_encoded_merchant_parameters_for_transaction( transaction, transaction_type diff --git a/tpv/tests/test_redsys.py b/tpv/tests/test_redsys.py index 2c95e05..405a40f 100644 --- a/tpv/tests/test_redsys.py +++ b/tpv/tests/test_redsys.py @@ -30,7 +30,9 @@ class TestRedsysTPV(APITestCase): transaction.save() client = RedsysClient() - merchant_parameters = client.get_merchant_parameters_for_transaction(transaction) + merchant_parameters = client.get_merchant_parameters_for_transaction( + transaction + ) assert merchant_parameters.get("DS_MERCHANT_ORDER") == transaction.hash.hex assert merchant_parameters.get("DS_MERCHANT_AMOUNT") == str( diff --git a/tpv/utils.py b/tpv/utils.py index 48ba8ea..0c17e1d 100644 --- a/tpv/utils.py +++ b/tpv/utils.py @@ -39,7 +39,9 @@ def compare_signatures(signature_1, signature_2): return sig1safe == sig2safe -def validate_payment_for_transaction(request, transaction: PaymentTransaction) -> Decimal: +def validate_payment_for_transaction( + request, transaction: PaymentTransaction +) -> Decimal: """ example_response_data = { 'Ds_MerchantCode': '999008881', diff --git a/tpv/views.py b/tpv/views.py index 227563c..bf7cfec 100644 --- a/tpv/views.py +++ b/tpv/views.py @@ -41,14 +41,18 @@ def webhook(request, transaction): try: amount_paid = validate_payment_for_transaction(request, transaction) pay_transaction(transaction, amount_paid) - redsys_payment_accepted.send_robust(PaymentTransaction.__class__, hash=transaction.hash) + redsys_payment_accepted.send_robust( + PaymentTransaction.__class__, hash=transaction.hash + ) return HttpResponse(status=200) except Exception as e: transaction.status = PaymentTransaction.StatusChoices.ERROR transaction.observations = str(e) transaction.save() - redsys_payment_rejected.send_robust(PaymentTransaction.__class__, hash=transaction.hash) + redsys_payment_rejected.send_robust( + PaymentTransaction.__class__, hash=transaction.hash + ) return HttpResponse(status=409) @@ -64,7 +68,9 @@ def transaction_created(request, transaction): template_name="tpv/order_created.html", context={ "form": UpdateEmailForm({"contact_email": transaction.contact_email}), - "action": reverse("tpv:order_created", kwargs={"transaction": transaction.hash}), + "action": reverse( + "tpv:order_created", kwargs={"transaction": transaction.hash} + ), "order": transaction, "signature_version": parameters.get("Ds_SignatureVersion"), "merchant_parameters": parameters.get("Ds_MerchantParameters"),