承接 einenlum/creem-php-sdk 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

einenlum/creem-php-sdk

Composer 安装命令:

composer require einenlum/creem-php-sdk

包简介

PHP SDK for the Creem merchant of records API

README 文档

README

A PHP SDK for the Creem merchant of records API. This library provides a clean, type-safe interface for interacting with all Creem API endpoints and handling webhooks.

Requirements

  • PHP 8.1 or higher
  • A PSR-18 HTTP client (e.g., Guzzle)
  • A PSR-17 HTTP factory (e.g., Nyholm PSR-7 or Guzzle PSR-7)

Installation

composer require einenlum/creem-php-sdk

If you don't have an HTTP client installed, you can install Guzzle:

composer require guzzlehttp/guzzle

Quick Start

use Einenlum\CreemPhpSdk\Creem;

// Create a client (auto-discovers PSR-18/17 implementations)
$creem = Creem::create(
    apiKey: 'creem_xxxxx',
    testMode: true // Use test API environment
);

// Get a product
$product = $creem->products->get('prod_xxxxx');
echo $product->name;

// Create a checkout session
$checkout = $creem->checkouts->create(
    productId: 'prod_xxxxx',
    successUrl: 'https://example.com/success'
);
echo $checkout->checkoutUrl;

Configuration

Basic Configuration

use Einenlum\CreemPhpSdk\Creem;

$creem = Creem::create(
    apiKey: 'creem_xxxxx',
    webhookSecret: 'whsec_xxxxx', // Optional, for webhook handling
    testMode: false               // false = production, true = test environment
);

Custom HTTP Client

If you need to customize the HTTP client (e.g., for timeouts or proxies):

use Einenlum\CreemPhpSdk\Creem;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;

$httpClient = new Client([
    'timeout' => 30,
    'proxy' => 'http://proxy.example.com:8080'
]);
$factory = new HttpFactory();

$creem = Creem::create(
    apiKey: 'creem_xxxxx',
    httpClient: $httpClient,
    requestFactory: $factory,
    streamFactory: $factory
);

API Resources

Products

// Get a product by ID
$product = $creem->products->get('prod_xxxxx');

// Create a product
$product = $creem->products->create([
    'name' => 'My Product',
    'price' => 1999, // Price in cents
    'currency' => 'USD'
]);

// Search products with pagination
$result = $creem->products->search(limit: 10, page: 1);
foreach ($result->items as $product) {
    echo $product->name;
}

Checkouts

// Create a checkout session
$checkout = $creem->checkouts->create(
    productId: 'prod_xxxxx',
    successUrl: 'https://example.com/success',
    customer: [
        'email' => 'customer@example.com',
        'name' => 'John Doe'
    ],
    metadata: [
        'order_id' => '12345'
    ]
);

// Redirect user to checkout
header('Location: ' . $checkout->checkoutUrl);

// Get checkout details
$checkout = $creem->checkouts->get('ch_xxxxx');

Customers

// Get a customer by ID or email
$customer = $creem->customers->get(customerId: 'cus_xxxxx');
$customer = $creem->customers->get(email: 'customer@example.com');

// List customers
$result = $creem->customers->list(limit: 20, page: 1);

// Create a billing portal session
$portal = $creem->customers->createBillingPortal('cus_xxxxx');
echo $portal->url;

Subscriptions

// Get subscription details
$subscription = $creem->subscriptions->get('sub_xxxxx');
echo $subscription->status->value; // 'active', 'canceled', etc.

// Update subscription units
$subscription = $creem->subscriptions->updateUnits('sub_xxxxx', units: 5);

// Upgrade to a different product
$subscription = $creem->subscriptions->upgrade('sub_xxxxx', newProductId: 'prod_yyyyy');

// Cancel subscription
$subscription = $creem->subscriptions->cancel('sub_xxxxx');

Discounts

// Create a discount
$discount = $creem->discounts->create([
    'code' => 'SUMMER20',
    'type' => 'percentage',
    'amount' => 20,
    'duration' => 'forever'
]);

// Get discount by ID or code
$discount = $creem->discounts->get(discountId: 'disc_xxxxx');
$discount = $creem->discounts->get(code: 'SUMMER20');

// Delete a discount
$creem->discounts->delete('disc_xxxxx');

Licenses

// Activate a license
$license = $creem->licenses->activate(
    key: 'XXXX-XXXX-XXXX-XXXX',
    instanceName: 'My Computer'
);
echo $license->instanceId;

// Validate a license
$license = $creem->licenses->validate(
    key: 'XXXX-XXXX-XXXX-XXXX',
    instanceId: 'inst_xxxxx'
);

