<!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的官方文檔查看更多具體的開發項設置。包括交易明細的獲取等等都是可以實現的。這裡就不具體講下去了。