Payfort - (00073) Неверный формат запроса

Надеюсь, у всех все хорошо. Я пытаюсь настроить payfort в Django, но постоянно сталкиваюсь с этой странной ошибкой. Я считаю, что выполняю все необходимые шаги, но всякий раз, когда я пытаюсь отправить запрос после токенизации, я получаю следующую ошибку.

{'response_code': '00073', 'response_message': 'Invalid request format', 'status': '00'}

Код токинизации:

def initiate_tokenization(request):
    current_timestamp = str(int(time.time()))
    requestParams = {
        'service_command': 'TOKENIZATION',
        'merchant_identifier': settings.MERCHANT_IDENTIFIER,
        'merchant_reference': current_timestamp,
        'language': settings.LANGUAGE,
        'access_code': settings.ACCESS_CODE,
        'return_url': 'http://localhost:8000/callback',
    }

    signature = calculate(requestParams, settings.SHA_REQUEST_PHRASE)
    requestParams['signature'] = signature

    redirectUrl = 'https://sbcheckout.payfort.com/FortAPI/paymentPage'
    response = "<html>\n<head>\n<title>Standard Merchant Page Sample</title>\n</head>\n<body>\n"
    response += "<h1 style='text-align:center'>Standard Merchant Page iFrame Sample</h1>\n"
    response += "<center><iframe style='width: 100vw; height: 500px; border:6px dotted green' name='myframe' src=''></iframe></center>\n"
    response += "<form action='" + redirectUrl + "' method='post' id='' target='myframe'>\n"
    for key, value in requestParams.items():
        response += "\t<input type='hidden' name='" + html.escape(key) + "' value='" + html.escape(value) + "'>\n"
    response += "\t<input value='Show Payment Form' type='submit' id='form1'>\n"
    response += "</form>\n</body>\n</html>"

    resp = HttpResponse(response)
    resp['X-Frame-Options'] = 'ALLOWALL'
    return resp

Код покупки:

import requests
import time
import hashlib


def calculate(unsorted_dict, sha_phrase, sha_method=hashlib.sha256):
    sorted_keys = sorted(unsorted_dict, key=lambda x: x.lower())
    sorted_dict = {k: unsorted_dict[k] for k in sorted_keys}
    result = "".join(f"{k}={v}" for k, v in sorted_dict.items())
    result_string = f"{sha_phrase}{result}{sha_phrase}"
    signature = sha_method(result_string.encode()).hexdigest()
    return signature

current_timestamp = str(int(time.time()))
url = 'https://sbpaymentservices.payfort.com/FortAPI/paymentApi';

arrData = {
    'command': 'PURCHASE',
    'merchant_identifier': 'xxxxxx',
    'merchant_reference': '1711551951',
    'currency': 'SAR',
    'language': 'en',
    'access_code': 'xxxxx',
    'customer_email': 'test@payfort.com',
    'order_description': 'iPhone 6-S',
    'amount': 10000,
    'return_url': 'http://localhost:8000/callback',
    'token_name' : 'xxxxxxxxx'
}

sorted_keys = sorted(arrData, key=lambda x: x.lower())
s_arrData = {k: arrData[k] for k in sorted_keys}

signature = calculate(arrData, 'Test123$')
s_arrData['signature'] = signature
print(s_arrData)

headers = {
    'Content-Type': 'application/json'
}
response = requests.post(url, data=s_arrData, headers=headers)
print(response.json())

Я пытаюсь решить проблему недействительного формата запроса.

Вернуться на верх