Thinkphp6接入PayPal支付

2024-04-14 15:36
文章标签 支付 接入 thinkphp6 paypal

本文主要是介绍Thinkphp6接入PayPal支付,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

沙盒环境示例

创建扩展封装类

<?php
namespace lib;class PayPalApi
{//clientIdprivate $clientId;//clientSecretprivate $clientSecret;//服务器地址private $host = 'https://api-m.sandbox.paypal.com/';//主机头private $headers = [];//api凭证private $token = '';//报文主体private $bodys = '';/*** 初始化类*/public function __construct(array $config = []){$this->clientId = isset($config['clientId']) ? $config['clientId'] : '';$this->clientSecret = isset($config['clientSecret']) ? $config['clientSecret'] : '';$this->getAccessToken();}/*** 创建订单*/public function create(array $data){if (!$this->token) {return ['errcode' => 5001,'errmsg' => '获取token失败',];}if (!(is_array($data) && $data)) {return ['errcode' => 5002,'errmsg' => '数据格式有误',];}$trade_no = isset($data['trade_no']) ? filterTags($data['trade_no']) : '';$trade_amount = isset($data['trade_amount']) ? sprfloat($data['trade_amount']) : 0;$funds_item = isset($data['funds_item']) ? filterTags($data['funds_item']) : '';$currency_code = isset($data['currency_code']) ? filterTags($data['currency_code']) : '';if (!$trade_no) {return ['errcode' => 5003,'errmsg' => '交易编号不能为空',];}if ($trade_amount <= 0) {return ['errcode' => 5004,'errmsg' => '交易金额无效',];}$this->headers = ['Content-Type: application/json','Authorization: Bearer ' . $this->token,'PayPal-Request-Id: Trade-' . $trade_no,];$bodys = ['intent' => 'CAPTURE','application_context' => ['brand_name' => 'BKB','landing_page' => 'LOGIN','user_action' => 'PAY_NOW','return_url' => request()->domain() . '/pc/paypal/callback','cancel_url' => request()->domain() . '/pc/paypal/fail',],'purchase_units' => [['reference_id' => $trade_no,'description' => $funds_item,'amount' => ['currency_code' => $currency_code,'value' => $trade_amount,'breakdown' => ['item_total' => ['currency_code' => $currency_code,'value' => $trade_amount,],'shipping' => ['currency_code' => $currency_code,'value' => 0,],'handling' => ['currency_code' => $currency_code,'value' => 0,],'tax_total' => ['currency_code' => $currency_code,'value' => 0,],'shipping_discount' => ['currency_code' => $currency_code,'value' => 0,],],],],],];$this->bodys = json_encode($bodys, JSON_UNESCAPED_UNICODE);$url = $this->host . 'v2/checkout/orders';$result = json_decode($this->post($url), true);if (!$result) {return ['errcode' => 5005,'errmsg' => '远程服务器无应答',];}file_put_contents(ROOT_PATH . 'paypal.log', print_r($result, true), FILE_APPEND);if (isset($result['name']) && isset($result['message'])) {return ['errcode' => 5006,'errmsg' => '[' . $result['name'] . '] - ' . $result['message'],];}if (!isset($result['status'])) {return ['errcode' => 5007,'errmsg' => '远程服务器响应异常',];}if ('CREATED' !== $result['status']) {$desc = isset($result['description']) ? filterTags($result['description'], 1) : $result['status'];return ['errcode' => 5008,'errmsg' => $desc,];}$trade_token = isset($result['id']) ? filterTags($result['id']) : '';if (!$trade_token) {return ['errcode' => 5009,'errmsg' => '远程服务器响应异常',];}return ['errcode' => 0,'errmsg' => 'success','data' => ['trade_token' => $trade_token,],];}/*** 捕获订单*/public function capture(array $data){if (!$this->token) {return ['errcode' => 5001,'errmsg' => '获取token失败',];}if (!(is_array($data) && $data)) {return ['errcode' => 5002,'errmsg' => '数据格式有误',];}$trade_no = isset($data['trade_no']) ? filterTags($data['trade_no']) : '';$trade_amount = isset($data['trade_amount']) ? sprfloat($data['trade_amount']) : 0;$trade_token = isset($data['trade_token']) ? filterTags($data['trade_token']) : '';$funds_item = isset($data['funds_item']) ? filterTags($data['funds_item']) : '';$currency_code = isset($data['currency_code']) ? filterTags($data['currency_code']) : '';if (!$trade_no) {return ['errcode' => 5003,'errmsg' => '交易编号不能为空',];}if ($trade_amount <= 0) {return ['errcode' => 5004,'errmsg' => '交易金额无效',];}if (!$trade_token) {return ['errcode' => 5005,'errmsg' => '交易令牌不能为空',];}$this->headers = ['Content-Type: application/json','Authorization: Bearer ' . $this->token,'PayPal-Request-Id: Trade-' . $trade_token,];$bodys = ['intent' => 'AUTHORIZE','application_context' => ['brand_name' => 'BKB','landing_page' => 'LOGIN','user_action' => 'PAY_NOW','return_url' => request()->domain() . '/pc/paypal/callback','cancel_url' => request()->domain() . '/pc/paypal/fail',],'purchase_units' => [['reference_id' => $trade_no,'description' => $funds_item,'amount' => ['currency_code' => $currency_code,'value' => $trade_amount,'breakdown' => ['item_total' => ['currency_code' => $currency_code,'value' => $trade_amount,],'shipping' => ['currency_code' => $currency_code,'value' => 0,],'handling' => ['currency_code' => $currency_code,'value' => 0,],'tax_total' => ['currency_code' => $currency_code,'value' => 0,],'shipping_discount' => ['currency_code' => $currency_code,'value' => 0,],],],],],];$this->bodys = json_encode($bodys, JSON_UNESCAPED_UNICODE);$url = $this->host . 'v2/checkout/orders/' . $trade_token . '/capture';$result = json_decode($this->post($url), true);if (!$result) {return ['errcode' => 5006,'errmsg' => '远程服务器无应答',];}file_put_contents(ROOT_PATH . 'paypal.log', print_r($result, true), FILE_APPEND);if (!isset($result['status'])) {return ['errcode' => 5007,'errmsg' => '远程服务器响应异常',];}if ('COMPLETED' !== $result['status']) {$desc = isset($result['description']) ? filterTags($result['description'], 1) : $result['status'];return ['errcode' => 5008,'errmsg' => $desc,];}if (!isset($result['purchase_units'][0]['payments']['captures'][0]['id'])) {return ['errcode' => 5009,'errmsg' => '远程服务器响应异常',];}$payment_no = filterTags($result['purchase_units'][0]['payments']['captures'][0]['id']);if (!$payment_no) {return ['errcode' => 5010,'errmsg' => '远程服务器响应异常',];}$payment_time = isset($resource['update_time']) ? strtotime($resource['update_time']) : 0;if ($payment_time <= 0) {$payment_time = time();}if ($payment_time > time()) {$payment_time = time();}return ['errcode' => 0,'errmsg' => 'success','data' => ['payment_no' => $payment_no,'payment_time' => $payment_time,],];}/*** 退款*/public function refund(array $data){if (!$this->token) {return ['errcode' => 5001,'errmsg' => '获取token失败',];}if (!(is_array($data) && $data)) {return ['errcode' => 5002,'errmsg' => '数据格式有误',];}$capture_id = isset($data['capture_id']) ? filterTags($data['capture_id']) : '';$amount = isset($data['amount']) ? sprfloat($data['amount']) : 0;$currency_code = isset($data['currency_code']) ? filterTags($data['currency_code']) : '';if (!$capture_id) {return ['errcode' => 5003,'errmsg' => '交易流水号不能为空',];}if ($amount <= 0) {return ['errcode' => 5004,'errmsg' => '退款金额无效',];}if (!$currency_code) {return ['errcode' => 5005,'errmsg' => '货币代号不能为空',];}$this->headers = ['Content-Type: application/json','Authorization: Bearer ' . $this->token,'PayPal-Request-Id: Refund-' . $capture_id,];$bodys = ['amount' => ['value' => $amount,'currency_code' => $currency_code,],];$this->bodys = json_encode($bodys, JSON_UNESCAPED_UNICODE);$url = $this->host . 'v2/payments/captures/' . $capture_id . '/refund';$result = json_decode($this->post($url), true);if (!$result) {return ['errcode' => 5006,'errmsg' => '远程服务器无应答',];}file_put_contents(ROOT_PATH . 'paypal.log', print_r($result, true), FILE_APPEND);if (!isset($result['status'])) {return ['errcode' => 5007,'errmsg' => '远程服务器响应异常',];}if ('COMPLETED' !== $result['status']) {$desc = isset($result['description']) ? filterTags($result['description'], 1) : $result['status'];return ['errcode' => 5008,'errmsg' => $desc,];}if (!isset($result['id'])) {return ['errcode' => 5009,'errmsg' => '远程服务器响应异常',];}$payment_no = filterTags($result['id']);if (!$payment_no) {return ['errcode' => 5010,'errmsg' => '远程服务器响应异常',];}$payment_time = isset($resource['update_time']) ? strtotime($resource['update_time']) : 0;if ($payment_time <= 0) {$payment_time = time();}if ($payment_time > time()) {$payment_time = time();}return ['errcode' => 0,'errmsg' => 'success','data' => ['payment_no' => $payment_no,'payment_time' => $payment_time,],];}/*** 获取token*/private function getAccessToken(){$token = '';$file = ASSETS_PATH . 'paypal/access_token.json';if (is_file($file)) {$data = json_decode(file_get_contents($file), true);$time = isset($data['expired_time']) ? fnumber($data['expired_time']) : 0;if ($time > time()) {$token = isset($data['access_token']) ? filterTags($data['access_token']) : '';}}if (!$token) {$this->headers = ['Content-Type: application/json','Accept-Language: en_US',];$this->bodys = 'grant_type=client_credentials';$url = $this->host . 'v1/oauth2/token';$result = json_decode($this->post($url, true), true);if ($result && isset($result['access_token'])) {$token = $result['access_token'];$time = $result['expires_in'];$path = dirname($file);if (!is_dir($path)) {mkdir($path, 0755, true);}file_put_contents($file, json_encode(['access_token' => $token,'expired_time' => time() + $time,]));}}$this->token = $token;}/** http协议发送get请求*/private function get(string $url){$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 2);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);$out = curl_exec($ch);curl_close($ch);return $out;}/** http协议发送post请求*/private function post(string $url, bool $credentials = false){$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($ch, CURLOPT_AUTOREFERER, 1);curl_setopt($ch, CURLOPT_HEADER, false);curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);if ($credentials) {curl_setopt($ch, CURLOPT_USERPWD, $this->clientId . ':' . $this->clientSecret);}curl_setopt($ch, CURLOPT_POSTFIELDS, $this->bodys);$result = curl_exec($ch);curl_close($ch);return $result;}
}

