104 lines
3.9 KiB
Python
104 lines
3.9 KiB
Python
import base64
|
|
import json
|
|
|
|
from django.conf import settings
|
|
from django.urls import reverse
|
|
from tpv.models import PaymentTransaction
|
|
from tpv.utils import compute_signature
|
|
|
|
|
|
class TransactionTypes:
|
|
AUTHORIZATION = 0
|
|
PREAUTHORIZATION = 1
|
|
REPLACEMENT_PREAUTHORIZATION = 11
|
|
CONFIRMATION = 2
|
|
REFUND = 3
|
|
SEPARATED_CONFIRMATION = 8
|
|
CANCELED = 9
|
|
PAYGOLD = 15
|
|
PUCE_AUTHENTICATION = 17
|
|
REFUND_WITHOUT_ORIGINAL = 34
|
|
BET_PRICE = 37
|
|
PAYMENT_CANCELED = 45
|
|
REFUND_CANCELED = 46
|
|
SEPARATED_CONFIRMATION_CANCELED = 47
|
|
LINK_EXPIRATION_CHANGED_FOR_PAYGOLD = 51
|
|
|
|
|
|
class RedsysClient:
|
|
DEBUG_ENVIRONMENT_URL = "https://sis-t.redsys.es:25443/sis/realizarPago"
|
|
PROD_ENVIRONMENT_URL = ""
|
|
|
|
def get_target_url(self):
|
|
if settings.DEBUG:
|
|
return self.DEBUG_ENVIRONMENT_URL
|
|
return self.PROD_ENVIRONMENT_URL
|
|
|
|
def get_merchant_code(self) -> str:
|
|
return settings.REDSYS_MERCHANT_CODE
|
|
|
|
def get_terminal(self) -> str:
|
|
return settings.REDSYS_TERMINAL
|
|
|
|
def get_currency_code(self) -> str:
|
|
return settings.REDSYS_CURRENCY_CODE
|
|
|
|
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:
|
|
path = reverse("tpv:ko", kwargs={"transaction": transaction.hash})
|
|
return f"{settings.REDSYS_TPV_DOMAIN}{path}"
|
|
|
|
def get_webhook_url_for_transaction(self, transaction: PaymentTransaction) -> str:
|
|
path = reverse("tpv:webhook", kwargs={"transaction": transaction.hash})
|
|
return f"{settings.REDSYS_TPV_DOMAIN}{path}"
|
|
|
|
def get_shared_secret(self) -> str:
|
|
return settings.REDSYS_SHARED_SECRET
|
|
|
|
def get_signature(self, hash, payload: str):
|
|
key = self.get_shared_secret()
|
|
return compute_signature(str(hash), payload, key).decode()
|
|
|
|
def get_merchant_parameters_for_transaction(
|
|
self, transaction: PaymentTransaction, transaction_type: int = TransactionTypes.AUTHORIZATION
|
|
) -> dict:
|
|
merchant_code = self.get_merchant_code()
|
|
|
|
return {
|
|
"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
|
|
"DS_MERCHANT_ORDER": transaction.hash.hex,
|
|
"DS_MERCHANT_TERMINAL": self.get_terminal(),
|
|
"DS_MERCHANT_TRANSACTIONTYPE": transaction_type,
|
|
# Página informativa al usuario - Pago erróneo
|
|
"DS_MERCHANT_URLKO": self.get_merchant_url_ko_for_transaction(transaction),
|
|
# Página informativa al usuario - Pago confirmado
|
|
"DS_MERCHANT_URLOK": self.get_merchant_url_ok_for_transaction(transaction),
|
|
}
|
|
|
|
def get_encoded_merchant_parameters_for_transaction(
|
|
self, transaction: PaymentTransaction, transaction_type: int = TransactionTypes.AUTHORIZATION
|
|
) -> str:
|
|
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
|
|
) -> dict:
|
|
merchant_parameters = self.get_encoded_merchant_parameters_for_transaction(
|
|
transaction, transaction_type
|
|
)
|
|
signature = self.get_signature(transaction.hash.hex, merchant_parameters)
|
|
|
|
return {
|
|
"Ds_MerchantParameters": merchant_parameters,
|
|
"Ds_SignatureVersion": "HMAC_SHA256_V1",
|
|
"Ds_Signature": signature,
|
|
}
|