feat: added tpv app
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
|
||||
import pyDes
|
||||
import re
|
||||
|
||||
from decimal import Decimal
|
||||
from django.utils.text import gettext_lazy as _
|
||||
|
||||
from tpv.exceptions import RedsysValidationException, RedsysPaymentException
|
||||
from tpv.models import PaymentTPV
|
||||
from tpv.settings import ERROR_CODES
|
||||
|
||||
|
||||
def compute_signature(salt, payload, key):
|
||||
"""
|
||||
:param salt: order number (Ds_Order or Ds_Merchant_Order)
|
||||
:param payload: Ds_MerchantParameters
|
||||
:param key: shared secret (aka key) from the Redsys Administration Module
|
||||
:return:
|
||||
"""
|
||||
b64_key = base64.b64decode(key)
|
||||
|
||||
des3 = pyDes.triple_des(
|
||||
b64_key, mode=pyDes.CBC, IV="\0" * 8, pad="\0", padmode=pyDes.PAD_NORMAL
|
||||
)
|
||||
pepper = des3.encrypt(str(salt))
|
||||
|
||||
payload_hash = hmac.new(pepper, payload.encode(), hashlib.sha256).digest()
|
||||
return base64.b64encode(payload_hash)
|
||||
|
||||
|
||||
def compare_signatures(signature_1, signature_2):
|
||||
alphanumeric_characters = re.compile("[^a-zA-Z0-9]")
|
||||
sig1safe = re.sub(alphanumeric_characters, "", signature_1)
|
||||
sig2safe = re.sub(alphanumeric_characters, "", signature_2)
|
||||
return sig1safe == sig2safe
|
||||
|
||||
|
||||
def validate_payment_for_order(request, order: PaymentTPV) -> Decimal:
|
||||
"""
|
||||
example_response_data = {
|
||||
'Ds_MerchantCode': '999008881',
|
||||
'Ds_Terminal': '001',
|
||||
'Ds_Order': 'a082bd1cda0b488786f23a62e89107c0',
|
||||
'Ds_Amount': '13532',
|
||||
'Ds_Currency': '978',
|
||||
'Ds_Date': '20/03/2024',
|
||||
'Ds_Hour': '12:21',
|
||||
'Ds_SecurePayment': '1',
|
||||
'Ds_Card_Number': '454881******1156',
|
||||
'Ds_Card_Country': '724',
|
||||
'Ds_Response': '0000',
|
||||
'Ds_MerchantData': '',
|
||||
'Ds_TransactionType': '0',
|
||||
'Ds_ConsumerLanguage': '1',
|
||||
'Ds_AuthorisationCode': '182670',
|
||||
'Ds_Card_Brand': '1',
|
||||
'Ds_ProcessedPayMethod': '80',
|
||||
'Ds_ECI': '05',
|
||||
'Ds_Response_Description': 'OPERACION AUTORIZADA'
|
||||
}
|
||||
|
||||
:param request:
|
||||
:param order:
|
||||
:return:
|
||||
"""
|
||||
data = request.POST
|
||||
|
||||
merchant_parameters = data.get("Ds_MerchantParameters")
|
||||
|
||||
if not merchant_parameters:
|
||||
raise RedsysValidationException(
|
||||
_("No se ha recibido ningún valor para Ds_MerchantParameters")
|
||||
)
|
||||
|
||||
merchant_params = base64.b64decode(merchant_parameters).decode()
|
||||
result = json.loads(merchant_params)
|
||||
|
||||
order_hex = result.get("Ds_Order")
|
||||
assert order_hex == order.hash.hex
|
||||
|
||||
status_code = result.get("Ds_Response")
|
||||
|
||||
if int(status_code) > 100:
|
||||
reason = ERROR_CODES.get(status_code, _("Error no tipificado"))
|
||||
|
||||
raise RedsysPaymentException(_(f"No se ha realizado el pago. Motivo: {reason}"))
|
||||
|
||||
amount = Decimal(result.get("Ds_Amount")) / 100
|
||||
|
||||
return amount
|
||||
|
||||
|
||||
def pay_order(order: PaymentTPV, amount_paid: Decimal):
|
||||
order.status = PaymentTPV.StatusChoices.PAID
|
||||
order.save()
|
||||
Reference in New Issue
Block a user