chore: refactor settings
This commit is contained in:
+22
-7
@@ -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
|
||||
|
||||
@@ -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
@@ -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
@@ -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"),
|
||||
|
||||
Reference in New Issue
Block a user