Django:从POST请求中读取参数

时间:2020-02-22 04:45:34

标签: php django payment-gateway

我正在使用付款网关(无现金付款网关)向我正在开发的网站添加定期付款系统。付款完成后,付款网关会使用一些带有POST请求为 given here的参数重定向到我的wesbite网址。我无法读取这些参数。这是我的第一个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'),但返回None。如何读取这些参数以及如何检查支付网关是否正在发送任何参数?

更新

我联系了无现金支付网关,他们回答这是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>

在安装debug-toolbar之后,它显示未传递任何参数 enter image description here

enter image description here

在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'])

1 个答案:

答案 0 :(得分:0)

正如注释中所读,重定向通常是使用浏览器中的GET方法进行的。

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

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