PHPackages                             erikwang2013/stripe-php-erik - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. erikwang2013/stripe-php-erik

ActiveLibrary[API Development](/categories/api)

erikwang2013/stripe-php-erik
============================

Stripe PHP 库为 PHP 应用提供了便捷的 Stripe API 访问能力。它包含一套预定义的 API 资源类，可以动态地从 API 响应中初始化，兼容多种版本的 Stripe API。开箱即用，已适配以下框架：Laravel、Webman、Hyperf、ThinkPHP。

v1.0.4(3w ago)1167↑79.6%MITPHPPHP &gt;=7.2.0

Since May 13Pushed 3w agoCompare

[ Source](https://github.com/erikwang2013/stripe-php-erik)[ Packagist](https://packagist.org/packages/erikwang2013/stripe-php-erik)[ Docs](https://erik.xyz/)[ RSS](/packages/erikwang2013-stripe-php-erik/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (4)Dependencies (3)Versions (5)Used By (0)

Stripe PHP bindings · Stripe PHP 支付集成库
======================================

[](#stripe-php-bindings--stripe-php-支付集成库)

[![Build Status](https://github.com/erikwang2013/stripe-php-erik/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/erikwang2013/stripe-php-erik/actions?query=branch%3Amaster)[![Latest Stable Version](https://camo.githubusercontent.com/f0dcb524df6e74f429a17024f481e290acb429e652ccef0f9d363bb8f2af546a/68747470733a2f2f706f7365722e707567782e6f72672f6572696b77616e67323031332f7374726970652d7068702d6572696b2f762f737461626c652e737667)](https://packagist.org/packages/erikwang2013/stripe-php-erik)[![Total Downloads](https://camo.githubusercontent.com/ad6aa80085416ae6b5bb46d01b2568086bea10a62eeea245e92bbae3169c12cb/68747470733a2f2f706f7365722e707567782e6f72672f6572696b77616e67323031332f7374726970652d7068702d6572696b2f646f776e6c6f6164732e737667)](https://packagist.org/packages/erikwang2013/stripe-php-erik)[![License](https://camo.githubusercontent.com/2ad084d6d1f53595da1a922cbc46c29943769411ecbafcb3724bce5fafb94563/68747470733a2f2f706f7365722e707567782e6f72672f6572696b77616e67323031332f7374726970652d7068702d6572696b2f6c6963656e73652e737667)](https://packagist.org/packages/erikwang2013/stripe-php-erik)

Tip

Want to chat live with Stripe engineers? Join us on our [Discord server](https://stripe.com/go/discord/php).

The Stripe PHP library provides convenient access to the Stripe API from applications written in the PHP language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the Stripe API.

Out-of-the-box integrations available for: **Laravel**, **Webman**, **Hyperf**, and **ThinkPHP**.

---

Stripe PHP 库为 PHP 应用提供了便捷的 Stripe API 访问能力。它包含一套预定义的 API 资源类，可以动态地从 API 响应中初始化，兼容多种版本的 Stripe API。

开箱即用，已适配以下框架：**Laravel**、**Webman**、**Hyperf**、**ThinkPHP**。

Requirements · 环境要求
-------------------

[](#requirements--环境要求)

PHP 7.2.0 and later.

Note that per our [language version support policy](https://docs.stripe.com/sdks/versioning?lang=php#stripe-sdk-language-version-support-policy), support for PHP 7.2 and 7.3 will be removed soon, so upgrade your runtime if you're able to.

Additional PHP versions will be dropped in future major versions, so upgrade to supported versions if possible.

---

PHP 7.2.0 及以上版本。

根据 [语言版本支持策略](https://docs.stripe.com/sdks/versioning?lang=php#stripe-sdk-language-version-support-policy)，PHP 7.2 和 7.3 的支持将很快被移除，请尽快升级运行环境。

Composer
--------

[](#composer)

You can install the bindings via [Composer](http://getcomposer.org/). Run the following command:

```
composer require erikwang2013/stripe-php-erik
```

To use the bindings, use Composer's [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading):

```
require_once 'vendor/autoload.php';
```

---

通过 [Composer](http://getcomposer.org/) 安装：

```
composer require erikwang2013/stripe-php-erik
```

使用 Composer 的自动加载引入库：

```
require_once 'vendor/autoload.php';
```

Manual Installation · 手动安装
--------------------------

[](#manual-installation--手动安装)

If you do not wish to use Composer, you can download the [latest release](https://github.com/erikwang2013/stripe-php-erik/releases). Then, to use the bindings, include the `init.php` file.

```
require_once '/path/to/stripe-php/init.php';
```

---

如果你不想使用 Composer，可以下载[最新版本](https://github.com/erikwang2013/stripe-php-erik/releases)，然后引入 `init.php` 文件：

```
require_once '/path/to/stripe-php/init.php';
```

Framework Integration · 框架集成
----------------------------

[](#framework-integration--框架集成)

This library provides out-of-the-box integration for the following PHP frameworks.

本库为以下 PHP 框架提供开箱即用的集成支持。

---

### Framework Comparison · 框架对比

[](#framework-comparison--框架对比)

特性 / FeatureLaravelWebmanHyperfThinkPHP**注册方式** RegistrationComposer auto-discovery `extra.laravel``WEBMAN_PLUGIN` 常量标记`ConfigProvider` + `extra.hyperf.config``think\Service` + `extra.think.services`**DI 支持** DI Support✅ 容器单例绑定❌ 无容器✅ 工厂 + 注解注入✅ 容器绑定**Facade** 门面✅ `Erikwang2013\Stripe\Laravel\Facade\Stripe`❌❌❌**配置发布** Config Publish`artisan vendor:publish`自动拷贝到 `config/plugin/stripe/``php bin/hyperf.php vendor:publish`手动复制到 `config/stripe.php`**配置路径** Config Path`config/stripe.php``config/plugin/stripe/app.php``config/autoload/stripe.php``config/stripe.php`**配置访问** Config Access`config('stripe.*')``config('plugin.stripe.*')``config('stripe.*')``config('stripe.*')`**Webhook 示例** Webhook Example✅✅✅✅**运行模式** RuntimePHP-FPM / OctaneWorkerman 常驻进程Swoole 协程PHP-FPM / Swoole**协程安全** Coroutine SafeN/AN/A⚠️ 需配置 SwooleCurl hookN/A### Architecture · 架构说明

[](#architecture--架构说明)

```
src/
├── Laravel/
│   ├── StripeServiceProvider.php   # 服务提供者：注册、配置发布、别名
│   └── Facade/
│       └── Stripe.php              # 门面：静态代理 StripeClient
├── Webman/
│   ├── Install.php                 # 插件安装器：WEBMAN_PLUGIN 标记 + 配置拷贝
│   └── StripeHelper.php            # 辅助类：init() + client()
├── Hyperf/
│   ├── ConfigProvider.php          # 配置提供器：DI 定义 + 配置发布 + boot()
│   └── StripeClientFactory.php     # 工厂：从容器创建 StripeClient
├── ThinkPHP/
│   ├── StripeService.php           # think\Service：容器绑定 + 自动注册
│   └── StripeHelper.php            # 辅助类：init() + client()
config/
└── stripe.php                      # 通用配置模板（含中英文注释）

```

类 / Class框架 / Framework作用 / Purpose`Erikwang2013\Stripe\Laravel\StripeServiceProvider`Laravel注册 `StripeClient` 单例、合并配置、发布 config`Erikwang2013\Stripe\Laravel\Facade\Stripe`LaravelFacade 静态代理，IDE 友好的 `@method` 提示`Erikwang2013\Stripe\Webman\Install`Webman`WEBMAN_PLUGIN` 标记，`install()`/`uninstall()` 配置拷贝`Erikwang2013\Stripe\Webman\StripeHelper`Webman从 `config('plugin.stripe')` 初始化全局静态属性`Erikwang2013\Stripe\Hyperf\ConfigProvider`Hyperf注册 DI 工厂、定义配置发布源和目的地`Erikwang2013\Stripe\Hyperf\StripeClientFactory`Hyperf实现 `__invoke(ContainerInterface)`, 返回 `StripeClient``Erikwang2013\Stripe\ThinkPHP\StripeService`ThinkPHP继承 `think\Service`，`register()` 绑定容器，`boot()` 初始化`Erikwang2013\Stripe\ThinkPHP\StripeHelper`ThinkPHP无框架启动时的后备方案，手动 `init()` + `client()`### Lifecycle · 生命周期

[](#lifecycle--生命周期)

```
composer require
      │
      ├─── Laravel ──→ auto-discovery → ServiceProvider::register() → 容器绑定
      │                                 → ServiceProvider::boot()     → 配置合并
      │
      ├─── Webman  ──→ WEBMAN_PLUGIN 标记 → webman 启动时自动发现
      │             ──→ process.php 中手动 StripeHelper::init()
      │
      ├─── Hyperf  ──→ ConfigProvider::__invoke() → DI 定义注册
      │             ──→ 手动 php bin/hyperf.php vendor:publish
      │             ──→ 应用启动时 ConfigProvider::boot()
      │
      └─── ThinkPHP ──→ extra.think.services → StripeService::register() → 容器绑定
                                                StripeService::boot()     → 初始化

```

---

### Laravel

[](#laravel)

**Auto-discovery** · **自动发现**: The package registers itself automatically via Composer. No manual setup required. 包会自动通过 Composer 注册，无需手动配置。

Publish the configuration file · 发布配置文件：

```
php artisan vendor:publish --tag=stripe-config
```

Set your Stripe credentials in `.env` · 在 `.env` 中配置：

```
STRIPE_API_KEY=sk_test_your_key
STRIPE_WEBHOOK_SECRET=whsec_your_secret
```

**Using Dependency Injection** · **依赖注入方式**：

```
use Erikwang2013\Stripe\StripeClient;

class PaymentController
{
    public function charge(StripeClient $stripe)
    {
        $charge = $stripe->charges->create([
            'amount' => 2000,
            'currency' => 'usd',
            'source' => 'tok_visa',
        ]);

        return response()->json($charge);
    }
}
```

**Using the Facade** · **门面方式**：

```
use Erikwang2013\Stripe\Laravel\Facade\Stripe;

$customer = Stripe::customers()->create([
    'email' => 'user@example.com',
]);
```

**Using the helper function** · **辅助函数方式**：

```
$stripe = app('stripe');
$customer = $stripe->customers->create(['email' => 'user@example.com']);
```

**Webhook handling** · **处理 Webhook**：

```
use Erikwang2013\Stripe\Webhook;
use Illuminate\Http\Request;

Route::post('/stripe/webhook', function (Request $request) {
    $signature = $request->header('Stripe-Signature');
    $secret = config('stripe.webhook_secret');

    try {
        $event = Webhook::constructEvent(
            $request->getContent(),
            $signature,
            $secret
        );

        // Handle event... · 处理事件...

        return response('', 200);
    } catch (\Exception $e) {
        return response('', 400);
    }
});
```

---

### Webman

[](#webman)

This package follows **webman basic plugin conventions** (`WEBMAN_PLUGIN`).

本包遵循 **webman 基础插件规范**（`WEBMAN_PLUGIN` 标记）。

Install · 安装：

```
composer require erikwang2013/stripe-php-erik
```

安装时自动将配置拷贝到 `config/plugin/stripe/app.php`，webman 会自动识别并合并该配置。

On install, the config is automatically copied to `config/plugin/stripe/app.php`, which webman auto-detects and merges.

The auto-generated config file · 自动生成的配置文件 `config/plugin/stripe/app.php`：

```
