【问题标题】:Django: read parameters from POST requestDjango:从 POST 请求中读取参数
【发布时间】:2020-02-22 04:45:34
【问题描述】:

我正在向我正在开发的网站添加使用支付网关(无现金支付网关)的定期支付系统。处理完付款后,支付网关将重定向到我的 wesbite url,其中包含一些参数,POST 请求为 given here。我无法读取这些参数。这是我的第一个 Web 开发项目,在这里我有点困惑。在文档中提到它是 POST 请求,但在后端 request.method 提供了 GET 方法。我正在使用以下代码

@csrf_exempt
@login_required
def cashfree_response(request):
    if request.method == "POST":
        print('inside post method')
    if request.method == "GET":
        print('inside get method')
        sub_ref = request.GET['cf_subReferenceId']

如何读取cf_subReferenceId参数值和支付网关传递的其他参数值?我也尝试使用sub_ref = request.GET.get('cf_subReferenceId'),但它返回无。如何读取这些参数以及如何检查支付网关是否正在发送任何参数?

更新:

我联系了无现金支付网关,他们回复说这是 POST 请求。但是当我 print(request.method) 时,它显示为 GET。他们给我发了几个 PHP 文件,但我不知道 PHP。下面是他们发给我的 PHP 文件。谁能帮我确定返回方法是什么以及如何读取返回参数?

<?php  
     $secretkey = "60e9cfebec82c9693d9423011fc2898766119d1c";
     $cf_subReferenceId = $_POST["cf_subReferenceId"];
     $cf_subscriptionId = $_POST["cf_subscriptionId"];
     $cf_authAmount = $_POST["cf_authAmount"];
     $cf_orderId = $_POST["cf_orderId"];
     $cf_referenceId = $_POST["cf_referenceId"];
     $cf_status = $_POST["cf_status"];
     $cf_message = $_POST["cf_message"];
     $signature = $_POST["signature"];
     $data = "";
     $postData = $_POST;
     ksort($postData);
     foreach ($postData as $key => $value) {
     if (substr($key, 0, 3) == "cf_") {
     $data .= $key . $value;
}
 }
 //echo($data);
         //die();
         $hash_hmac = hash_hmac('sha256', $data, $secretkey, true) ;
         $computedSignature = base64_encode($hash_hmac);
         if ($signature == $computedSignature) {
         print_r("yes");
         }else{
         print_r("no");
         }
?>

<!DOCTYPE html>
<html>
<head>
    <title>Cashfree - PG Response Details</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
    <h1 align="center">PG Response</h1> 

    <?php  
         $secretkey = "60e9cfebec82c9693d9423011fc2898766119d1c";
         $cf_subReferenceId = $_POST["cf_subReferenceId"];
         $cf_subscriptionId = $_POST["cf_subscriptionId"];
         $cf_authAmount = $_POST["cf_authAmount"];
         $cf_orderId = $_POST["cf_orderId"];
         $cf_referenceId = $_POST["cf_referenceId"];
         $cf_status = $_POST["cf_status"];
         $cf_message = $_POST["cf_message"];
         $signature = $_POST["signature"];
         $data = "";
         $postData = $_POST;
         ksort($postData);
         foreach ($postData as $key => $value) {
         if (substr($key, 0, 3) == "cf_") {
         $data .= $key . $value;
}
 }
 //echo($data);
         //die();
         $hash_hmac = hash_hmac('sha256', $data, $secretkey, true) ;
         $computedSignature = base64_encode($hash_hmac);
         if ($signature == $computedSignature) {
     ?>
    <div class="container"> 
    <div class="panel panel-success">
      <div class="panel-heading">Signature Verification Successful</div>
      <div class="panel-body">
        <!-- <div class="container"> -->
            <table class="table table-hover">
                <tbody>
                  <tr>
                    <td>cf_subReferenceId</td>
                    <td><?php echo $cf_subReferenceId; ?></td>
                  </tr>
                  <tr>
                    <td>cf_subscriptionId</td>
                    <td><?php echo $cf_subscriptionId; ?></td>
                  </tr>
                  <tr>
                    <td>cf_authAmount</td>
                    <td><?php echo $cf_authAmount; ?></td>
                  </tr>
                  <tr>
                    <td>cf_orderId</td>
                    <td><?php echo $cf_orderId; ?></td>
                  </tr>
                  <tr>
                    <td>cf_referenceId </td>
                    <td><?php echo $cf_referenceId; ?></td>
                  </tr>
                  <tr>
                    <td>cf_status</td>
                    <td><?php echo $cf_status; ?></td>
                  </tr>
                  <tr>
                    <td>cf_message</td>
                    <td><?php echo $cf_message; ?></td>
                  </tr>
                </tbody>
            </table>
        <!-- </div> -->

       </div>
    </div>
    </div>
     <?php   
        } else {

     ?>
    <div class="container"> 
    <div class="panel panel-danger">
      <div class="panel-heading">Signature Verification failed</div>
      <div class="panel-body">
        <!-- <div class="container"> -->
            <table class="table table-hover">
                <tbody>
                  <tr>
                    <td>cf_subReferenceId</td>
                    <td><?php echo $cf_subReferenceId; ?></td>
                  </tr>
                  <tr>
                    <td>cf_subscriptionId</td>
                    <td><?php echo $cf_subscriptionId; ?></td>
                  </tr>
                  <tr>
                    <td>cf_authAmount</td>
                    <td><?php echo $cf_authAmount; ?></td>
                  </tr>
                  <tr>
                    <td>cf_orderId</td>
                    <td><?php echo $cf_orderId; ?></td>
                  </tr>
                  <tr>
                    <td>cf_referenceId </td>
                    <td><?php echo $cf_referenceId; ?></td>
                  </tr>
                  <tr>
                    <td>cf_status</td>
                    <td><?php echo $cf_status; ?></td>
                  </tr>
                  <tr>
                    <td>cf_message</td>
                    <td><?php echo $cf_message; ?></td>
                  </tr>
                </tbody>
            </table>
        <!-- </div> -->
      </div>    
    </div>  
    </div>

    <?php   
        }
     ?>

