Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix card source expiry date update #393

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/StripeObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static function init()
'address',
'address_kana',
'address_kanji',
'card',
Copy link
Contributor

@arcanedev-maroc arcanedev-maroc Nov 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR is a breaking change because it replace Stripe\Card to Stripe\AttachedObject object.

Skipped: https://github.com/stripe/stripe-php/blob/master/lib/Util/Util.php#L81

Try to var_dump($source->card) to see the result.

cc: @ob-stripe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The card hash under source objects does not have an "object" => "card" entry (because it's not a card object), so convertToStripeObject() would not convert those hashes to \Stripe\Card objects but just to a generic \Stripe\Object.

You're correct that this patch did cause the type to be changed from \Stripe\Object to \Stripe\AttachedObject, which might be seen as a breaking change, but I have a hard time thinking of a practical case where that would break anything. What do you think?

Copy link
Contributor

@arcanedev-maroc arcanedev-maroc Nov 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to add this test in TokenTest class to see the issue:

/** @test */
public function it_can_create_card_token()
{
    $token = self::createCardToken();

    $this->assertInstanceOf(Token::class, $token);
    $this->assertSame('card', $token->type);

    $this->assertInstanceOf(Card::class, $token->card);
    $this->assertSame('Visa', $token->card->brand);
}

/**
  * Create a card Token for tests
  *
  * @return Token
  */
private static function createCardToken()
{
    return Token::create([
        'card' => [
            'number'    => '4242424242424242',
            'exp_month' => date('n'),
            'exp_year'  => date('Y') + 1,
            'cvc'       => '314',
        ]
    ]);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, indeed, the card hash nested in token objects is an actual card object, and this patch does cause the regression you described.

I'll try to push a fix shortly.

Copy link
Contributor

@arcanedev-maroc arcanedev-maroc Nov 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try to remove/revert only this line and see if the tests passes ?

If it passes, you can close #396 without merging

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make your new test pass, but cause the test I added in this PR to fail (i.e. it would make it impossible to update expiry dates on card sources).

'dob',
'inventory',
'legal_entity',
Expand Down
38 changes: 38 additions & 0 deletions tests/SourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,44 @@ public function testSaveOwner()
$this->assertSame($source->owner['address']['country'], "Test Country");
}


public function testSaveCardExpiryDate()
{
$response = array(
'id' => 'src_foo',
'object' => 'source',
'card' => array(
'exp_month' => 8,
'exp_year' => 2019,
),
);
$source = Source::constructFrom(
$response,
new Util\RequestOptions()
);

$response['card']['exp_month'] = 12;
$response['card']['exp_year'] = 2022;
$this->mockRequest(
'POST',
'/v1/sources/src_foo',
array(
'card' => array(
'exp_month' => 12,
'exp_year' => 2022,
)
),
$response
);

$source->card->exp_month = 12;
$source->card->exp_year = 2022;
$source->save();

$this->assertSame(12, $source->card->exp_month);
$this->assertSame(2022, $source->card->exp_year);
}

public function testDetachAttached()
{
$response = array(
Expand Down