如何使用贝宝付款,然后从我的表单中保存参数?
Posted
技术标签:
【中文标题】如何使用贝宝付款,然后从我的表单中保存参数?【英文标题】:How to pay with paypal and then save parameters from my form? 【发布时间】:2015-11-04 13:22:39 【问题描述】:我有一个工作网站(html,引导程序)
它有一个表单,我可以在其中收集有关用户的各种数据(例如电子邮件、日期)。单击“完成”按钮后,表单将所有数据发送到服务器(我在 python 上开发的)。服务器通过保存从表单收集的所有数据来注册用户。
我想添加一个 PayPal 支付系统,以便用户支付注册费用,然后启动我的服务器端脚本,并保存表单中的所有数据。即和现在一样,我只是希望用户付费然后保存数据。
正如这里的成员所建议的,我必须使用“快速结帐付款方式”(我说的对吗?)。但是我在网上看到的解释不清楚,我不知道该怎么做。
怎么做?
【问题讨论】:
我的项目中有一个快速结帐设置。你介意我和你分享吗?但是你必须给我一些时间,因为它是一个很大的脚本和解释...... @Saswat,我很高兴!谢谢。从您的代码中可以清楚地知道如何做吗? 是的,@Yura。但是,我的代码是基于 Codeigniter 框架(php)的,但是如果您有关于函数调用的概念和关于 OOP 工作原理的基本概念,那么您将不会有任何问题来完成它 我会不断编辑我的答案,所以请耐心等待我更新... 【参考方案1】:Express Checkout 的工作原理说明:
Express Checkout Method是一种paypal交易方式,基本上分为3个阶段,分别是:
-
SetExpressCheckout:要使用 Express Checkout,您需要调用 SetExpressCheckout API。在 API 调用中,您指定
产品、金额和 RETURNURL。
GetExpressCheckout:一旦买家同意您的购买,他将被重定向回您在 RETURNURL 中指定的 URL。你
现在应该显示订单确认,并致电
获取ExpressCheckoutDetails API**。打电话时
GetExpressCheckoutDetails,提供令牌。在里面
GetExpressCheckoutDetails API 响应,您将找到一个 PayerID。
DoExpressCheckout:现在您可以致电 DoExpressCheckoutPayment 并向买家收费。请记住在调用 DoExpressCheckoutPayment 时同时包含令牌和付款人 ID。
首先是取消功能。如果支付被取消,那么这个方法会被调用。
function payment_failure()
echo "payment cancelled by the user";
现在是支付成功方法:
function payment_success()
// Obtain the token from PayPal.
if(!array_key_exists('token', $_REQUEST))
exit('Token is not received.');
// Set request-specific fields.
$token = urlencode(htmlspecialchars($_REQUEST['token']));
// Add request-specific fields to the request string.
$nvpStr = "&TOKEN=$token";
// Execute the API operation; see the PPHttpPost function above.
$httpParsedResponseAr = $this->PPHttpPost('GetExpressCheckoutDetails', $nvpStr);
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
$payerID = urlencode($httpParsedResponseAr["PAYERID"]);
$paymentType = urlencode('Sale'); // or 'Sale' or 'Order'
$paymentAmount = urlencode($_SESSION['total_amount']);
$currencyID = urlencode($_SESSION['cur']); // or other currency code ('GBP', 'EUR', 'JPY', 'CAD', 'AUD')
$nvpStr = "&TOKEN=$token&PAYERID=$payerID&PAYMENTACTION=$paymentType&AMT=$paymentAmount&CURRENCYCODE=$currencyID";
$httpParsedResponseAr = $this->PPHttpPost('DoExpressCheckoutPayment', $nvpStr);
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
$transaction_secret=md5(uniqid());
unset($_SESSION['fname']);
unset($_SESSION['lname']);
unset($_SESSION['email']);
unset($_SESSION['password']);
// save the data in the database along with a secret key to uniquely identify the user later(if needed).
else
exit('DoExpressCheckoutDetails failed: ' . print_r($httpParsedResponseAr, true));
//echo "Payment failed for unknown reason";
else
//exit('GetExpressCheckoutDetails failed: ' . print_r($httpParsedResponseAr, true));
echo "Payment failed for unknown reason";
前两个是成功方法和取消方法。
现在是接受来自提交表单的数据的函数,并通过将参数传递给ExpressCheckout
方法来调用ExpressCheckout
方法...
function paypal_order()
$_SESSION['fname'] = $_POST['fname']; // fetching the data submitted from the form
$_SESSION['lname'] = $_SESSION['lname']);
$_SESSION['email'] = $_SESSION['email']);
$_SESSION['password'] = $_SESSION['password'];
if($_SESSION['cur']=='USD')
$currencyID = urlencode('USD');
else if($_SESSION['cur']=='INR')
$_SESSION['cur'] = 'USD';
$currencyID = urlencode('USD');
else if($_SESSION['cur']=='EUR')
$currencyID = urlencode('EUR');
else if($_SESSION['cur']=='GBP')
$currencyID = urlencode('GBP');
$paymentType = urlencode('Order');
$returnURL = (base_url()."paypal-payment-success"); // this call the payment_success() method using the router technique;
$cancelURL = (base_url()."paypal-payment-failure"); // this call the payment_failure() method using the router technique;
$nvpStr="&METHOD=SetExpressCheckout
&RETURNURL=$returnURL
&CANCELURL=$cancelURL";
$i=0;
$str = "
&L_PAYMENTREQUEST_0_NAME$i=User-Registration
&L_PAYMENTREQUEST_0_NUMBER$i=1
&L_PAYMENTREQUEST_0_AMT$i=20
&L_PAYMENTREQUEST_0_DESC$i=User-Registration";
$nvpStr=$nvpStr.$str;
$nvpStr=$nvpStr."&PAYMENTREQUEST_0_AMT=20&PAYMENTREQUEST_0_CURRENCYCODE=$currencyID";
$httpParsedResponseAr = $this->PPHttpPost('SetExpressCheckout', $nvpStr);
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
$token = urldecode($httpParsedResponseAr["TOKEN"]);
$payPalURL = "https://www.paypal.com/webscr&cmd=_express-checkout&token=$token";
if("sandbox" === $environment)
$payPalURL = "https://www.$environment.paypal.com/webscr&cmd=_express-checkout&token=$token";
header("Location: $payPalURL");
exit;
else
exit('SetExpressCheckout failed: ' . print_r($httpParsedResponseAr, true));
最终,以下是 httppost 方法,该方法由 SetExpressCheckout、GetExpressCheckout 和 DoExpressCheckout 等传递参数调用。
以下函数在成功的快速结账交易中被调用三次:
private function PPHttpPost($methodName_, $nvpStr_)
// Set up your API credentials, PayPal end point, and API version.
$environment = "sandbox"; //or "live" for original live transaction;
$API_UserName = "expresscheckout API username goes here";
$API_Password = "expresscheckout API password goes here";
$API_Signature = "expresscheckout API signature goes here";
//$API_UserName = urlencode('saswat_paypay_business_api1.gmail.com');
//$API_Password = urlencode('1365495686');
//$API_Signature = urlencode('AfOa1sjCuxeiTRYj4tqlG6nUGUmhAvv0pzdavzgFM3272hn8CqS5OY0A');
$API_Endpoint = "https://api-3t.paypal.com/nvp";
if("sandbox" === $environment)
$API_Endpoint = "https://api-3t.$environment.paypal.com/nvp";
$version = urlencode('65.0');
// Set the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Turn off the server and peer verification (TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
// Set the API operation, version, and API signature in the request.
$nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";
// Set the request as a POST FIELD for curl.
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
// Get response from the server.
$httpResponse = curl_exec($ch);
if(!$httpResponse)
exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
// Extract the response details.
$httpResponseAr = explode("&", $httpResponse);
$httpParsedResponseAr = array();
foreach ($httpResponseAr as $i => $value)
$tmpAr = explode("=", $value);
if(sizeof($tmpAr) > 1)
$httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr))
exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
return $httpParsedResponseAr;
您的表单提交应该使数据流向或重定向到负责调用所有其他函数的函数 paypal_order()。
【讨论】:
谢谢!我如何调用这些函数?我应该在 HTML 中何时以及做什么? @Yura,我的回答很完整.....你可以复习一下。如果您遇到任何困惑,请随时询问。以上是关于如何使用贝宝付款,然后从我的表单中保存参数?的主要内容,如果未能解决你的问题,请参考以下文章