PHP将更新的POST请求发送到外部服务器并访问页面

时间:2013-03-15 15:51:00

标签: php post payment-processing

我正在使用Cashflows在线支付系统,并尝试根据网页上的表单向其服务器提交动态生成的价格。但是,在我需要发送到Cashflows终端的POST请求中,我需要包含一个带有哈希键的输入字段。我的问题是哈希是基于一个密钥,并且这个密钥的生成是其中一个参数是价格(见下文),如integration guide所示。

如何使用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>

3 个答案:

答案 0 :(得分:1)

我要做的是将表单发布到服务器上的PHP页面,生成哈希值,然后将其发布到将接收它的页面。您可以使用fsockopen()或cURL完成所有这些操作,并且有很多关于如何使用它们的示例。

绝对不要将该变量存储为表单上的隐藏字段,因为它可以被读取,拦截和操纵,并且会在以后为您的电子商务系统引起麻烦。

答案 1 :(得分:0)

您可以POST到您的页面,该页面将生成密钥并向用户发送另一个带有隐藏字段的表单,这些字段会在页面加载后自动提交到现金流。以下是该表单的示例:

<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>

答案 2 :(得分:0)

最后,答案相对简单 - 我找不到一个没有JavaScript的解决方案,所以我没有计算客户端的所有内容,而是使用AJAX来提取一个带有正确生成哈希的表单。