php - 通过 cURL 提交表单并将浏览器重定向到 PayPal

标签 php curl paypal

我正在开发一个网站,客户可以在其中使用多种付款方式,包括 PayPal Payments Standard。由于我正在收集有关客户的大量数据,因此我想在将用户发送到 PayPal 的服务器之前在我的服务器上处理表单。一种选择是将数据连接成单个字符串,将字符串分配给 custom 字段,然后在 IPN 响应中对其进行处理,但我发现这是一个非常不雅的解决方案。相反,在收集用户数据后,我尝试使用 cURL 提交标准的 HTML PayPal 表单。如何将用户重定向到 PayPal 以完成结帐流程?

  // Process PayPal payment
  if ($method == 'PayPal') {

    // Prepare POST data
    $query = array();
    $query['notify_url'] = 'http://example.com/ipn';
    $query['cmd'] = '_cart';
    $query['upload'] = '1';
    $query['business'] = 'email@example.com';
    $query['address_override'] = '1';
    $query['first_name'] = $first_name;
    $query['last_name'] = $last_name;
    $query['email'] = $email;
    $query['address1'] = $ship_to_address;
    $query['city'] = $ship_to_city;
    $query['state'] = $ship_to_state;
    $query['zip'] = $ship_to_zip;
    $query['item_name_'.$i] = $item['description'];
    $query['quantity_'.$i] = $item['quantity'];
    $query['amount_'.$i] = $item['info']['price'];

    // Prepare query string
    $query_string = '';
    foreach ($query as $key=>$value) {
      $query_string .= $key.'='.urlencode($value).'&';
    }
    $query_string = rtrim($query_string, '&');

    // Open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, 'https://www.paypal.com/cgi-bin/webscr');
    curl_setopt($ch,CURLOPT_POST, count($query));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $query_string);

    // Execute post
    $result = curl_exec($ch);

    // Close connection
    curl_close($ch);
  }

最佳答案

WARNING: this answer has a security deficit. Passing sensitive data (such as item and price) through the client allows the client to modify the transaction. ie. change the item, or change the price. See the PayPal documentation on how to implement IPN.

您应该使用 php header 函数重定向用户,并将变量作为 GET 而不是 POST 发送。

// Process PayPal payment
if ($method == 'PayPal') {

    // Prepare GET data
    $query = array();
    $query['notify_url'] = 'http://jackeyes.com/ipn';
    $query['cmd'] = '_cart';
    $query['upload'] = '1';
    $query['business'] = 'social@jackeyes.com';
    $query['address_override'] = '1';
    $query['first_name'] = $first_name;
    $query['last_name'] = $last_name;
    $query['email'] = $email;
    $query['address1'] = $ship_to_address;
    $query['city'] = $ship_to_city;
    $query['state'] = $ship_to_state;
    $query['zip'] = $ship_to_zip;
    $query['item_name_'.$i] = $item['description'];
    $query['quantity_'.$i] = $item['quantity'];
    $query['amount_'.$i] = $item['info']['price'];

    // Prepare query string
    $query_string = http_build_query($query);

    header('Location: https://www.paypal.com/cgi-bin/webscr?' . $query_string);
}

关于php - 通过 cURL 提交表单并将浏览器重定向到 PayPal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14843212/

相关文章:

javascript - Ajax 没有回答我真实的情况

curl - Databricks:将 dbfs:/FileStore 文件下载到我的本地机器?

python - 使用 Python 和 Alfresco API 上传文件

paypal - 收到 Paypal 付款后如何执行代码?

php - 通过 php 处理 mysql 的 JSON 输出

php - 将硬编码值从路由传递到 Controller (Laravel)的正确方法?

javascript - Laravel + AngularJs,使用 ng-scr 的基本路径而不是当前路径

java - 如何将评论 curl 到 Jersey 客户端

http - 如何在 JSP 页面中使用 HTTP POST 将 FORM 发送到 Paypal 并读取响应?

php - 在 POST、PayPal 表单之前捕获表单数据