paddlehq/paddle-php-sdk 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

paddlehq/paddle-php-sdk

Composer 安装命令:

composer require paddlehq/paddle-php-sdk

包简介

Paddle's PHP SDK for Paddle Billing.

README 文档

README

Build Status Latest Stable Version Total Downloads License

Paddle Billing is the developer-first merchant of record, designed for modern SaaS, AI, mobile app, and digital product businesses. We take care of payments, tax, subscriptions, and metrics with one unified API that does it all.

This is a PHP SDK that you can use to integrate Paddle Billing with applications written in PHP.

For working with Paddle in your frontend, use Paddle.js. You can open checkouts, securely collect payment information, build pricing pages, and integrate with Paddle Retain.

Important: This package works with Paddle Billing. It does not support Paddle Classic. To work with Paddle Classic, see: Paddle Classic dev docs

Requirements

PHP 8.1 and later.

Composer

You can install the bindings via Composer. Run the following command:

composer require paddlehq/paddle-php-sdk

To use the bindings, use Composer's autoload:

require_once 'vendor/autoload.php';

Usage

To authenticate, you'll need an API key. You can create and manage API keys in Paddle > Developer tools > Authentication.

Pass your API key while initializing a new Paddle client.

use Paddle\SDK\Client;

$paddle = new Client('API_KEY');

You can also pass an environment to work with the sandbox:

use Paddle\SDK\Client;
use Paddle\SDK\Environment;
use Paddle\SDK\Options;

$paddle = new Client(
    apiKey: 'API_KEY',
    options: new Options(Environment::SANDBOX),
);

Keep in mind that API keys are separate for your sandbox and live accounts, so you'll need to generate keys for each environment.

Examples

List entities

You can list supported entities with the list function in the resource. It returns an iterator to help when working with multiple pages.

use Paddle\SDK\Client;

$paddle = new Client('API_KEY');

$products = $paddle->products->list();

// List returns an iterable, so pagination is handled automatically.
foreach ($products as $product) {
    echo $product->id;
}

Create an entity

You can create a supported entity with the create function in the resource. It accepts the resource's corresponding Create operation e.g. CreateProduct. The created entity is returned.

use Paddle\SDK\Client;
use Paddle\SDK\Entities\Shared\TaxCategory;
use Paddle\SDK\Resources\Products\Operations\CreateProduct;

$paddle = new Client('API_KEY');

$product = $paddle->products->create(
    new CreateProduct(
        name: 'ChatApp Education',
        taxCategory: TaxCategory::Standard(),
    ),
);

Update an entity

You can update a supported entity with the update function in the resource. It accepts the id of the entity to update and the corresponding Update operation e.g. UpdateProduct. The updated entity is returned.

use Paddle\SDK\Client;
use Paddle\SDK\Resources\Products\Operations\UpdateProduct;

$paddle = new Client('API_KEY');

$operation = new UpdateProduct(
    name: 'ChatApp Professional'
);

$product = $paddle->products->update('id', $operation);

Where operations require more than one id, the update function accepts multiple arguments. For example, to update an address for a customer, pass the customerId and the addressId:

$address = $paddle->addresses->update(
    'customer_id',
    'address_id',
    $operation,
);

Get an entity

You can get an entity with the get function in the resource. It accepts the id of the entity to get. The entity is returned.

use Paddle\SDK\Client;

$paddle = new Client('API_KEY');

$product = $paddle->products->get('id');

Error Handling

If a request fails, Paddle throws an ApiError exception that contains the same information as errors returned by the API. You can use the errorCode attribute to search an error in the error reference and to handle the error in your app. Validation errors also return an array of errors that tell you which fields failed validation. The retryAfter property will be set for too_many_requests errors.

This example shows how to handle an error with the code conflict:

use Paddle\SDK\Exceptions\ApiError;

try {
    // Call functions from the SDK
} catch (ApiError $e) {
    // $e->errorCode will always follow the error code defined in our documentation
    if ($e->errorCode === 'conflict') {
        // Handle Conflict error
    }
}

Resources

Webhook signature verification

The SDK includes a helper class to verify webhook signatures sent by Notifications from Paddle.

use Paddle\SDK\Notifications\Secret;
use Paddle\SDK\Notifications\Verifier;

(new Verifier())->verify(
    $request,
    new Secret('WEBHOOK_SECRET_KEY')
);

Learn more

paddlehq/paddle-php-sdk 适用场景与选型建议

paddlehq/paddle-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 345.26k 次下载、GitHub Stars 达 55, 最近一次更新时间为 2024 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「sdk」 「paddle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 paddlehq/paddle-php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 paddlehq/paddle-php-sdk 我们能提供哪些服务?
定制开发 / 二次开发

基于 paddlehq/paddle-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 345.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 56
  • 点击次数: 27
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 55
  • Watchers: 12
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2024-01-11