chore: refactor settings
This commit is contained in:
@@ -1,4 +0,0 @@
|
|||||||
from .base import *
|
|
||||||
|
|
||||||
DEBUG = True
|
|
||||||
ALLOWED_HOSTS = ["*"]
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
from .base import *
|
|
||||||
@@ -35,14 +35,14 @@ class ShopModelsTest(TestCase):
|
|||||||
|
|
||||||
def test_create_order(self):
|
def test_create_order(self):
|
||||||
self.customer = Customer.objects.create(
|
self.customer = Customer.objects.create(
|
||||||
first_name='Luke',
|
first_name="Luke",
|
||||||
last_name='Skywalker',
|
last_name="Skywalker",
|
||||||
vat_id='11111111H',
|
vat_id="11111111H",
|
||||||
address='Farm Skywalker',
|
address="Farm Skywalker",
|
||||||
city='Desert',
|
city="Desert",
|
||||||
state='Mos-Eisley',
|
state="Mos-Eisley",
|
||||||
zip='00001',
|
zip="00001",
|
||||||
country='Tatooine',
|
country="Tatooine",
|
||||||
)
|
)
|
||||||
|
|
||||||
l1 = create_order_line_for_product(
|
l1 = create_order_line_for_product(
|
||||||
|
|||||||
+5
-9
@@ -24,18 +24,16 @@ def create_order_line_for_product(product: Product, quantity: Decimal, tax: Tax)
|
|||||||
def create_order(
|
def create_order(
|
||||||
customer: Customer,
|
customer: Customer,
|
||||||
lines: Iterable,
|
lines: Iterable,
|
||||||
|
|
||||||
billing_address: str,
|
billing_address: str,
|
||||||
billing_city: str,
|
billing_city: str,
|
||||||
billing_state: str,
|
billing_state: str,
|
||||||
billing_country: str,
|
billing_country: str,
|
||||||
billing_zip: str,
|
billing_zip: str,
|
||||||
|
shipping_address: str = "",
|
||||||
shipping_address: str = '',
|
shipping_city: str = "",
|
||||||
shipping_city: str = '',
|
shipping_state: str = "",
|
||||||
shipping_state: str = '',
|
shipping_country: str = "",
|
||||||
shipping_country: str = '',
|
shipping_zip: str = "",
|
||||||
shipping_zip: str = '',
|
|
||||||
) -> Order:
|
) -> Order:
|
||||||
billing_address = billing_address if billing_address else customer.address
|
billing_address = billing_address if billing_address else customer.address
|
||||||
billing_city = billing_city if billing_city else customer.city
|
billing_city = billing_city if billing_city else customer.city
|
||||||
@@ -51,13 +49,11 @@ def create_order(
|
|||||||
|
|
||||||
order = Order.objects.create(
|
order = Order.objects.create(
|
||||||
customer=customer,
|
customer=customer,
|
||||||
|
|
||||||
billing_address=billing_address,
|
billing_address=billing_address,
|
||||||
billing_city=billing_city,
|
billing_city=billing_city,
|
||||||
billing_state=billing_state,
|
billing_state=billing_state,
|
||||||
billing_country=billing_country,
|
billing_country=billing_country,
|
||||||
billing_zip=billing_zip,
|
billing_zip=billing_zip,
|
||||||
|
|
||||||
shipping_address=shipping_address,
|
shipping_address=shipping_address,
|
||||||
shipping_city=shipping_city,
|
shipping_city=shipping_city,
|
||||||
shipping_state=shipping_state,
|
shipping_state=shipping_state,
|
||||||
|
|||||||
+22
-7
@@ -43,11 +43,15 @@ class RedsysClient:
|
|||||||
def get_currency_code(self) -> str:
|
def get_currency_code(self) -> str:
|
||||||
return settings.REDSYS_CURRENCY_CODE
|
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})
|
path = reverse("tpv:ok", kwargs={"transaction": transaction.hash})
|
||||||
return f"{settings.REDSYS_TPV_DOMAIN}{path}"
|
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})
|
path = reverse("tpv:ko", kwargs={"transaction": transaction.hash})
|
||||||
return f"{settings.REDSYS_TPV_DOMAIN}{path}"
|
return f"{settings.REDSYS_TPV_DOMAIN}{path}"
|
||||||
|
|
||||||
@@ -63,7 +67,9 @@ class RedsysClient:
|
|||||||
return compute_signature(str(hash), payload, key).decode()
|
return compute_signature(str(hash), payload, key).decode()
|
||||||
|
|
||||||
def get_merchant_parameters_for_transaction(
|
def get_merchant_parameters_for_transaction(
|
||||||
self, transaction: PaymentTransaction, transaction_type: int = TransactionTypes.AUTHORIZATION
|
self,
|
||||||
|
transaction: PaymentTransaction,
|
||||||
|
transaction_type: int = TransactionTypes.AUTHORIZATION,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
merchant_code = self.get_merchant_code()
|
merchant_code = self.get_merchant_code()
|
||||||
|
|
||||||
@@ -71,7 +77,10 @@ class RedsysClient:
|
|||||||
"DS_MERCHANT_AMOUNT": str(transaction.amount_integer),
|
"DS_MERCHANT_AMOUNT": str(transaction.amount_integer),
|
||||||
"DS_MERCHANT_CURRENCY": self.get_currency_code(),
|
"DS_MERCHANT_CURRENCY": self.get_currency_code(),
|
||||||
"DS_MERCHANT_MERCHANTCODE": merchant_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_ORDER": transaction.hash.hex,
|
||||||
"DS_MERCHANT_TERMINAL": self.get_terminal(),
|
"DS_MERCHANT_TERMINAL": self.get_terminal(),
|
||||||
"DS_MERCHANT_TRANSACTIONTYPE": transaction_type,
|
"DS_MERCHANT_TRANSACTIONTYPE": transaction_type,
|
||||||
@@ -82,14 +91,20 @@ class RedsysClient:
|
|||||||
}
|
}
|
||||||
|
|
||||||
def get_encoded_merchant_parameters_for_transaction(
|
def get_encoded_merchant_parameters_for_transaction(
|
||||||
self, transaction: PaymentTransaction, transaction_type: int = TransactionTypes.AUTHORIZATION
|
self,
|
||||||
|
transaction: PaymentTransaction,
|
||||||
|
transaction_type: int = TransactionTypes.AUTHORIZATION,
|
||||||
) -> str:
|
) -> 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)
|
stringified_body = json.dumps(body)
|
||||||
return base64.b64encode(stringified_body.encode()).decode("utf-8")
|
return base64.b64encode(stringified_body.encode()).decode("utf-8")
|
||||||
|
|
||||||
def get_body_for_transaction(
|
def get_body_for_transaction(
|
||||||
self, transaction: PaymentTransaction, transaction_type: int = TransactionTypes.AUTHORIZATION
|
self,
|
||||||
|
transaction: PaymentTransaction,
|
||||||
|
transaction_type: int = TransactionTypes.AUTHORIZATION,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
merchant_parameters = self.get_encoded_merchant_parameters_for_transaction(
|
merchant_parameters = self.get_encoded_merchant_parameters_for_transaction(
|
||||||
transaction, transaction_type
|
transaction, transaction_type
|
||||||
|
|||||||
@@ -30,7 +30,9 @@ class TestRedsysTPV(APITestCase):
|
|||||||
transaction.save()
|
transaction.save()
|
||||||
|
|
||||||
client = RedsysClient()
|
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_ORDER") == transaction.hash.hex
|
||||||
assert merchant_parameters.get("DS_MERCHANT_AMOUNT") == str(
|
assert merchant_parameters.get("DS_MERCHANT_AMOUNT") == str(
|
||||||
|
|||||||
+3
-1
@@ -39,7 +39,9 @@ def compare_signatures(signature_1, signature_2):
|
|||||||
return sig1safe == sig2safe
|
return sig1safe == sig2safe
|
||||||
|
|
||||||
|
|
||||||
def validate_payment_for_transaction(request, transaction: PaymentTransaction) -> Decimal:
|
def validate_payment_for_transaction(
|
||||||
|
request, transaction: PaymentTransaction
|
||||||
|
) -> Decimal:
|
||||||
"""
|
"""
|
||||||
example_response_data = {
|
example_response_data = {
|
||||||
'Ds_MerchantCode': '999008881',
|
'Ds_MerchantCode': '999008881',
|
||||||
|
|||||||
+9
-3
@@ -41,14 +41,18 @@ def webhook(request, transaction):
|
|||||||
try:
|
try:
|
||||||
amount_paid = validate_payment_for_transaction(request, transaction)
|
amount_paid = validate_payment_for_transaction(request, transaction)
|
||||||
pay_transaction(transaction, amount_paid)
|
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)
|
return HttpResponse(status=200)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
transaction.status = PaymentTransaction.StatusChoices.ERROR
|
transaction.status = PaymentTransaction.StatusChoices.ERROR
|
||||||
transaction.observations = str(e)
|
transaction.observations = str(e)
|
||||||
transaction.save()
|
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)
|
return HttpResponse(status=409)
|
||||||
|
|
||||||
@@ -64,7 +68,9 @@ def transaction_created(request, transaction):
|
|||||||
template_name="tpv/order_created.html",
|
template_name="tpv/order_created.html",
|
||||||
context={
|
context={
|
||||||
"form": UpdateEmailForm({"contact_email": transaction.contact_email}),
|
"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,
|
"order": transaction,
|
||||||
"signature_version": parameters.get("Ds_SignatureVersion"),
|
"signature_version": parameters.get("Ds_SignatureVersion"),
|
||||||
"merchant_parameters": parameters.get("Ds_MerchantParameters"),
|
"merchant_parameters": parameters.get("Ds_MerchantParameters"),
|
||||||
|
|||||||
Reference in New Issue
Block a user