<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>支付页面</title> </head> <body> <div> <form action="checkout.php" method="post" autocomplete="off"> <label for="item"> 产品名称 <input type="text" name="product"> </label> <br> <label for="amount"> 价格 <input type="text" name="price"> </label> <br> <input type="submit" value="去付款"> </form> </div> </body> </html>
{ "require" : { "paypal/rest-api-sdk-php" : "1.5.1" } }
这里如果是 linux/unix系统就直接再根目录执行composer install来获取包内容。
<?php require "vendor/autoload.php"; //载入sdk的自动加载文件 define(‘SITE_URL‘, ‘http://www.paydemo.com‘); //网站url自行定义 //创建支付对象实例 $paypal = new \PayPal\Rest\ApiContext( new \PayPal\Auth\OAuthTokenCredential( ‘你的Client ID‘ ‘你的secret‘ ) );
<?php /** * @author xxxxxxxx * @brief 简介: * @date 15/9/2 * @time 下午5:00 */ use \PayPal\Api\Payer; use \PayPal\Api\Item; use \PayPal\Api\ItemList; use \PayPal\Api\Details; use \PayPal\Api\Amount; use \PayPal\Api\Transaction; use \PayPal\Api\RedirectUrls; use \PayPal\Api\Payment; use \PayPal\Exception\PayPalConnectionException; require "app/start.php"; if (!isset($_POST[‘product‘], $_POST[‘price‘])) { die("lose some params"); } $product = $_POST[‘product‘]; $price = $_POST[‘price‘]; $shipping = 2.00; //运费 $total = $price + $shipping; $payer = new Payer(); $payer->setPaymentMethod(‘paypal‘); $item = new Item(); $item->setName($product) ->setCurrency(‘USD‘) ->setQuantity(1) ->setPrice($price); $itemList = new ItemList(); $itemList->setItems([$item]); $details = new Details(); $details->setShipping($shipping) ->setSubtotal($price); $amount = new Amount(); $amount->setCurrency(‘USD‘) ->setTotal($total) ->setDetails($details); $transaction = new Transaction(); $transaction->setAmount($amount) ->setItemList($itemList) ->setDescription("支付描述内容") ->setInvoiceNumber(uniqid()); $redirectUrls = new RedirectUrls(); $redirectUrls->setReturnUrl(SITE_URL . ‘/pay.php?success=true‘) ->setCancelUrl(SITE_URL . ‘/pay.php?success=false‘); $payment = new Payment(); $payment->setIntent(‘sale‘) ->setPayer($payer) ->setRedirectUrls($redirectUrls) ->setTransactions([$transaction]); try { $payment->create($paypal); } catch (PayPalConnectionException $e) { echo $e->getData(); die(); } $approvalUrl = $payment->getApprovalLink(); header("Location: {$approvalUrl}");
checkout.php通过表单提交上来的参数对支付具体细节和参数进行初始化和设置。这里只列出了常用的部分。paypal提供了很多参数设置。具体更丰富的可以自己参考paypal官方开发者文档。
<?php require ‘app/start.php‘; use PayPal\Api\Payment; use PayPal\Api\PaymentExecution; if(!isset($_GET[‘success‘], $_GET[‘paymentId‘], $_GET[‘PayerID‘])){ die(); } if((bool)$_GET[‘success‘]=== ‘false‘){ echo ‘Transaction cancelled!‘; die(); } $paymentID = $_GET[‘paymentId‘]; $payerId = $_GET[‘PayerID‘]; $payment = Payment::get($paymentID, $paypal); $execute = new PaymentExecution(); $execute->setPayerId($payerId); try{ $result = $payment->execute($execute, $paypal); }catch(Exception $e){ die($e); } echo ‘支付成功!感谢支持!‘;
好了。到这里一个简单的paypal支付的demo其实已经走通了。懂得支付原理之后、想要再你自己的项目里面进行更丰富的扩展、就去paypal的官方文档查看更多具体的开发项设置。包括交易明细的获取等等都是可以实现的。这里就不具体讲下去了。
原文:http://www.cnblogs.com/miaoshiqian/p/4803655.html