PHP语言开发Paypal支付demo的具体实现,phppaypal支付demo
PHP语言开发Paypal支付demo的具体实现,phppaypal支付demo
如果我们的应用是面向国际的、那么支付的时候通常会考虑使用paypal。以下为个人写的一个paypal支付示例,已亲测可行。paypal有个很不错的地方就是为开发者提供了sandbox(沙箱)测试功能。(即为开发者在开发环境提供了一个虚拟的卖家账户和金额、以及一个虚拟的买家账户和金额、虚拟卡号等。能够让我们不用使用真实的金钱进行测试。)
一、开发前准备
- https://developer.paypal.com/ 到paypal的开发者官网注册开发者账号。
- 用账号登录之后、点击导航上面的 dashboard、进入dashboard面版。如下截图、后续的操作都是在这个面板中操作。
- 上面截图中菜单 Sandbox下面的Accounts里面能看到你的 sandbox测试的买家账号和卖家账号。2个测试账号里面都有profile选项里面有changepassword可以设置虚拟账号的密码。
- 上面截图中菜单Sandbox下面的Transactions就是你的交易记录。
- 点击截图页面右上角的 Create App按钮。创建一个应用。创建好后、会给你提供一个Client ID 和 Secret。这两个可以配置为php常量后面开发中会用到。
二、进入支付Demo开发
- 随便在本地建立一个开发代码根目录、先建立一个index.html里面就放一个简单的产品名称和产品价格两个input项即可、代码和截图如下:
-
<span><!</span><span>DOCTYPE html</span><span>></span> <span><</span><span>html </span><span>lang</span><span>="en"</span><span>></span> <span><</span><span>head</span><span>></span> <span><</span><span>meta </span><span>charset</span><span>="utf-8"</span><span>></span> <span><</span><span>title</span><span>></span>支付页面<span></</span><span>title</span><span>></span> <span></</span><span>head</span><span>></span> <span><</span><span>body</span><span>></span> <span><</span><span>div</span><span>></span> <span><</span><span>form </span><span>action</span><span>="checkout.php"</span><span> method</span><span>="post"</span><span> autocomplete</span><span>="off"</span><span>></span> <span><</span><span>label </span><span>for</span><span>="item"</span><span>></span><span> 产品名称 </span><span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="product"</span><span>></span> <span></</span><span>label</span><span>></span> <span><</span><span>br</span><span>></span> <span><</span><span>label </span><span>for</span><span>="amount"</span><span>></span><span> 价格 </span><span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="price"</span><span>></span> <span></</span><span>label</span><span>></span> <span><</span><span>br</span><span>></span> <span><</span><span>input </span><span>type</span><span>="submit"</span><span> value</span><span>="去付款"</span><span>></span> <span></</span><span>form</span><span>></span> <span></</span><span>div</span><span>></span> <span></</span><span>body</span><span>></span> <span></</span><span>html</span><span>></span>
Copy after login - 输入产品名称 和 价格。点击去付款就会到paypal的付款页面。用你的sandbox测试买家账号去付款。就会发现付款成功。然后登陆你的测试卖家账号。会发现卖家账号已经收到付款。当然这里会扣除paypal收取的手续费。手续费收的是卖家的。
- 下面来具体看看php是怎么实现的。首先先要把paypal提供的 php-sdk给弄到你的代码目录中来。这里介绍使用php的包管理器composer来获取最新sdk、当然你可以可以从github等其他渠道获取最新的paypal php-sdk。
- 默认你的电脑已经安装composer了。如果没有自己去度娘或者google下composer安装。
- 然后在你的代码根目录写一个composer.json文件来获取包内容。json文件代码如下:
<span>{ </span>"require" :<span> { </span>"paypal/rest-api-sdk-php" : "1.5.1"<span> } }</span>
Copy after login这里如果是 linux/unix系统就直接再根目录执行composer install来获取包内容。
- 安装好之后。根目录下面会产生一个vendor目录。里面有composer 和 paypal两个子目录。composer里面实现了自动加载、paypal则是你的sdk内容。
- 接下来我们来写一个公共文件(这里默认用 app/start.php、你的项目中可以自定义)、其实里面就只是实现了 sdk的autoload.php自动加载 和 创建刚才上面的的client id 和 secret生成的paypal支付对象实例。start.php代码如下:
-
<?<span>php </span><span>require</span> "vendor/autoload.php"; <span>//</span><span>载入sdk的自动加载文件</span> <span>define</span>('SITE_URL', 'http://www.paydemo.com'); <span>//</span><span>网站url自行定义 //创建支付对象实例</span> <span>$paypal</span> = <span>new</span><span> \PayPal\Rest\ApiContext( </span><span>new</span><span> \PayPal\Auth\OAuthTokenCredential( </span>'你的Client ID' '你的secret'<span> ) );</span>
Copy after login - 接下来就来实现表单中提交的处理文件 checkout.php。代码内容如下:
-
<?<span>php </span><span>/*</span><span>* * @author xxxxxxxx * @brief 简介: * @date 15/9/2 * @time 下午5:00 </span><span>*/</span> <span>use</span><span> \PayPal\Api\Payer; </span><span>use</span><span> \PayPal\Api\Item; </span><span>use</span><span> \PayPal\Api\ItemList; </span><span>use</span><span> \PayPal\Api\Details; </span><span>use</span><span> \PayPal\Api\Amount; </span><span>use</span><span> \PayPal\Api\Transaction; </span><span>use</span><span> \PayPal\Api\RedirectUrls; </span><span>use</span><span> \PayPal\Api\Payment; </span><span>use</span> \PayPal\<span>Exception</span><span>\PayPalConnectionException; </span><span>require</span> "app/start.php"<span>; </span><span>if</span> (!<span>isset</span>(<span>$_POST</span>['product'], <span>$_POST</span>['price'<span>])) { </span><span>die</span>("lose some params"<span>); } </span><span>$product</span> = <span>$_POST</span>['product'<span>]; </span><span>$price</span> = <span>$_POST</span>['price'<span>]; </span><span>$shipping</span> = 2.00; <span>//</span><span>运费</span> <span>$total</span> = <span>$price</span> + <span>$shipping</span><span>; </span><span>$payer</span> = <span>new</span><span> Payer(); </span><span>$payer</span>->setPaymentMethod('paypal'<span>); </span><span>$item</span> = <span>new</span><span> Item(); </span><span>$item</span>->setName(<span>$product</span><span>) </span>->setCurrency('USD'<span>) </span>->setQuantity(1<span>) </span>->setPrice(<span>$price</span><span>); </span><span>$itemList</span> = <span>new</span><span> ItemList(); </span><span>$itemList</span>->setItems([<span>$item</span><span>]); </span><span>$details</span> = <span>new</span><span> Details(); </span><span>$details</span>->setShipping(<span>$shipping</span><span>) </span>->setSubtotal(<span>$price</span><span>); </span><span>$amount</span> = <span>new</span><span> Amount(); </span><span>$amount</span>->setCurrency('USD'<span>) </span>->setTotal(<span>$total</span><span>) </span>->setDetails(<span>$details</span><span>); </span><span>$transaction</span> = <span>new</span><span> Transaction(); </span><span>$transaction</span>->setAmount(<span>$amount</span><span>) </span>->setItemList(<span>$itemList</span><span>) </span>->setDescription("支付描述内容"<span>) </span>->setInvoiceNumber(<span>uniqid</span><span>()); </span><span>$redirectUrls</span> = <span>new</span><span> RedirectUrls(); </span><span>$redirectUrls</span>->setReturnUrl(SITE_URL . '/pay.php?success=true'<span>) </span>->setCancelUrl(SITE_URL . '/pay.php?success=false'<span>); </span><span>$payment</span> = <span>new</span><span> Payment(); </span><span>$payment</span>->setIntent('sale'<span>) </span>->setPayer(<span>$payer</span><span>) </span>->setRedirectUrls(<span>$redirectUrls</span><span>) </span>->setTransactions([<span>$transaction</span><span>]); </span><span>try</span><span> { </span><span>$payment</span>->create(<span>$paypal</span><span>); } </span><span>catch</span> (PayPalConnectionException <span>$e</span><span>) { </span><span>echo</span> <span>$e</span>-><span>getData(); </span><span>die</span><span>(); } </span><span>$approvalUrl</span> = <span>$payment</span>-><span>getApprovalLink(); </span><span>header</span>("Location: {<span>$approvalUrl</span>}");
Copy after logincheckout.php通过表单提交上来的参数对支付具体细节和参数进行初始化和设置。这里只列出了常用的部分。paypal提供了很多参数设置。具体更丰富的可以自己参考paypal官方开发者文档。
- checkout.php设置完参数之后。会生成一个支付链接。用header跳转到这个支付链接(就是paypal的支付页面)到这个支付页面上面就可以用你的sandbox提供的buyer账号去支付了。截图如下:
- 用buyer账号支付完成之后。去看看你的sandbox的商家账户余额吧。就会发现已经收到了扣除手续费外的钱了。
- 这里支付成功 或者 失败后还有一个回调的处理。回调处理的php文件再上面的checkout.php里面的setReturnUrl处设置。这里设置的是/pay.php?success=true
- 接下来我们来看看pay.php是怎么简单处理回调的。先贴上pay.php的代码:
-
<?<span>php </span><span>require</span> 'app/start.php'<span>; </span><span>use</span><span> PayPal\Api\Payment; </span><span>use</span><span> PayPal\Api\PaymentExecution; </span><span>if</span>(!<span>isset</span>(<span>$_GET</span>['success'], <span>$_GET</span>['paymentId'], <span>$_GET</span>['PayerID'<span>])){ </span><span>die</span><span>(); } </span><span>if</span>((bool)<span>$_GET</span>['success']=== 'false'<span>){ </span><span>echo</span> 'Transaction cancelled!'<span>; </span><span>die</span><span>(); } </span><span>$paymentID</span> = <span>$_GET</span>['paymentId'<span>]; </span><span>$payerId</span> = <span>$_GET</span>['PayerID'<span>]; </span><span>$payment</span> = Payment::get(<span>$paymentID</span>, <span>$paypal</span><span>); </span><span>$execute</span> = <span>new</span><span> PaymentExecution(); </span><span>$execute</span>->setPayerId(<span>$payerId</span><span>); </span><span>try</span><span>{ </span><span>$result</span> = <span>$payment</span>->execute(<span>$execute</span>, <span>$paypal</span><span>); }</span><span>catch</span>(<span>Exception</span> <span>$e</span><span>){ </span><span>die</span>(<span>$e</span><span>); } </span><span>echo</span> '支付成功!感谢支持!';
Copy after login好了。到这里一个简单的paypal支付的demo其实已经走通了。懂得支付原理之后、想要再你自己的项目里面进行更丰富的扩展、就去paypal的官方文档查看更多具体的开发项设置。包括交易明细的获取等等都是可以实现的。这里就不具体讲下去了。
本文中的demo是在 2015-9-2中 使用 最新的paypal php-sdk版本 1.5.1测试通过的。如sdk版本的相差较远可能会有些许差别。
借阅者自行借助参考即可!希望能给刚刚想要开发paypal支付、确无从入手的人带来点帮助!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

