之前的企业商务网站只限于国内支付宝的表单交易,没法面向国际外贸的支付业务如paypal,visa等,固然joomla国外cms内容管理系统已经提升这样的支付插件,这里我提到的是自定义paypal支付的功能。 php
开发步骤以下: html
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来获取包内容。 linux
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官方开发者文档。 git
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支付程序已经实现。 github
end json