pandawan-technology / stripe-web-hook
简化Stripe API WebHooks的支持
dev-master
2017-10-02 16:22 UTC
Requires
- php: ^7.1
- stripe/stripe-php: ^5.2
- symfony/event-dispatcher: ^3.3
- symfony/http-kernel: ^3.3
Requires (Dev)
- phpunit/phpunit: ^6.3
This package is auto-updated.
Last update: 2024-09-20 07:49:17 UTC
README
这个库旨在通过使用
symfony/event-dispatcher
库来简化Stripe WebHooks的行为。
安装
为了安装这个库,您只需使用以下命令
$ composer require pandawan-technology/stripe-web-hook
用法
为了更好地了解库的工作方式,让我们实现Stripe提供的示例,了解如何使用Webhooks 发送失败的支付电子邮件
在我们的控制器中,我们应该有类似以下的内容
$endpointSecret = ''; // See the doc to get the correct value $event = new StripeFactoryEvent($request, Events::INVOICE_PAYMENT_FAILED, $endpointSignature); $eventDispatcher->dispatch(Events::STRIPE_WEBHOOK, $event); if ($exception = $event->getException()) { throw $exception; } return new Response();
现在,我们需要创建我们的EventSubscriber
<?php namespace App\EventSubscriber; use PandawanTechnology\StripeWebHook\Events; use Symfony\Component\EventDispatcher\EventSubscriberInterface; // For demo purpose use App\Repository\CustomerRepository; use SomeMailerInterface; class InvoicePaymentFailedEvent implements EventSubscriberInterface { /** * @return CustomerRepository */ protected $customerRepository; /** * @return SomeMailerInterface */ protected $mailer; public function __construct(CustomerRepository $customerRepository, SomeMailerInterface $mailer) { $this->customerRepository = $customerRepository; $this->mailer = $mailer; } /** * @inheritDoc */ public static function getSubscribedEvents() { return [ Events::INVOICE_PAYMENT_FAILED => 'onInvoicePaymentFailed', ]; } public function onInvoicePaymentFailed(InvoicePaymentFailedEvent $event, string $eventName, EventDispatcherInterface $eventDispatcher) { if (!$customer = $this->customerRepository->find($event->getCustomerId())) { $event->setException(new NotFoundHttpException(sprintf('No customer with Stripe ID "%s"', $event->getCustomerId()))); return; // No need to stop propagation here! } $this->mailer->send( 'myapp@demo.com', // From [$customer->getEmail() => $customer->getName()], // To 'Invoice payment failure', // Subject sprintf('Payment for %d%s failed', $event->getAmountDue(), $event->getCurrencyName()) ); } }