// Deactivate a license
$license = $creem->licenses->deactivate(
    key: 'XXXX-XXXX-XXXX-XXXX',
    instanceId: 'inst_xxxxx'
);

Transactions

// Search transactions
$result = $creem->transactions->search(
    customerId: 'cus_xxxxx',
    limit: 50,
    page: 1
);

// Search by subscription
$result = $creem->transactions->search(
    subscriptionId: 'sub_xxxxx'
);

Webhook Handling

The SDK provides a WebhookHandler class for processing incoming webhooks with signature verification.

Basic Webhook Setup

use Einenlum\CreemPhpSdk\Webhook\WebhookHandler;
use Einenlum\CreemPhpSdk\Exception\WebhookException;

$handler = new WebhookHandler('whsec_xxxxx');

// Register event handlers
$handler
    ->onCheckoutCompleted(function ($event) {
        $checkout = $event->object;
        // Grant access to the customer
    })
    ->onSubscriptionActive(function ($event) {
        $subscription = $event->object;
        // Activate subscription
    })
    ->onSubscriptionCanceled(function ($event) {
        $subscription = $event->object;
        // Handle cancellation
    });

// Process webhook in your endpoint
try {
    $payload = file_get_contents('php://input');
    $signature = $_SERVER['HTTP_CREEM_SIGNATURE'] ?? '';

    $event = $handler->handle($payload, $signature);

    http_response_code(200);
    echo json_encode(['received' => true]);
} catch (WebhookException $e) {
    http_response_code(400);
    echo json_encode(['error' => $e->getMessage()]);
}

Available Webhook Events

Event Handler Method Description
checkout.completed onCheckoutCompleted() Checkout was completed
subscription.active onSubscriptionActive() Subscription is now active
subscription.paid onSubscriptionPaid() Subscription payment received
subscription.canceled onSubscriptionCanceled() Subscription was canceled
subscription.expired onSubscriptionExpired() Subscription has expired
subscription.trialing onSubscriptionTrialing() Subscription is in trial
subscription.paused onSubscriptionPaused() Subscription was paused
subscription.update onSubscriptionUpdate() Subscription was updated
refund.created onRefundCreated() Refund was created
dispute.created onDisputeCreated() Dispute was created

Generic Event Handler

use Einenlum\CreemPhpSdk\Enum\WebhookEvent;

$handler->on(WebhookEvent::CheckoutCompleted, function ($event) {
    // Handle event
});

Error Handling

The SDK throws specific exceptions for different error types:

use Einenlum\CreemPhpSdk\Exception\AuthenticationException;
use Einenlum\CreemPhpSdk\Exception\ValidationException;
use Einenlum\CreemPhpSdk\Exception\NotFoundException;
use Einenlum\CreemPhpSdk\Exception\RateLimitException;
use Einenlum\CreemPhpSdk\Exception\ApiException;
use Einenlum\CreemPhpSdk\Exception\CreemException;

try {
    $product = $creem->products->get('prod_xxxxx');
} catch (AuthenticationException $e) {
    // Invalid API key (401/403)
} catch (ValidationException $e) {
    // Invalid request data (400)
} catch (NotFoundException $e) {
    // Resource not found (404)
} catch (RateLimitException $e) {
    // Too many requests (429)
} catch (ApiException $e) {
    // Server error (500+)
} catch (CreemException $e) {
    // Other errors
}

All exceptions include response details:

try {
    $product = $creem->products->get('invalid_id');
} catch (CreemException $e) {
    echo $e->getMessage();
    print_r($e->getResponseBody());
    print_r($e->getResponseHeaders());
}

Enums

The SDK uses PHP enums for type-safe values:

use Einenlum\CreemPhpSdk\Enum\SubscriptionStatus;
use Einenlum\CreemPhpSdk\Enum\LicenseStatus;
use Einenlum\CreemPhpSdk\Enum\DiscountType;
use Einenlum\CreemPhpSdk\Enum\DiscountDuration;

// Check subscription status
if ($subscription->status === SubscriptionStatus::Active) {
    // Subscription is active
}

// All enum values
SubscriptionStatus::Active;
SubscriptionStatus::Canceled;
SubscriptionStatus::Expired;
SubscriptionStatus::Trialing;
SubscriptionStatus::Paused;

Testing

For testing your integration, use the test mode:

$creem = Creem::create(
    apiKey: 'creem_test_xxxxx',
    testMode: true
);

This will route all API calls to https://test-api.creem.io.

License

MIT License

einenlum/creem-php-sdk 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-10