这是对以下问题的后续问题:PHP: Easy way to start PayPal checkout?
所以,我的问题是我正在指定返回 url。但是,在使用 PayPal 付款后,我最终会看到一个屏幕,上面写着:
您刚刚完成付款。 XXXX,您刚刚完成付款。您的这笔付款的交易 ID 是:XXXXXXXXXXXXX。我们将向 XX@XXXX.com 发送确认电子邮件。此交易将在您的对帐单上显示为 PAYPAL。转到 PayPal 帐户概览
我需要它不显示此屏幕并直接转到返回 URL。我有:
设置“返回”变量
将“rm”变量设置为:2(根据指南=“使用POST方法将买家的浏览器重定向到返回URL,并包含所有支付变量”)
事实上,这是我的整个表格:
<form method="post" action="https://www.sandbox.paypal.com/cgi-bin/webscr">
<input type="hidden" value="_xclick" name="cmd">
<input type="hidden" value="onlinestore@thegreekmerchant.com" name="business">
<!-- <input type="hidden" name="undefined_quantity" value="1" /> -->
<input type="hidden" value="Order at The Greek Merchant:<Br />Goldfish Flock BLG<br />" name="item_name">
<input type="hidden" value="NA" name="item_number">
<input type="hidden" value="22.16" name="amount">
<input type="hidden" value="5.17" name="shipping">
<input type="hidden" value="0" name="discount_amount">
<input type="hidden" value="0" name="no_shipping">
<input type="hidden" value="No comments" name="cn">
<input type="hidden" value="USD" name="currency_code">
<input type="hidden" value="http://XXX/XXX/XXX/paypal/return" name="return">
<input type="hidden" value="2" name="rm">
<input type="hidden" value="11255XXX" name="invoice">
<input type="hidden" value="US" name="lc">
<input type="hidden" value="PP-BuyNowBF" name="bn">
<input type="submit" value="Place Order!" name="finalizeOrder" id="finalizeOrder" class="submitButton">
</form>
知道如何让它自动返回吗?或者,如何将付款结果返回到我的网站,以便更新数据库?什么是 IPN?
您必须在您的 PayPal 帐户中启用自动退货,否则它将忽略 return
字段。
从文档(更新以反映 2019 年 1 月的新布局):
自动返回默认关闭。要开启自动退货: 在 https://www.paypal.com 或 https://www.sandbox.paypal.com 登录您的 PayPal 账户 出现我的账户概览页面。点击右上角的齿轮图标。将显示配置文件摘要页面。单击左栏中的我的销售偏好链接。在“在线销售”部分下,单击“网站偏好”行中的“更新”链接。网站付款首选项页面出现在网站付款的自动退货下,单击打开单选按钮以启用自动退货。在返回 URL 字段中,输入您希望付款人在完成付款后重定向到的 URL。注意:PayPal 会检查您输入的退货 URL。如果 URL 格式不正确或无法验证,PayPal 将不会激活自动退货。滚动到页面底部,然后单击保存按钮。
IPN 用于即时付款通知。它将为您提供比从自动返回中获得的更可靠/有用的信息。
IPN 的文档在这里:https://www.x.com/sites/default/files/ipnguide.pdf
IPN 的在线文档:https://developer.paypal.com/docs/classic/ipn/gs_IPN/
一般过程是您将 notify_url
参数与请求一起传递,并设置一个处理和验证 IPN 通知的页面,PayPal 将向该页面发送请求以在付款/退款/等时通知您。经过。然后,该 IPN 处理程序页面将是更新数据库以将订单标记为已付款的正确位置。
使用 PHP 进行直接付款的示例表单。
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="you@youremail.com">
<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
<input type="hidden" name="amount_' . $x . '" value="' . $price . '">
<input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">
<input type="hidden" name="custom" value="' . $product_id_array . '">
<input type="hidden" name="notify_url" value="https://www.yoursite.com/my_ipn.php">
<input type="hidden" name="return" value="https://www.yoursite.com/checkout_complete.php">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="Return to The Store">
<input type="hidden" name="cancel_return" value="https://www.yoursite.com/paypal_cancel.php">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>
请浏览字段 notify_url、return、cancel_return
支付完成后paypal请求的处理ipn(my_ipn.php)的示例代码。
有关创建 IPN 的更多信息,请参阅 this 链接。
<?php
// Check to see there are posted variables coming into the script
if ($_SERVER['REQUEST_METHOD'] != "POST")
die("No Post Variables");
// Initialize the $req variable and add CMD key value pair
$req = 'cmd=_notify-validate';
// Read the post from PayPal
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// Now Post all of that back to PayPal's server using curl, and validate everything with PayPal
// We will use CURL instead of PHP for this for a more universally operable script (fsockopen has issues on some environments)
//$url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
$url = "https://www.paypal.com/cgi-bin/webscr";
$curl_result = $curl_err = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($req)));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$curl_result = @curl_exec($ch);
$curl_err = curl_error($ch);
curl_close($ch);
$req = str_replace("&", "\n", $req); // Make it a nice list in case we want to email it to ourselves for reporting
// Check that the result verifies
if (strpos($curl_result, "VERIFIED") !== false) {
$req .= "\n\nPaypal Verified OK";
} else {
$req .= "\n\nData NOT verified from Paypal!";
mail("you@youremail.com", "IPN interaction not verified", "$req", "From: you@youremail.com");
exit();
}
/* CHECK THESE 4 THINGS BEFORE PROCESSING THE TRANSACTION, HANDLE THEM AS YOU WISH
1. Make sure that business email returned is your business email
2. Make sure that the transaction�s payment status is �completed�
3. Make sure there are no duplicate txn_id
4. Make sure the payment amount matches what you charge for items. (Defeat Price-Jacking) */
// Check Number 1 ------------------------------------------------------------------------------------------------------------
$receiver_email = $_POST['receiver_email'];
if ($receiver_email != "you@youremail.com") {
//handle the wrong business url
exit(); // exit script
}
// Check number 2 ------------------------------------------------------------------------------------------------------------
if ($_POST['payment_status'] != "Completed") {
// Handle how you think you should if a payment is not complete yet, a few scenarios can cause a transaction to be incomplete
}
// Check number 3 ------------------------------------------------------------------------------------------------------------
$this_txn = $_POST['txn_id'];
//check for duplicate txn_ids in the database
// Check number 4 ------------------------------------------------------------------------------------------------------------
$product_id_string = $_POST['custom'];
$product_id_string = rtrim($product_id_string, ","); // remove last comma
// Explode the string, make it an array, then query all the prices out, add them up, and make sure they match the payment_gross amount
// END ALL SECURITY CHECKS NOW IN THE DATABASE IT GOES ------------------------------------
////////////////////////////////////////////////////
// Homework - Examples of assigning local variables from the POST variables
$txn_id = $_POST['txn_id'];
$payer_email = $_POST['payer_email'];
$custom = $_POST['custom'];
// Place the transaction into the database
// Mail yourself the details
mail("you@youremail.com", "NORMAL IPN RESULT YAY MONEY!", $req, "From: you@youremail.com");
?>
https://i.stack.imgur.com/e6MVS.png
如需进一步阅读,请参阅以下链接;
https://www.paypal.com/cgi-bin/webscr?cmd=p/pdn/howto_checkout-outside
https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_html_Appx_websitestandard_htmlvariables
希望这对你有帮助.. :)
我发现的一种方法:
尝试将此字段插入到您生成的表单代码中:
<input type='hidden' name='rm' value='2'>
rm 表示返回方法;
2 表示(帖子)
比在用户购买并返回您的网站 url 之后,该 url 也会获取 POST 参数
ps 如果使用 php,请尝试在您的返回 url(脚本)中插入 var_dump($_POST);
,然后进行测试购买,当您返回您的站点时,您将看到您的 url 上有哪些变量。
分享这个,因为我最近遇到了问题similar to this thread
很长一段时间以来,我的脚本运行良好(基本支付表单)并将 POST 变量返回到我的 success.php 页面,并将 IPN 数据也作为 POST 变量返回。但是,最近,我注意到返回页面(success.php)不再接收任何 POST 变量。我在 Sandbox 中进行了测试,并且我很确定 PayPal 已经改变了一些东西!
notify_url 仍然接收到正确的 IPN 数据,允许我更新数据库,但我无法在返回 URL (success.php) 页面上显示成功消息。
尽管尝试了许多组合来打开和关闭 PayPal 网站支付首选项和 IPN 中的选项,但我不得不对我的脚本进行一些更改以确保我仍然可以处理消息。我通过打开 PDT 和自动返回来完成此操作,after following this excellent guide。
现在一切正常,但唯一的问题是返回 URL 包含所有丑陋的 PDT 变量!
您还可以找到this helpful
我认为 Kevin 上面描述的设置自动返回值的想法有点奇怪!
例如,假设您有多个网站使用同一个 PayPal 帐户来处理您的付款,或者假设您在一个网站中有多个部分执行不同的购买任务,并且在付款时需要不同的退货地址完成了。如果我在“使用 PHP 进行直接付款的示例表单”部分中如上所述在我的页面上放置了一个按钮,您可以看到那里有一行:
input type="hidden" name="return" value="https://www.yoursite.com/checkout_complete.php"
您在其中设置单独的返回值。为什么必须在配置文件部分进行一般设置?!?!
此外,因为您只能在配置文件部分设置一个值,这意味着(AFAIK)您不能在具有多个操作的站点上使用自动返回。
请评论??
return
值?所以我可以有不同的返回页面,它们仍然会接收 PDT 数据?
在结帐页面上,查找“cancel_return”隐藏表单元素:
将 cancel_return 表单元素的值设置为您希望返回的 URL:
My Selling Tools
步骤 4) 点击Selling Online
下的Website Preferences
return
字段。” 您传递给结帐的任何return
URL 参数都将被接受并覆盖,这不太准确在卖家的 PayPal 账户配置文件中配置的任何自动退货 URL(或缺少),但如果他们没有在此处启用自动退货,则买家需要在结帐结束后手动点击才能重定向到该 URL,而不是自动重定向。