wemakecustom / stripe-bundle
Symfony 2 StripeBundle,Stripe Api 的包装器
v0.3
2015-08-20 18:51 UTC
Requires
- php: >=5.4
- stripe/stripe-php: ^2.0
- symfony/framework-bundle: ^2.3
This package is not auto-updated.
Last update: 2024-09-14 15:52:51 UTC
README
提供一个简单的 Symfony 2 Bundle,用于包装 Stripe PHP SDK - https://github.com/stripe/stripe-php
安装
composer.json
{ "require": { "wemakecustom/stripe-bundle": "dev-master" } }
app/AppKernel.php
public function registerBundles() { $bundles = array( // ... new WMC\StripeBundle\WMCStripeBundle(), ); }
配置
编辑您的 symfony config.yml 文件并添加以下至少几行
wmc_stripe:
api_secret_key: stripe_secret_key
api_publishable_key: stripe_publishable_key
利用
认证
在 Stripe 中,您有两种认证方式:您的服务器和您的客户端。
使用 Stripe 秘密密钥对您的服务器进行认证
$this->container->get('wmc_stripe.stripe')->auth(); // Instead of : Stripe::setApiKey("sk_somekey");
使用 Stripe 公开密钥对您的客户端进行认证
一次性在 twig 中包含所有 js
{% include "WMCStripeBundle::stripe.js.html.twig" %}
如果您想手动进行,您有一个twig扩展可以获取 stripe_publishable_key
<script type="text/javascript">Stripe.setPublishableKey('{{stripe_publishable_key}}');</script>
示例
如果您想进行基本的卡提交,请参阅 https://stripe.com/docs/tutorials/forms https://stripe.com/docs/tutorials/charges#saving-credit-card-details-for-later
客户(卡详情)的基本保存,控制器和视图
public function newPaymentMethodAction(Request $request) { $this->container->get('wmc_stripe.stripe')->auth(); $form = $this->createForm(new CardFormType()); $formHandler = new CardFormHandler($form, $request, $stripeClientDescription); if($formHandler->process()){ //Persist flush the customerId somewhere $stripeCustomerId = $formHandler->getCustomer()->id; } return array('form' => $form->createView()); }
{% extends "::base.html.twig" %} {% block content %} {{ form_start(form) }} {{ form_errors(form) }} {{ form_widget(form) }} <button type="submit">Submit</button> {{ form_end(form) }} {% endblock %} {% block foot_script %} {{ parent() }} {% include "WMCStripeBundle::stripe.js.html.twig" %} {% endblock %}
向客户收费
\Stripe\Charge::create(array( "amount" => round($price * 100), "currency" => "usd", "customer" => $customerId) );
##TODO
- 声明表单类型和处理程序为服务