miracode/stripe-bundle

Symfony 扩展包,用于集成 Stripe PHP SDK。支持使用 Doctrine 将 Stripe 对象保存到数据库。

安装次数: 13,041

依赖者: 0

建议者: 0

安全: 0

星标: 9

关注者: 3

分支: 16

开放问题: 5

类型:symfony-bundle

v1.0.9 2018-02-19 16:55 UTC

README

MiracodeStripeBundle 集成 Stripe PHP SDK 到您的 Symfony 项目中。您还可以配置扩展包以将 Stripe 数据保存到数据库。您可以自由选择要存储哪些 Stripe 对象。

此扩展包已在 Symfony 版本 2.7, 2.8, 3.1, 3.3, 3.4, 4.0 上进行测试。与 Symfony >=2.4 兼容。

Build Status

安装

要安装此扩展包,运行以下命令,您将获得最新稳定版本。

composer require miracode/stripe-bundle

注册扩展包

// app/AppKernel.php
public function registerBundles()
{
  $bundles = array(
    // [...]
    new Miracode\StripeBundle\MiracodeStripeBundle(),
  );
}

对于 Symfony >=3.4

// config/bundles.php
return [
    // [...]
    Miracode\StripeBundle\MiracodeStripeBundle::class => ['all' => true],
];

并设置所需配置

# app/config/config.yml (or config/packages/miracode_stripe.yaml for Symfony >=3.4)
miracode_stripe:
    secret_key: "%stripe_secret_key%"

使用方法

完成最小扩展包配置后,您就可以开始使用 Stripe SDK。

例如创建新的客户

$customer = \Stripe\Customer::create([
    'email' => 'newcustomer@example.com'
]);

Stripe 事件

将扩展包路由配置添加到配置中,以启用 Stripe Webhook 处理器

# app/config/routing.yml (or config/routing.yaml for Symfony >=3.4)
miracode_stripe:
    resource: '@MiracodeStripeBundle/Resources/config/routing.xml'

这将在 URL 为 /stripe/webhook 的路由上注册。您应在 Stripe 控制台中添加此 Webhook 端点。最后,您将能够监听所有 Stripe 事件。

例如,对于 charge.succeeded 事件,Webhook 控制器将触发 stripe.charge.succeeded 事件。

事件订阅者示例

// src/EventListener/StripeSubscriber.php

namespace App\EventListener;

use Miracode\StripeBundle\Event\StripeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class StripeSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            'stripe.charge.succeeded' => 'onChargeSucceededEvent',
        ];
    }

    //[...]
    
    public function onChargeSucceededEvent(StripeEvent $event)
    {
        $stripeEvent = $event->getEvent(); //Stripe event object (instanceof \Stripe\Event)
        $charge = $event->getObjectData(); //Stripe charge object (instanceof \Stripe\Charge)
    }
}

将 Stripe 数据保存到数据库

目前仅提供 Doctrine ORM 驱动程序。

在扩展包中,有抽象实体类和 ORM 映射,用于主要的 Stripe 对象

  • card: Miracode\StripeBundle\Model\AbstractCardModel
  • charge: Miracode\StripeBundle\Model\AbstractChargeModel
  • coupon: Miracode\StripeBundle\Model\AbstractCouponModel
  • customer: Miracode\StripeBundle\Model\AbstractCustomerModel
  • invoice: Miracode\StripeBundle\Model\AbstractInvoiceModel
  • plan: Miracode\StripeBundle\Model\AbstractPlanModel
  • refund: Miracode\StripeBundle\Model\AbstractRefundModel
  • subscription: Miracode\StripeBundle\Model\AbstractSubscriptionModel

使用这些抽象类来创建实体。例如,创建 charge 实体类

// src/Entity/Charge.php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Miracode\StripeBundle\Model\AbstractChargeModel;

/**
 * @ORM\Entity()
 * @ORM\Table(name="stripe_charge")
 */
class Charge extends AbstractChargeModel
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @var int
     */
    protetced $id;
}

您还必须在扩展包配置中指定实体类

# app/config/config.yml (or config/packages/miracode_stripe.yaml for Symfony >=3.4)
miracode_stripe:
    #...
    database:
        model:
            charge:   'App\Entity\Charge'   #To store stripe charges
            card:     'App\Entity\Card'     #To store stripe cards
            customer: 'App\Entity\Customer' #To store stripe customers  
            #...

在扩展包配置中添加实体类后,您可以使用模型管理器服务来存储 Stripe 数据。

例如

//Create new customer
$customer = \Stripe\Customer::create([
    'email' => 'newcustomer@example.com'
]);

//Create new card for customer
$card = $customer->sources->create([
    'source' => [
        'object' => 'card',
        'exp_month' => '12',
        'exp_year' => '2020',
        'number' => '4111111111111111',
        'cvc' => '123'
    ]
]);

//Create payment with customer card
$charge = \Stripe\Charge::create([
    'customer' => $customer->id,
    'amount' => 100,
    'currency' => 'usd',
    'source' => $card->id
]);

//Save stripe objects in database
//You can remove this code and entities will be created automatically by webhooks handler 
//(see Miracode\StripeBundle\EventListener\StripeEventSubscriber)
$this->get('miracode_stripe.model_manager')->save($customer);     //Don't flush changes. Return new Customer entity object.
$this->get('miracode_stripe.model_manager')->save($card);         //Don't flush changes. Return new Card entity object.
$this->get('miracode_stripe.model_manager')->save($charge, true); //FLUSH changes in DB. Return new Charge entity object.

如果您启用了 Webhook 处理(如上所述),则可以省略使用模型管理器服务来保存对象数据。存在事件订阅者,它将自动通过 Stripe 事件保存/更新/删除配置的实体。

注意一些数据可能由 Webhook 处理器删除。如果您想使用安全删除技术,请在您的实体类中实现 Miracode\StripeBundle\Model\SafeDeleteModelInterface 接口。您还可以使用 Miracode\StripeBundle\Model\Traits\SafeDeleteTrait 特性以简化接口实现。

许可证

此扩展包在 MIT 许可证下发布。有关更多信息,请参阅包含的 LICENSE 文件。