2013-03-15 48 views
1

我正在使用Cashflows在線支付系統,並試圖根據網頁上的表單向服務器提交動態生成的價格。但是,在我需要發送到Cashflows終端的POST請求中,我需要包含一個帶有散列鍵的輸入字段。我的問題是散列基於密鑰,並且該密鑰是以其中一個參數作爲價格生成的(請參見下文),如integration guide所示。PHP將更新的POST請求發送到外部服務器並訪問頁面

如何使用PHP將下面的POST參數提交給門戶網站?

PHP:

<?php 

    $secret_key = 'foobar'; 
    $store_id = $_POST['store_id']; 
    $cart_id = $_POST['cart_id']; 
    $amount = $_POST['amount']; 
    $currency = $_POST['currency']; 
    $test = $_POST['test']; 
    $description = $_POST['description']; 
    echo $check = hash('sha256', $secret_key . ':' . $store_id . ':' . $cart_id . ':' . $amount . ':' . $currency . ':' . $test . ':' . $description); 
    $price = $_POST['price']; 
    $qty = $_POST['qty']; 
    $carriage_amount = $_POST['carriage_amount']; 
    $postage_and_packaging = $_POST['postage_and_packaging']; 
    $name = $_POST['name']; 
    $address = $_POST['address']; 
    $postcode = $_POST['postcode']; 
    $country = $_POST['country']; 
    $tel = $_POST['tel']; 
    $email = $_POST['email']; 
    $amount = $_POST['amount']; 

?> 

形成動態生成的價格,他們integration example的修改版本:

<form action="submit.php" method="POST"> 
    <input type="hidden" name="store_id" value="5939523" /> 
    <input type="hidden" name="cart_id" value="captubes" /> 
    <input type="hidden" name="currency" value="GBP" /> 
    <input type="hidden" name="test" value="1" /> 
    <input type="hidden" name="description" value="Fruush" /> 
    <input type="hidden" name="check" value="SOME KEY HERE" /> 

    <script type="text/javascript" type="text/javascript"> 

     // The next two functions round numbers to numerical formatting. They do not need to be altered when adding or removing products. 
     function roundOff2(value, precision) { 
      return places(value,1,precision); 
     } 

     function places(X, M, N) { 
      var T, S=new String(Math.round(X*Number("1e"+N))) 
      while (S.length<M+N) S='0'+S 
      var y = S.substr(0, T=(S.length-N)); 
      if(N>0) 
      { 
       y += '.' + S.substr(T, N); 
      } 

      return y; 
     } 

     // This function checks for empty quantities. It does not need to be altered when adding or removing products. 
     function CheckNull2(value) { 
      if (value == "") { 
       value = "0"; 
      } 

      return value; 
     } 

     // This function defines the postage and packaging location. It does not need to be altered when adding or removing products. 
     function typeOfCarriage(x,whereabouts) { 
      x.carriage_amount.value = whereabouts; 
     } 

     // This function addeds the postage and packaging to the total price of the products. Add new postage rates here, and also edit further down the page to add them to the table. 
     function calculate(x) { 

      basicprice = calc(x); 

      if(Number(basicprice) > 0) { 

       var postage_and_packaging = 0; 

       switch (x.carriage_amount.value) { 
        case "uk" : 
         postage_and_packaging = 1.99; 
         break; 
        case "europe" : 
         postage_and_packaging = 2.99; 
         break; 
        default : 
         postage_and_packaging = 4.99; 
         break; 
       } 

       x.amount.value = Number(basicprice) + postage_and_packaging; 

      } else { 

       x.amount.value = "0"; 

      } 

      x.amount.value = roundOff2(x.amount.value,2); 

     } 

     // The standard price, exluding postage and packaging is calculated here. It does not need to be altered when adding or removing products. 
     function calc(x) { 

      var b = Number(CheckNull2(x.price.value)); 
      var c = Number(CheckNull2(x.qty.value)); 
      var a = (b * c); 

      return a; 

     } 

    </script> 

    <p> 
     <h3>Number of caps</h3> 
     Tube of 6 caps: &pound;4.99 - Quantity: <input name="price" value="4.99" type="hidden" /><input name="qty" size="3" value="1" /> 
    </p> 

    <p> 
     <h3>Postage &amp; Packaging:</h3> 
     <input name="carriage_amount" value="uk" type="hidden"> 
     <input checked="checked" name="postage_and_packaging" onClick="typeOfCarriage(this.form,'uk');calculate(this.form)" value="" type="radio" />UK (&pound;1.99) 
     <input name="postage_and_packaging" onClick="typeOfCarriage(this.form,'europe');calculate(this.form)" value="" type="radio" />Europe(&pound;2.99) 
     <input name="postage_and_packaging" onClick="typeOfCarriage(this.form,'world');calculate(this.form)" value="" type="radio" />Rest of World (&pound;4.99) 
    </p> 

    <p> 
     <h3>Your Details (you will get a chance to change these):</h3> 
     <span style="width: 100px; float: left;">Name:</span> <input type="text" name="name" /><br /> 
     <span style="width: 100px; float: left;">Address:</span> <input type="text" name="address" /><br /> 
     <span style="width: 100px; float: left;">Postcode:</span> <input type="text" name="postcode" /><br /> 
     <span style="width: 100px; float: left;">Country:</span> <input type="text" name="country" /><br /> 
     <span style="width: 100px; float: left;">Telephone:</span> <input type="text" name="tel" /><br /> 
     <span style="width: 100px; float: left;">Email:</span> <input type="text" name="email" /> 
    </p> 

    <input name="calcButton" onClick="calculate(this.form)" value="Calculate Total" type="button"> Total: &pound; <input type="text" name="amount" value="6.98" /> 
    <input value="Checkout" onClick="calculate(this.form)" type="submit" /> 