With the rapid development of the Internet, more and more companies choose to sell products and services online, which makes online payment a major need for companies. As the world's leading online payment platform, PayPal has also become the first choice for many companies. This article will introduce how to use PHP and PayPal to implement online payments. We will divide it into the following steps: Create a PayPal account and application Integrate PayPalSDK Obtain payment Token Processing Payment processing Payment confirmation Create a PayPal account and application To use P

The reasons why PayPal cannot pay are due to insufficient account balance, payment method restrictions, transactions intercepted by the risk control system, payee account problems, network connection problems, and user account abnormalities, etc. Detailed introduction: 1. If the account balance is insufficient, you can increase the account balance through bank transfer or credit card recharge; 2. The payment method is restricted, check the payment settings and ensure that the selected payment method is not restricted; 3. The transaction is intercepted by the risk control system , contact PayPal customer service, provide relevant information to prove the legitimacy of the transaction, and request to lift payment restrictions, etc.

GCash on Tuesday said PayPal USD (PYUSD) tokens could now be traded via GCrypto, an in-app feature powered by the Philippine Digital Asset Exchange, at “low transaction fees.”

The inability to pay with Paypal is caused by problems with payment methods, account balances, Paypal balances, payment information, network problems, Paypal systems, merchants, and browsers. Detailed introduction: 1. Payment method, please make sure that the payment method used has been added to the Paypal account; 2. Account balance, ensure that the Paypal account balance is sufficient to pay the order amount; 3. Paypal balance, check the account status to see if there are any abnormalities; 4. Payment information, make sure the payment information entered is correct, such as credit card number, expiration date, etc.