未完待续……

欢迎加入IT技术交流接单微信群

这篇关于Thinkphp6接入PayPal支付的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/903371

相关文章

Java应用对接pinpoint监控工具的时候,应用名称长度超出限制而导致接入失败

一、背景 java应用需要接入pinpoint,同一个虚拟机上的其他应用接入成功,唯独本应用不行。 首先排除是pinpoint agent的问题,因为其他应用都正常。 然后,我就对比二者的启动脚本。 -javaagent:/opt/pinpoint/pinpoint-bootstrap.jar -Dpinpoint.agentId=DA301004_17 -Dpinpoint.applic

jsapi 支付缺少appid ¬ify_url

$.ajax({url: 'url',type: 'get',dataType: "json",//改成jsonsuccess: function (data) {//$('#xx').val(data)WeixinJSBridge.invoke('getBrandWCPayRequest', $.parseJSON(data),function(res){if(res.err_msg == "

“设计模式双剑合璧:工厂模式与策略模式在支付系统中的完美结合”

工厂模式(Factory Pattern)和策略模式(Strategy Pattern)都是常见的设计模式,但它们解决的问题和应用场景不同。下面是它们的区别: 1. 目的不同: 工厂模式(Factory Pattern): 工厂模式的主要目的是创建对象。它通过定义一个创建对象的接口,让子类决定实例化哪一个具体类,从而将对象创建的逻辑与使用的代码分离。 工厂模式可以分为简单工厂、工厂方法和抽象

一步步教你接入个推 推送

一: manifast中的配置 权限: <!-- 个推SDK权限配置开始 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permissi

一步步教你如何为你的app接入支付宝

官方接口文档步骤链接: https://doc.open.alipay.com/doc2/detail?treeId=59&articleId=103563&docType=1 1首先,你的要有一个企业的账户,并且已经和支付宝平台签约了, (具体操作查看https://doc.open.alipay.com/doc2/detail.htm?treeId=58&articleId=1035

兼容Trino Connector,扩展Apache Doris数据源接入能力|Lakehouse 使用手册(四)

Apache Doris 内置支持包括 Hive、Iceberg、Hudi、Paimon、LakeSoul、JDBC 在内的多种 Catalog,并为其提供原生高性能且稳定的访问能力,以满足与数据湖的集成需求。而随着 Apache Doris 用户的增加,新的数据源连接需求也随之增加。因此,从 3.0 版本开始,Apache Doris 引入了 Trino Connector 兼容框架。 Tri

社群空间站付费入群系统易支付版全套搭建教程

社群空间站9.9付费入群系统易支付版全套搭建教程 1.创建站点 2.搭建环境 php7.2 3.上传源码包 数据库批量修改sq9.dongge1.icu s10.dongge1.icu 改为你的域名 4.上传数据库 修改数据库文件/data/config/ 5.访问域名 6.账户密码 admin 123456 7.易支付修改地址是在/data/tpl/app/default/yy_

强化网络安全:通过802.1X协议保障远程接入设备安全认证

随着远程办公和移动设备的普及,企业网络面临着前所未有的安全挑战。为了确保网络的安全性,同时提供无缝的用户体验,我们的 ASP 身份认证平台引入了先进的 802.1X 认证协议,确保只有经过认证的设备才能接入您的网络。本文档将详细介绍我们的平台如何通过 802.1X 协议实现高效、安全的远程接入认证。 产品亮点 1. 无缝集成 我们的 ASP 身份认证平台支持无缝集成到现有的网络基础设施中

店匠科技携手Stripe共谋电商支付新篇章

在全球电商行业蓬勃发展的背景下,支付环节作为交易闭环的核心,其重要性日益凸显。随着消费者对支付体验要求的不断提高,以及跨境电商的迅猛发展,支付市场正经历着前所未有的变革与挑战。在这一充满机遇与竞争的领域,店匠科技(Shoplazza)凭借其创新的嵌入式支付解决方案—— Shoplazza Payments,成功在市场中占据了一席之地。 近日,在新加坡举办的 Stripe Tour 新加坡站 20

浪子易支付8.29版本PHP网站源码

源码下载 浪子易支付8.29版本PHP网站源码 更新记录 2024/08/29: 1.付款记录管理支持批量操作 2.优化数据清理功能 3.修复了一些已知问题 2024/07/21: 1.更新全新的V2版API接口,使用RSA公私钥验证 2.支持通过接口发起代付转账、退款、查询等 3.支持通过接口发起付款码支付、JSAPI支付、APP支付 4.订单退款支持多次部分金额退款 5.针对插件开