[django]在我的 django 项目中集成 phonepe 时,在 /pay/ 处获得服务不可用和内部服务器错误和 JSONDecodeError

· 收录于 2024-01-06 14:12:07 · source URL

问题详情

这是我的代码,请检查一次 我在此代码的 pay 函数和这一行收到错误 responseData = response.json()

def calculate_sha256_string(input_string):
    # Create a new SHA-256 hash object
    sha256 = SHA256.new()
    # Update hash with the encoded string
    sha256.update(input_string.encode('utf-8'))
    # Return the hexadecimal representation of the hash
    return sha256.hexdigest()

def base64_encode(input_dict):
    # Convert the dictionary to a JSON string
    json_data = jsons.dumps(input_dict)
    # Encode the JSON string to bytes
    data_bytes = json_data.encode('utf-8')
    # Perform Base64 encoding and return the result as a string
    return base64.b64encode(data_bytes).decode('utf-8')

def pay(request):
    MAINPAYLOAD = {
        "merchantId": "PGTESTPAYUAT",
        "merchantTransactionId": shortuuid.uuid(),
        "merchantUserId": "MUID123",
        "amount": 10000,
        "redirectUrl": "http://127.0.0.1:8000/return-to-me",
        "redirectMode": "POST",
        "callbackUrl": "http://127.0.0.1:8000/return-to-me",
        "mobileNumber": "9999999999",
        "paymentInstrument": {
            "type": "PAY_PAGE"
        }
    }
    # SETTING
    INDEX = "1"
    ENDPOINT = "/pg/v1/pay"
    SALTKEY = "099eb0cd-02cf-4e2a-8aca-3e6c6aff0399"
    base64String = base64_encode(MAINPAYLOAD)
    mainString = base64String + ENDPOINT + SALTKEY
    sha256Val = calculate_sha256_string(mainString)
    checkSum = sha256Val + '###' + INDEX
    print("Checksum:", checkSum)
    # Payload Send
    headers = {
        'accept': 'application/json',
        'Content-Type': 'application/json',
        'X-VERIFY': checkSum
    }
    payload = {
        'request': base64String
    }
    print("Sending request to PhonePe API:")
    print("URL:", 'https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay')
    print("Headers:", headers)
    print("Payload:", payload)
    response = requests.post('https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay', headers=headers, json=payload)
    print("Raw Response Content:")
    print(response.text)
    responseData = response.json() #getting error here
    print("Response from PhonePe API:")
    print(responseData)

    # Print the raw response content

    redirect_url = responseData['data']['instrumentResponse']['redirectInfo']['url']
    print("Redirect URL:", redirect_url)

    return redirect(redirect_url)

def payment_return(request):

    INDEX = "1"
    SALTKEY = "099eb0cd-02cf-4e2a-8aca-3e6c6aff0399"
    form_data = request.form
    form_data_dict = dict(form_data)
    # respond_json_data = jsonify(form_data_dict)
    # 1.In the live please match the amount you get byamount you send also so that hacker can't pass static value.
    # 2.Don't take Marchent ID directly validate it with yoir Marchent ID
    if request.form.get('transactionId'):
        request_url = 'https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/status/PGTESTPAYUAT/' + request.form.get('transactionId')
        sha256_Pay_load_String = '/pg/v1/status/PGTESTPAYUAT/' + request.form.get('transactionId') + SALTKEY
        sha256_val = calculate_sha256_string(sha256_Pay_load_String)
        checksum = sha256_val + '###' + INDEX
        # Payload Send
        headers = {
            'Content-Type': 'application/json',
            'X-VERIFY': checksum,
            'X-MERCHANT-ID': request.form.get('transactionId'),
            'accept': 'application/json',
        }
        response = requests.get(request_url, headers=headers)
        #print(response.text);
    return render('main/try.html', page_respond_data=form_data_dict, page_respond_data_varify=response.text)

这是我的代码

尝试了我只想打开我面前的付款屏幕的一切我不知道错误是来自我这边还是来自 phonepe 这边,它显示错误在这一行 responseData = response.json()

最佳回答

暂无回答