herdwatch / stripe-bundle
用于集成 Stripe PHP SDK 的 Symfony 扩展包。可以使用 Doctrine 在数据库中保存 Stripe 对象。
Requires
- php: ^8.2
- stripe/stripe-php: >=9.0
- symfony/config: ^5.4 || ^6.4
- symfony/framework-bundle: ^5.4 || ^6.4
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8
- doctrine/orm: >=2.2
- phpunit/phpunit: ^10.5
- symfony/yaml: ^5.4 || ^6.4
Suggests
- doctrine/orm: If you want to save stripe data in database
- 3.x-dev
- v3.0.1
- v3.0.0
- 2.x-dev
- v2.1.0
- v2.0.0
- 1.0.x-dev
- dev-master / 1.0.x-dev
- v1.0.9.7
- v1.0.9.6
- 1.0.9.5
- v1.0.9.4
- v1.0.9.3
- v1.0.9.1
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.9.9.2
- v0.9.9.1
- v0.9.9
- v0.9.8.4
- v0.9.8.3
- v0.9.8.2
- v0.9.8.1
- v0.9.8
- v0.9.7.1
- v0.9.7
- v0.9.6
- v0.9.5
- v0.9.4
- v0.9.3.1
- v0.9.3
- v0.9.2
- v0.9.1
- dev-3.x-autowaring
- dev-fndn-4992-add-pipelines-to-check-symfony-versions-2.0
- dev-fndn-4992-add-pipelines-to-check-symfony-versions
This package is auto-updated.
Last update: 2024-08-30 17:51:57 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 兼容。
安装
要安装此扩展包,运行以下命令,您将获取最新稳定版本。
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 文件。