Thinkphp5 使用Paypal 支付
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)在代码中打开生产环境即可
Thinkphp5 使用Paypal 支付相关推荐
- thinkphp5.1接入paypal支付
thinkphp5.1接入paypal支付 php计入paypal,要先到https://developer.paypal.com/developer/dashboard/sandbox 下面以测试环 ...
- Android下集成Paypal支付
近期项目需要研究paypal支付,官网上的指导写的过于复杂,可能是老外的思维和中国人不一样吧.难得是发现下面这篇文章: http://www.androidhive.info/2015/02/andr ...
- PAYPAL 支付,sandbox测试的时候遇到异常:请求被中止: 未能创建 SSL/TLS 安全通道,以及解决方法。...
场景描述: 我公司的网站之前就已经开发好了下单支持paypal支付,当然,这个是其他同事开发的.前段时间,主管让我在另一个场景开发支持使用paypal支付,当时花了些时间看看关于paypal代码,然后 ...
- google支付接入PHP语言,PHP语言开发Paypal支付demo的具体实现
一.paypal支付接口准备工作 首先去申请一个paypal账号,https://www.paypal.com/. 申请完毕并登录,进入https://developer.paypal.com/dev ...
- PAYPAL 支付,sandbox测试的时候遇到异常:请求被中止: 未能创建 SSL/TLS 安全通道,以及解决方法。
PAYPAL 支付,sandbox测试的时候遇到异常:请求被中止: 未能创建 SSL/TLS 安全通道,以及解决方法. 参考文章: (1)PAYPAL 支付,sandbox测试的时候遇到异常:请求被中 ...
- PayPal支付集成到自己Web网站
PayPal如何集成到B2C网站? 因为网上实在找不到集成教程,我习惯性的google了一下相关资料,发现关于paypal接口的中文文档并不多,也不详细,只好自己研究,还好paypal上的资料非常齐全 ...
- uni-app:实现PayPal支付功能
一.相关文档或网址 1.uni-app官方文档地址:开通 | uni-app官网 2.paypal开发者中心:Applications - PayPal Developer 二.开发流程 1.开发前配 ...
- vue模块给模块传参_Vue店面的Paypal支付模块
vue模块给模块传参 Vue店面PayPal付款扩展 (Vue Storefront PayPal Payment Extension) PayPal Payment module for vue-s ...
- Java集成PayPal支付
Java集成PayPal支付 1.申请账号 浏览器中输入:https://www.paypal.com,点击 "注册" 选择 "企业账号" ,信息可以随意填写 ...
最新文章
- 新冠研究数据哪里找?科研工作者必看(附链接)
- Athentech Perfectly Clear中文版
- 工作总结书写的意义及要求
- Linux 0.00 的编译和运行
- 计算矩阵的逆和行列式的值(高斯消元+LU分解)
- 每日一题(39)—— sizeof
- Wamp环境下集成【禅道】管理软件
- 关于WEB ServiceWCFWebApi实现身份验证之WEB Service篇
- 【问链财经-区块链基础知识系列】 第十六课 区块链将变革教育产业,未来有六大应用方向
- 调查了23款加速器,只剩这几款可以白嫖了!
- PMP-8. 项目经理的能力
- java 可达性分析算法_垃圾回收:可达性分析算法、生存还是死亡(两次标记)
- 考研日语线上笔记(五):中级日语语法总结20课(11~20)
- 【MATLAB】进阶绘图 ( Stairs 阶梯图 | stairs 函数 | Stem 离散序列数据图 | stem 函数 | 正弦函数采样 )
- 研报摘要|元宇宙:下一代互联网启程(附元宇宙深度报告PDF)
- gpu浮点计算能力floaps_认识GPU浮点计算精度
- 用文字总结出计算机组装步骤,项目教学法在《计算机组装与维护》中的运用与反思(杜燕)...
- 445/139端口的作用与危害
- 解决fileReader读取文件乱码
- 企业微信支付收款码申请开通方法