gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:10:36 26 4
gpt4 key购买 nike

我正在开发一个网站,客户可以在其中使用多种付款方式,包括 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/

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