</form> 
+0

你需要將它發送到那裏的服務器,而不是你的。 – 2013-03-15 15:56:23

+0

這正是我想要做的 - 我首先需要通過我的服務器發送它,以便我可以從私有/受保護密鑰生成散列 – jacktheripper 2013-03-15 16:00:21

+0

然後爲什麼在您的代碼中有

'必須存在提供商網關的網址。支付模塊的標準程序是您生成代碼,然後生成帶有隱藏字段的表單,然後將其發送到支付網關。 – 2013-03-15 17:21:05

回答

0

最終,答案是相對簡單 - 我無法找到一個沒有JavaScript的解決方案,所以不是計算所有客戶端,我用AJAX來拉一個正確生成散列的表單。

1

我會做的是發佈形式在一個PHP頁面你的服務器,生成哈希,然後將其發佈到將接收它的頁面。你可以用fsockopen()或者cURL來做所有的事情,並且有很多關於如何使用它們的例子。

絕對不要將該變量作爲隱藏字段存儲在表單上,​​因爲它可以被讀取,攔截和操作,並且稍後會導致您的電子商務系統頭痛。

+0

我閱讀了cURL和fsockopen(),但我的印象是這些不會將用戶重定向到他們要發佈的頁面 - 用戶仍然需要訪問Cashflows門戶以接收付款。我懷疑將反饋放入iframe中會起作用。 – jacktheripper 2013-03-15 16:01:23

+0

這就是爲什麼你需要發送散列給支付提供商。不要誤解我的意思,但對提供商網關的付款請求使用隱藏字段沒有任何問題。甚至有很多提供者阻止某些方法,如cURL。我會堅持提供文檔中給出的例子。 – 2013-03-15 17:23:53

+0

有趣的是,這是基於他們的文檔! – jacktheripper 2013-03-15 18:55:48

0

您可以發佈到您的頁面以生成密鑰並向用戶發送另一個帶有隱藏字段的表單,這些隱​​藏字段會在頁面加載後自動提交到現金流。下面是形式的一個例子:

<html> 
<head> 
<script type="text/javascript"> 
function submit_form() 
{ 
    document.myform.submit(); 
} 
</script> 
</head> 
<body onload="submit_form();"> 
    <form method="POST" name="myform" action="http://www.google.com/"> 
     <input type="hidden" name="field1" value="value1"/> 
     <input type="hidden" name="field2" value="value2"/> 
    </form> 
</body> 
</html> 
+0

如果用戶禁用JavaScript,該怎麼辦?我也想到了這一點,但肯定這是一個應該在後端完成的過程? – jacktheripper 2013-03-15 16:09:26

+0

問題是,您需要通過POST請求將用戶發送到外部URL。你不能用PHP來做到這一點。這是方式。爲了反擊無JS,你只需要在上一步(當用戶按下「購買」按鈕時)或簡單地添加「'到該表單 – Ranty 2013-03-15 18:09:15

相關問題