Europeans use PayPal, but it is not universal and can only be used in areas where it is opened; PayPal is an online payment service provider headquartered in San Jose, California, USA; PayPal account is a secure online electronic account launched by PayPal. Use It can effectively reduce the occurrence of online fraud; the advanced management functions integrated into the PayPal account can control the details of every transaction.

The stablecoin asset issued by Paypal is now the sixth largest stablecoin asset today after growing significantly over the past ten days.

To download the PayPal official app, please visit the PayPal official website: https://www.paypal.com/ Click "Download", select the appropriate app store according to your device, search for "PayPal", download and install, and finally log in Your PayPal account. The app allows you to easily manage your account, stay secure, track spending, make payments seamlessly, and is available for iOS and Android devices.

According to news on March 8, PayPal Holdings Inc. recently issued an announcement announcing that millions of U.S. small businesses, all of which are users of Venmo and PayPal Zettle, now do not require any additional hardware such as expansion accessories or Bluetooth card readers. By supporting Apple's TaptoPay function, you can achieve contact-free payment with just an iPhone. Apple's TaptoPay feature, launched in May 2022, allows U.S. merchants to accept ApplePay and other contactless payment methods using iPhones and merchant-supported iOS apps. Through this service, users with compatible iPhone devices are able to securely process contactless payments as well as those with enabled
