【问题标题】:Paypal form - How to save fields as variables?Paypal 表单 - 如何将字段保存为变量?
【发布时间】:2018-11-28 18:49:01
【问题描述】:

我有以下 Paypal 付款方式:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="******">
<input type="hidden" name="item_name" value="******">
<input type="hidden" name="item_number" value="1">
<input type="hidden" name="amount" value="25">
<input type="hidden" name="no_shipping" value="0">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input id="afield" type="text" placeholder="Name"</input>
<input id="afield" type="text" placeholder="Email Address"</input>
<!-- Saved buttons display an appropriate button image. -->
<input type="image" name="submit"
src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif"
alt="PayPal - The safer, easier way to pay online">
<img alt="" width="1" height="1"
src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >

我的问题是,我怎样才能获得这两个输入(id:afield),以便我可以保存它们以供用户在付款后返回我的网站时使用它们。我正在使用 PHP。

谢谢

【问题讨论】:

  • 饼干? PHP 会话?数据库?根据您的需要选择您想要的
  • 不,我的意思是,我如何首先将它们作为变量获取?表单操作正在使用 Paypal 的脚本:
    paypal.com/cgi-bin/webscr" method="post">
  • 您不应该有 2 个具有相同 ID 的字段。让它们独一无二。另外,给他们一个name 属性。当响应从 PayPal 返回时,它将以 GET 或 POST 请求的形式返回。你必须弄清楚这一点。一旦知道它是 GET 还是 POST 请求,您就可以分别通过$_GET['name_of_field'] 或$_POST['name_of_field'] 访问数据。只需将其分配给一个变量。 $myVariable = $_GET['name_of_field'];
  • 是的,您将在 IPN 中将它们作为自定义字段接收
  • 不确定是否清楚,但此时您不应尝试保存它们。您向我们展示的表格是向 PayPal 发出的outgoing 请求。当来自 PayPal (IPN) 的响应返回时,您将可以访问它们。您可以检查响应并获取所需的任何数据。

标签: php paypal


【解决方案1】:

这是我过去的处理方式。

第一步是修复输入字段:

<input id="afield" type="text" placeholder="Name" name="afield">

我已更正了您的 HTML 语法,并将字段命名为 afield

然后您需要设置另一个名为 notify_url 的隐藏字段 - 这是 PayPal 将在付款后将交易详细信息发回的 URL。

例如

<input type="hidden" name="notify_url" value="https://www.yourdomain.com/paypal.php">

在paypal.php 中,您可以访问$_POST['afield']

如果有帮助,这是您在 paypal.php 中需要的东西 - 这可以验证从 PayPal 发回的交易详细信息是真实的并且付款成功:

$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
  $keyval = explode ('=', $keyval);
  if (count($keyval) == 2)
    $myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$req = 'cmd=_notify-validate';
if (function_exists('get_magic_quotes_gpc')) {
  $get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
  if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
    $value = urlencode(stripslashes($value));
  } else {
    $value = urlencode($value);
  }
  $req .= "&$key=$value";
}

// Step 2: POST IPN data back to PayPal to validate
$ch = curl_init('https://ipnpb.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
if ( !($res = curl_exec($ch)) ) {
  curl_close($ch);
  exit;
}
curl_close($ch);

if (strcmp ($res, "VERIFIED") == 0 and $_POST['payment_status'] == 'Completed') {   

  // payment has been verified

}

【讨论】:

    【解决方案2】:

    您可以添加名称为 custom 的字段

    唯一的问题是 Paypal 无法在答案中返回此字段,即使在 PDT 中也是如此

    只能在IPN文本中看到该字段

    https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNandPDTVariables/#transaction-and-notification-related-variables

    https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNPDTAnAlternativetoIPN/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-29
      • 2014-05-28
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2017-04-04
      相关资源
      最近更新 更多