Skip to content

Commit

Permalink
Deal with customer.card.created special webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Dolbeau committed Oct 23, 2019
1 parent 24d46d1 commit ba18244
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/EventBuilder.php
Expand Up @@ -37,7 +37,6 @@ final class EventBuilder
'customer.discount.created' => Event\CustomerDiscountCreatedEvent::class,
'customer.discount.deleted' => Event\CustomerDiscountDeletedEvent::class,
'customer.discount.updated' => Event\CustomerDiscountUpdatedEvent::class,
'customer.source.created' => Event\CustomerSourceCreatedEvent::class,
'customer.source.deleted' => Event\CustomerSourceDeletedEvent::class,
'customer.source.expiring' => Event\CustomerSourceExpiringEvent::class,
'customer.source.updated' => Event\CustomerSourceUpdatedEvent::class,
Expand Down Expand Up @@ -96,6 +95,14 @@ public function createEventFromArray(array $data): Event\Event
return \call_user_func($this->events[$data['type']].'::createFromArray', $data);
}

if ('customer.source.created' === $data['type']) {
if ('card' === $data['data']['object']['object']) {
return Event\CustomerCardCreatedEvent::createFromArray($data);
}

return Event\CustomerSourceCreatedEvent::createFromArray($data);
}

throw new InvalidArgumentException("Unable to process event: Event \"{$data['type']}\" is not supported yet.");
}
}
17 changes: 17 additions & 0 deletions src/Model/Event/ContainsCard.php
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

/*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace Shapin\Stripe\Model\Event;

use Shapin\Stripe\Model\Card\Card;

interface ContainsCard extends Event
{
public function getCard(): Card;
}
22 changes: 22 additions & 0 deletions src/Model/Event/ContainsCardTrait.php
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace Shapin\Stripe\Model\Event;

use Shapin\Stripe\Model\Card\Card;

trait ContainsCardTrait
{
use EventTrait;

public function getCard(): Card
{
return $this->card;
}
}
15 changes: 15 additions & 0 deletions src/Model/Event/CustomerCardCreatedEvent.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

/*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace Shapin\Stripe\Model\Event;

final class CustomerCardCreatedEvent implements ContainsCard
{
use ContainsCardTrait;
}
11 changes: 11 additions & 0 deletions tests/EventBuilderTest.php
Expand Up @@ -181,6 +181,17 @@ public function testInvoiceMarkedAsUncollectibleEvent()
$this->assertSame('invoice.payment_action_required', $event->getType());
}

public function testCustomerCardCreatedEvent()
{
$data = json_decode(file_get_contents(__DIR__.'/fixtures/events/customer.card.created.json'), true);
$this->assertSame(\JSON_ERROR_NONE, json_last_error());

$this->assertSame('customer.source.created', $data['type']);

$event = (new EventBuilder())->createEventFromArray($data);
$this->assertSame('customer.source.created', $event->getType());
}

/**
* @dataProvider unsupportedEvents
*/
Expand Down
41 changes: 41 additions & 0 deletions tests/fixtures/events/customer.card.created.json
@@ -0,0 +1,41 @@
{
"id": "evt_1FWhZZIpafQncvOMFWe2QlHA",
"object": "event",
"api_version": "2019-09-09",
"created": 1571828413,
"data": {
"object": {
"id": "card_1FWhZZIpafQncvOMOIqSU62P",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_G2n9nb3ufiT1iy",
"cvc_check": null,
"dynamic_last4": null,
"exp_month": 10,
"exp_year": 2020,
"fingerprint": "yhdusyvLVRkh3oYc",
"funding": "credit",
"last4": "4242",
"metadata": {
},
"name": null,
"tokenization_method": null
}
},
"livemode": false,
"pending_webhooks": 1,
"request": {
"id": "req_8P1yFTkKtYdH92",
"idempotency_key": null
},
"type": "customer.source.created"
}
8 changes: 4 additions & 4 deletions tests/fixtures/events/customer.source.created.json
@@ -1,12 +1,12 @@
{
"created": 1326853478,
"livemode": false,
"id": "customer.source.created_00000000000000",
"id": "evt_00000000000000",
"type": "customer.source.created",
"object": "event",
"request": null,
"pending_webhooks": 1,
"api_version": "2018-11-08",
"api_version": "2019-09-09",
"data": {
"object": {
"id": "src_00000000000000",
Expand All @@ -19,8 +19,8 @@
"swift_code": "TSTEZ122"
},
"amount": null,
"client_secret": "src_client_secret_ETT9dPgHcXfUbKe0VE5YIUDj",
"created": 1549381912,
"client_secret": "src_client_secret_G2n6cnHECHjxABPeS45kLo7q",
"created": 1571828189,
"currency": "eur",
"flow": "receiver",
"livemode": false,
Expand Down

0 comments on commit ba18244

Please sign in to comment.