chronon / stripe
CakePHP 2.x Stripe支付处理组件。
Requires
- php: >=5.3.0
- ext-curl: *
- composer/installers: *
- stripe/stripe-php: 1.18.*
README
注意: 此插件仅适用于CakePHP 2,且不会更新以兼容CakePHP 3。对于CakePHP 3,请考虑查看Omnipay。有关如何在CakePHP 3中使用它的详细介绍,请参阅Jose的帖子。
这是一个简单的组件,它将CakePHP应用程序与Stripe的PHP API库接口连接起来。将包含至少金额和Stripe令牌ID的数组传递给组件,它将尝试进行收费,并返回您想要的字段数组。
版本2增加了创建和检索客户的功能,可选择为他们订阅定期付款计划或仅对他们进行收费。
兼容性
与CakePHP 2.2.x及更高版本进行了测试,但请注意,它不兼容CakePHP 3.x。所需的Stripe PHP API库需要PHP 5以及cURL支持,且版本必须为1.18.0或以下。此插件现在无需修改即可与版本2.0.0及以上版本一起工作。
安装
在项目的composer.json
文件中
{ "require": { "chronon/stripe": "~2.0" }, "config": { "vendor-dir": "Vendor" } }
这将把插件安装到Plugin/Stripe
中,并将Stripe库(从Packagist获取)安装到您的Vendor
目录中。
在应用的Config/bootstrap.php
中,导入composer的自动加载文件
<?php App::import('Vendor', array('file' => 'autoload'));
使用git
强烈建议使用git安装而不是Composer安装。
您需要组件(作为插件打包),以及Stripe的PHP库。Stripe库需要位于此插件的Vendor目录中,并且必须命名为'Stripe'。使用git,您可以添加此插件和所需的Stripe库(作为git子模块)。从您的APP根目录(您可以看到Model、Controller、Plugin等目录)运行
git clone --recursive git@github.com:chronon/CakePHP-StripeComponent-Plugin.git Plugin/Stripe
或者
git clone --recursive https://github.com/chronon/CakePHP-StripeComponent-Plugin.git Plugin/Stripe
配置
所有配置都在APP/Config/bootstrap.php中。
必需:加载插件
<?php CakePlugin::load('Stripe');
或加载所有插件
<?php CakePlugin::loadAll();
必需:设置Stripe的密钥API(测试和实时)
<?php Configure::write('Stripe.TestSecret', 'yourStripeTestingAPIKeyHere'); Configure::write('Stripe.LiveSecret', 'yourStripeLiveAPIKeyHere');
可选:设置Stripe模式,可以是“实时”或“测试”。如果未设置,则默认为“测试”。
<?php Configure::write('Stripe.mode', 'Test');
可选:设置货币。默认为“usd”。目前Stripe仅支持usd。
<?php Configure::write('Stripe.currency', 'usd');
可选:将组件返回的字段映射到=> Stripe收费对象响应字段。默认为'stripe_id' => 'id'
。有关可用字段,请参阅Stripe API文档中的Stripe_Charge::create()。例如
<?php Configure::write('Stripe.fields', array( 'stripe_id' => 'id', 'stripe_last4' => array('card' => 'last4'), 'stripe_address_zip_check' => array('card' => 'address_zip_check'), 'stripe_cvc_check' => array('card' => 'cvc_check'), 'stripe_amount' => 'amount' ));
如果Stripe.fields
令人困惑,请参阅下面的用法。
可选:添加日志配置
<?php CakeLog::config('stripe', array( 'engine' => 'FileLog', 'types' => array('info', 'error'), 'scopes' => array('stripe'), 'file' => 'stripe', ));
创建收费
按您喜欢的任何方式创建支付表单,有关示例代码,请参阅Stripe文档或使用Stripe出色的结账按钮。将组件添加到您的控制器中
<?php public $components = array( 'Stripe.Stripe' );
格式化您的表单数据,以便发送包含至少金额、Stripe令牌(键为stripeToken
)或Stripe客户ID(键为stripeCustomer
)的数组的组件。
<?php $data = array( 'amount' => '7.59', 'stripeToken' => 'tok_0NAEASV7h0m7ny', // either the token 'stripeCustomer' => 'cus_2x62nI9WxHsL37' // or the customer id, not both. );
可选地,您可以包含一个description
键(默认为null)
一个任意的字符串,您可以将它附加到费用对象上。当在Web界面中与费用一起显示时,它会显示。通常使用电子邮件地址作为描述以便稍后跟踪是个好主意。
可选地,您可以包含一个设置为true或false的capture
键(默认为true)
是否立即扣费。当为false时,费用会发出授权(或预授权),稍后需要扣费。未扣费的费用在7天后失效。
例如
<?php $data = array( 'amount' => '7.59', 'stripeToken' => 'tok_0NAEASV7h0m7ny', 'description' => 'Casi Robot - casi@robot.com' ); $result = $this->Stripe->charge($data);
如果费用成功,$result
将是一个如Stripe.fields
配置值所描述的数组。如果未设置Stripe.fields
<?php $result = array( 'stripe_id' => 'ch_0NXLLCydWzSIeE' );
如果设置了Stripe.fields
,按照配置部分上述示例,您将得到
<?php $result = array( 'stripe_id' => 'ch_0NXLLCydWzSIeE', 'stripe_last4' => '4242', 'stripe_address_zip_check' => 'pass', 'stripe_cvc_check' => 'pass', 'stripe_amount' => 769 );
如果费用不成功,$result
将是一个包含错误消息的字符串,并记录错误。
创建客户
创建带有卡片绑定的客户可用于定期账单/订阅,或者可以立即扣费。
<?php $data = array( 'stripeToken' => 'tok_0NAEASV7h0m7ny', 'description' => 'Casi Robot - casi@robot.com' ); $result = $this->Stripe->customerCreate($data);
如果创建客户成功,$result
将是一个如Stripe.fields
配置值所描述的数组。如果未设置Stripe.fields
<?php $result = array( 'stripe_id' => 'cus_2x62nI9WxHsL37' );
如果创建客户失败,$result
将是一个包含错误消息的字符串,并记录错误。
您可以通过Stripe的API创建客户所描述的任何有效键/数据来传递customerCreate()
方法。有关键列表,请参阅API参考。客户可以不绑定卡片创建,但显然在绑定卡片之前不能扣费或订阅。
示例:要一步创建客户并将他们订阅到计划,您可以这样做
<?php $data = array( 'stripeToken' => 'tok_0NAEASV7h0m7ny', 'description' => 'Casi Robot', 'email' => 'casi@robot.com', 'plan' => 'Silver Plan Deluxe' ); $result = $this->Stripe->customerCreate($data);
检索客户
一旦创建了客户,您就可以通过客户ID轻松检索客户对象。
<?php $customer = $this->Stripe->customerRetrieve('cus_2x62nI9WxHsL37');
一旦您有了$customer
对象,您就可以根据需要更新和删除。例如,要更改现有客户的电子邮件地址
<?php $customer = $this->Stripe->customerRetrieve('cus_2x62nI9WxHsL37'); $customer->email = 'new@address.com'; $customer->save();
检索并扣费客户
<?php $customer = $this->Stripe->customerRetrieve('cus_2x62nI9WxHsL37'); $chargeData = array( 'amount' => '14.69', 'stripeCustomer' => $customer['stripe_id'] ); $charge = $this->Stripe->charge($chargeData);
使用令牌检索并更新客户的卡
<?php $customer = $this->Stripe->customerRetrieve('cus_2x62nI9WxHsL37'); $customer->card = $this->request->data['stripeToken']; $customer->save();
贡献者
@louisroy, @PhantomWatson