rozmarbeka / paddle-php-api
Paddle.com API PHP 库
Requires
- php: >=5.3.0
- ext-curl: *
Requires (Dev)
- phpunit/phpunit: ~4.2.2
README
此库提供了一种方便的方式从PHP代码中查询Paddle API。
要求
PHP 5.3 或更高版本。
通过 Composer 安装
您可以通过 Composer 安装此库。将其添加到您的 composer.json 文件中:
{ "require": { "rozmarbeka/paddle-php-api": "1.*" } }
然后通过以下命令安装:
composer.phar install
要使用此库,您可以使用 Composer 的自动加载:
require_once('vendor/autoload.php');
或者手动:
require_once('/path/to/library/autoload.php');
手动安装
使用以下命令获取 Paddle 库的最新版本:
git clone https://github.com/PaddleHQ/paddle-php
要使用此库,将以下内容添加到您的 PHP 脚本中:
require_once('/path/to/library/autoload.php');
入门指南
要与 Paddle API 交互,您需要创建一个 API 对象,并使用 vendor_id / vendor_api_key 进行授权。
$api = new \Paddle\Api(); $api->authorize_vendor($vendor_id, $vendor_auth_code);
可选地,您可以在创建新的 API 对象时设置 HTTP 超时(默认为 30 秒)。
$api->set_timeout(60);
在创建新的 API 对象时,也可以设置授权和超时。
$api = new \Paddle\Api($vendor_id, $vendor_auth_code, 60);
generate_license() 方法的示例用法
// define $vendor_id and $vendor_auth_code first $api = new \Paddle\Api($vendor_id, $vendor_auth_code, 60); $product_id = 100; $license_code = $api->generate_license($product_id);
常见异常
所有方法都可以抛出以下类型的 \Exception 异常:
-
1XX - API 响应错误
-
code: 100 message: '无法找到请求的许可证'
-
code: 101 message: '方法调用错误'
-
code: 102 message: 'API密钥错误'
-
code: 103 message: '时间戳太旧或无效'
-
code: 104 message: '许可证代码已被使用'
-
code: 105 message: '许可证代码未激活'
-
code: 106 message: '无法找到请求的激活'
-
code: 107 message: '您没有权限访问此资源'
-
code: 108 message: '无法找到请求的产品'
-
code: 109 message: '提供的货币无效'
-
code: 110 message: '无法找到请求的购买'
-
code: 111 message: '无效的认证令牌'
-
code: 112 message: '无效的验证令牌'
-
code: 113 message: '解密字符串的填充无效'
-
code: 114 message: '无效或重复的联盟'
-
code: 115 message: '无效或缺失的联盟佣金'
-
code: 116 message: '缺失一个或多个必填参数'
-
code: 117 message: '提供的过期时间不正确'
-
2XX - 一般错误
-
code: 200 message: 'CURL错误'
-
code: 201 message: '不正确的HTTP响应代码'
-
code: 202 message: '不正确的API响应'
-
code: 203 message: '超时必须是一个正整数'
-
code: 204 message: '未提供供应商凭证'
可用方法
生成普通产品的支付链接
string generate_product_pay_link (int $product_id, [array $optional_arguments = array()])
参数
- int $product_id - 产品的ID
- array $optional_arguments - 可选参数的关联数组
- string 'title' - 覆盖产品标题
- string 'image_url' - 覆盖产品图片
- float 'price' - 覆盖产品价格
- string 'return_url' - 交易完成后要重定向的URL
- bool 'discountable' - 是否可以应用优惠券进行结账
- string 'coupon_code' - 折扣优惠券代码
- bool 'locker_visible' - 产品是否可见在用户的存衣柜中
- bool 'quantity_variable' - 产品数量是否可以由用户更改
- string 'paypal_cancel_url' - PayPal 交易取消时重定向的URL
- int 'expires' - 结账过期日期,时间戳
- bool 'is_popup' - 是否以弹出窗口显示结账
- string 'parent_url' - 点击结账弹出窗口上的关闭按钮时要重定向的URL
- 数组 'affiliates' - 每个元素应包含 affiliate_id 作为键,affiliate_commission 作为值。
- 佣金值应为浮点数,因此佣金 '0.1' 等于 10%。
- 数组 'stylesheets' - 每个元素应包含 stylesheet type 作为键,code 作为值
类型为 \InvalidArgumentException 的验证异常
- code: 300 message: '$product_id 必须是正整数'
- code: 301 message: '$title 必须是字符串'
- code: 302 message: '$image_url 必须是有效的 URL'
- code: 303 message: '$price 必须是数字'
- code: 304 message: '$price 不能为负数'
- code: 305 message: '$return_url 必须是有效的 URL'
- code: 306 message: '$paypal_cancel_url 必须是有效的 URL'
- code: 307 message: '$expires 必须是有效的时间戳'
- code: 308 message: '$expires 必须在将来'
- code: 309 message: '$parent_url 必须是有效的 URL'
- code: 310 message: '$affiliates 必须是数组'
- code: 311 message: '提供 $affiliates 作为键->值包含的数组,格式为 vendor_id->vendor_commission'
- code: 312 message: '$stylesheets 必须是数组'
- code: 313 message: '提供 $stylesheets 作为键->值包含的数组,格式为 stylesheet_type->stylesheet_code'
- code: 314 message: '$webhook_url 只能用于自定义产品'
为自定义(不在 Paddle 数据库中存在)的产品生成支付链接
string generate_custom_product_pay_link (string $title, float $price, string $image_url, string $webhook_url, array $optional_arguments)
参数
- string $title - 自定义产品的标题
- float $price - 自定义产品的价格
- string $image_url - 自定义产品的图片
- string $webhook_url - 自定义产品的 webhook_url
- array $optional_arguments - 可选参数的关联数组
- string 'return_url' - 交易完成后要重定向的URL
- bool 'locker_visible' - 产品是否可见在用户的存衣柜中
- bool 'quantity_variable' - 产品数量是否可以由用户更改
- string 'paypal_cancel_url' - PayPal 交易取消时重定向的URL
- int 'expires' - 结账过期日期,时间戳
- bool 'is_popup' - 是否以弹出窗口显示结账
- string 'parent_url' - 点击结账弹出窗口上的关闭按钮时要重定向的URL
- 数组 'affiliates' - 每个元素应包含 affiliate_id 作为键,affiliate_commission 作为值。
- 佣金值应为浮点数,因此佣金 '0.1' 等于 10%。
- 数组 'stylesheets' - 每个元素应包含 stylesheet type 作为键,code 作为值
类型为 \InvalidArgumentException 的验证异常
- code: 301 message: '$title 必须是字符串'
- code: 302 message: '$image_url 必须是有效的 URL'
- code: 303 message: '$price 必须是数字'
- code: 304 message: '$price 不能为负数'
- code: 305 message: '$return_url 必须是有效的 URL'
- code: 306 message: '$paypal_cancel_url 必须是有效的 URL'
- code: 307 message: '$expires 必须是有效的时间戳'
- code: 308 message: '$expires 必须在将来'
- code: 309 message: '$parent_url 必须是有效的 URL'
- code: 310 message: '$affiliates 必须是数组'
- code: 311 message: '提供 $affiliates 作为键->值包含的数组,格式为 vendor_id->vendor_commission'
- code: 312 message: '$stylesheets 必须是数组'
- code: 313 message: '提供 $stylesheets 作为键->值包含的数组,格式为 stylesheet_type->stylesheet_code'
- code: 315 message: '$webhook_url 必须是有效的 URL'
- code: 316 message: '$discountable 不允许用于自定义产品'
- code: 317 message: '$coupon_code 不允许用于自定义产品'
- code: 318 message: '$product_id 不允许用于自定义产品'
为框架产品生成许可证代码
string generate_license (int $product_id)
参数
- int $product_id - 产品的ID
类型为 \InvalidArgumentException 的验证异常
- code: 300 message: '$product_id 必须是正整数'
获取包括每个产品详细信息的分页产品列表
array get_products ([int $limit = 1], [int $offset = 0])
参数
- int $limit - 返回的产品数量
- int $offset - 从第一个产品开始的偏移量
返回值
返回的数组包含
- int 'total' - 产品总数
- int 'count' - 返回的产品数量
- array 'products' - 返回的产品,每个产品包含
- int 'id' - 产品 id
- string 'name' - 产品名称
- string 'description' - 产品描述
- float 'base_price' - 产品基础价格
- float 'sale_price' - 产品销售价格
- array 'screenshots' - 产品截图
- string 'icon' - 产品图片
类型为 \InvalidArgumentException 的验证异常
- code: 319 message: '$limit 必须是正整数'
- code: 320 message: '$offset 必须是非负整数'
获取客户详情数组
array generate_customers_report ([int $product_id = null])
参数
- int $product_id - 将创建报告的产品 id,如果未提供,报告将包含所有产品的数据
返回值
返回的数组包含
- string 'full_name' - 客户全名
- string 'email' - 客户电子邮件地址
类型为 \InvalidArgumentException 的验证异常
- code: 300 message: '$product_id 必须是正整数'
获取许可证激活详情数组
激活在发生后的一天报告 - 因此今天发生的任何激活将不会被包括
array generate_license_activations_report ([int $product_id = null], [int $start_timestamp = null], [int $end_timestamp = null])
参数
- int $product_id - 将创建报告的产品 id,如果未提供,报告将包含所有产品的数据
- int $start_timestamp - 报告开始时间
- int $end_timestamp - 报告结束日期
返回值
返回的数组包含
- string 'license_code' - 许可证代码
- string 'activation_date' - 激活日期
- string 'customer_ip' - 客户 IP
- string 'customer_email' - 客户电子邮件
类型为 \InvalidArgumentException 的验证异常
- code: 300 message: '$product_id 必须是正整数'
- code: 321 message: '$start_timestamp 必须是时间戳'
- code: 322 message: '$end_timestamp 必须是时间戳'
获取订单详情数组
array generate_orders_report ([int $product_id = null], [int $start_timestamp = null], [int $end_timestamp = null])
参数
- int $product_id - 将创建报告的产品 id,如果未提供,报告将包含所有产品的数据
- int $start_timestamp - 报告开始时间
- int $end_timestamp - 报告结束日期
返回值
返回的数组包含
- int 'order_id' - 订单 id
- string 'product_name' - 产品名称
- float 'your_earnings' - 你的收入
- 字符串 'earnings_currency' - 收益货币
- 字符串 'sale_date' - 销售日期
类型为 \InvalidArgumentException 的验证异常
- code: 300 message: '$product_id 必须是正整数'
- code: 321 message: '$start_timestamp 必须是时间戳'
- code: 322 message: '$end_timestamp 必须是时间戳'
获取已发送许可证详情的数组
array generate_sent_licenses_report ([int $product_id = null])
参数
- int $product_id - 将创建报告的产品 id,如果未提供,报告将包含所有产品的数据
返回值
返回的数组包含
- 字符串 'customer_name' - 客户的全名
- 字符串 'customer_email' - 客户的电子邮件地址
- string 'product_name' - 产品名称
- string 'license_code' - 许可证代码
类型为 \InvalidArgumentException 的验证异常
- code: 300 message: '$product_id 必须是正整数'
生成用于调用其他API方法的凭证
array generate_auth_code (string $vendor_email, string $vendor_password)
参数
- 字符串 $vendor_email
- 字符串 $vendor_password
返回值
返回的数组包含
- 整型 'vendor_id'
- 字符串 'vendor_auth_code'
类型为 \InvalidArgumentException 的验证异常
- 代码:323 信息:'$vendor_email' 必须是有效的
注册外部应用程序并接收授权码,该应用程序可以使用授权码调用API方法
string register_external_application (string $application_name, string $application_description, string $application_icon_url)
类型为 \InvalidArgumentException 的验证异常
- 代码:324 信息:'$application_icon_url' 必须是有效的URL
获取供应商的公钥
string get_vendor_public_key ()