Skip to content

Commit

Permalink
detach method for detaching sources from customers
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Oct 11, 2017
1 parent 9e8d565 commit 8a01a90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
24 changes: 18 additions & 6 deletions lib/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public function save($opts = null)

/**
* @param array|null $params
* @param array|string|null $opts
* @param array|string|null $options
*
* @return Source The deleted source.
* @return Source The detached source.
*/
public function delete($params = null, $options = null)
public function detach($params = null, $options = null)
{
self::_validateParams($params);

Expand All @@ -93,13 +93,25 @@ public function delete($params = null, $options = null)
$this->refreshFrom($response, $opts);
return $this;
} else {
$message = "Source objects cannot be deleted, they can only be "
. "detached from customer objects. This source object does not "
. "appear to be currently attached to a customer object.";
$message = "This source object does not appear to be currently attached "
. "to a customer object.";
throw new Error\Api($message);
}
}

/**
* @param array|null $params
* @param array|string|null $options
*
* @return Source The detached source.
*
* @deprecated Use the `detach` method instead.
*/
public function delete($params = null, $options = null)
{
$this->detach($params, $options);
}

/**
* @param array|null $params
* @param array|string|null $options
Expand Down
26 changes: 10 additions & 16 deletions tests/SourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,16 @@ public function testSaveOwner()
$this->assertSame($source->owner['address']['country'], "Test Country");
}

public function testDeleteAttached()
public function testDetachAttached()
{
$response = array(
'id' => 'src_foo',
'object' => 'source',
'customer' => 'cus_bar',
);
$this->mockRequest(
'GET',
'/v1/sources/src_foo',
array(),
$response
$source = Source::constructFrom(
$response,
new Util\RequestOptions()
);

unset($response['customer']);
Expand All @@ -159,29 +157,25 @@ public function testDeleteAttached()
$response
);

$source = Source::retrieve('src_foo');
$source->delete();
$source->detach();
$this->assertFalse(array_key_exists('customer', $source));
}

/**
* @expectedException Stripe\Error\Api
*/
public function testDeleteUnattached()
public function testDetachUnattached()
{
$response = array(
'id' => 'src_foo',
'object' => 'source',
);
$this->mockRequest(
'GET',
'/v1/sources/src_foo',
array(),
$response
$source = Source::constructFrom(
$response,
new Util\RequestOptions()
);

$source = Source::retrieve('src_foo');
$source->delete();
$source->detach();
}

public function testVerify()
Expand Down

3 comments on commit 8a01a90

@EatonZ
Copy link

@EatonZ EatonZ commented on 8a01a90 Oct 19, 2017

Choose a reason for hiding this comment

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

Appears this is bugged, see below:
PHP Fatal error: Uncaught Error: Call to undefined method Stripe\Card::detach()

@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.

Hey @EatonZ. The detach() method only exists on \Stripe\Source objects.

\Stripe\Card objects have a delete() method that is more or less equivalent in the sense that it removes the card from the list of the customer's saved payment sources.

@EatonZ
Copy link

@EatonZ EatonZ commented on 8a01a90 Oct 19, 2017

Choose a reason for hiding this comment

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

I've been using delete, but changed it to detach after reading the changelog. Had the first crash this morning. I thought it applied to cards too.

Please sign in to comment.