Heim >Backend-Entwicklung >PHP-Tutorial >PHP语言开发Paypal支付demo的具体实现,phppaypal支付demo_PHP教程
<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>
<span>{ </span>"require" :<span> { </span>"paypal/rest-api-sdk-php" : "1.5.1"<span> } }</span>
这里如果是 linux/unix系统就直接再根目录执行composer install来获取包内容。
<?<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>
<?<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>}");
checkout.php通过表单提交上来的参数对支付具体细节和参数进行初始化和设置。这里只列出了常用的部分。paypal提供了很多参数设置。具体更丰富的可以自己参考paypal官方开发者文档。
<?<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> '支付成功!感谢支持!';
好了。到这里一个简单的paypal支付的demo其实已经走通了。懂得支付原理之后、想要再你自己的项目里面进行更丰富的扩展、就去paypal的官方文档查看更多具体的开发项设置。包括交易明细的获取等等都是可以实现的。这里就不具体讲下去了。