【问题标题】:Send data to PayPal; submit with PHP向 PayPal 发送数据;用 PHP 提交
【发布时间】:2017-03-27 03:19:35
【问题描述】:

这是我的代码:

<form action="" method="post">
<input class="payment_input" type="text" name="name" placeholder="Name">
<input class="payment_input" type="text" name="address" placeholder="Address">
<input class="payment_input" type="text" name="email" placeholder="E-mail"><br>
<input class="payment_input" type="submit" name="submit_paypal" value="Go to PayPal">
</form>

<?php
$order_token = md5(uniqid($_POST['name'], true)); 

if (isset($_POST["submit_paypal"])) {

$statement = $pdo->prepare("INSERT INTO customers (name, address, email, order_token) VALUES (:name, :address, :email, :order_token)");
$statement->execute(array(':name' => $_POST['name'], ':address' => $_POST['address'], ':email' => $_POST['email'], ':order_token' => $order_token));

echo "
<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">
<input type=\"hidden\" name=\"cmd\" value=\"_xclick\">
<input type=\"hidden\" name=\"business\" value=\"test@icloud.com\">

<input type=\"hidden\" name=\"item_name\" value=\"eBook\">
<input type=\"hidden\" name=\"amount\" value=\"10\">
<input type=\"hidden\" name=\"return\" value=\"http://www.example.com/book.php?payment=success&orderToken=".$order_token."\">
<input type=\"hidden\" name=\"cancel_return\" value=\"http://example.com/book.php?payment=canceled\">
<input type=\"submit\">
</form>
";
}
?>

如下: 用户获取一个表单来插入他的数据(姓名、地址、电子邮件)。当用户提交表单时,应该会发生两件事:用户数据使用随机 order_token 存储在我的数据库中。付款完成时需要此 order_token 来识别用户数据。 (当order_token e.g.为“5dcb567fb34805d45d55218995df12f5”时,PayPal返回链接(如果支付成功)为“http://www.example.com/book.php?payment=success&orderToken=5dcb567fb34805d45d55218995df12f5”)。

但是我有两个问题:

  1. 如何在用户在我的网站上提交订单后立即提交 PayPal 表单,而无需用户必须单击另一个提交按钮?

  2. 如何防止该用户可以操纵 PayPal 表单?目前,用户可以简单地更改金额,例如10比1。

我希望有人有精力帮助我,因为我从晚上开始就在努力。

【问题讨论】:

  • 您应该从用户那里获取信息,然后在屏幕后面将 PP 请求放在一起并发送。表格中不应包含任何信息。表单是可以操作的,每个人都知道。
  • @RST 谢谢你的回答。你是什​​么意思?你介意举个小例子吗?

标签: php forms paypal token paypal-ipn


【解决方案1】:

借助一些 JavaScript,可以自动提交 PayPal 表单。

代码:

<script type="text/javascript">
    document.myform.submit();
</script>

要使用它,您需要在您的 PayPal 表单中添加 name="myform"

此外,您没有显示任何 PayPal IPN 系统,我建议您调查一下以确保您正确设置了支付网关。

除了防止用户更改 PayPal 表单代码外,使用 IPN 还可阻止此操作。收到付款后,PayPal 将联系您的 PHP 脚本 (IPN),您将能够验证商品名称、支付金额等。

【讨论】:

    【解决方案2】:

    最好反转您的 PHP 和 HTML 代码——因为您不想在提交时再次显示表单。

    (1) 一种方法是使用查询字符串将数据发送到 Paypal。

    <?php
    $order_token = md5(uniqid($_POST['name'], true)); 
    
    if (isset($_POST["submit_paypal"])) {
        $statement = $pdo->prepare("INSERT INTO customers (name, address, email, order_token) VALUES (?, ?, ?, ?)");
        $statement->execute(array($_POST['name'], $_POST['address'], $_POST['email'], $order_token));
    
        if ($statement->rowCount()) {
            $query = array();
            $query['cmd'] = '_xclick';
            $query['business'] = 'test@icloud.com';
            $query['item_name'] = 'eBook';
            $query['amount'] = 10;
            $query['return'] = 'http://www.example.com/book.php?payment=success&orderToken=' . $order_token;
            $query['cancel_return'] = 'http://example.com/book.php?payment=canceled';
    
            header('Location: https://www.paypal.com/cgi-bin/webscr?' . http_build_query($query));
            exit(0);
        }
    }
    ?>
    
    <form action="" method="post">
        <input class="payment_input" type="text" name="name" placeholder="Name">
        <input class="payment_input" type="text" name="address" placeholder="Address">
        <input class="payment_input" type="text" name="email" placeholder="E-mail"><br>
        <input class="payment_input" type="submit" name="submit_paypal" value="Go to PayPal">
    </form>
    

    (2) 使用这种方法,我认为没有办法阻止某人更改信息。您可以做的最好的事情是在提交之前添加验证。您还可以使用Paypal IPN 在付款时通知您。您将创建一个脚本,该脚本将在购买时接收有关购买的信息。在那里您可以添加进一步的验证。

    【讨论】:

      猜你喜欢
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 2018-12-31
      • 2015-10-31
      相关资源
      最近更新 更多