2017 © Pedro Peláez
 

symfony-bundle stripe-bundle

Integrates stripe in Symfony2

image

aimir/stripe-bundle

Integrates stripe in Symfony2

  • Sunday, January 17, 2016
  • by aimir
  • Repository
  • 2 Watchers
  • 0 Stars
  • 90 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 18 Versions
  • 18 % Grown

The README.md

MiracodeStripeBundle

The MiracodeStripeBundle integrates Stripe PHP SDK to your Symfony project. Also you can configure bundle to save Stripe data in database. You are free to choose what Stripe objects will be stored., (*1)

This bundle tested on Symfony versions 2.7, 2.8, 3.1, 3.3, 3.4, 4.0. Compatible with Symfony >=2.4, (*2)

Build Status, (*3)

Installation

To install this bundle, run the command below and you will get the latest stable version., (*4)

``` bash composer require miracode/stripe-bundle, (*5)


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

For Symfony >=3.4, (*6)

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

And set-up required configuration

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

Usage

After minimal bundle configuration you can start using Stripe SDK., (*8)

For example create new customer:, (*9)

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


#### Stripe Events Add bundle routing configuration to enable Stripe webhooks handler ``` yaml # app/config/routing.yml (or config/routing.yaml for Symfony >=3.4) miracode_stripe: resource: '@MiracodeStripeBundle/Resources/config/routing.xml'

This will register route with url /stripe/webhook. You should add this webhook endpoint in Stripe Dashboard. Finally you will be able to listen all Stripe events., (*11)

For example for stripe event charge.succeeded webhook controller will dispatch event stripe.charge.succeeded., (*12)

Event Subscriber example:, (*13)

``` php // src/EventListener/StripeSubscriber.php, (*14)

namespace App\EventListener;, (*15)

use Miracode\StripeBundle\Event\StripeEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface;, (*16)

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

//[...]

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

}, (*18)


#### Saving stripe data in database Now only Doctrine ORM driver is available. In bundle there are abstract entity classes with orm mapping for main stripe objects: - 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` Use this abstract classes to create entities. For example charge entity class: ``` php // 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; }

You must also specify entity classes in the bundle configuration, (*19)

``` yaml, (*20)

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
#..., (*21)


After adding entity classes in bundle configuration you can use model manager service to store stripe data. For example: ``` php //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.

If you enabled webhooks handling (described above), you can omit using model manager service to save objects data. There is event subscriber that will save/update/remove configured entities by stripe events automatically., (*22)

Note some data can be deleted by webhooks handler. If you want to use safe delete technique, implement interface Miracode\StripeBundle\Model\SafeDeleteModelInterface in your entity classes. Also you can use trait Miracode\StripeBundle\Model\Traits\SafeDeleteTrait for easy interface implementation., (*23)

License

This bundle is released under the MIT license. See the included LICENSE file for more information., (*24)

The Versions

17/01 2016

dev-master

9999999-dev

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

17/01 2016

v0.9.9.2

0.9.9.2

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

17/01 2016

v0.9.9.1

0.9.9.1

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

14/01 2016

v0.9.9

0.9.9.0

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

14/01 2016

v0.9.8.4

0.9.8.4

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

14/01 2016

v0.9.8.3

0.9.8.3

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

13/01 2016

v0.9.8.2

0.9.8.2

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

03/12 2015

v0.9.8.1

0.9.8.1

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

03/12 2015

v0.9.8

0.9.8.0

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

03/12 2015

v0.9.7.1

0.9.7.1

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

02/12 2015

v0.9.7

0.9.7.0

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

30/11 2015

v0.9.6

0.9.6.0

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

30/11 2015

v0.9.5

0.9.5.0

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

26/11 2015

v0.9.4

0.9.4.0

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

26/11 2015

v0.9.3.1

0.9.3.1

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

26/11 2015

v0.9.3

0.9.3.0

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

24/11 2015

v0.9.2

0.9.2.0

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe

24/11 2015

v0.9.1

0.9.1.0

Integrates stripe in Symfony2

  Sources   Download

MIT

The Requires

 

by Aleksey Mirovskyi

payment symfony2 stripe