chore: refactor settings

This commit is contained in:
2024-03-24 20:59:23 +01:00
parent 05ece2c074
commit 6b04fe7e23
8 changed files with 57 additions and 41 deletions
-4
View File
@@ -1,4 +0,0 @@
from .base import *
DEBUG = True
ALLOWED_HOSTS = ["*"]
-1
View File
@@ -1 +0,0 @@
from .base import *
+8 -8
View File
@@ -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(
+5 -9
View File
@@ -24,18 +24,16 @@ 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 = '',
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,
+22 -7
View File
@@ -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
+3 -1
View File
@@ -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(
+3 -1
View File
@@ -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',
+9 -3
View File
@@ -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"),