adsy2010 / laravel-stripe-wrapper
Stripe API包装器
Requires
- php: ^7.2.5|^8.0
- stripe/stripe-php: ^v7.77.0
This package is auto-updated.
Last update: 2021-09-02 20:24:05 UTC
README
Laravel stripe wrapper旨在简化设置凭证并将其传递给Stripe API的过程。
要从Composer安装,请运行以下命令(这是一个alpha版本,尚无自动版本)
composer require adsy2010/laravel-stripe-wrapper:v0.0.2-alpha
将提供者添加到config/app.php
中的服务提供者数组
'providers' => [ ... \Adsy2010\LaravelStripeWrapper\LaravelStripeWrapperServiceProvider::class, ... ],
如果您想使用全部迁移而不发布它们,请将以下服务提供者添加到config/app.php
中的服务提供者数组
'providers' => [ ... \Adsy2010\LaravelStripeWrapper\LaravelStripeWrapperServiceProvider::class, \Adsy2010\LaravelStripeWrapper\LaravelStripeWrapperMigrationServiceProvider::class, ... ],
最后,发布迁移
php artisan vendor:publish --provider=Adsy2010\LaravelStripeWrapper\LaravelStripeWrapperServiceProvider
可选地,您可以跳过发布所有迁移并运行标签来发布所需的迁移
php artisan vendor:publish --tag='credential-migrations' php artisan vendor:publish --tag='customer-migrations' php artisan vendor:publish --tag='product-migrations'
使用方法
凭证
要将API密钥添加到数据库中,您可以运行以下命令
(new StripeCredential) ->store(['key' => 'Public Key', 'value' => 'YOUR_STRIPE_PUBLIC_API_KEY_HERE']) ->includeScopes([StripeScope::PUBLISHABLE], 'w'); (new StripeCredential) ->store(['key' => 'Secret Key', 'value' => 'YOUR_SECRET_OR_RESTRICTED API_KEY']) ->includeScopes([StripeScope::SECRET], 'w') ->includeScopes([StripeScope::PRODUCTS, StripeScope::CHECKOUT_SESSIONS]);
请注意,默认情况下,添加的权限是只读的;如果指定为'w'作为访问类型,则API密钥权限将被分类为可写。
如果您只想使用此包的凭证功能,可以通过以下代码实现
$stripe = StripeCredentialScope::client([StripeScope::PRODUCTS, StripeScope::SECRET], 'w');
此代码将从数据库中检索任何匹配指定权限的API密钥,并从stripe/stripe-php
库中创建一个\Stripe\StripeClient
实例。
产品
要在Stripe上创建产品,请使用store方法
(new StripeProduct)->store(['name'=>'my test product']);
如果您希望在处理Stripe产品时使用本地数据库,请在语句末尾添加true
(new StripeProduct)->store(['name'=>'test product also stored locally'], true);
要从Stripe检索产品,请使用retrieve方法。
(new StripeProduct)->retrieve('prod_123456789'); //add true to update the local database
有一个retrieve all方法,可以一次获取Stripe上的所有产品(可选存储)。第一个参数是过滤参数列表,例如,以下展示了所有活动产品的示例。
(new StripeProduct)->retrieveAll(['active' => 1]);
如果您想存储但不过滤所有记录,应输入一个包含true的空数组
(new StripeProduct)->retrieveAll([], true); //retrieve all with no filtering
要更新Stripe上的产品,请使用change方法。
(new StripeProduct)->change('prod_123456789', ['name'=>'new product name', ...]); //add true to update the local database
要从Stripe删除产品,请使用trash方法。
(new StripeProduct)->trash('prod_123456789'); //add true to update the local database
如果将true添加到retrieve语句的末尾,它将更新来自Stripe的数据库记录
注意:如果您想使用本地Stripe产品表,您应该使用提供的迁移或使用具有相同表名的迁移。所有方法都有一个可选的store属性,如果设置为true,将更新产品的本地数据库版本。
客户
Stripe客户目前对本地存储的详细信息有限制。税号数据和支付方式功能尚未实现。然而,默认支付方式ID已包括在内。
要在Stripe上创建客户,请使用store方法
$customerDetails = [ 'email' => 'test@example.com', 'name' => 'Bob Smith' ]; (new StripeCustomer)->store($customerDetails);
如果您希望在处理Stripe客户时使用本地数据库,请在语句末尾添加true
$customerDetails = [ 'email' => 'test@example.com', 'name' => 'Bob Smith' ]; (new StripeCustomer)->store($customerDetails, true);
要从Stripe检索客户,请使用retrieve方法
(new StripeCustomer)->retrieve('cust_123456789'); //add true to update the local database
有一个retrieve all方法,可以一次获取Stripe上的所有客户(可选存储)。第一个参数是过滤参数列表,例如,以下展示了最近3个客户的检索。
(new StripeCustomer)->retrieveAll(['limit' => 3]); //add true to update the local database
如果您想存储但不过滤所有记录,应输入一个包含true的空数组
(new StripeCustomer)->retrieveAll([], true); //retrieve all with no filtering
要更新Stripe上的客户,请使用change方法
$newcustomerDetails = [ 'email' => 'test2@example.com', 'name' => 'Bobby Smith' ]; (new StripeCustomer)->change('cust_123456789', $newCustomerData); //add true to update the local database
要从Stripe删除客户,请使用trash方法
(new StripeCustomer)->trash('cust_123456789'); //add true to update the local database
注意:如果您想使用本地条纹客户表,您应该使用提供的迁移工具,或者使用具有相同表名的迁移工具。所有方法都有一个可选的store属性,如果将其设置为true,将更新产品的本地数据库版本。
支付
即将推出!