paypal 按钮支付
Surfing the Internet, I noticed that many programmers and web developers have problems with implementing a PayPal "Buy Now" button. Let me say that I'm not a PayPal developer, nor a guru of informatic science or a genius of programming art... But I needed to insert a Buy Now button in a my site and I found a simple way to do it.
在网上冲浪时,我注意到许多程序员和Web开发人员在实现PayPal“立即购买”按钮时遇到了问题。 让我说我既不是PayPal开发人员,也不是信息科学专家或编程艺术天才……但是我需要在自己的网站上插入“立即购买”按钮,因此我找到了一种简单的方法。
So I decided to write a simple, little article to share this little piece of knowledge. Before to start, I have to say that this is not an exhaustive tutorial about implementing PayPal functions and options in a Web site. I only describe a step-by-step procedure to:
因此,我决定写一篇简单的小文章来分享这一小知识。 在开始之前,我不得不说,这不是一个有关在网站上实现PayPal功能和选项的详尽教程。 我仅描述一个分步过程以:
1. use in a web page a PayPal Buy Now button (from here BNB) in order to send our customer to PayPal site (other payment options will not be considered);
1.在网页上使用“贝宝立即购买”按钮(从此处BNB)以将我们的客户发送到贝宝站点(其他付款方式将不被考虑);
2. let our customer use PayPal site to pay our product even if he has not a PayPal account;
2.让我们的客户使用PayPal网站付款,即使他没有PayPal帐户;
3. make our customer to return to our site automatically;
3.让我们的客户自动返回我们的网站;
4. receive from PayPal the confirmation that transaction has been succsfully completed using PDT and a PHP script included in our site (IPN will not be treated);
4.从PayPal收到使用PDT和我们网站中包含PHP脚本成功完成交易的确认(将不处理IPN);
5. test all procedure using PayPal Sandbox.
5.使用PayPal沙盒测试所有过程。
Of course, I'll assume you have a PayPal account: if you don't have it, I suggest you to create a new one before to proceed.
当然,我假设您有一个PayPal帐户:如果您没有,我建议您在继续之前创建一个新帐户。
1. Creating a PayPal Buy Now button.1.创建PayPal立即购买按钮。Make Login your PayPal. In the horizontal tabbed menu click 'PayPal Merchant Services', then click 'Sell single items' and then follow these steps:
使登录成为您的贝宝。 在水平选项卡式菜单中,单击“ PayPal Merchant Services”,然后单击“出售单个项目”,然后按照以下步骤操作:
Step1:第1步:- Fill in Item name box and provide (optionally) an unique ID for it
-填写商品名称框并为其提供(可选)唯一的ID
- Choose price and currency values
-选择价格和货币值
- Set the postage cost, if needed
-如果需要,设置邮费
- choose the email address to receive payement notifications
-选择电子邮件地址以接收付款通知
Step2:第2步:- Check 'Save button at Paypal'
-选中“在Paypal上保存按钮”
- Leave unchecked other options (only because I've not tested yet: feel free to
-取消选中其他选项(仅因为我尚未测试:请随时
use them if you need them)
如果需要,请使用它们)
Step3:第三步:Set following options accordingly to your needs
根据需要设置以下选项
- Do you want to let your customer change order quantities?
-是否要让客户更改订单数量?
- Can your customer add special instructions in a message to you?
-您的客户可以在给您的消息中添加特殊说明吗?
- Need your customer's postal address?
-需要您客户的邮政地址吗?
- Take customers to this URL when they cancel their checkout
-取消结帐时将客户带到此URL
Set following option with the exact url of the page of your Web site wich contains the script that will process the order and will update database, will allow a download and so on... For our examples we'll call this page 'http://yourDomain.com/ppreturn.php'. Be sure to check the box
使用网站页面的确切URL设置以下选项,其中包含处理订单,更新数据库,允许下载等的脚本。对于我们的示例,我们将此页面称为“ http: //yourDomain.com/ppreturn.php' 。 请务必选中该框
- Take customers to this URL when they finish checkout
-完成结帐后,将客户带到此URL
Well, all done. Now you have to click 'Create button' and you'll get the code to insert in your page: select the code copy it and paste it where you wish your button will appear.
好吧,一切都完成了。 现在,您必须单击“创建按钮”,然后将代码插入页面:选择代码,将其复制并粘贴到希望按钮出现的位置。
Now, you have to set up all preferences for Website Payment Preferences. Go back to the PayPal home page, the one that appears after you logged in. There, under the horizontal tabbed menu, with My Account tab selected, you'll see the menu item 'Profile'. Moving your mouse over it, a second v ertical menu will appear but this is not interesting for us: click 'Profile'.
现在,您必须为“网站付款首选项”设置所有首选项。 返回到PayPal主页,即登录后出现的主页。在水平选项卡式菜单下的My Account(我的帐户)选项卡处于选中状态,您将看到菜单项'Profile'。 将鼠标移到它上面,将显示另一个垂直菜单,但这对我们来说并不有趣:单击“配置文件”。
In this way you'll be redirected to My Profile page. Here there are three columns: 'Account Information', 'Financial Information', 'Selling Preferences'. In the last one column look for 'Website Payment Preferences' and click it. In the new page you'll see there are several options to set. First af all, to answer to your question, scroll the page and look for 'Payment Data Transfer (optional)' section (is near the top of the page): be sure to turn On this option and get your Identity Token.
这样,您将被重定向到“我的资料”页面。 这里有三列:“帐户信息”,“财务信息”,“销售首选项”。 在最后一列中查找“网站付款首选项”,然后单击它。 在新页面中,您将看到几个设置选项。 首先,要回答您的问题,请滚动页面并查找“付款数据传输(可选)”部分(位于页面顶部附近):请务必打开此选项并获取您的身份令牌。
About the other options, be sure its are set as following instructions:
关于其他选项,请确保按照以下说明进行设置:
- Auto Return for Website Payments:
-自动退回网站付款:
- On
- 上
- Return URL: the page you wish your customer will be redirected to. we call this page http://yourDomain.com/ppreturn.php
-返回网址:您希望客户重定向到的页面。 我们称此页面为http://yourDomain.com/ppreturn.php
- Encrypted Website Payments
-加密的网站付款
- I don't use it so I have set it to Off
-我不使用它,所以将其设置为“关”
- PayPal Account Optional
-PayPal帐户可选
- On
- 上
- Contact Telephone Number
-联络电话
- I don't use it so I have set it to Off (reccomended by Paypal)
-我不使用它,所以我将其设置为“关”(贝宝推荐)
- PayPal Express Checkout Settings
-PayPal Express结帐设定
- Set to No
-设置为否
Well, that's all. Remember that the page you use in Return URL option must be the same set in BNB configuration.
好,仅此而已。 请记住,在“返回URL”选项中使用的页面必须与BNB配置中的页面相同。
2. The script2.脚本BNB has to be placed in the page where is your product description. The script you find in the box code below has to be placed in the page your customer will be redirected to by PayPal system when he will complete transaction (or when he will decide not to buy, sigh!). In our example, this page is 'http://yourDomain.com/ppreturn.php'.
BNB必须放置在您的产品说明所在的页面上。 您在下面的方框代码中找到的脚本必须放置在客户将在完成交易时(或在他决定不购买时,叹息!)的页面中,您的客户将被PayPal系统重定向到该页面。 在我们的示例中,此页面为' http://yourDomain.com/ppreturn.php' 。
Obviously, this script has to be included or inserted in a web page in the exact point where you wish messages appear.
显然,必须在您希望消息出现的确切位置上将该脚本包含或插入到网页中。
3. PayPal Sandbox3.贝宝沙盒Now, all is ready to make us reach and happy but... it will work fine? PayPal give us a chance to test our code quite easily, the PayPal Sandbox. Direct your browser to http://www.sandbox.paypal.com and you'll see a page very similar to the PayPal login page. You are requested to signup to PayPal Sandbox so to create an account. Once you have your Sandbox account and you are logged in, you have to create two test user: the first one will be the ghost-buyer the second one the ghost-seller. Look for 'Test accounts' just in front of you. Click 'Create a preconfigured buyer or seller account.' and follow easy steps to create a new buyer account. When you have created the first account you are redirected to the Test accounts page. Here click 'Home' in the left menu and repeat all steps to create a new seller account.
现在,一切准备就绪,可以使我们感动和快乐,但是...会正常吗? 贝宝(PayPal)沙盒(PayPal Sandbox)使我们有机会轻松测试我们的代码。 将您的浏览器定向到http://www.sandbox.paypal.com ,您会看到与PayPal登录页面非常相似的页面。 您需要注册到PayPal Sandbox才能创建一个帐户。 拥有沙盒帐户并登录后,您必须创建两个测试用户:第一个将是“幽灵购买者”,第二个将是“幽灵卖方”。 在您眼前寻找“测试帐户”。 点击“创建预先配置的买方或卖方帐户”。 并按照简单的步骤创建新的买方帐户。 创建第一个帐户后,您将被重定向到“测试帐户”页面。 在这里单击左侧菜单中的“主页”,然后重复所有步骤以创建新的卖方帐户。
When you are creating these two test user, be sure to take note of usernames and passwords: you'll need them soon. The ghost-seller will simulate your real seller account. How? Go in the test user page, select the ghost-seller account and click the 'Test this account' button below. A popup window will appear and you'll be prompted to login: use username and password of your ghost-seller account. Once you are logged in, repeat all steps you have done in the real world of PayPal site. Click 'PayPal Merchant Services', then click on 'Sell single items' and so on.
创建这两个测试用户时,请确保记下用户名和密码:您很快就会需要它们。 虚假卖家将模拟您的真实卖家帐户。 怎么样? 进入测试用户页面,选择虚拟卖家帐户,然后点击下面的“测试此帐户”按钮。 将出现一个弹出窗口,并提示您登录:使用您的幽灵卖家帐户的用户名和密码。 登录后,重复您在PayPal网站真实世界中完成的所有步骤。 单击“ PayPal商业服务”,然后单击“出售单个项目”,依此类推。
During this process copy the code to insert the button in your site and take note of your sandbox token.
在此过程中,复制代码以将按钮插入您的网站,并记下您的沙盒令牌。
Now you can close the ghost-seller account window.
现在,您可以关闭幻影卖家帐户窗口。
Once you have created your sandbox BNB and you have setup Website Payment Preferences, you have to make some changes in your site. First, you have to replace the original BNB code with the one you have taken in Sandbox. Second, in the script in ppreturn.php go to line where you found your token:
创建沙盒BNB并设置“网站付款首选项”后,您必须在网站中进行一些更改。 首先,您必须将原始BNB代码替换为在Sandbox中获取的代码。 其次,在ppreturn.php中的脚本中,找到找到令牌的行:
$auth_token = "your_paypal_token";
$ auth_token =“您的paypal_token”;
and add this line immediately below:
并在下面添加此行:
$auth_token = "your_sandbox_token";
$ auth_token =“ your_sandbox_token”;
Then comment the line with the PayPal token and leave uncommented the line with sandbox token. When you'll have tested your selling system you'll have to invert comment (commenting sandbox token and uncommenting PayPal token).
然后,使用PayPal令牌注释该行,并使用沙盒令牌对此行取消注释。 测试销售系统后,您必须将注释(注释沙盒令牌和取消注释PayPal令牌)进行倒置。
Finally, open in your browser the page of your website where is the (sandbox) Buy Now button and click it. You'll be redirected to the sandbox site and you'll have to login using your ghost-buyer account username and password. Complete this simulated transaction and if it's all right you'll be redirected to your ppreturn.php page where you'll see transaction data and your own-written message.
最后,在浏览器中打开网站页面(沙盒)立即购买按钮,然后单击它。 您将被重定向到沙箱站点,并且必须使用幽灵购买者帐户用户名和密码登录。 完成此模拟交易,如果可以,您将被重定向到ppreturn.php页面,您将在其中看到交易数据和您自己编写的消息。
Well, as Warners said, that's all, folks! Good coding.
好吧,正如华纳兄弟所说,仅此而已! 良好的编码。
<?php
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = "your_paypal_token";
$req .= "&tx=$tx_token&at=$auth_token";
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
if (!$fp) {
exit ("You don't come from Paypal site. Please, complete transaction in Paypal.");
} else {
//verifies if token is correct and retrieves transaction data from PayPal
fputs ($fp, $header . $req);
$res = '';
$headerdone = false;
while (!feof($fp)) {
$line = fgets ($fp, 1024);
if (strcmp($line, "\r\n") == 0){
$headerdone = true;
} else if ($headerdone){
$res .= $line;
}
}
$lines = explode("\n", $res);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
//if transaction was successfull insert data in a database (optional), print a message
//to inform customer about transaction data (recomended) and optionally redirect to another page
//i.e. to allow to download a file
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
$firstname = $keyarray['first_name'];
$lastname = $keyarray['last_name'];
$itemname = $keyarray['item_name'];
$email = $keyarray['payer_email'];
$amount = $keyarray['mc_gross'];
$country = $keyarray['residence_country'];
$payerid = $keyarray['payer_id'];
$txn_id = $keyarray['txn_id'];
$status = $keyarray['payment_status'];
$query = "INSERT INTO your_customers_table (firstname, lastname, email)
VALUES('$firstname', '$lastname', '$email')";
$result = @mysql_query($query);
if (!$result){mysql_error();}
echo ("<center><h3>Thank you for your purchase!</h3></center>");
echo ("<p>Your transaction has been completed, and a receipt for your purchase has
been emailed to you.<br>You may log into your account at <a href='https://www.paypal.com'>
www.paypal.com</a> to view details of this transaction.<br><br /></p>");
echo ("<p><b>Payment Details</b></p>");
echo ("<ul><li>First name: $firstname</li>");
echo ("<li>Item: $itemname</li>");
echo ("<li>Amount: $amount</li>");
echo ("<li>Last name: $lastname</li>");
echo "<li>Residence Country: $country</li>";
echo "<li>Buyer email: $email</li>";
echo "<li>Buyer id: $payerid</li>";
echo "<li>Transaction id: $txn_id</li>";
echo "<li>Transaction status: $status</li>";
echo ("</ul>");
echo ("");
}else{
//otherwise print a message to inform customer that something has gone wrong, ofering support and so on
echo "Your transaction has not been successfully completed. Please contact the Administrator.";
}
fclose ($fp);
}
?>
paypal 按钮支付