</body>
</html>

安装调试工具栏后显示没有传递任何参数

在 views.py 中创建计划和订阅并将用户发送到 authlink。它正在创建计划和订阅,我被重定向到 authlink,我可以在其中输入卡详细信息并进行授权。由于是测试模式,我选择了成功,然后返回到我给的 url。那是我没有任何参数的地方。

@login_required
def payment_process(request):
    if request.method == "POST":
        Sub_value =  int(request.POST.get('sub_value'))
        creator =  request.POST.get('creator')
        url = "https://test.cashfree.com/api/v2/subscription-plans"
        appID = settings.CASHFREEID
        secretKey = settings.CASHFREESECRETKEY
        headers = {
            'cache-control': 'no-cache',
            'content-type': 'application/json',
            'X-Client-Id': appID,
            'X-Client-Secret': secretKey,
        }
        data = {"planId":"plan_1", "planName":"Booster","type":"PERIODIC","amount":Sub_value,"intervalType":"week","intervals":2,"description":"This is the standard planfor our services"}
        data=json.dumps(data)
        response = requests.post('https://test.cashfree.com/api/v2/subscription-plans', headers=headers, data=data)
        response_text = json.loads(response.text)
        if not response_text['status'] == 'OK':
            # redirect to a page to tell the user to try again later!!
            pass

        data = {"subscriptionId":"sub1", "planId":plan_id, "amount":Sub_value, "customerEmail":request.user.email,"customerPhone":"7427259375","expiresOn":"2030-12-31 23:59:59","returnUrl":"http://127.0.0.1:8000/cashfreeresponse/"}
        data=json.dumps(data)
        response = requests.post('https://test.cashfree.com/api/v2/subscriptions', headers=headers, data=data)
        response_text = json.loads(response.text)

        if not response_text['status'] == 'OK':
            # redirect to a page to tell the user to try again later!!
            pass

        return redirect(response_text['authLink'])

【问题讨论】:

  • 不可能让浏览器发出 POST 请求以响应重定向 - 重定向只会导致浏览器对目标 URL 发出 GET 请求 您所使用的支付网关的文档using 写得不好。您正在寻找的参数将作为 GET 参数提供。
  • @solarissmoke 如何读取 GET 参数?我正在使用subID = request.GET.get('cf_subReferenceId',None),但它返回无。

标签: php django payment-gateway


【解决方案1】:

如 cmets 中所述,重定向通常使用浏览器中的 GET 方法进行。

在Django中获取GET参数可以check this solution

另一方面,如video of Cashfree 所示,您可以从浏览器的“网络”选项卡进行调试,检查保留日志选项,发出请求并检查标头值。

【讨论】:

  • 我联系了无现金支付网关,他们说这是 POST 请求。但是request.method 显示为 GET 方法。我也无法从“网络”选项卡中看到所有详细信息。我用一些额外的信息更新了我的问题。
  • @sreekanthkura7 安装 django-debug-toolbar 并使用它进行调试。该应用程序将帮助您。请告诉我以后情况如何。 PHP 行也在做同样的事情,尝试从 POST 方法 ($_POST["cf_subReferenceId"]) 获取数据,然后在 HTML 中打印(回显)
  • 我安装了 django-debug-toolbar。它显示没有传递任何参数。请查看更新后的问题。
  • @sreekanthkura7 那么在我看来,您在使用 CashFree 实施时做错了什么。也许在没有任何重要数据的情况下共享问题中的代码?
  • 我用 CashFree 实施更新了这个问题。只想提一下,为了让事情变得更简单,我做了一些改动。
猜你喜欢
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 2019-06-14
  • 2012-07-05
  • 1970-01-01
  • 2016-10-17
  • 2021-07-08
相关资源
最近更新 更多