Skip to content

Commit

Permalink
Merge pull request #378 from stripe/ob-detach-sources
Browse files Browse the repository at this point in the history
detach method for detaching sources from customers
  • Loading branch information
brandur-stripe authored Oct 11, 2017
2 parents 9e8d565 + 8a01a90 commit 15e14cb
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

0 comments on commit 15e14cb

Please sign in to comment.