herdwatch/stripe-bundle

用于集成 Stripe PHP SDK 的 Symfony 扩展包。可以使用 Doctrine 在数据库中保存 Stripe 对象。

安装次数: 54,206

依赖关系: 0

建议者: 0

安全性: 0

星级: 0

关注者: 0

分支: 16

类型:symfony-bundle

v3.0.1 2024-05-30 17:22 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 Stripe 事件,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 驱动程序。

在扩展包中,有抽象实体类,用于对主要 stripe 对象进行 ORM 映射

  • 卡片:Miracode\StripeBundle\Model\AbstractCardModel
  • 收费:Miracode\StripeBundle\Model\AbstractChargeModel
  • 优惠券:Miracode\StripeBundle\Model\AbstractCouponModel
  • 客户:Miracode\StripeBundle\Model\AbstractCustomerModel
  • 发票:Miracode\StripeBundle\Model\AbstractInvoiceModel
  • 计划:Miracode\StripeBundle\Model\AbstractPlanModel
  • 退款:Miracode\StripeBundle\Model\AbstractRefundModel
  • 订阅:Miracode\StripeBundle\Model\AbstractSubscriptionModel

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

// 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 文件。