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

Save the last response made on StripeObjects #206

Merged
merged 1 commit into from
Oct 22, 2015
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 init.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require(dirname(__FILE__) . '/lib/Error/RateLimit.php');

// Plumbing
require(dirname(__FILE__) . '/lib/ApiResponse.php');
require(dirname(__FILE__) . '/lib/JsonSerializable.php');
require(dirname(__FILE__) . '/lib/StripeObject.php');
require(dirname(__FILE__) . '/lib/ApiRequestor.php');
Expand Down
5 changes: 3 additions & 2 deletions lib/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static function _encodeObjects($d)
* @param array|null $params
* @param array|null $headers
*
* @return array An array whose first element is the response and second
* @return array An array whose first element is an API response and second
* element is the API key used to make the request.
*/
public function request($method, $url, $params = null, $headers = null)
Expand All @@ -57,7 +57,8 @@ public function request($method, $url, $params = null, $headers = null)
}
list($rbody, $rcode, $rheaders, $myApiKey) =
$this->_requestRaw($method, $url, $params, $headers);
$resp = $this->_interpretResponse($rbody, $rcode, $rheaders);
$json = $this->_interpretResponse($rbody, $rcode, $rheaders);
$resp = new ApiResponse($rbody, $rcode, $rheaders, $json);
return array($resp, $myApiKey);
}

Expand Down
15 changes: 11 additions & 4 deletions lib/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function refresh()
$this->_retrieveOptions,
$this->_opts->headers
);
$this->refreshFrom($response, $this->_opts);
$this->setLastResponse($response);
$this->refreshFrom($response->json, $this->_opts);
return $this;
}

Expand Down Expand Up @@ -95,7 +96,9 @@ private static function _validateParams($params = null)
protected function _request($method, $url, $params = array(), $options = null)
{
$opts = $this->_opts->merge($options);
return static::_staticRequest($method, $url, $params, $opts);
list($resp, $options) = static::_staticRequest($method, $url, $params, $opts);
$this->setLastResponse($resp);
return array($resp->json, $options);
}

protected static function _staticRequest($method, $url, $params, $options)
Expand Down Expand Up @@ -125,7 +128,9 @@ protected static function _all($params = null, $options = null)
$url = static::classUrl();

list($response, $opts) = static::_staticRequest('get', $url, $params, $options);
return Util\Util::convertToStripeObject($response, $opts);
$obj = Util\Util::convertToStripeObject($response->json, $opts);
$obj->setLastResponse($response);
return $obj;
}

protected static function _create($params = null, $options = null)
Expand All @@ -135,7 +140,9 @@ protected static function _create($params = null, $options = null)
$url = static::classUrl();

list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
return Util\Util::convertToStripeObject($response, $opts);
$obj = Util\Util::convertToStripeObject($response->json, $opts);
$obj->setLastResponse($response);
return $obj;
}

protected function _save($options = null)
Expand Down
27 changes: 27 additions & 0 deletions lib/ApiResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Stripe;

class ApiResponse
{
public $headers;
public $body;
public $json;
public $code;

/**
* @param string $body
* @param integer $body
* @param array|null $headers
* @param array|null $json
*
* @return obj An APIResponse
*/
public function __construct($body, $code, $headers, $json)
{
$this->body = $body;
$this->code = $code;
$this->headers = $headers;
$this->json = $json;
}
}
4 changes: 3 additions & 1 deletion lib/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public static function upcoming($params = null, $opts = null)
{
$url = static::classUrl() . '/upcoming';
list($response, $opts) = static::_staticRequest('get', $url, $params, $opts);
return Util\Util::convertToStripeObject($response, $opts);
$obj = Util\Util::convertToStripeObject($response->json, $opts);
$obj->setLastResponse($response);
return $obj;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions lib/StripeObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,30 @@ public static function init()
));
}

/**
* @return object The last response from the Stripe API
*/
public function getLastResponse()
{
return $this->_lastResponse;
}

/**
* @param ApiResponse
*
* @return void Set the last response from the Stripe API
*/
public function setLastResponse($resp)
{
$this->_lastResponse = $resp;
}

protected $_opts;
protected $_values;
protected $_unsavedValues;
protected $_transientValues;
protected $_retrieveOptions;
protected $_lastResponse;

public function __construct($id = null, $opts = null)
{
Expand Down
4 changes: 3 additions & 1 deletion tests/ChargeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testIdempotentCreate()
);

$this->assertTrue($c->paid);
$this->assertFalse($c->refunded);
$this->assertSame(200, $c->getLastResponse()->code);
}

public function testRetrieve()
Expand All @@ -75,6 +75,7 @@ public function testRetrieve()
)
);
$d = Charge::retrieve($c->id);
$this->assertSame(200, $d->getLastResponse()->code);
$this->assertSame($d->id, $c->id);
}

Expand Down Expand Up @@ -123,6 +124,7 @@ public function testUpdateMetadataAll()

$charge->metadata = array('test' => 'foo bar');
$charge->save();
$this->assertSame(200, $charge->getLastResponse()->code);

$updatedCharge = Charge::retrieve($charge->id);
$this->assertSame('foo bar', $updatedCharge->metadata['test']);
Expand Down