Skip to content

Commit

Permalink
Simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Feb 19, 2015
1 parent b56f21f commit d2b2209
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
26 changes: 9 additions & 17 deletions src/Service/CardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,22 @@ public function attachToCustomer(CustomerInterface $customer, $card)
'id' => $customer->getStripeId(),
'card' => $card
]);

// Extract the main card from the list of cards
$stripeDefaultCard = [];

foreach ($stripeCustomer['cards']['data'] as $stripeCard) {
if ($stripeCard['id'] === $stripeCustomer['default_card']) {
$stripeDefaultCard = $stripeCard;
break;
}
}
} else {
$stripeCustomer = $this->stripeClient->updateCustomer([
'id' => $customer->getStripeId(),
'source' => $card
]);
}

// Extract the main card from the list of cards
$stripeDefaultCard = [];
// Extract the main card from the list of cards
$stripeDefaultCard = [];

foreach ($stripeCustomer['sources']['data'] as $stripeSource) {
if ($stripeSource['id'] === $stripeCustomer['default_source']) {
$stripeDefaultCard = $stripeSource;
break;
}
// Note that for compatibility purposes, Stripe output the "sources" even on older versions of the API,
// so we do not need to branch here
foreach ($stripeCustomer['sources']['data'] as $stripeCard) {
if ($stripeCard['id'] === $stripeCustomer['default_source']) {
$stripeDefaultCard = $stripeCard;
break;
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/Service/CustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,11 @@ public function create(CustomerInterface $customer, array $options = [])

$customer->setStripeId($stripeCustomer['id']);

$cards = ($apiVersion < '2015-02-18') ? $stripeCustomer['cards']['data'] : $stripeCustomer['sources']['data'];

if (!empty($cards)) {
if (!empty($stripeCustomer['sources']['data'])) {
$card = new Card();
$customer->setCard($card);

$this->populateCardFromStripeResource($card, current($cards));
$this->populateCardFromStripeResource($card, current($stripeCustomer['sources']['data']));
}

if (null !== $stripeCustomer['discount']) {
Expand Down
14 changes: 10 additions & 4 deletions tests/Service/CardServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function testAssertCanAttachCardForOldStripeVersion($hasExistingCard)
$customer->expects($this->any())->method('getStripeId')->willReturn($stripeCustomerId);

$stripeCustomer = [
'cards' => [
'sources' => [
'data' => [
[
'id' => 'card_def',
Expand All @@ -109,10 +109,13 @@ public function testAssertCanAttachCardForOldStripeVersion($hasExistingCard)
]
]
],
'default_card' => 'card_def'
'default_source' => 'card_def'
];

$this->stripeClient->expects($this->once())->method('updateCustomer')->willReturn($stripeCustomer);
$this->stripeClient->expects($this->once())
->method('updateCustomer')
->with(['id' => 'cus_abc', 'card' => 'tok_def'])
->willReturn($stripeCustomer);

$card = $this->cardService->attachToCustomer($customer, 'tok_def');

Expand Down Expand Up @@ -159,7 +162,10 @@ public function testAssertCanAttachCardForNewVersion($hasExistingCard)
'default_source' => 'card_def'
];

$this->stripeClient->expects($this->once())->method('updateCustomer')->willReturn($stripeCustomer);
$this->stripeClient->expects($this->once())
->method('updateCustomer')
->with(['id' => 'cus_abc', 'source' => 'tok_def'])
->willReturn($stripeCustomer);

$card = $this->cardService->attachToCustomer($customer, 'tok_def');

Expand Down

0 comments on commit d2b2209

Please sign in to comment.