1,首先前往官网https://www.paypal.com 创建一个账户,我创建的是一个企业账户
2,前往paypal开发者平台https://developer.paypal.com/ 进行创建应用(使用谷歌自带的翻译,把网页翻译过来....)
3,点击之后出现如下界面,沙盒开发人员帐户 是在创建账号的时候默认生成的两个账号其中一个企业账号,还有一个是个人账号,供测试使用的,还可以多创建几个。
3,前往 https://github.com/paypal/PayPal-PHP-SDK 下载Paypal SDK,我只用 lib 里面的 PayPal,把PayPal 文件夹移动到 tp5 根目录extend 里面,放到这里就不需要动了。
4,之后我创建了一个控制器,点击支付则调用
代码:
<?phpnamespace app\home\controller;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\Auth\OAuthTokenCredential;
use PayPal\Exception\PayPalConnectionException;
use PayPal\Rest\ApiContext;
use PayPal\Api\PaymentExecution;
use Think\DB;class Paypal
{ // sandbox 开发环境// const clientId = 'xxxxxxxxxx';// const clientSecret = 'xxxxxxxx';// live 生产环境const clientId = 'xxxxxxxxxx';const clientSecret = 'xxxxxxxxxxxxxx';const accept_url = 'https://baidu.com';//返回地址const Currency = 'USD'; //币种 美元protected $PayPal;protected $order_number;public function __construct(){$this->PayPal = new ApiContext(new OAuthTokenCredential(self::clientId,self::clientSecret));//如果是沙盒测试环境不设置,请注释掉// $this->PayPal->setConfig(// array(// 'mode' => 'live',// )// );}public function index(){// 这里可以把数据传过来,也可以查出来// code ....// 商品名称$product = '商品商品';if (empty($product)) {$this->error('Goods cannot be empty.');}// 总价$price = '100';if (empty($price)) {$this->error('Prices cannot be empty.');}// 运费 $shipping = input('shipping', 0);// 商品描述$description = '商品描述';if (empty($description)) {$this->error('Description cannot be empty.');}$this->pay($product, $price, $shipping, $description);}/*** @param* $product 商品* $price 价钱* $shipping 运费* $description 描述内容*/public function pay($product, $price, $shipping = 0, $description){$paypal = $this->PayPal;$total = $price + $shipping;//总价$payer = new Payer();$payer->setPaymentMethod('paypal');$item = new Item();$item->setName($product)->setCurrency(self::Currency)->setQuantity(1)->setPrice($price);$itemList = new ItemList();$itemList->setItems([$item]);$details = new Details();$details->setShipping($shipping)->setSubtotal($price);$amount = new Amount();$amount->setCurrency(self::Currency)->setTotal($total)->setDetails($details);$transaction = new Transaction();$transaction->setAmount($amount)->setItemList($itemList)->setDescription($description)->setInvoiceNumber(uniqid());$redirectUrls = new RedirectUrls();// $redirectUrls->setReturnUrl(self::accept_url . '?success=true')->setCancelUrl(self::accept_url . '/?success=false');$redirectUrls->setReturnUrl(self::accept_url .'?success=true')->setCancelUrl(self::accept_url .'?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}");}/*** 回调*/public function Callback(){// 修改订单状态$success = trim($_GET['success']);if ($success == 'false' && !isset($_GET['paymentId']) && !isset($_GET['PayerID'])) {echo "<script>alert('取消支付。');history.go(-4);</script>";exit();}$paymentId = trim($_GET['paymentId']);$PayerID = trim($_GET['PayerID']);if (!isset($success, $paymentId, $PayerID)) {echo 'Failure to pay。';exit();}if ((bool)$_GET['success'] === 'false') {echo 'Failure to pay,payment ID【' . $paymentId . '】,Payer ID【' . $PayerID . '】';exit();}$payment = Payment::get($paymentId, $this->PayPal);$execute = new PaymentExecution();$execute->setPayerId($PayerID);try {$payment->execute($execute, $this->PayPal);} catch (Exception $e) {echo $e . ',支付失败,支付ID【' . $paymentId . '】,支付人ID【' . $PayerID . '】';exit();}// 到这里就支付成功了,可以修改订单状态,需要自己传参数,可以在成功回调地址后面加// code....// 可以跳转订单页面$url = 'http://www.baidu.com';header("Location:$url");die;}
}
代码end;
5,测试 id和key在应用里面,(不知道是我电脑原因还是其他原因,打开自动翻译之后点击显示秘钥没有反应。)
6,Paypal还是挺好用的,如果出现id key不正确的错误,可以尝试重新创建一个应用(一般不会错的),
如果一直调用不出来支付页面可能是网络原因
7,测试环境完成,接下来就到了切换到生产环境了,找了半小时没有找到生产环境怎么开启 ...
(1)点击下图live(生活) 则是生产环境的id和key,在代码里更换就可以了
(2)在代码中打开生产环境即可