gpt4 book ai didi

php - 通过重定向客户端通过 "GET"而不是 "POST"发送 PayPal 表单

转载 作者:太空宇宙 更新时间:2023-11-03 15:53:00 25 4
gpt4 key购买 nike

POST 是通过 PayPal 发送 PayPal 按钮的推荐方法 documentations .他们明确表示:

<FORM action="https://www.paypal.com/cgi-bin/webscr" method="post">

Important: Do not change these values. These attributes are required for all payment buttons and the Cart Upload command

然而在this tutorial ,作者在服务器端构建按钮变量,然后将客户端重定向到 PayPal,这将使请求通过“GET”而不是“POST”发送到 PayPal。像这样的东西

这是发送给客户的表单(它没有 PayPal 变量,只有按钮和一些与我的业务相关的可选变量)

<form class="paypal" action="/payments.php" method="post">
<select name="product">
<option value="A">A</option>
<option value="B">B</option>
</select>
<input name="discountCode" type="text">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" name="submit" alt="PayPal - The safer, easier way to pay online!">
</form>

然后他在服务器上构建了 PayPal 变量并将客户端重定向到 PayPal 以继续

payments.php

$paypal_email = 'user@example.com';
$return_url = 'http://example.com/payment-successful.html';
$cancel_url = 'http://example.com/payment-cancelled.html';
$notify_url = 'http://example.com/payments.php';//same file
$item_name = 'Test Item'; //build the item and amount depend on what is selected in the form
$item_amount = 5.00;

$querystring = '';
$querystring .= "?business=".urlencode($paypal_email)."&";
$querystring .= "cmd=_xclick&";
$querystring .= "item_name=".urlencode($item_name)."&";
$querystring .= "amount=".urlencode($item_amount)."&";
$querystring .= "return=".urlencode(stripslashes($return_url))."&";
$querystring .= "cancel_return=".urlencode(stripslashes($cancel_url))."&";
$querystring .= "notify_url=".urlencode($notify_url);

// Redirect to paypal (will cause GET request to PayPal)
header('location:https://www.sandbox.paypal.com/cgi-bin/webscr'.$querystring);
exit();

这种方法对我有用,但是 POST 方法是 PayPal 推荐的方法。我发现 PHP 更舒服,让我有机会在客户使用 PayPal 之前做一些工作,比如根据用户选择更新我的后端系统或从 HTML 中隐藏一些敏感变量(如 notify_url)<form> (虽然客户仍然可以看到他被定向到哪里)。

在服务器端构建 PayPal 变量然后使用 GET 请求将客户端重定向到 PayPal 是否可靠且受支持?

最佳答案

并不是说这是最好的选择,但要创建一个帖子表单,您可以按如下方式编辑您的 payments.php:

<?php

$paypal_email = 'user@example.com';
$return_url = 'http://example.com/payment-successful.html';
$cancel_url = 'http://example.com/payment-cancelled.html';
$notify_url = 'http://example.com/payments.php';//same file
$item_name = 'Test Item'; //build the item and amount depend on what is selected in the form
$item_amount = 5.00;

$querystring = [
'business' => urlencode($paypal_email),
'cmd' => '_xclick',
'item_name' => urlencode($item_name),
'amount' => urlencode($item_amount),
'return' => urlencode(stripslashes($return_url)),
'cancel_return' => urlencode(stripslashes($cancel_url)),
'notify_url' => urlencode($notify_url),
];

?>

<html>
<head>
</head>
<body>
<form id="myForm" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST">
<?php
foreach ($querystring as $a => $b) {
echo "<input type='hidden' name='".htmlentities($a)."' value='".htmlentities($b)."' />";
}
?>
</form>
<script type="text/javascript">
document.getElementById('myForm').submit();
</script>
</body>
</html>
<?php
exit();

关于php - 通过重定向客户端通过 "GET"而不是 "POST"发送 PayPal 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46975240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com