2017 © Pedro Peláez
 

library stripe-web-hook

Ease the Stripe' API webhook supports

image

pandawan-technology/stripe-web-hook

Ease the Stripe' API webhook supports

  • Monday, October 2, 2017
  • by xavismeh
  • Repository
  • 1 Watchers
  • 1 Stars
  • 78 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 5 % Grown

The README.md

pandawan-technology/stripe-web-hook

Build Status This library intends to ease the Stripe WebHooks behaviors using the symfony/event-dispatcher library., (*1)

Installation

In order to install the library, you should simple use the following command:, (*2)

$ composer require pandawan-technology/stripe-web-hook

Usage

To have a better overview of how the library works, let's implement the example provided by Stripe on how to use webhooks to send an email for failed payments, (*3)

In our controller, we should have something similar as:, (*4)

        $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();

Now, we need to create our EventSubscriber:, (*5)

<?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())
        );
    }
}

The Versions

02/10 2017

dev-master

9999999-dev

Ease the Stripe' API webhook supports

  Sources   Download

MIT

The Requires

 

The Development Requires

api payment event stripe callback webhook