1,首先前往官网https://www.paypal.com 创建一个账户,我创建的是一个个人账户
2,前往paypal开发者平台https://developer.paypal.com/ 进行创建应用
3,SandBox 沙箱模式,可以用来测试,live正式模式,点击create App 创建应用.
4,输入应用名称,选择应用类型,是商户还是平台,在使用沙箱模式测试时Sandbox Business Account派上用场,它是测试账号及密码.
5,记录ClientID Secret 这两个值,后期开发中需要使用
5,添加 webhook 填写webhook url 全部选中所有规则,之后保存,配置完成
6,前往 https://github.com/paypal/PayPal-PHP-SDK 下载Paypal SDK,我只用 lib 里面的 PayPal,把PayPal 文件夹移动到 tp5 根目录extend 里面,放到这里就不需要动了。
7,之后我创建了一个控制器,点击支付则调用
代码:
<?php
/***
* Created By JetBrains PhpStorm.
* USER : Swoole
* EMAIL:wyh757787026@163.com
* DATE :2021/5/10
* TIME :8:35
*/
namespace app\controller;
use app\BaseController;
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
{
//沙箱模式
//public $clientId = '';
//public $clientSecret = '';
//生活模式
public $clientId = '';
public $clientSecret = '';
public $accept_url = 'https://www.baidu.com';//返回地址
public $currency = 'USD'; //币种 美元
protected $PayPal;
public function __construct()
{
$this->PayPal = new ApiContext(
new OAuthTokenCredential(
$this->clientId,
$this->clientSecret
)
);
//生活模式开启,沙箱模式注释
$this->PayPal->setConfig(array('mode'=>'live'));
}
public function pay($product, $price, $shipping, $description){
$paypal = $this->PayPal;
$total = $price + $shipping;//总价
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item = new Item();
$item->setName($product)->setCurrency($this->currency)->setQuantity(1)->setPrice($price);
$itemList = new ItemList();
$itemList->setItems([$item]);
$details = new Details();
$details->setShipping($shipping)->setSubtotal($price);
$amount = new Amount();
$amount->setCurrency($this->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($this->accept_url .'?success=true&order_sn=1111111111111')->setCancelUrl($this->accept_url .'?success=false&order_sn=1111111111111');
$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();
}
// 到这里就支付成功了,可以修改订单状态,需要自己传参数,可以在成功回调地址后面加
// 可以跳转订单页面
$url = 'http://www.baidu.com';
header("Location:$url");die;
}
}