forked from Wavyzz/dolibarr
NEW Upgrade of Stripe lib to 6.4.1
This commit is contained in:
@@ -3,7 +3,6 @@ sudo: false
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.3
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
|
||||
0
htdocs/includes/stripe/build.php
Executable file → Normal file
0
htdocs/includes/stripe/build.php
Executable file → Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
Before Width: | Height: | Size: 95 B |
@@ -1,43 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
use Countable;
|
||||
|
||||
/**
|
||||
* Class AttachedObject
|
||||
*
|
||||
* e.g. metadata on Stripe objects.
|
||||
*
|
||||
* @package Stripe
|
||||
*/
|
||||
class AttachedObject extends StripeObject implements Countable
|
||||
{
|
||||
/**
|
||||
* Updates this object.
|
||||
*
|
||||
* @param array $properties A mapping of properties to update on this object.
|
||||
*/
|
||||
public function replaceWith($properties)
|
||||
{
|
||||
$removed = array_diff(array_keys($this->_values), array_keys($properties));
|
||||
// Don't unset, but rather set to null so we send up '' for deletion.
|
||||
foreach ($removed as $k) {
|
||||
$this->$k = null;
|
||||
}
|
||||
|
||||
foreach ($properties as $k => $v) {
|
||||
$this->$k = $v;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the number of elements in the AttachedObject instance.
|
||||
*
|
||||
* @return int the number of elements
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->_values);
|
||||
}
|
||||
}
|
||||
@@ -20,11 +20,20 @@ abstract class Base extends Exception
|
||||
$this->httpHeaders = $httpHeaders;
|
||||
$this->requestId = null;
|
||||
|
||||
// TODO: make this a proper constructor argument in the next major
|
||||
// release.
|
||||
$this->stripeCode = isset($jsonBody["error"]["code"]) ? $jsonBody["error"]["code"] : null;
|
||||
|
||||
if ($httpHeaders && isset($httpHeaders['Request-Id'])) {
|
||||
$this->requestId = $httpHeaders['Request-Id'];
|
||||
}
|
||||
}
|
||||
|
||||
public function getStripeCode()
|
||||
{
|
||||
return $this->stripeCode;
|
||||
}
|
||||
|
||||
public function getHttpStatus()
|
||||
{
|
||||
return $this->httpStatus;
|
||||
|
||||
@@ -15,12 +15,16 @@ class Card extends Base
|
||||
) {
|
||||
parent::__construct($message, $httpStatus, $httpBody, $jsonBody, $httpHeaders);
|
||||
$this->stripeParam = $stripeParam;
|
||||
|
||||
// TODO: once Error\Base accepts the error code as an argument, pass it
|
||||
// in the call to parent::__construct() and stop setting it here.
|
||||
$this->stripeCode = $stripeCode;
|
||||
|
||||
// This one is not like the others because it was added later and we're
|
||||
// trying to do our best not to change the public interface of this class'
|
||||
// constructor. We should consider changing its implementation on the
|
||||
// next major version bump of this library.
|
||||
// constructor.
|
||||
// TODO: make this a proper constructor argument in the next major
|
||||
// release.
|
||||
$this->declineCode = isset($jsonBody["error"]["decline_code"]) ? $jsonBody["error"]["decline_code"] : null;
|
||||
}
|
||||
|
||||
@@ -29,11 +33,6 @@ class Card extends Base
|
||||
return $this->declineCode;
|
||||
}
|
||||
|
||||
public function getStripeCode()
|
||||
{
|
||||
return $this->stripeCode;
|
||||
}
|
||||
|
||||
public function getStripeParam()
|
||||
{
|
||||
return $this->stripeParam;
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
/**
|
||||
* Class ExternalAccount
|
||||
*
|
||||
* @property string $id
|
||||
* @property string $object
|
||||
*
|
||||
* @package Stripe
|
||||
*/
|
||||
abstract class ExternalAccount extends ApiResource
|
||||
{
|
||||
/**
|
||||
* @return string The instance URL for this resource. It needs to be special
|
||||
* cased because it doesn't fit into the standard resource pattern.
|
||||
*/
|
||||
public function instanceUrl()
|
||||
{
|
||||
$id = $this['id'];
|
||||
if (!$id) {
|
||||
$class = get_class($this);
|
||||
$msg = "Could not determine which URL to request: $class instance "
|
||||
. "has invalid ID: $id";
|
||||
throw new Error\InvalidRequest($msg, null);
|
||||
}
|
||||
|
||||
if ($this['customer']) {
|
||||
$parent = $this['customer'];
|
||||
$base = Customer::classUrl();
|
||||
$path = 'sources';
|
||||
} elseif ($this['account']) {
|
||||
$parent = $this['account'];
|
||||
$base = Account::classUrl();
|
||||
$path = 'external_accounts';
|
||||
} elseif ($this['recipient']) {
|
||||
$parent = $this['recipient'];
|
||||
$base = Recipient::classUrl();
|
||||
$path = 'cards';
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
$parent = Util\Util::utf8($parent);
|
||||
$id = Util\Util::utf8($id);
|
||||
|
||||
$parentExtn = urlencode($parent);
|
||||
$extn = urlencode($id);
|
||||
return "$base/$parentExtn/$path/$extn";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null $params
|
||||
* @param array|string|null $opts
|
||||
*
|
||||
* @return ExternalAccount The deleted external account.
|
||||
*/
|
||||
public function delete($params = null, $opts = null)
|
||||
{
|
||||
return $this->_delete($params, $opts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string|null $opts
|
||||
*
|
||||
* @return ExternalAccount The saved external account.
|
||||
*/
|
||||
public function save($opts = null)
|
||||
{
|
||||
return $this->_save($opts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null $params
|
||||
* @param array|string|null $opts
|
||||
*
|
||||
* @return ExternalAccount The verified (or not) external account.
|
||||
*/
|
||||
public function verify($params = null, $opts = null)
|
||||
{
|
||||
if ($this['customer']) {
|
||||
$url = $this->instanceUrl() . '/verify';
|
||||
list($response, $options) = $this->_request('post', $url, $params, $opts);
|
||||
$this->refreshFrom($response, $options);
|
||||
return $this;
|
||||
} else {
|
||||
$message = 'Only customer external accounts can be verified in this manner.';
|
||||
throw new Error\Api($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,8 @@ interface ClientInterface
|
||||
* @param array $params KV pairs for parameters. Can be nested for arrays and hashes
|
||||
* @param boolean $hasFile Whether or not $params references a file (via an @ prefix or
|
||||
* CurlFile)
|
||||
* @throws Error\Api & Error\ApiConnection
|
||||
* @return array($rawBody, $httpStatusCode, $httpHeader)
|
||||
* @throws \Stripe\Error\Api & \Stripe\Error\ApiConnection
|
||||
* @return [$rawBody, $httpStatusCode, $httpHeader]
|
||||
*/
|
||||
public function request($method, $absUrl, $headers, $params, $hasFile);
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@ class CurlClient implements ClientInterface
|
||||
|
||||
protected $defaultOptions;
|
||||
|
||||
protected $userAgentInfo;
|
||||
|
||||
/**
|
||||
* CurlClient constructor.
|
||||
*
|
||||
@@ -49,9 +51,20 @@ class CurlClient implements ClientInterface
|
||||
*
|
||||
* @param array|callable|null $defaultOptions
|
||||
*/
|
||||
public function __construct($defaultOptions = null)
|
||||
public function __construct($defaultOptions = null, $randomGenerator = null)
|
||||
{
|
||||
$this->defaultOptions = $defaultOptions;
|
||||
$this->randomGenerator = $randomGenerator ?: new Util\RandomGenerator();
|
||||
$this->initUserAgentInfo();
|
||||
}
|
||||
|
||||
public function initUserAgentInfo()
|
||||
{
|
||||
$curlVersion = curl_version();
|
||||
$this->userAgentInfo = [
|
||||
'httplib' => 'curl ' . $curlVersion['version'],
|
||||
'ssllib' => $curlVersion['ssl_version'],
|
||||
];
|
||||
}
|
||||
|
||||
public function getDefaultOptions()
|
||||
@@ -59,6 +72,11 @@ class CurlClient implements ClientInterface
|
||||
return $this->defaultOptions;
|
||||
}
|
||||
|
||||
public function getUserAgentInfo()
|
||||
{
|
||||
return $this->userAgentInfo;
|
||||
}
|
||||
|
||||
// USER DEFINED TIMEOUTS
|
||||
|
||||
const DEFAULT_TIMEOUT = 80;
|
||||
@@ -93,10 +111,9 @@ class CurlClient implements ClientInterface
|
||||
|
||||
public function request($method, $absUrl, $headers, $params, $hasFile)
|
||||
{
|
||||
$curl = curl_init();
|
||||
$method = strtolower($method);
|
||||
|
||||
$opts = array();
|
||||
$opts = [];
|
||||
if (is_callable($this->defaultOptions)) { // call defaultOptions callback, set options to return value
|
||||
$opts = call_user_func_array($this->defaultOptions, func_get_args());
|
||||
if (!is_array($opts)) {
|
||||
@@ -114,24 +131,32 @@ class CurlClient implements ClientInterface
|
||||
}
|
||||
$opts[CURLOPT_HTTPGET] = 1;
|
||||
if (count($params) > 0) {
|
||||
$encoded = self::encode($params);
|
||||
$encoded = Util\Util::urlEncode($params);
|
||||
$absUrl = "$absUrl?$encoded";
|
||||
}
|
||||
} elseif ($method == 'post') {
|
||||
$opts[CURLOPT_POST] = 1;
|
||||
$opts[CURLOPT_POSTFIELDS] = $hasFile ? $params : self::encode($params);
|
||||
$opts[CURLOPT_POSTFIELDS] = $hasFile ? $params : Util\Util::urlEncode($params);
|
||||
} elseif ($method == 'delete') {
|
||||
$opts[CURLOPT_CUSTOMREQUEST] = 'DELETE';
|
||||
if (count($params) > 0) {
|
||||
$encoded = self::encode($params);
|
||||
$encoded = Util\Util::urlEncode($params);
|
||||
$absUrl = "$absUrl?$encoded";
|
||||
}
|
||||
} else {
|
||||
throw new Error\Api("Unrecognized method $method");
|
||||
}
|
||||
|
||||
// It is only safe to retry network failures on POST requests if we
|
||||
// add an Idempotency-Key header
|
||||
if (($method == 'post') && (Stripe::$maxNetworkRetries > 0)) {
|
||||
if (!isset($headers['Idempotency-Key'])) {
|
||||
array_push($headers, 'Idempotency-Key: ' . $this->randomGenerator->uuid());
|
||||
}
|
||||
}
|
||||
|
||||
// Create a callback to capture HTTP headers for the response
|
||||
$rheaders = array();
|
||||
$rheaders = [];
|
||||
$headerCallback = function ($curl, $header_line) use (&$rheaders) {
|
||||
// Ignore the HTTP request line (HTTP/1.1 200 OK)
|
||||
if (strpos($header_line, ":") === false) {
|
||||
@@ -163,50 +188,63 @@ class CurlClient implements ClientInterface
|
||||
$opts[CURLOPT_TIMEOUT] = $this->timeout;
|
||||
$opts[CURLOPT_HEADERFUNCTION] = $headerCallback;
|
||||
$opts[CURLOPT_HTTPHEADER] = $headers;
|
||||
if (!Stripe::$verifySslCerts) {
|
||||
$opts[CURLOPT_CAINFO] = Stripe::getCABundlePath();
|
||||
if (!Stripe::getVerifySslCerts()) {
|
||||
$opts[CURLOPT_SSL_VERIFYPEER] = false;
|
||||
}
|
||||
|
||||
list($rbody, $rcode) = $this->executeRequestWithRetries($opts, $absUrl);
|
||||
|
||||
return [$rbody, $rcode, $rheaders];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $opts cURL options
|
||||
*/
|
||||
private function executeRequestWithRetries($opts, $absUrl)
|
||||
{
|
||||
$numRetries = 0;
|
||||
|
||||
while (true) {
|
||||
$rcode = 0;
|
||||
$errno = 0;
|
||||
|
||||
$curl = curl_init();
|
||||
curl_setopt_array($curl, $opts);
|
||||
$rbody = curl_exec($curl);
|
||||
|
||||
if (!defined('CURLE_SSL_CACERT_BADFILE')) {
|
||||
define('CURLE_SSL_CACERT_BADFILE', 77); // constant not defined in PHP
|
||||
}
|
||||
|
||||
$errno = curl_errno($curl);
|
||||
if ($errno == CURLE_SSL_CACERT ||
|
||||
$errno == CURLE_SSL_PEER_CERTIFICATE ||
|
||||
$errno == CURLE_SSL_CACERT_BADFILE
|
||||
) {
|
||||
array_push(
|
||||
$headers,
|
||||
'X-Stripe-Client-Info: {"ca":"using Stripe-supplied CA bundle"}'
|
||||
);
|
||||
$cert = self::caBundle();
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($curl, CURLOPT_CAINFO, $cert);
|
||||
$rbody = curl_exec($curl);
|
||||
}
|
||||
|
||||
if ($rbody === false) {
|
||||
$errno = curl_errno($curl);
|
||||
$message = curl_error($curl);
|
||||
} else {
|
||||
$rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
}
|
||||
curl_close($curl);
|
||||
$this->handleCurlError($absUrl, $errno, $message);
|
||||
|
||||
if ($this->shouldRetry($errno, $rcode, $numRetries)) {
|
||||
$numRetries += 1;
|
||||
$sleepSeconds = $this->sleepTime($numRetries);
|
||||
usleep(intval($sleepSeconds * 1000000));
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
curl_close($curl);
|
||||
return array($rbody, $rcode, $rheaders);
|
||||
if ($rbody === false) {
|
||||
$this->handleCurlError($absUrl, $errno, $message, $numRetries);
|
||||
}
|
||||
|
||||
return [$rbody, $rcode];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param number $errno
|
||||
* @param string $url
|
||||
* @param int $errno
|
||||
* @param string $message
|
||||
* @param int $numRetries
|
||||
* @throws Error\ApiConnection
|
||||
*/
|
||||
private function handleCurlError($url, $errno, $message)
|
||||
private function handleCurlError($url, $errno, $message, $numRetries)
|
||||
{
|
||||
switch ($errno) {
|
||||
case CURLE_COULDNT_CONNECT:
|
||||
@@ -231,52 +269,66 @@ class CurlClient implements ClientInterface
|
||||
$msg .= " let us know at support@stripe.com.";
|
||||
|
||||
$msg .= "\n\n(Network error [errno $errno]: $message)";
|
||||
|
||||
if ($numRetries > 0) {
|
||||
$msg .= "\n\nRequest was retried $numRetries times.";
|
||||
}
|
||||
|
||||
throw new Error\ApiConnection($msg);
|
||||
}
|
||||
|
||||
private static function caBundle()
|
||||
{
|
||||
return dirname(__FILE__) . '/../../data/ca-certificates.crt';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $arr An map of param keys to values.
|
||||
* @param string|null $prefix
|
||||
*
|
||||
* Only public for testability, should not be called outside of CurlClient
|
||||
*
|
||||
* @return string A querystring, essentially.
|
||||
* Checks if an error is a problem that we should retry on. This includes both
|
||||
* socket errors that may represent an intermittent problem and some special
|
||||
* HTTP statuses.
|
||||
* @param int $errno
|
||||
* @param int $rcode
|
||||
* @param int $numRetries
|
||||
* @return bool
|
||||
*/
|
||||
public static function encode($arr, $prefix = null)
|
||||
private function shouldRetry($errno, $rcode, $numRetries)
|
||||
{
|
||||
if (!is_array($arr)) {
|
||||
return $arr;
|
||||
if ($numRetries >= Stripe::getMaxNetworkRetries()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$r = array();
|
||||
foreach ($arr as $k => $v) {
|
||||
if (is_null($v)) {
|
||||
continue;
|
||||
// Retry on timeout-related problems (either on open or read).
|
||||
if ($errno === CURLE_OPERATION_TIMEOUTED) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($prefix) {
|
||||
if ($k !== null && (!is_int($k) || is_array($v))) {
|
||||
$k = $prefix."[".$k."]";
|
||||
} else {
|
||||
$k = $prefix."[]";
|
||||
}
|
||||
// Destination refused the connection, the connection was reset, or a
|
||||
// variety of other connection failures. This could occur from a single
|
||||
// saturated server, so retry in case it's intermittent.
|
||||
if ($errno === CURLE_COULDNT_CONNECT) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (is_array($v)) {
|
||||
$enc = self::encode($v, $k);
|
||||
if ($enc) {
|
||||
$r[] = $enc;
|
||||
}
|
||||
} else {
|
||||
$r[] = urlencode($k)."=".urlencode($v);
|
||||
}
|
||||
// 409 conflict
|
||||
if ($rcode === 409) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return implode("&", $r);
|
||||
return false;
|
||||
}
|
||||
|
||||
private function sleepTime($numRetries)
|
||||
{
|
||||
// Apply exponential backoff with $initialNetworkRetryDelay on the
|
||||
// number of $numRetries so far as inputs. Do not allow the number to exceed
|
||||
// $maxNetworkRetryDelay.
|
||||
$sleepSeconds = min(
|
||||
Stripe::getInitialNetworkRetryDelay() * 1.0 * pow(2, $numRetries - 1),
|
||||
Stripe::getMaxNetworkRetryDelay()
|
||||
);
|
||||
|
||||
// Apply some jitter by randomizing the value in the range of
|
||||
// ($sleepSeconds / 2) to ($sleepSeconds).
|
||||
$sleepSeconds *= 0.5 * (1 + $this->randomGenerator->randFloat());
|
||||
|
||||
// But never sleep less than the base sleep seconds.
|
||||
$sleepSeconds = max(Stripe::getInitialNetworkRetryDelay(), $sleepSeconds);
|
||||
|
||||
return $sleepSeconds;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
// JsonSerializable only exists in PHP 5.4+. Stub if out if it doesn't exist
|
||||
if (interface_exists('\JsonSerializable', false)) {
|
||||
interface JsonSerializable extends \JsonSerializable
|
||||
{
|
||||
}
|
||||
} else {
|
||||
// PSR2 wants each interface to have its own file.
|
||||
// @codingStandardsIgnoreStart
|
||||
interface JsonSerializable
|
||||
{
|
||||
// @codingStandardsIgnoreEnd
|
||||
public function jsonSerialize();
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ class AutoPagingIterator implements \Iterator
|
||||
private $lastId = null;
|
||||
private $page = null;
|
||||
private $pageOffset = 0;
|
||||
private $params = array();
|
||||
private $params = [];
|
||||
|
||||
public function __construct($collection, $params)
|
||||
{
|
||||
@@ -42,8 +42,8 @@ class AutoPagingIterator implements \Iterator
|
||||
$this->pageOffset += count($this->page->data);
|
||||
if ($this->page['has_more']) {
|
||||
$this->params = array_merge(
|
||||
$this->params ? $this->params : array(),
|
||||
array('starting_after' => $this->lastId)
|
||||
$this->params ?: [],
|
||||
['starting_after' => $this->lastId]
|
||||
);
|
||||
$this->page = $this->page->all($this->params);
|
||||
} else {
|
||||
|
||||
@@ -6,10 +6,18 @@ use Stripe\Error;
|
||||
|
||||
class RequestOptions
|
||||
{
|
||||
/**
|
||||
* @var array A list of headers that should be persisted across requests.
|
||||
*/
|
||||
public static $HEADERS_TO_PERSIST = [
|
||||
'Stripe-Account',
|
||||
'Stripe-Version',
|
||||
];
|
||||
|
||||
public $headers;
|
||||
public $apiKey;
|
||||
|
||||
public function __construct($key = null, $headers = array())
|
||||
public function __construct($key = null, $headers = [])
|
||||
{
|
||||
$this->apiKey = $key;
|
||||
$this->headers = $headers;
|
||||
@@ -32,6 +40,18 @@ class RequestOptions
|
||||
return $other_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discards all headers that we don't want to persist across requests.
|
||||
*/
|
||||
public function discardNonPersistentHeaders()
|
||||
{
|
||||
foreach ($this->headers as $k => $v) {
|
||||
if (!in_array($k, self::$HEADERS_TO_PERSIST)) {
|
||||
unset($this->headers[$k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpacks an options array into an RequestOptions object
|
||||
* @param array|string|null $options a key => value array
|
||||
@@ -45,15 +65,15 @@ class RequestOptions
|
||||
}
|
||||
|
||||
if (is_null($options)) {
|
||||
return new RequestOptions(null, array());
|
||||
return new RequestOptions(null, []);
|
||||
}
|
||||
|
||||
if (is_string($options)) {
|
||||
return new RequestOptions($options, array());
|
||||
return new RequestOptions($options, []);
|
||||
}
|
||||
|
||||
if (is_array($options)) {
|
||||
$headers = array();
|
||||
$headers = [];
|
||||
$key = null;
|
||||
if (array_key_exists('api_key', $options)) {
|
||||
$key = $options['api_key'];
|
||||
|
||||
@@ -9,9 +9,9 @@ class Set implements IteratorAggregate
|
||||
{
|
||||
private $_elts;
|
||||
|
||||
public function __construct($members = array())
|
||||
public function __construct($members = [])
|
||||
{
|
||||
$this->_elts = array();
|
||||
$this->_elts = [];
|
||||
foreach ($members as $item) {
|
||||
$this->_elts[$item] = true;
|
||||
}
|
||||
|
||||
@@ -7,24 +7,26 @@ use Stripe\StripeObject;
|
||||
abstract class Util
|
||||
{
|
||||
private static $isMbstringAvailable = null;
|
||||
private static $isHashEqualsAvailable = null;
|
||||
|
||||
/**
|
||||
* Whether the provided array (or other) is a list rather than a dictionary.
|
||||
* A list is defined as an array for which all the keys are consecutive
|
||||
* integers starting at 0. Empty arrays are considered to be lists.
|
||||
*
|
||||
* @param array|mixed $array
|
||||
* @return boolean True if the given object is a list.
|
||||
* @return boolean true if the given object is a list.
|
||||
*/
|
||||
public static function isList($array)
|
||||
{
|
||||
if (!is_array($array)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: generally incorrect, but it's correct given Stripe's response
|
||||
foreach (array_keys($array) as $k) {
|
||||
if (!is_numeric($k)) {
|
||||
return false;
|
||||
if ($array === []) {
|
||||
return true;
|
||||
}
|
||||
if (array_keys($array) !== range(0, count($array) - 1)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -37,7 +39,7 @@ abstract class Util
|
||||
*/
|
||||
public static function convertStripeObjectToArray($values)
|
||||
{
|
||||
$results = array();
|
||||
$results = [];
|
||||
foreach ($values as $k => $v) {
|
||||
// FIXME: this is an encapsulation violation
|
||||
if ($k[0] == '_') {
|
||||
@@ -63,26 +65,34 @@ abstract class Util
|
||||
*/
|
||||
public static function convertToStripeObject($resp, $opts)
|
||||
{
|
||||
$types = array(
|
||||
$types = [
|
||||
// data structures
|
||||
'list' => 'Stripe\\Collection',
|
||||
|
||||
// business objects
|
||||
'account' => 'Stripe\\Account',
|
||||
'alipay_account' => 'Stripe\\AlipayAccount',
|
||||
'apple_pay_domain' => 'Stripe\\ApplePayDomain',
|
||||
'bank_account' => 'Stripe\\BankAccount',
|
||||
'application_fee' => 'Stripe\\ApplicationFee',
|
||||
'balance' => 'Stripe\\Balance',
|
||||
'balance_transaction' => 'Stripe\\BalanceTransaction',
|
||||
'bank_account' => 'Stripe\\BankAccount',
|
||||
'bitcoin_receiver' => 'Stripe\\BitcoinReceiver',
|
||||
'bitcoin_transaction' => 'Stripe\\BitcoinTransaction',
|
||||
'card' => 'Stripe\\Card',
|
||||
'charge' => 'Stripe\\Charge',
|
||||
'country_spec' => 'Stripe\\CountrySpec',
|
||||
'coupon' => 'Stripe\\Coupon',
|
||||
'customer' => 'Stripe\\Customer',
|
||||
'dispute' => 'Stripe\\Dispute',
|
||||
'list' => 'Stripe\\Collection',
|
||||
'ephemeral_key' => 'Stripe\\EphemeralKey',
|
||||
'event' => 'Stripe\\Event',
|
||||
'exchange_rate' => 'Stripe\\ExchangeRate',
|
||||
'fee_refund' => 'Stripe\\ApplicationFeeRefund',
|
||||
'file_upload' => 'Stripe\\FileUpload',
|
||||
'invoice' => 'Stripe\\Invoice',
|
||||
'invoiceitem' => 'Stripe\\InvoiceItem',
|
||||
'event' => 'Stripe\\Event',
|
||||
'file' => 'Stripe\\FileUpload',
|
||||
'token' => 'Stripe\\Token',
|
||||
'transfer' => 'Stripe\\Transfer',
|
||||
'transfer_reversal' => 'Stripe\\TransferReversal',
|
||||
'login_link' => 'Stripe\\LoginLink',
|
||||
'order' => 'Stripe\\Order',
|
||||
'order_return' => 'Stripe\\OrderReturn',
|
||||
'payout' => 'Stripe\\Payout',
|
||||
@@ -93,15 +103,17 @@ abstract class Util
|
||||
'refund' => 'Stripe\\Refund',
|
||||
'sku' => 'Stripe\\SKU',
|
||||
'source' => 'Stripe\\Source',
|
||||
'source_transaction' => 'Stripe\\SourceTransaction',
|
||||
'subscription' => 'Stripe\\Subscription',
|
||||
'subscription_item' => 'Stripe\\SubscriptionItem',
|
||||
'three_d_secure' => 'Stripe\\ThreeDSecure',
|
||||
'fee_refund' => 'Stripe\\ApplicationFeeRefund',
|
||||
'bitcoin_receiver' => 'Stripe\\BitcoinReceiver',
|
||||
'bitcoin_transaction' => 'Stripe\\BitcoinTransaction',
|
||||
);
|
||||
'token' => 'Stripe\\Token',
|
||||
'topup' => 'Stripe\\Topup',
|
||||
'transfer' => 'Stripe\\Transfer',
|
||||
'transfer_reversal' => 'Stripe\\TransferReversal',
|
||||
];
|
||||
if (self::isList($resp)) {
|
||||
$mapped = array();
|
||||
$mapped = [];
|
||||
foreach ($resp as $i) {
|
||||
array_push($mapped, self::convertToStripeObject($i, $opts));
|
||||
}
|
||||
@@ -143,4 +155,84 @@ abstract class Util
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two strings for equality. The time taken is independent of the
|
||||
* number of characters that match.
|
||||
*
|
||||
* @param string $a one of the strings to compare.
|
||||
* @param string $b the other string to compare.
|
||||
* @return bool true if the strings are equal, false otherwise.
|
||||
*/
|
||||
public static function secureCompare($a, $b)
|
||||
{
|
||||
if (self::$isHashEqualsAvailable === null) {
|
||||
self::$isHashEqualsAvailable = function_exists('hash_equals');
|
||||
}
|
||||
|
||||
if (self::$isHashEqualsAvailable) {
|
||||
return hash_equals($a, $b);
|
||||
} else {
|
||||
if (strlen($a) != strlen($b)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = 0;
|
||||
for ($i = 0; $i < strlen($a); $i++) {
|
||||
$result |= ord($a[$i]) ^ ord($b[$i]);
|
||||
}
|
||||
return ($result == 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $arr A map of param keys to values.
|
||||
* @param string|null $prefix
|
||||
*
|
||||
* @return string A querystring, essentially.
|
||||
*/
|
||||
public static function urlEncode($arr, $prefix = null)
|
||||
{
|
||||
if (!is_array($arr)) {
|
||||
return $arr;
|
||||
}
|
||||
|
||||
$r = [];
|
||||
foreach ($arr as $k => $v) {
|
||||
if (is_null($v)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($prefix) {
|
||||
if ($k !== null && (!is_int($k) || is_array($v))) {
|
||||
$k = $prefix."[".$k."]";
|
||||
} else {
|
||||
$k = $prefix."[]";
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($v)) {
|
||||
$enc = self::urlEncode($v, $k);
|
||||
if ($enc) {
|
||||
$r[] = $enc;
|
||||
}
|
||||
} else {
|
||||
$r[] = urlencode($k)."=".urlencode($v);
|
||||
}
|
||||
}
|
||||
|
||||
return implode("&", $r);
|
||||
}
|
||||
|
||||
public static function normalizeId($id)
|
||||
{
|
||||
if (is_array($id)) {
|
||||
$params = $id;
|
||||
$id = $params['id'];
|
||||
unset($params['id']);
|
||||
} else {
|
||||
$params = [];
|
||||
}
|
||||
return [$id, $params];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,300 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class AccountTest extends TestCase
|
||||
{
|
||||
private function managedAccountResponse($id)
|
||||
{
|
||||
return array(
|
||||
'id' => $id,
|
||||
'currencies_supported' => array('usd', 'aed', 'afn', '...'),
|
||||
'object' => 'account',
|
||||
'business_name' => 'Stripe.com',
|
||||
'bank_accounts' => array(
|
||||
'object' => 'list',
|
||||
'total_count' => 0,
|
||||
'has_more' => false,
|
||||
'url' => '/v1/accounts/' . $id . '/bank_accounts',
|
||||
'data' => array()
|
||||
),
|
||||
'verification' => array(
|
||||
'fields_needed' => array(
|
||||
'product_description',
|
||||
'business_url',
|
||||
'support_phone',
|
||||
'bank_account',
|
||||
'tos_acceptance.ip',
|
||||
'tos_acceptance.date'
|
||||
),
|
||||
'due_by' => null,
|
||||
'contacted' => false
|
||||
),
|
||||
'tos_acceptance' => array(
|
||||
'ip' => null,
|
||||
'date' => null,
|
||||
'user_agent' => null
|
||||
),
|
||||
'legal_entity' => array(
|
||||
'type' => null,
|
||||
'business_name' => null,
|
||||
'address' => array(
|
||||
'line1' => null,
|
||||
'line2' => null,
|
||||
'city' => null,
|
||||
'state' => null,
|
||||
'postal_code' => null,
|
||||
'country' => 'US'
|
||||
),
|
||||
'first_name' => null,
|
||||
'last_name' => null,
|
||||
'additional_owners' => null,
|
||||
'verification' => array(
|
||||
'status' => 'unverified',
|
||||
'document' => null,
|
||||
'details' => null
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function deletedAccountResponse($id)
|
||||
{
|
||||
return array(
|
||||
'id' => $id,
|
||||
'deleted' => true
|
||||
);
|
||||
}
|
||||
|
||||
public function testBasicRetrieve()
|
||||
{
|
||||
$this->mockRequest('GET', '/v1/account', array(), $this->managedAccountResponse('acct_ABC'));
|
||||
$account = Account::retrieve();
|
||||
$this->assertSame($account->id, 'acct_ABC');
|
||||
}
|
||||
|
||||
public function testIDRetrieve()
|
||||
{
|
||||
$this->mockRequest('GET', '/v1/accounts/acct_DEF', array(), $this->managedAccountResponse('acct_DEF'));
|
||||
$account = Account::retrieve('acct_DEF');
|
||||
$this->assertSame($account->id, 'acct_DEF');
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/accounts',
|
||||
array('managed' => 'true'),
|
||||
$this->managedAccountResponse('acct_ABC')
|
||||
);
|
||||
$account = Account::create(array(
|
||||
'managed' => true
|
||||
));
|
||||
$this->assertSame($account->id, 'acct_ABC');
|
||||
}
|
||||
|
||||
public function testDelete()
|
||||
{
|
||||
$account = self::createTestAccount();
|
||||
|
||||
$this->mockRequest(
|
||||
'DELETE',
|
||||
'/v1/accounts/' . $account->id,
|
||||
array(),
|
||||
$this->deletedAccountResponse('acct_ABC')
|
||||
);
|
||||
$deleted = $account->delete();
|
||||
$this->assertSame($deleted->id, $account->id);
|
||||
$this->assertTrue($deleted->deleted);
|
||||
}
|
||||
|
||||
public function testReject()
|
||||
{
|
||||
$account = self::createTestAccount();
|
||||
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/accounts/' . $account->id . '/reject',
|
||||
array('reason' => 'fraud'),
|
||||
$this->deletedAccountResponse('acct_ABC')
|
||||
);
|
||||
$rejected = $account->reject(array('reason' => 'fraud'));
|
||||
$this->assertSame($rejected->id, $account->id);
|
||||
}
|
||||
|
||||
public function testSaveLegalEntity()
|
||||
{
|
||||
$response = $this->managedAccountResponse('acct_ABC');
|
||||
$this->mockRequest('POST', '/v1/accounts', array('managed' => 'true'), $response);
|
||||
|
||||
$response['legal_entity']['first_name'] = 'Bob';
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/accounts/acct_ABC',
|
||||
array('legal_entity' => array('first_name' => 'Bob')),
|
||||
$response
|
||||
);
|
||||
|
||||
$account = Account::create(array('managed' => true));
|
||||
$account->legal_entity->first_name = 'Bob';
|
||||
$account->save();
|
||||
|
||||
$this->assertSame('Bob', $account->legal_entity->first_name);
|
||||
}
|
||||
|
||||
public function testUpdateLegalEntity()
|
||||
{
|
||||
$response = $this->managedAccountResponse('acct_ABC');
|
||||
$this->mockRequest('POST', '/v1/accounts', array('managed' => 'true'), $response);
|
||||
|
||||
$response['legal_entity']['first_name'] = 'Bob';
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/accounts/acct_ABC',
|
||||
array('legal_entity' => array('first_name' => 'Bob')),
|
||||
$response
|
||||
);
|
||||
|
||||
$account = Account::create(array('managed' => true));
|
||||
$account = Account::update($account['id'], array(
|
||||
'legal_entity' => array(
|
||||
'first_name' => 'Bob'
|
||||
)
|
||||
));
|
||||
|
||||
$this->assertSame('Bob', $account->legal_entity->first_name);
|
||||
}
|
||||
|
||||
public function testCreateAdditionalOwners()
|
||||
{
|
||||
$request = array(
|
||||
'managed' => true,
|
||||
'country' => 'GB',
|
||||
'legal_entity' => array(
|
||||
'additional_owners' => array(
|
||||
0 => array(
|
||||
'dob' => array(
|
||||
'day' => 12,
|
||||
'month' => 5,
|
||||
'year' => 1970,
|
||||
),
|
||||
'first_name' => 'xgvukvfrde',
|
||||
'last_name' => 'rtcyvubhy',
|
||||
),
|
||||
1 => array(
|
||||
'dob' => array(
|
||||
'day' => 8,
|
||||
'month' => 4,
|
||||
'year' => 1979,
|
||||
),
|
||||
'first_name' => 'yutreuk',
|
||||
'last_name' => 'dfcgvhbjihmv',
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$acct = Account::create($request);
|
||||
$response = $acct->__toArray(true);
|
||||
|
||||
$req_ao = $request['legal_entity']['additional_owners'];
|
||||
$resp_ao = $response['legal_entity']['additional_owners'];
|
||||
|
||||
$this->assertSame($req_ao[0]['dob'], $resp_ao[0]['dob']);
|
||||
$this->assertSame($req_ao[1]['dob'], $resp_ao[1]['dob']);
|
||||
|
||||
$this->assertSame($req_ao[0]['first_name'], $resp_ao[0]['first_name']);
|
||||
$this->assertSame($req_ao[1]['first_name'], $resp_ao[1]['first_name']);
|
||||
}
|
||||
|
||||
public function testUpdateAdditionalOwners()
|
||||
{
|
||||
$response = $this->managedAccountResponse('acct_ABC');
|
||||
$this->mockRequest('POST', '/v1/accounts', array('managed' => 'true'), $response);
|
||||
|
||||
$response['legal_entity']['additional_owners'] = array(array(
|
||||
'first_name' => 'Bob',
|
||||
'last_name' => null,
|
||||
'address' => array(
|
||||
'line1' => null,
|
||||
'line2' => null,
|
||||
'city' => null,
|
||||
'state' => null,
|
||||
'postal_code' => null,
|
||||
'country' => null
|
||||
),
|
||||
'verification' => array(
|
||||
'status' => 'unverified',
|
||||
'document' => null,
|
||||
'details' => null
|
||||
)
|
||||
));
|
||||
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/accounts/acct_ABC',
|
||||
array('legal_entity' => array('additional_owners' => array(array('first_name' => 'Bob')))),
|
||||
$response
|
||||
);
|
||||
|
||||
$response['legal_entity']['additional_owners'][0]['last_name'] = 'Smith';
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/accounts/acct_ABC',
|
||||
array('legal_entity' => array('additional_owners' => array(array('last_name' => 'Smith')))),
|
||||
$response
|
||||
);
|
||||
|
||||
$response['legal_entity']['additional_owners'][0]['last_name'] = 'Johnson';
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/accounts/acct_ABC',
|
||||
array('legal_entity' => array('additional_owners' => array(array('last_name' => 'Johnson')))),
|
||||
$response
|
||||
);
|
||||
|
||||
$response['legal_entity']['additional_owners'][0]['verification']['document'] = 'file_123';
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/accounts/acct_ABC',
|
||||
array('legal_entity' => array('additional_owners' => array(array('verification' => array('document' => 'file_123'))))),
|
||||
$response
|
||||
);
|
||||
|
||||
$response['legal_entity']['additional_owners'][1] = array(
|
||||
'first_name' => 'Jane',
|
||||
'last_name' => 'Doe'
|
||||
);
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/accounts/acct_ABC',
|
||||
array('legal_entity' => array('additional_owners' => array(1 => array('first_name' => 'Jane')))),
|
||||
$response
|
||||
);
|
||||
|
||||
$account = Account::create(array('managed' => true));
|
||||
$account->legal_entity->additional_owners = array(array('first_name' => 'Bob'));
|
||||
$account->save();
|
||||
$this->assertSame(1, count($account->legal_entity->additional_owners));
|
||||
$this->assertSame('Bob', $account->legal_entity->additional_owners[0]->first_name);
|
||||
|
||||
$account->legal_entity->additional_owners[0]->last_name = 'Smith';
|
||||
$account->save();
|
||||
$this->assertSame(1, count($account->legal_entity->additional_owners));
|
||||
$this->assertSame('Smith', $account->legal_entity->additional_owners[0]->last_name);
|
||||
|
||||
$account['legal_entity']['additional_owners'][0]['last_name'] = 'Johnson';
|
||||
$account->save();
|
||||
$this->assertSame(1, count($account->legal_entity->additional_owners));
|
||||
$this->assertSame('Johnson', $account->legal_entity->additional_owners[0]->last_name);
|
||||
|
||||
$account->legal_entity->additional_owners[0]->verification->document = 'file_123';
|
||||
$account->save();
|
||||
$this->assertSame('file_123', $account->legal_entity->additional_owners[0]->verification->document);
|
||||
|
||||
$account->legal_entity->additional_owners[1] = array('first_name' => 'Jane');
|
||||
$account->save();
|
||||
$this->assertSame('Jane', $account->legal_entity->additional_owners[1]->first_name);
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
use Stripe\HttpClient\CurlClient;
|
||||
|
||||
class ApiRequestorTest extends TestCase
|
||||
{
|
||||
public function testEncodeObjects()
|
||||
{
|
||||
$reflector = new \ReflectionClass('Stripe\\ApiRequestor');
|
||||
$method = $reflector->getMethod('_encodeObjects');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$a = array('customer' => new Customer('abcd'));
|
||||
$enc = $method->invoke(null, $a);
|
||||
$this->assertSame($enc, array('customer' => 'abcd'));
|
||||
|
||||
// Preserves UTF-8
|
||||
$v = array('customer' => "☃");
|
||||
$enc = $method->invoke(null, $v);
|
||||
$this->assertSame($enc, $v);
|
||||
|
||||
// Encodes latin-1 -> UTF-8
|
||||
$v = array('customer' => "\xe9");
|
||||
$enc = $method->invoke(null, $v);
|
||||
$this->assertSame($enc, array('customer' => "\xc3\xa9"));
|
||||
}
|
||||
|
||||
public function testHttpClientInjection()
|
||||
{
|
||||
$reflector = new \ReflectionClass('Stripe\\ApiRequestor');
|
||||
$method = $reflector->getMethod('httpClient');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$curl = new CurlClient();
|
||||
$curl->setTimeout(10);
|
||||
ApiRequestor::setHttpClient($curl);
|
||||
|
||||
$injectedCurl = $method->invoke(new ApiRequestor());
|
||||
$this->assertSame($injectedCurl, $curl);
|
||||
}
|
||||
|
||||
public function testDefaultHeaders()
|
||||
{
|
||||
$reflector = new \ReflectionClass('Stripe\\ApiRequestor');
|
||||
$method = $reflector->getMethod('_defaultHeaders');
|
||||
$method->setAccessible(true);
|
||||
|
||||
// no way to stub static methods with PHPUnit 4.x :(
|
||||
Stripe::setAppInfo('MyTestApp', '1.2.34', 'https://mytestapp.example');
|
||||
$apiKey = 'sk_test_notarealkey';
|
||||
|
||||
$headers = $method->invoke(null, $apiKey);
|
||||
|
||||
$ua = json_decode($headers['X-Stripe-Client-User-Agent']);
|
||||
$this->assertSame($ua->application->name, 'MyTestApp');
|
||||
$this->assertSame($ua->application->version, '1.2.34');
|
||||
$this->assertSame($ua->application->url, 'https://mytestapp.example');
|
||||
|
||||
$this->assertSame(
|
||||
$headers['User-Agent'],
|
||||
'Stripe/v1 PhpBindings/' . Stripe::VERSION . ' MyTestApp/1.2.34 (https://mytestapp.example)'
|
||||
);
|
||||
|
||||
$this->assertSame($headers['Authorization'], 'Bearer ' . $apiKey);
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class ApplePayDomainTest extends TestCase
|
||||
{
|
||||
public function testCreation()
|
||||
{
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/apple_pay/domains',
|
||||
array('domain_name' => 'test.com'),
|
||||
array(
|
||||
'id' => 'apwc_create',
|
||||
'object' => 'apple_pay_domain'
|
||||
)
|
||||
);
|
||||
$d = ApplePayDomain::create(array(
|
||||
'domain_name' => 'test.com'
|
||||
));
|
||||
$this->assertSame('apwc_create', $d->id);
|
||||
$this->assertInstanceOf('Stripe\\ApplePayDomain', $d);
|
||||
}
|
||||
|
||||
public function testRetrieve()
|
||||
{
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/apple_pay/domains/apwc_retrieve',
|
||||
array(),
|
||||
array(
|
||||
'id' => 'apwc_retrieve',
|
||||
'object' => 'apple_pay_domain'
|
||||
)
|
||||
);
|
||||
$d = ApplePayDomain::retrieve('apwc_retrieve');
|
||||
$this->assertSame('apwc_retrieve', $d->id);
|
||||
$this->assertInstanceOf('Stripe\\ApplePayDomain', $d);
|
||||
}
|
||||
|
||||
public function testDeletion()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$d = ApplePayDomain::create(array(
|
||||
'domain_name' => 'jackshack.website'
|
||||
));
|
||||
$this->assertInstanceOf('Stripe\\ApplePayDomain', $d);
|
||||
$this->mockRequest(
|
||||
'DELETE',
|
||||
'/v1/apple_pay/domains/' . $d->id,
|
||||
array(),
|
||||
array('deleted' => true)
|
||||
);
|
||||
$d->delete();
|
||||
$this->assertTrue($d->deleted);
|
||||
}
|
||||
|
||||
public function testList()
|
||||
{
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/apple_pay/domains',
|
||||
array(),
|
||||
array(
|
||||
'url' => '/v1/apple_pay/domains',
|
||||
'object' => 'list'
|
||||
)
|
||||
);
|
||||
$all = ApplePayDomain::all();
|
||||
$this->assertSame($all->url, '/v1/apple_pay/domains');
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class ApplicationFeeRefundTest extends TestCase
|
||||
{
|
||||
public function testUrls()
|
||||
{
|
||||
$refund = new ApplicationFeeRefund();
|
||||
$refund->id = 'refund_id';
|
||||
$refund->fee = 'fee_id';
|
||||
|
||||
$this->assertSame(
|
||||
$refund->instanceUrl(),
|
||||
'/v1/application_fees/fee_id/refunds/refund_id'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class ApplicationFeeTest extends TestCase
|
||||
{
|
||||
public function testUrls()
|
||||
{
|
||||
$applicationFee = new ApplicationFee('abcd/efgh');
|
||||
$this->assertSame(
|
||||
$applicationFee->instanceUrl(),
|
||||
'/v1/application_fees/abcd%2Fefgh'
|
||||
);
|
||||
}
|
||||
|
||||
public function testList()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$d = ApplicationFee::all();
|
||||
$this->assertSame($d->url, '/v1/application_fees');
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class AuthenticationErrorTest extends TestCase
|
||||
{
|
||||
public function testInvalidCredentials()
|
||||
{
|
||||
Stripe::setApiKey('invalid');
|
||||
try {
|
||||
Customer::create();
|
||||
} catch (Error\Authentication $e) {
|
||||
$this->assertSame(401, $e->getHttpStatus());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class BalanceTest extends TestCase
|
||||
{
|
||||
public function testRetrieve()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$d = Balance::retrieve();
|
||||
$this->assertSame($d->object, "balance");
|
||||
$this->assertTrue(Util\Util::isList($d->available));
|
||||
$this->assertTrue(Util\Util::isList($d->pending));
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class BalanceTransactionTest extends TestCase
|
||||
{
|
||||
public function testList()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$d = BalanceTransaction::all();
|
||||
$this->assertSame($d->url, '/v1/balance/history');
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class BankAccountTest extends TestCase
|
||||
{
|
||||
public function testVerify()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$customer = self::createTestCustomer();
|
||||
|
||||
$bankAccount = $customer->sources->create(array(
|
||||
'source' => array(
|
||||
'object' => 'bank_account',
|
||||
'account_holder_type' => 'individual',
|
||||
'account_number' => '000123456789',
|
||||
'account_holder_name' => 'John Doe',
|
||||
'routing_number' => '110000000',
|
||||
'country' => 'US'
|
||||
)
|
||||
));
|
||||
|
||||
$this->assertSame($bankAccount->status, 'new');
|
||||
|
||||
$bankAccount = $bankAccount->verify(array(
|
||||
'amounts' => array(32, 45)
|
||||
));
|
||||
|
||||
$this->assertSame($bankAccount->status, 'verified');
|
||||
}
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class BitcoinReceiverTest extends TestCase
|
||||
{
|
||||
public function testUrls()
|
||||
{
|
||||
$classUrl = BitcoinReceiver::classUrl('Stripe_BitcoinReceiver');
|
||||
$this->assertSame($classUrl, '/v1/bitcoin/receivers');
|
||||
$receiver = new BitcoinReceiver('abcd/efgh');
|
||||
$instanceUrl = $receiver->instanceUrl();
|
||||
$this->assertSame($instanceUrl, '/v1/bitcoin/receivers/abcd%2Fefgh');
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$receiver = $this->createTestBitcoinReceiver("do+fill_now@stripe.com");
|
||||
|
||||
$this->assertSame(100, $receiver->amount);
|
||||
$this->assertNotNull($receiver->id);
|
||||
}
|
||||
|
||||
public function testRetrieve()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$receiver = $this->createTestBitcoinReceiver("do+fill_now@stripe.com");
|
||||
|
||||
$r = BitcoinReceiver::retrieve($receiver->id);
|
||||
$this->assertSame($receiver->id, $r->id);
|
||||
|
||||
$this->assertInstanceOf('Stripe\\BitcoinTransaction', $r->transactions->data[0]);
|
||||
}
|
||||
|
||||
public function testList()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$receiver = $this->createTestBitcoinReceiver("do+fill_now@stripe.com");
|
||||
|
||||
$receivers = BitcoinReceiver::all();
|
||||
$this->assertGreaterThan(0, count($receivers->data));
|
||||
}
|
||||
|
||||
public function testListTransactions()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$receiver = $this->createTestBitcoinReceiver("do+fill_now@stripe.com");
|
||||
$this->assertSame(0, count($receiver->transactions->data));
|
||||
|
||||
$transactions = $receiver->transactions->all(array("limit" => 1));
|
||||
$this->assertSame(1, count($transactions->data));
|
||||
}
|
||||
|
||||
public function testDeleteWithCustomer()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$receiver = $this->createTestBitcoinReceiver("do+fill_now@stripe.com");
|
||||
$customer = Customer::create(array("source" => $receiver->id));
|
||||
$charge = Charge::create(array(
|
||||
"customer" => $customer->id,
|
||||
"amount" => $receiver->amount,
|
||||
"currency" => $receiver->currency
|
||||
));
|
||||
$receiver = BitcoinReceiver::retrieve($receiver->id);
|
||||
$response = $receiver->delete();
|
||||
$this->assertTrue($response->deleted);
|
||||
}
|
||||
|
||||
public function testUpdateWithCustomer()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$receiver = $this->createTestBitcoinReceiver("do+fill_now@stripe.com");
|
||||
$customer = Customer::create(array("source" => $receiver->id));
|
||||
$receiver = BitcoinReceiver::retrieve($receiver->id);
|
||||
|
||||
$receiver->description = "a new description";
|
||||
$receiver->save();
|
||||
|
||||
$base = Customer::classUrl();
|
||||
$parentExtn = $receiver['customer'];
|
||||
$extn = $receiver['id'];
|
||||
$this->assertEquals("$base/$parentExtn/sources/$extn", $receiver->instanceUrl());
|
||||
|
||||
$updatedReceiver = BitcoinReceiver::retrieve($receiver->id);
|
||||
$this->assertEquals($receiver["description"], $updatedReceiver["description"]);
|
||||
}
|
||||
|
||||
public function testUpdateWithoutCustomer()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$receiver = $this->createTestBitcoinReceiver("do+fill_now@stripe.com");
|
||||
|
||||
$receiver->description = "a new description";
|
||||
$receiver->save();
|
||||
|
||||
$this->assertEquals(BitcoinReceiver::classUrl() . "/" . $receiver['id'], $receiver->instanceUrl());
|
||||
|
||||
$updatedReceiver = BitcoinReceiver::retrieve($receiver->id);
|
||||
$this->assertEquals($receiver["description"], $updatedReceiver["description"]);
|
||||
}
|
||||
|
||||
public function testRefund()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$receiver = $this->createTestBitcoinReceiver("do+fill_now@stripe.com");
|
||||
|
||||
$receiver = BitcoinReceiver::retrieve($receiver->id);
|
||||
$this->assertNull($receiver->refund_address);
|
||||
|
||||
$refundAddress = "REFUNDHERE";
|
||||
$receiver->refund(array("refund_address" => $refundAddress));
|
||||
|
||||
$this->assertSame($refundAddress, $receiver->refund_address);
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class CardErrorTest extends TestCase
|
||||
{
|
||||
public function testDecline()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$card = array(
|
||||
'number' => '4000000000000002',
|
||||
'exp_month' => '3',
|
||||
'exp_year' => '2020'
|
||||
);
|
||||
|
||||
$charge = array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
'card' => $card
|
||||
);
|
||||
|
||||
try {
|
||||
Charge::create($charge);
|
||||
} catch (Error\Card $e) {
|
||||
$this->assertSame(402, $e->getHttpStatus());
|
||||
$this->assertTrue(strpos($e->getRequestId(), "req_") === 0, $e->getRequestId());
|
||||
$actual = $e->getJsonBody();
|
||||
$this->assertSame(
|
||||
array('error' => array(
|
||||
'message' => 'Your card was declined.',
|
||||
'type' => 'card_error',
|
||||
'code' => 'card_declined',
|
||||
'decline_code' => 'generic_decline',
|
||||
'charge' => $actual['error']['charge'],
|
||||
)),
|
||||
$actual
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class ChargeTest extends TestCase
|
||||
{
|
||||
public function testUrls()
|
||||
{
|
||||
$this->assertSame(Charge::classUrl(), '/v1/charges');
|
||||
$charge = new Charge('abcd/efgh');
|
||||
$this->assertSame($charge->instanceUrl(), '/v1/charges/abcd%2Fefgh');
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$card = array(
|
||||
'number' => '4242424242424242',
|
||||
'exp_month' => 5,
|
||||
'exp_year' => date('Y') + 1
|
||||
);
|
||||
|
||||
$c = Charge::create(
|
||||
array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
'card' => $card
|
||||
)
|
||||
);
|
||||
$this->assertTrue($c->paid);
|
||||
$this->assertFalse($c->refunded);
|
||||
}
|
||||
|
||||
public function testIdempotentCreate()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$card = array(
|
||||
'number' => '4242424242424242',
|
||||
'exp_month' => 5,
|
||||
'exp_year' => date('Y') + 1
|
||||
);
|
||||
|
||||
$c = Charge::create(
|
||||
array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
'card' => $card
|
||||
),
|
||||
array(
|
||||
'idempotency_key' => self::generateRandomString(),
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertTrue($c->paid);
|
||||
$this->assertSame(200, $c->getLastResponse()->code);
|
||||
}
|
||||
|
||||
public function testRetrieve()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$card = array(
|
||||
'number' => '4242424242424242',
|
||||
'exp_month' => 5,
|
||||
'exp_year' => date('Y') + 1
|
||||
);
|
||||
|
||||
$c = Charge::create(
|
||||
array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
'card' => $card
|
||||
)
|
||||
);
|
||||
$d = Charge::retrieve($c->id);
|
||||
$this->assertSame(200, $d->getLastResponse()->code);
|
||||
$this->assertSame($d->id, $c->id);
|
||||
}
|
||||
|
||||
public function testUpdateMetadata()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$card = array(
|
||||
'number' => '4242424242424242',
|
||||
'exp_month' => 5,
|
||||
'exp_year' => date('Y') + 1
|
||||
);
|
||||
|
||||
$charge = Charge::create(
|
||||
array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
'card' => $card
|
||||
)
|
||||
);
|
||||
|
||||
$charge->metadata['test'] = 'foo bar';
|
||||
$charge->save();
|
||||
|
||||
$updatedCharge = Charge::retrieve($charge->id);
|
||||
$this->assertSame('foo bar', $updatedCharge->metadata['test']);
|
||||
}
|
||||
|
||||
public function testUpdateMetadataAll()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$card = array(
|
||||
'number' => '4242424242424242',
|
||||
'exp_month' => 5,
|
||||
'exp_year' => date('Y') + 1
|
||||
);
|
||||
|
||||
$charge = Charge::create(
|
||||
array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
'card' => $card
|
||||
)
|
||||
);
|
||||
|
||||
$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']);
|
||||
}
|
||||
|
||||
public function testMarkAsFraudulent()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$card = array(
|
||||
'number' => '4242424242424242',
|
||||
'exp_month' => 5,
|
||||
'exp_year' => date('Y') + 1
|
||||
);
|
||||
|
||||
$charge = Charge::create(
|
||||
array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
'card' => $card
|
||||
)
|
||||
);
|
||||
|
||||
$charge->refunds->create();
|
||||
$charge->markAsFraudulent();
|
||||
|
||||
$updatedCharge = Charge::retrieve($charge->id);
|
||||
$this->assertSame(
|
||||
'fraudulent',
|
||||
$updatedCharge['fraud_details']['user_report']
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreateWithBitcoinReceiverSource()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$receiver = $this->createTestBitcoinReceiver("do+fill_now@stripe.com");
|
||||
|
||||
$charge = Charge::create(
|
||||
array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
'source' => $receiver->id
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertSame($receiver->id, $charge->source->id);
|
||||
$this->assertSame("bitcoin_receiver", $charge->source->object);
|
||||
$this->assertSame("succeeded", $charge->status);
|
||||
$this->assertInstanceOf('Stripe\\BitcoinReceiver', $charge->source);
|
||||
}
|
||||
|
||||
public function markAsSafe()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$card = array(
|
||||
'number' => '4242424242424242',
|
||||
'exp_month' => 5,
|
||||
'exp_year' => date('Y') + 1
|
||||
);
|
||||
|
||||
$charge = Charge::create(
|
||||
array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
'card' => $card
|
||||
)
|
||||
);
|
||||
|
||||
$charge->markAsSafe();
|
||||
|
||||
$updatedCharge = Charge::retrieve($charge->id);
|
||||
$this->assertSame('safe', $updatedCharge['fraud_details']['user_report']);
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class CollectionTest extends TestCase
|
||||
{
|
||||
private function pageableModelResponse($ids, $hasMore)
|
||||
{
|
||||
$data = array();
|
||||
foreach ($ids as $id) {
|
||||
array_push($data, array(
|
||||
'id' => $id,
|
||||
'object' => 'pageablemodel'
|
||||
));
|
||||
}
|
||||
return array(
|
||||
'object' => 'list',
|
||||
'url' => '/v1/pageablemodels',
|
||||
'data' => $data,
|
||||
'has_more' => $hasMore
|
||||
);
|
||||
}
|
||||
|
||||
public function testAutoPagingOnePage()
|
||||
{
|
||||
$collection = Collection::constructFrom(
|
||||
$this->pageableModelResponse(array('pm_123', 'pm_124'), false),
|
||||
new Util\RequestOptions()
|
||||
);
|
||||
|
||||
$seen = array();
|
||||
foreach ($collection->autoPagingIterator() as $item) {
|
||||
array_push($seen, $item['id']);
|
||||
}
|
||||
|
||||
$this->assertSame($seen, array('pm_123', 'pm_124'));
|
||||
}
|
||||
|
||||
public function testAutoPagingThreePages()
|
||||
{
|
||||
$collection = Collection::constructFrom(
|
||||
$this->pageableModelResponse(array('pm_123', 'pm_124'), true),
|
||||
new Util\RequestOptions()
|
||||
);
|
||||
$collection->setRequestParams(array('foo' => 'bar'));
|
||||
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/pageablemodels',
|
||||
array(
|
||||
'foo' => 'bar',
|
||||
'starting_after' => 'pm_124'
|
||||
),
|
||||
$this->pageableModelResponse(array('pm_125', 'pm_126'), true)
|
||||
);
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/pageablemodels',
|
||||
array(
|
||||
'foo' => 'bar',
|
||||
'starting_after' => 'pm_126'
|
||||
),
|
||||
$this->pageableModelResponse(array('pm_127'), false)
|
||||
);
|
||||
|
||||
$seen = array();
|
||||
foreach ($collection->autoPagingIterator() as $item) {
|
||||
array_push($seen, $item['id']);
|
||||
}
|
||||
|
||||
$this->assertSame($seen, array('pm_123', 'pm_124', 'pm_125', 'pm_126', 'pm_127'));
|
||||
}
|
||||
|
||||
public function testIteratorToArray()
|
||||
{
|
||||
$collection = Collection::constructFrom(
|
||||
$this->pageableModelResponse(array('pm_123', 'pm_124'), true),
|
||||
new Util\RequestOptions()
|
||||
);
|
||||
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/pageablemodels',
|
||||
array(
|
||||
'starting_after' => 'pm_124'
|
||||
),
|
||||
$this->pageableModelResponse(array('pm_125', 'pm_126'), true)
|
||||
);
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/pageablemodels',
|
||||
array(
|
||||
'starting_after' => 'pm_126'
|
||||
),
|
||||
$this->pageableModelResponse(array('pm_127'), false)
|
||||
);
|
||||
|
||||
$seen = array();
|
||||
foreach (iterator_to_array($collection->autoPagingIterator()) as $item) {
|
||||
array_push($seen, $item['id']);
|
||||
}
|
||||
|
||||
$this->assertSame($seen, array('pm_123', 'pm_124', 'pm_125', 'pm_126', 'pm_127'));
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class CountrySpecTest extends TestCase
|
||||
{
|
||||
public function testRetrieve()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$country = "US";
|
||||
$d = CountrySpec::retrieve($country);
|
||||
$this->assertSame($d->object, "country_spec");
|
||||
$this->assertSame($d->id, $country);
|
||||
$this->assertGreaterThan(0, count($d->supported_bank_account_currencies));
|
||||
$this->assertGreaterThan(0, count($d->supported_payment_currencies));
|
||||
$this->assertGreaterThan(0, count($d->supported_payment_methods));
|
||||
$this->assertGreaterThan(0, count($d->verification_fields));
|
||||
}
|
||||
|
||||
public function testList()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$d = CountrySpec::all();
|
||||
$this->assertSame($d->object, "list");
|
||||
$this->assertGreaterThan(0, count($d->data));
|
||||
$this->assertSame($d->data[0]->object, "country_spec");
|
||||
$this->assertInstanceOf("Stripe\\CountrySpec", $d->data[0]);
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class CouponTest extends TestCase
|
||||
{
|
||||
public function testSave()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$id = 'test_coupon-' . self::generateRandomString(20);
|
||||
$c = Coupon::create(
|
||||
array(
|
||||
'percent_off' => 25,
|
||||
'duration' => 'repeating',
|
||||
'duration_in_months' => 5,
|
||||
'id' => $id,
|
||||
)
|
||||
);
|
||||
$this->assertSame($id, $c->id);
|
||||
// @codingStandardsIgnoreStart
|
||||
$this->assertSame(25, $c->percent_off);
|
||||
// @codingStandardsIgnoreEnd
|
||||
$c->metadata['foo'] = 'bar';
|
||||
$c->save();
|
||||
|
||||
$stripeCoupon = Coupon::retrieve($id);
|
||||
$this->assertEquals($c->metadata, $stripeCoupon->metadata);
|
||||
}
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
use Stripe\HttpClient\CurlClient;
|
||||
|
||||
class CurlClientTest extends TestCase
|
||||
{
|
||||
public function testTimeout()
|
||||
{
|
||||
$curl = new CurlClient();
|
||||
$this->assertSame(CurlClient::DEFAULT_TIMEOUT, $curl->getTimeout());
|
||||
$this->assertSame(CurlClient::DEFAULT_CONNECT_TIMEOUT, $curl->getConnectTimeout());
|
||||
|
||||
// implicitly tests whether we're returning the CurlClient instance
|
||||
$curl = $curl->setConnectTimeout(1)->setTimeout(10);
|
||||
$this->assertSame(1, $curl->getConnectTimeout());
|
||||
$this->assertSame(10, $curl->getTimeout());
|
||||
|
||||
$curl->setTimeout(-1);
|
||||
$curl->setConnectTimeout(-999);
|
||||
$this->assertSame(0, $curl->getTimeout());
|
||||
$this->assertSame(0, $curl->getConnectTimeout());
|
||||
}
|
||||
|
||||
public function testDefaultOptions()
|
||||
{
|
||||
// make sure options array loads/saves properly
|
||||
$optionsArray = array(CURLOPT_PROXY => 'localhost:80');
|
||||
$withOptionsArray = new CurlClient($optionsArray);
|
||||
$this->assertSame($withOptionsArray->getDefaultOptions(), $optionsArray);
|
||||
|
||||
// make sure closure-based options work properly, including argument passing
|
||||
$ref = null;
|
||||
$withClosure = new CurlClient(function ($method, $absUrl, $headers, $params, $hasFile) use (&$ref) {
|
||||
$ref = func_get_args();
|
||||
return array();
|
||||
});
|
||||
|
||||
$withClosure->request('get', 'https://httpbin.org/status/200', array(), array(), false);
|
||||
$this->assertSame($ref, array('get', 'https://httpbin.org/status/200', array(), array(), false));
|
||||
|
||||
// this is the last test case that will run, since it'll throw an exception at the end
|
||||
$withBadClosure = new CurlClient(function () {
|
||||
return 'thisShouldNotWork';
|
||||
});
|
||||
$this->setExpectedException('Stripe\Error\Api', "Non-array value returned by defaultOptions CurlClient callback");
|
||||
$withBadClosure->request('get', 'https://httpbin.org/status/200', array(), array(), false);
|
||||
}
|
||||
|
||||
public function testEncode()
|
||||
{
|
||||
$a = array(
|
||||
'my' => 'value',
|
||||
'that' => array('your' => 'example'),
|
||||
'bar' => 1,
|
||||
'baz' => null
|
||||
);
|
||||
|
||||
$enc = CurlClient::encode($a);
|
||||
$this->assertSame('my=value&that%5Byour%5D=example&bar=1', $enc);
|
||||
|
||||
$a = array('that' => array('your' => 'example', 'foo' => null));
|
||||
$enc = CurlClient::encode($a);
|
||||
$this->assertSame('that%5Byour%5D=example', $enc);
|
||||
|
||||
$a = array('that' => 'example', 'foo' => array('bar', 'baz'));
|
||||
$enc = CurlClient::encode($a);
|
||||
$this->assertSame('that=example&foo%5B%5D=bar&foo%5B%5D=baz', $enc);
|
||||
|
||||
$a = array(
|
||||
'my' => 'value',
|
||||
'that' => array('your' => array('cheese', 'whiz', null)),
|
||||
'bar' => 1,
|
||||
'baz' => null
|
||||
);
|
||||
|
||||
$enc = CurlClient::encode($a);
|
||||
$expected = 'my=value&that%5Byour%5D%5B%5D=cheese'
|
||||
. '&that%5Byour%5D%5B%5D=whiz&bar=1';
|
||||
$this->assertSame($expected, $enc);
|
||||
|
||||
// Ignores an empty array
|
||||
$enc = CurlClient::encode(array('foo' => array(), 'bar' => 'baz'));
|
||||
$expected = 'bar=baz';
|
||||
$this->assertSame($expected, $enc);
|
||||
|
||||
$a = array('foo' => array(array('bar' => 'baz'), array('bar' => 'bin')));
|
||||
$enc = CurlClient::encode($a);
|
||||
$this->assertSame('foo%5B0%5D%5Bbar%5D=baz&foo%5B1%5D%5Bbar%5D=bin', $enc);
|
||||
}
|
||||
|
||||
public function testSslOption()
|
||||
{
|
||||
// make sure options array loads/saves properly
|
||||
$optionsArray = array(CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1);
|
||||
$withOptionsArray = new CurlClient($optionsArray);
|
||||
$this->assertSame($withOptionsArray->getDefaultOptions(), $optionsArray);
|
||||
}
|
||||
}
|
||||
@@ -1,278 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class CustomerTest extends TestCase
|
||||
{
|
||||
public function testDeletion()
|
||||
{
|
||||
$customer = self::createTestCustomer();
|
||||
$customer->delete();
|
||||
|
||||
$this->assertTrue($customer->deleted);
|
||||
$this->assertNull($customer['active_card']);
|
||||
}
|
||||
|
||||
public function testSave()
|
||||
{
|
||||
$customer = self::createTestCustomer();
|
||||
|
||||
$customer->email = 'gdb@stripe.com';
|
||||
$customer->save();
|
||||
$this->assertSame($customer->email, 'gdb@stripe.com');
|
||||
|
||||
$stripeCustomer = Customer::retrieve($customer->id);
|
||||
$this->assertSame($customer->email, $stripeCustomer->email);
|
||||
|
||||
Stripe::setApiKey(null);
|
||||
$customer = Customer::create(null, self::API_KEY);
|
||||
$customer->email = 'gdb@stripe.com';
|
||||
$customer->save();
|
||||
|
||||
self::authorizeFromEnv();
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$this->assertSame($updatedCustomer->email, 'gdb@stripe.com');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Stripe\Error\InvalidRequest
|
||||
*/
|
||||
public function testBogusAttribute()
|
||||
{
|
||||
$customer = self::createTestCustomer();
|
||||
$customer->bogus = 'bogus';
|
||||
$customer->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testUpdateDescriptionEmpty()
|
||||
{
|
||||
$customer = self::createTestCustomer();
|
||||
$customer->description = '';
|
||||
}
|
||||
|
||||
public function testUpdateDescriptionNull()
|
||||
{
|
||||
$customer = self::createTestCustomer(array('description' => 'foo bar'));
|
||||
$customer->description = null;
|
||||
|
||||
$customer->save();
|
||||
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$this->assertSame(null, $updatedCustomer->description);
|
||||
}
|
||||
|
||||
public function testUpdateMetadata()
|
||||
{
|
||||
$customer = self::createTestCustomer();
|
||||
|
||||
$customer->metadata['test'] = 'foo bar';
|
||||
$customer->save();
|
||||
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$this->assertSame('foo bar', $updatedCustomer->metadata['test']);
|
||||
}
|
||||
|
||||
public function testDeleteMetadata()
|
||||
{
|
||||
$customer = self::createTestCustomer();
|
||||
|
||||
$customer->metadata = null;
|
||||
$customer->save();
|
||||
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$this->assertSame(0, count($updatedCustomer->metadata->keys()));
|
||||
}
|
||||
|
||||
public function testUpdateSomeMetadata()
|
||||
{
|
||||
$customer = self::createTestCustomer();
|
||||
$customer->metadata['shoe size'] = '7';
|
||||
$customer->metadata['shirt size'] = 'XS';
|
||||
$customer->save();
|
||||
|
||||
$customer->metadata['shoe size'] = '9';
|
||||
$customer->save();
|
||||
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$this->assertSame('XS', $updatedCustomer->metadata['shirt size']);
|
||||
$this->assertSame('9', $updatedCustomer->metadata['shoe size']);
|
||||
}
|
||||
|
||||
public function testUpdateAllMetadata()
|
||||
{
|
||||
$customer = self::createTestCustomer();
|
||||
$customer->metadata['shoe size'] = '7';
|
||||
$customer->metadata['shirt size'] = 'XS';
|
||||
$customer->save();
|
||||
|
||||
$customer->metadata = array('shirt size' => 'XL');
|
||||
$customer->save();
|
||||
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$this->assertSame('XL', $updatedCustomer->metadata['shirt size']);
|
||||
$this->assertFalse(isset($updatedCustomer->metadata['shoe size']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Stripe\Error\InvalidRequest
|
||||
*/
|
||||
public function testUpdateInvalidMetadata()
|
||||
{
|
||||
$customer = self::createTestCustomer();
|
||||
$customer->metadata = 'something';
|
||||
$customer->save();
|
||||
}
|
||||
|
||||
public function testCancelSubscription()
|
||||
{
|
||||
$planID = 'gold-' . self::generateRandomString(20);
|
||||
self::retrieveOrCreatePlan($planID);
|
||||
|
||||
$customer = self::createTestCustomer(
|
||||
array(
|
||||
'plan' => $planID,
|
||||
)
|
||||
);
|
||||
|
||||
$customer->cancelSubscription(array('at_period_end' => true));
|
||||
$this->assertSame($customer->subscription->status, 'active');
|
||||
$this->assertTrue($customer->subscription->cancel_at_period_end);
|
||||
$customer->cancelSubscription();
|
||||
$this->assertSame($customer->subscription->status, 'canceled');
|
||||
}
|
||||
|
||||
public function testCustomerAddCard()
|
||||
{
|
||||
$token = Token::create(
|
||||
array("card" => array(
|
||||
"number" => "4242424242424242",
|
||||
"exp_month" => 5,
|
||||
"exp_year" => date('Y') + 3,
|
||||
"cvc" => "314"
|
||||
))
|
||||
);
|
||||
|
||||
$customer = $this->createTestCustomer();
|
||||
$createdCard = $customer->sources->create(array("card" => $token->id));
|
||||
$customer->save();
|
||||
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$updatedCards = $updatedCustomer->sources->all();
|
||||
$this->assertSame(count($updatedCards["data"]), 2);
|
||||
}
|
||||
|
||||
public function testCustomerUpdateCard()
|
||||
{
|
||||
$customer = $this->createTestCustomer();
|
||||
$customer->save();
|
||||
|
||||
$sources = $customer->sources->all();
|
||||
$this->assertSame(count($sources["data"]), 1);
|
||||
|
||||
$card = $sources['data'][0];
|
||||
$card->name = "Jane Austen";
|
||||
$card->save();
|
||||
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$updatedCards = $updatedCustomer->sources->all();
|
||||
$this->assertSame($updatedCards["data"][0]->name, "Jane Austen");
|
||||
}
|
||||
|
||||
public function testCustomerDeleteCard()
|
||||
{
|
||||
$token = Token::create(
|
||||
array("card" => array(
|
||||
"number" => "4242424242424242",
|
||||
"exp_month" => 5,
|
||||
"exp_year" => date('Y') + 3,
|
||||
"cvc" => "314"
|
||||
))
|
||||
);
|
||||
|
||||
$customer = $this->createTestCustomer();
|
||||
$createdCard = $customer->sources->create(array("card" => $token->id));
|
||||
$customer->save();
|
||||
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$updatedCards = $updatedCustomer->sources->all();
|
||||
$this->assertSame(count($updatedCards["data"]), 2);
|
||||
|
||||
$deleteStatus = $updatedCustomer->sources->retrieve($createdCard->id)->delete();
|
||||
$this->assertTrue($deleteStatus->deleted);
|
||||
$updatedCustomer->save();
|
||||
|
||||
$postDeleteCustomer = Customer::retrieve($customer->id);
|
||||
$postDeleteCards = $postDeleteCustomer->sources->all();
|
||||
$this->assertSame(count($postDeleteCards["data"]), 1);
|
||||
}
|
||||
|
||||
public function testCustomerAddSource()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$token = Token::create(
|
||||
array("card" => array(
|
||||
"number" => "4242424242424242",
|
||||
"exp_month" => 5,
|
||||
"exp_year" => date('Y') + 3,
|
||||
"cvc" => "314"
|
||||
))
|
||||
);
|
||||
|
||||
$customer = $this->createTestCustomer();
|
||||
$createdSource = $customer->sources->create(array("source" => $token->id));
|
||||
$customer->save();
|
||||
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$updatedSources = $updatedCustomer->sources->all();
|
||||
$this->assertSame(count($updatedSources["data"]), 2);
|
||||
}
|
||||
|
||||
public function testCustomerUpdateSource()
|
||||
{
|
||||
$customer = $this->createTestCustomer();
|
||||
$customer->save();
|
||||
|
||||
$sources = $customer->sources->all();
|
||||
$this->assertSame(count($sources["data"]), 1);
|
||||
|
||||
$source = $sources['data'][0];
|
||||
$source->name = "Jane Austen";
|
||||
$source->save();
|
||||
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$updatedSources = $updatedCustomer->sources->all();
|
||||
$this->assertSame($updatedSources["data"][0]->name, "Jane Austen");
|
||||
}
|
||||
|
||||
public function testCustomerDeleteSource()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$token = Token::create(
|
||||
array("card" => array(
|
||||
"number" => "4242424242424242",
|
||||
"exp_month" => 5,
|
||||
"exp_year" => date('Y') + 3,
|
||||
"cvc" => "314"
|
||||
))
|
||||
);
|
||||
|
||||
$customer = $this->createTestCustomer();
|
||||
$createdSource = $customer->sources->create(array("source" => $token->id));
|
||||
$customer->save();
|
||||
|
||||
$updatedCustomer = Customer::retrieve($customer->id);
|
||||
$updatedSources = $updatedCustomer->sources->all();
|
||||
$this->assertSame(count($updatedSources["data"]), 2);
|
||||
|
||||
$deleteStatus = $updatedCustomer->sources->retrieve($createdSource->id)->delete();
|
||||
$this->assertTrue($deleteStatus->deleted);
|
||||
$updatedCustomer->save();
|
||||
|
||||
$postDeleteCustomer = Customer::retrieve($customer->id);
|
||||
$postDeleteSources = $postDeleteCustomer->sources->all();
|
||||
$this->assertSame(count($postDeleteSources["data"]), 1);
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class DiscountTest extends TestCase
|
||||
{
|
||||
public function testDeletion()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$id = 'test-coupon-' . self::generateRandomString(20);
|
||||
$coupon = Coupon::create(
|
||||
array(
|
||||
'percent_off' => 25,
|
||||
'duration' => 'repeating',
|
||||
'duration_in_months' => 5,
|
||||
'id' => $id,
|
||||
)
|
||||
);
|
||||
$customer = self::createTestCustomer(array('coupon' => $id));
|
||||
|
||||
$this->assertTrue(isset($customer->discount));
|
||||
$this->assertTrue(isset($customer->discount->coupon));
|
||||
$this->assertSame($id, $customer->discount->coupon->id);
|
||||
|
||||
$customer->deleteDiscount();
|
||||
$this->assertFalse(isset($customer->discount));
|
||||
|
||||
$customer = Customer::retrieve($customer->id);
|
||||
$this->assertFalse(isset($customer->discount));
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class DisputeTest extends TestCase
|
||||
{
|
||||
public function testUrls()
|
||||
{
|
||||
$this->assertSame(Dispute::classUrl(), '/v1/disputes');
|
||||
$dispute = new Dispute('dp_123');
|
||||
$this->assertSame($dispute->instanceUrl(), '/v1/disputes/dp_123');
|
||||
}
|
||||
|
||||
private function createDisputedCharge()
|
||||
{
|
||||
$card = array(
|
||||
'number' => '4000000000000259',
|
||||
'exp_month' => 5,
|
||||
'exp_year' => date('Y') + 1
|
||||
);
|
||||
|
||||
$c = Charge::create(
|
||||
array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
'card' => $card
|
||||
)
|
||||
);
|
||||
$c = Charge::retrieve($c->id);
|
||||
|
||||
$attempts = 0;
|
||||
|
||||
while ($c->dispute === null) {
|
||||
if ($attempts > 5) {
|
||||
throw new \Exception("Charge is taking too long to be disputed");
|
||||
}
|
||||
sleep(1);
|
||||
$c = Charge::retrieve($c->id);
|
||||
$attempts += 1;
|
||||
}
|
||||
|
||||
return $c;
|
||||
}
|
||||
|
||||
public function testAll()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$sublist = Dispute::all(
|
||||
array(
|
||||
'limit' => 3,
|
||||
)
|
||||
);
|
||||
$this->assertSame(3, count($sublist->data));
|
||||
}
|
||||
|
||||
|
||||
public function testUpdate()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$c = $this->createDisputedCharge();
|
||||
|
||||
$d = Dispute::retrieve($c->dispute);
|
||||
$d->evidence["customer_name"] = "Bob";
|
||||
$s = $d->save();
|
||||
|
||||
$this->assertSame($c->dispute, $s->id);
|
||||
$this->assertSame("Bob", $s->evidence["customer_name"]);
|
||||
}
|
||||
|
||||
public function testClose()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$c = $this->createDisputedCharge();
|
||||
$d = Dispute::retrieve($c->dispute);
|
||||
|
||||
$this->assertNotSame("lost", $d->status);
|
||||
|
||||
$d->close();
|
||||
|
||||
$this->assertSame("lost", $d->status);
|
||||
}
|
||||
|
||||
public function testRetrieve()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$c = $this->createDisputedCharge();
|
||||
|
||||
$d = Dispute::retrieve($c->dispute);
|
||||
|
||||
$this->assertSame($c->dispute, $d->id);
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class ErrorTest extends TestCase
|
||||
{
|
||||
public function testCreation()
|
||||
{
|
||||
try {
|
||||
throw new Error\Api(
|
||||
"hello",
|
||||
500,
|
||||
"{'foo':'bar'}",
|
||||
array('foo' => 'bar')
|
||||
);
|
||||
$this->fail("Did not raise error");
|
||||
} catch (Error\Api $e) {
|
||||
$this->assertSame("hello", $e->getMessage());
|
||||
$this->assertSame(500, $e->getHttpStatus());
|
||||
$this->assertSame("{'foo':'bar'}", $e->getHttpBody());
|
||||
$this->assertSame(array('foo' => 'bar'), $e->getJsonBody());
|
||||
$this->assertSame(null, $e->getHttpHeaders());
|
||||
$this->assertSame(null, $e->getRequestId());
|
||||
}
|
||||
}
|
||||
|
||||
public function testResponseHeaders()
|
||||
{
|
||||
try {
|
||||
throw new Error\Api(
|
||||
"hello",
|
||||
500,
|
||||
"{'foo':'bar'}",
|
||||
array('foo' => 'bar'),
|
||||
array('Request-Id' => 'req_bar')
|
||||
);
|
||||
$this->fail("Did not raise error");
|
||||
} catch (Error\Api $e) {
|
||||
$this->assertSame(array('Request-Id' => 'req_bar'), $e->getHttpHeaders());
|
||||
$this->assertSame('req_bar', $e->getRequestId());
|
||||
}
|
||||
}
|
||||
|
||||
public function testCode()
|
||||
{
|
||||
try {
|
||||
throw new Error\Card(
|
||||
"hello",
|
||||
"some_param",
|
||||
"some_code",
|
||||
400,
|
||||
"{'foo':'bar'}",
|
||||
array('foo' => 'bar')
|
||||
);
|
||||
$this->fail("Did not raise error");
|
||||
} catch (Error\Card $e) {
|
||||
$this->assertSame("some_param", $e->getStripeParam());
|
||||
$this->assertSame('some_code', $e->getStripeCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class ExternalAccountTest extends TestCase
|
||||
{
|
||||
public function testVerify()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$bankAccountToken = Token::create(
|
||||
array(
|
||||
'bank_account' => array(
|
||||
'country' => 'US',
|
||||
'routing_number' => '110000000',
|
||||
'account_number' => '000123456789',
|
||||
'account_holder_name' => 'Jane Austen',
|
||||
'account_holder_type' => 'company'
|
||||
)
|
||||
)
|
||||
);
|
||||
$customer = Customer::create();
|
||||
$externalAccount = $customer->sources->create(array('bank_account' => $bankAccountToken->id));
|
||||
$verifiedAccount = $externalAccount->verify(array('amounts' => array(32, 45)), null);
|
||||
|
||||
$base = Customer::classUrl();
|
||||
$parentExtn = $externalAccount['customer'];
|
||||
$extn = $externalAccount['id'];
|
||||
$this->assertEquals("$base/$parentExtn/sources/$extn", $externalAccount->instanceUrl());
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class FileUploadTest extends TestCase
|
||||
{
|
||||
public function testCreateFile()
|
||||
{
|
||||
$fp = fopen(dirname(__FILE__).'/../data/test.png', 'r');
|
||||
self::authorizeFromEnv();
|
||||
$file = FileUpload::create(
|
||||
array(
|
||||
'purpose' => 'dispute_evidence',
|
||||
'file' => $fp,
|
||||
)
|
||||
);
|
||||
fclose($fp);
|
||||
$this->assertSame(95, $file->size);
|
||||
$this->assertSame('png', $file->type);
|
||||
}
|
||||
|
||||
public function testCreateAndRetrieveCurlFile()
|
||||
{
|
||||
if (!class_exists('\CurlFile', false)) {
|
||||
// Older PHP versions don't support this
|
||||
return;
|
||||
}
|
||||
|
||||
$curlFile = new \CurlFile(dirname(__FILE__).'/../data/test.png');
|
||||
self::authorizeFromEnv();
|
||||
$file = FileUpload::create(
|
||||
array(
|
||||
'purpose' => 'dispute_evidence',
|
||||
'file' => $curlFile,
|
||||
)
|
||||
);
|
||||
$this->assertSame(95, $file->size);
|
||||
$this->assertSame('png', $file->type);
|
||||
|
||||
// Just check that we don't get exceptions
|
||||
$file = FileUpload::retrieve($file->id);
|
||||
$file->refresh();
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class InvalidRequestErrorTest extends TestCase
|
||||
{
|
||||
public function testInvalidObject()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
try {
|
||||
Customer::retrieve('invalid');
|
||||
} catch (Error\InvalidRequest $e) {
|
||||
$this->assertSame(404, $e->getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
public function testBadData()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
try {
|
||||
Charge::create();
|
||||
} catch (Error\InvalidRequest $e) {
|
||||
$this->assertSame(400, $e->getHttpStatus());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class InvoiceTest extends TestCase
|
||||
{
|
||||
public function testUpcoming()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$customer = self::createTestCustomer();
|
||||
|
||||
InvoiceItem::create(array(
|
||||
'customer' => $customer->id,
|
||||
'amount' => 0,
|
||||
'currency' => 'usd',
|
||||
));
|
||||
|
||||
$invoice = Invoice::upcoming(array(
|
||||
'customer' => $customer->id,
|
||||
));
|
||||
$this->assertSame($invoice->customer, $customer->id);
|
||||
$this->assertSame($invoice->attempted, false);
|
||||
}
|
||||
|
||||
public function testItemsAccessWithParameter()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$customer = self::createTestCustomer();
|
||||
|
||||
InvoiceItem::create(array(
|
||||
'customer' => $customer->id,
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
));
|
||||
|
||||
$invoice = Invoice::upcoming(
|
||||
array(
|
||||
'customer' => $customer->id,
|
||||
)
|
||||
);
|
||||
|
||||
$lines = $invoice->lines->all(array('limit' => 10));
|
||||
|
||||
$this->assertSame(count($lines->data), 1);
|
||||
$this->assertSame($lines->data[0]->amount, 100);
|
||||
}
|
||||
|
||||
// This is really just making sure that this operation does not trigger any
|
||||
// warnings, as it's highly nested.
|
||||
public function testAll()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$invoices = Invoice::all();
|
||||
$this->assertGreaterThan(0, count($invoices));
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class PayoutTest extends TestCase
|
||||
{
|
||||
private $managedAccount = null;
|
||||
|
||||
/**
|
||||
* Create a managed account and put enough funds in the balance
|
||||
* to be able to create a payout afterwards. Also try to re-use
|
||||
* the managed account across the tests to avoid hitting the
|
||||
* rate limit for account creation.
|
||||
*/
|
||||
private function createAccountWithBalance()
|
||||
{
|
||||
if ($this->managedAccount === null) {
|
||||
self::authorizeFromEnv();
|
||||
$account = self::createTestManagedAccount();
|
||||
|
||||
$charge = \Stripe\Charge::create(array(
|
||||
'currency' => 'usd',
|
||||
'amount' => '10000',
|
||||
'source' => array(
|
||||
'object' => 'card',
|
||||
'number' => '4000000000000077',
|
||||
'exp_month' => '09',
|
||||
'exp_year' => date('Y') + 3,
|
||||
),
|
||||
'destination' => array(
|
||||
'account' => $account->id
|
||||
)
|
||||
));
|
||||
|
||||
$this->managedAccount = $account;
|
||||
}
|
||||
|
||||
return $this->managedAccount;
|
||||
}
|
||||
|
||||
private function createPayoutFromManagedAccount($accountId)
|
||||
{
|
||||
$payout = Payout::create(
|
||||
array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
),
|
||||
array(
|
||||
'stripe_account' => $accountId
|
||||
)
|
||||
);
|
||||
|
||||
return $payout;
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$account = self::createAccountWithBalance();
|
||||
$payout = self::createPayoutFromManagedAccount($account->id);
|
||||
|
||||
$this->assertSame('pending', $payout->status);
|
||||
}
|
||||
|
||||
public function testRetrieve()
|
||||
{
|
||||
$account = self::createAccountWithBalance();
|
||||
$payout = self::createPayoutFromManagedAccount($account->id);
|
||||
$reloaded = Payout::retrieve($payout->id, array('stripe_account' => $account->id));
|
||||
$this->assertSame($reloaded->id, $payout->id);
|
||||
}
|
||||
|
||||
public function testPayoutUpdateMetadata()
|
||||
{
|
||||
$account = self::createAccountWithBalance();
|
||||
$payout = self::createPayoutFromManagedAccount($account->id);
|
||||
$payout->metadata['test'] = 'foo bar';
|
||||
$payout->save();
|
||||
|
||||
$updatedPayout = Payout::retrieve($payout->id, array('stripe_account' => $account->id));
|
||||
$this->assertSame('foo bar', $updatedPayout->metadata['test']);
|
||||
}
|
||||
|
||||
public function testPayoutUpdateMetadataAll()
|
||||
{
|
||||
$account = self::createAccountWithBalance();
|
||||
$payout = self::createPayoutFromManagedAccount($account->id);
|
||||
|
||||
$payout->metadata = array('test' => 'foo bar');
|
||||
$payout->save();
|
||||
|
||||
$updatedPayout = Payout::retrieve($payout->id, array('stripe_account' => $account->id));
|
||||
$this->assertSame('foo bar', $updatedPayout->metadata['test']);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class PermissionErrorTest extends TestCase
|
||||
{
|
||||
private function permissionErrorResponse()
|
||||
{
|
||||
return array(
|
||||
'error' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Stripe\Error\Permission
|
||||
*/
|
||||
public function testPermission()
|
||||
{
|
||||
$this->mockRequest('GET', '/v1/accounts/acct_DEF', array(), $this->permissionErrorResponse(), 403);
|
||||
Account::retrieve('acct_DEF');
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class PlanTest extends TestCase
|
||||
{
|
||||
public function testDeletion()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$p = Plan::create(array(
|
||||
'amount' => 2000,
|
||||
'interval' => 'month',
|
||||
'currency' => 'usd',
|
||||
'name' => 'Plan',
|
||||
'id' => 'gold-' . self::generateRandomString(20)
|
||||
));
|
||||
$p->delete();
|
||||
$this->assertTrue($p->deleted);
|
||||
}
|
||||
|
||||
public function testFalseyId()
|
||||
{
|
||||
try {
|
||||
$retrievedPlan = Plan::retrieve('0');
|
||||
} catch (Error\InvalidRequest $e) {
|
||||
// Can either succeed or 404, all other errors are bad
|
||||
if ($e->httpStatus !== 404) {
|
||||
$this->fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testSave()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
$planID = 'gold-' . self::generateRandomString(20);
|
||||
$p = Plan::create(array(
|
||||
'amount' => 2000,
|
||||
'interval' => 'month',
|
||||
'currency' => 'usd',
|
||||
'name' => 'Plan',
|
||||
'id' => $planID
|
||||
));
|
||||
$p->name = 'A new plan name';
|
||||
$p->save();
|
||||
$this->assertSame($p->name, 'A new plan name');
|
||||
|
||||
$stripePlan = Plan::retrieve($planID);
|
||||
$this->assertSame($p->name, $stripePlan->name);
|
||||
}
|
||||
}
|
||||
@@ -1,158 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class ProductSKUOrderTest extends TestCase
|
||||
{
|
||||
public function testProductFalseyId()
|
||||
{
|
||||
try {
|
||||
Stripe::setApiKey('sk_test_JieJALRz7rPz7boV17oMma7a');
|
||||
$retrievedProduct = Product::retrieve('0');
|
||||
} catch (Error\InvalidRequest $e) {
|
||||
// Can either succeed or 404, all other errors are bad
|
||||
if ($e->httpStatus !== 404) {
|
||||
$this->fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testProductCreateUpdateRead()
|
||||
{
|
||||
|
||||
Stripe::setApiKey('sk_test_JieJALRz7rPz7boV17oMma7a');
|
||||
$ProductID = 'gold-' . self::generateRandomString(20);
|
||||
$p = Product::create(array(
|
||||
'name' => 'Gold Product',
|
||||
'id' => $ProductID,
|
||||
'url' => 'www.stripe.com/gold'
|
||||
));
|
||||
$this->assertSame($p->url, 'www.stripe.com/gold');
|
||||
|
||||
$p->name = 'A new Product name';
|
||||
$p->save();
|
||||
$this->assertSame($p->name, 'A new Product name');
|
||||
$this->assertSame($p->url, 'www.stripe.com/gold');
|
||||
|
||||
$stripeProduct = Product::retrieve($ProductID);
|
||||
$this->assertSame($p->name, $stripeProduct->name);
|
||||
$this->assertSame($stripeProduct->url, 'www.stripe.com/gold');
|
||||
}
|
||||
|
||||
public function testSKUCreateUpdateRead()
|
||||
{
|
||||
Stripe::setApiKey('sk_test_JieJALRz7rPz7boV17oMma7a');
|
||||
$ProductID = 'silver-' . self::generateRandomString(20);
|
||||
$p = Product::create(array(
|
||||
'name' => 'Silver Product',
|
||||
'id' => $ProductID,
|
||||
'url' => 'www.stripe.com/silver'
|
||||
));
|
||||
|
||||
$SkuID = 'silver-sku-' . self::generateRandomString(20);
|
||||
$sku = SKU::create(array(
|
||||
'price' => 500,
|
||||
'currency' => 'usd',
|
||||
'id' => $SkuID,
|
||||
'inventory' => array(
|
||||
'type' => 'finite',
|
||||
'quantity' => 40
|
||||
),
|
||||
'product' => $ProductID
|
||||
));
|
||||
|
||||
$sku->price = 600;
|
||||
$sku->inventory->quantity = 50;
|
||||
$sku->save();
|
||||
$this->assertSame($sku->price, 600);
|
||||
$this->assertSame(50, $sku->inventory->quantity);
|
||||
|
||||
$stripeSku = SKU::retrieve($SkuID);
|
||||
$this->assertSame($sku->price, 600);
|
||||
$this->assertSame('finite', $sku->inventory->type);
|
||||
$this->assertSame(50, $sku->inventory->quantity);
|
||||
}
|
||||
|
||||
public function testSKUProductDelete()
|
||||
{
|
||||
Stripe::setApiKey('sk_test_JieJALRz7rPz7boV17oMma7a');
|
||||
$ProductID = 'silver-' . self::generateRandomString(20);
|
||||
$p = Product::create(array(
|
||||
'name' => 'Silver Product',
|
||||
'id' => $ProductID,
|
||||
'url' => 'stripe.com/silver'
|
||||
));
|
||||
|
||||
$SkuID = 'silver-sku-' . self::generateRandomString(20);
|
||||
$sku = SKU::create(array(
|
||||
'price' => 500,
|
||||
'currency' => 'usd',
|
||||
'id' => $SkuID,
|
||||
'inventory' => array(
|
||||
'type' => 'finite',
|
||||
'quantity' => 40
|
||||
),
|
||||
'product' => $ProductID
|
||||
));
|
||||
|
||||
$deletedSku = $sku->delete();
|
||||
$this->assertTrue($deletedSku->deleted);
|
||||
|
||||
$deletedProduct = $p->delete();
|
||||
$this->assertTrue($deletedProduct->deleted);
|
||||
}
|
||||
|
||||
public function testOrderCreateUpdateRetrievePayReturn()
|
||||
{
|
||||
Stripe::setApiKey('sk_test_JieJALRz7rPz7boV17oMma7a');
|
||||
$ProductID = 'silver-' . self::generateRandomString(20);
|
||||
$p = Product::create(array(
|
||||
'name' => 'Silver Product',
|
||||
'id' => $ProductID,
|
||||
'url' => 'www.stripe.com/silver',
|
||||
'shippable' => false,
|
||||
));
|
||||
|
||||
$SkuID = 'silver-sku-' . self::generateRandomString(20);
|
||||
$sku = SKU::create(array(
|
||||
'price' => 500,
|
||||
'currency' => 'usd',
|
||||
'id' => $SkuID,
|
||||
'inventory' => array(
|
||||
'type' => 'finite',
|
||||
'quantity' => 40
|
||||
),
|
||||
'product' => $ProductID
|
||||
));
|
||||
|
||||
$order = Order::create(array(
|
||||
'items' => array(
|
||||
0 => array(
|
||||
'type' => 'sku',
|
||||
'parent' => $SkuID,
|
||||
),
|
||||
),
|
||||
'currency' => 'usd',
|
||||
'email' => 'foo@bar.com',
|
||||
));
|
||||
|
||||
$order->metadata->foo = "bar";
|
||||
$order->save();
|
||||
|
||||
$stripeOrder = Order::retrieve($order->id);
|
||||
$this->assertSame($order->metadata->foo, "bar");
|
||||
|
||||
$order->pay(array(
|
||||
'source' => array(
|
||||
'object' => 'card',
|
||||
'number' => '4242424242424242',
|
||||
'exp_month' => '05',
|
||||
'exp_year' => '2017'
|
||||
),
|
||||
));
|
||||
$this->assertSame($order->status, 'paid');
|
||||
|
||||
$orderReturn = $order->returnOrder();
|
||||
$this->assertSame($orderReturn->order, $order->id);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class RateLimitErrorTest extends TestCase
|
||||
{
|
||||
private function rateLimitErrorResponse()
|
||||
{
|
||||
return array(
|
||||
'error' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Stripe\Error\RateLimit
|
||||
*/
|
||||
public function testRateLimit()
|
||||
{
|
||||
$this->mockRequest('GET', '/v1/accounts/acct_DEF', array(), $this->rateLimitErrorResponse(), 429);
|
||||
Account::retrieve('acct_DEF');
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class RecipientTest extends TestCase
|
||||
{
|
||||
public function testDeletion()
|
||||
{
|
||||
$recipient = self::createTestRecipient();
|
||||
$recipient->delete();
|
||||
|
||||
$this->assertTrue($recipient->deleted);
|
||||
}
|
||||
|
||||
public function testSave()
|
||||
{
|
||||
$recipient = self::createTestRecipient();
|
||||
|
||||
$recipient->email = 'gdb@stripe.com';
|
||||
$recipient->save();
|
||||
$this->assertSame($recipient->email, 'gdb@stripe.com');
|
||||
|
||||
$stripeRecipient = Recipient::retrieve($recipient->id);
|
||||
$this->assertSame($recipient->email, $stripeRecipient->email);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Stripe\Error\InvalidRequest
|
||||
*/
|
||||
public function testBogusAttribute()
|
||||
{
|
||||
$recipient = self::createTestRecipient();
|
||||
$recipient->bogus = 'bogus';
|
||||
$recipient->save();
|
||||
}
|
||||
|
||||
public function testRecipientAddCard()
|
||||
{
|
||||
$token = Token::create(
|
||||
array("card" => array(
|
||||
"number" => "4000056655665556",
|
||||
"exp_month" => 5,
|
||||
"exp_year" => date('Y') + 3,
|
||||
"cvc" => "314"
|
||||
))
|
||||
);
|
||||
|
||||
$recipient = $this->createTestRecipient();
|
||||
$createdCard = $recipient->cards->create(array("card" => $token->id));
|
||||
$recipient->save();
|
||||
|
||||
$updatedRecipient = Recipient::retrieve($recipient->id);
|
||||
$updatedCards = $updatedRecipient->cards->all();
|
||||
$this->assertSame(count($updatedCards["data"]), 1);
|
||||
}
|
||||
|
||||
public function testRecipientUpdateCard()
|
||||
{
|
||||
$token = Token::create(
|
||||
array("card" => array(
|
||||
"number" => "4000056655665556",
|
||||
"exp_month" => 5,
|
||||
"exp_year" => date('Y') + 3,
|
||||
"cvc" => "314"
|
||||
))
|
||||
);
|
||||
|
||||
$recipient = $this->createTestRecipient();
|
||||
$createdCard = $recipient->cards->create(array("card" => $token->id));
|
||||
$recipient->save();
|
||||
|
||||
$createdCards = $recipient->cards->all();
|
||||
$this->assertSame(count($createdCards["data"]), 1);
|
||||
|
||||
$card = $createdCards['data'][0];
|
||||
$card->name = "Jane Austen";
|
||||
$card->save();
|
||||
|
||||
$updatedRecipient = Recipient::retrieve($recipient->id);
|
||||
$updatedCards = $updatedRecipient->cards->all();
|
||||
$this->assertSame($updatedCards["data"][0]->name, "Jane Austen");
|
||||
}
|
||||
|
||||
public function testRecipientDeleteCard()
|
||||
{
|
||||
$token = Token::create(
|
||||
array("card" => array(
|
||||
"number" => "4000056655665556",
|
||||
"exp_month" => 5,
|
||||
"exp_year" => date('Y') + 3,
|
||||
"cvc" => "314"
|
||||
))
|
||||
);
|
||||
|
||||
$recipient = $this->createTestRecipient();
|
||||
$createdCard = $recipient->cards->create(array("card" => $token->id));
|
||||
$recipient->save();
|
||||
|
||||
$updatedRecipient = Recipient::retrieve($recipient->id);
|
||||
$updatedCards = $updatedRecipient->cards->all();
|
||||
$this->assertSame(count($updatedCards["data"]), 1);
|
||||
|
||||
$deleteStatus =
|
||||
$updatedRecipient->cards->retrieve($createdCard->id)->delete();
|
||||
$this->assertTrue($deleteStatus->deleted);
|
||||
$updatedRecipient->save();
|
||||
|
||||
$postDeleteRecipient = Recipient::retrieve($recipient->id);
|
||||
$postDeleteCards = $postDeleteRecipient->cards->all();
|
||||
$this->assertSame(count($postDeleteCards["data"]), 0);
|
||||
}
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class RefundTest extends TestCase
|
||||
{
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$charge = self::createTestCharge();
|
||||
$refund = Refund::create(array('amount' => 100, 'charge' => $charge->id));
|
||||
$this->assertSame(100, $refund->amount);
|
||||
$this->assertSame($charge->id, $refund->charge);
|
||||
}
|
||||
|
||||
public function testUpdateAndRetrieve()
|
||||
{
|
||||
$charge = self::createTestCharge();
|
||||
$ref = Refund::create(array('amount' => 100, 'charge' => $charge->id));
|
||||
$ref->metadata["key"] = "value";
|
||||
$ref->save();
|
||||
$ref = Refund::retrieve($ref->id);
|
||||
$this->assertSame("value", $ref->metadata["key"], "value");
|
||||
}
|
||||
|
||||
public function testListForCharge()
|
||||
{
|
||||
$charge = self::createTestCharge();
|
||||
$refA = Refund::create(array('amount' => 100, 'charge' => $charge->id));
|
||||
$refB = Refund::create(array('amount' => 50, 'charge' => $charge->id));
|
||||
|
||||
$all = Refund::all(array('charge' => $charge));
|
||||
$this->assertSame(false, $all['has_more']);
|
||||
$this->assertSame(2, count($all->data));
|
||||
$this->assertSame($refB->id, $all->data[0]->id);
|
||||
$this->assertSame($refA->id, $all->data[1]->id);
|
||||
}
|
||||
|
||||
public function testList()
|
||||
{
|
||||
$all = Refund::all();
|
||||
|
||||
// Fetches all refunds on this test account.
|
||||
$this->assertSame(true, $all['has_more']);
|
||||
$this->assertSame(10, count($all->data));
|
||||
}
|
||||
|
||||
public function testCreateForBitcoin()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$receiver = $this->createTestBitcoinReceiver("do+fill_now@stripe.com");
|
||||
|
||||
$charge = Charge::create(
|
||||
array(
|
||||
'amount' => $receiver->amount,
|
||||
'currency' => $receiver->currency,
|
||||
'description' => $receiver->description,
|
||||
'source' => $receiver->id
|
||||
)
|
||||
);
|
||||
|
||||
$ref = Refund::create(
|
||||
array(
|
||||
'amount' => $receiver->amount,
|
||||
'refund_address' => 'ABCDEF',
|
||||
'charge' => $charge->id
|
||||
)
|
||||
);
|
||||
$this->assertSame($receiver->amount, $ref->amount);
|
||||
$this->assertNotNull($ref->id);
|
||||
}
|
||||
|
||||
// Deprecated charge endpoints:
|
||||
|
||||
public function testCreateViaCharge()
|
||||
{
|
||||
$charge = self::createTestCharge();
|
||||
$ref = $charge->refunds->create(array('amount' => 100));
|
||||
$this->assertSame(100, $ref->amount);
|
||||
$this->assertSame($charge->id, $ref->charge);
|
||||
}
|
||||
|
||||
public function testUpdateAndRetrieveViaCharge()
|
||||
{
|
||||
$charge = self::createTestCharge();
|
||||
$ref = $charge->refunds->create(array('amount' => 100));
|
||||
$ref->metadata["key"] = "value";
|
||||
$ref->save();
|
||||
$ref = $charge->refunds->retrieve($ref->id);
|
||||
$this->assertSame("value", $ref->metadata["key"], "value");
|
||||
}
|
||||
|
||||
public function testListViaCharge()
|
||||
{
|
||||
$charge = self::createTestCharge();
|
||||
$refA = $charge->refunds->create(array('amount' => 50));
|
||||
$refB = $charge->refunds->create(array('amount' => 50));
|
||||
|
||||
$all = $charge->refunds->all();
|
||||
$this->assertSame(false, $all['has_more']);
|
||||
$this->assertSame(2, count($all->data));
|
||||
$this->assertSame($refB->id, $all->data[0]->id);
|
||||
$this->assertSame($refA->id, $all->data[1]->id);
|
||||
}
|
||||
|
||||
public function testCreateForBitcoinViaCharge()
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$receiver = $this->createTestBitcoinReceiver("do+fill_now@stripe.com");
|
||||
|
||||
$charge = Charge::create(
|
||||
array(
|
||||
'amount' => $receiver->amount,
|
||||
'currency' => $receiver->currency,
|
||||
'description' => $receiver->description,
|
||||
'source' => $receiver->id
|
||||
)
|
||||
);
|
||||
|
||||
$ref = $charge->refunds->create(
|
||||
array(
|
||||
'amount' => $receiver->amount,
|
||||
'refund_address' => 'ABCDEF'
|
||||
)
|
||||
);
|
||||
$this->assertSame($receiver->amount, $ref->amount);
|
||||
$this->assertNotNull($ref->id);
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class RequestOptionsTest extends TestCase
|
||||
{
|
||||
public function testStringAPIKey()
|
||||
{
|
||||
$opts = Util\RequestOptions::parse("foo");
|
||||
$this->assertSame("foo", $opts->apiKey);
|
||||
$this->assertSame(array(), $opts->headers);
|
||||
}
|
||||
|
||||
public function testNull()
|
||||
{
|
||||
$opts = Util\RequestOptions::parse(null);
|
||||
$this->assertSame(null, $opts->apiKey);
|
||||
$this->assertSame(array(), $opts->headers);
|
||||
}
|
||||
|
||||
public function testEmptyArray()
|
||||
{
|
||||
$opts = Util\RequestOptions::parse(array());
|
||||
$this->assertSame(null, $opts->apiKey);
|
||||
$this->assertSame(array(), $opts->headers);
|
||||
}
|
||||
|
||||
public function testAPIKeyArray()
|
||||
{
|
||||
$opts = Util\RequestOptions::parse(
|
||||
array(
|
||||
'api_key' => 'foo',
|
||||
)
|
||||
);
|
||||
$this->assertSame('foo', $opts->apiKey);
|
||||
$this->assertSame(array(), $opts->headers);
|
||||
}
|
||||
|
||||
public function testIdempotentKeyArray()
|
||||
{
|
||||
$opts = Util\RequestOptions::parse(
|
||||
array(
|
||||
'idempotency_key' => 'foo',
|
||||
)
|
||||
);
|
||||
$this->assertSame(null, $opts->apiKey);
|
||||
$this->assertSame(array('Idempotency-Key' => 'foo'), $opts->headers);
|
||||
}
|
||||
|
||||
public function testKeyArray()
|
||||
{
|
||||
$opts = Util\RequestOptions::parse(
|
||||
array(
|
||||
'idempotency_key' => 'foo',
|
||||
'api_key' => 'foo'
|
||||
)
|
||||
);
|
||||
$this->assertSame('foo', $opts->apiKey);
|
||||
$this->assertSame(array('Idempotency-Key' => 'foo'), $opts->headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Stripe\Error\Api
|
||||
*/
|
||||
public function testWrongType()
|
||||
{
|
||||
$opts = Util\RequestOptions::parse(5);
|
||||
}
|
||||
}
|
||||
@@ -1,218 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class SourceTest extends TestCase
|
||||
{
|
||||
public function testRetrieve()
|
||||
{
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/sources/src_foo',
|
||||
array(),
|
||||
array(
|
||||
'id' => 'src_foo',
|
||||
'object' => 'source',
|
||||
)
|
||||
);
|
||||
$source = Source::retrieve('src_foo');
|
||||
$this->assertSame($source->id, 'src_foo');
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/sources',
|
||||
array(
|
||||
'type' => 'bitcoin',
|
||||
'amount' => 1000,
|
||||
'currency' => 'usd',
|
||||
'owner' => array('email' => 'jenny.rosen@example.com'),
|
||||
),
|
||||
array(
|
||||
'id' => 'src_foo',
|
||||
'object' => 'source'
|
||||
)
|
||||
);
|
||||
$source = Source::create(array(
|
||||
'type' => 'bitcoin',
|
||||
'amount' => 1000,
|
||||
'currency' => 'usd',
|
||||
'owner' => array('email' => 'jenny.rosen@example.com'),
|
||||
));
|
||||
$this->assertSame($source->id, 'src_foo');
|
||||
}
|
||||
|
||||
public function testSave()
|
||||
{
|
||||
$response = array(
|
||||
'id' => 'src_foo',
|
||||
'object' => 'source',
|
||||
'metadata' => array(),
|
||||
);
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/sources/src_foo',
|
||||
array(),
|
||||
$response
|
||||
);
|
||||
|
||||
$response['metadata'] = array('foo' => 'bar');
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/sources/src_foo',
|
||||
array(
|
||||
'metadata' => array('foo' => 'bar'),
|
||||
),
|
||||
$response
|
||||
);
|
||||
|
||||
$source = Source::retrieve('src_foo');
|
||||
$source->metadata['foo'] = 'bar';
|
||||
$source->save();
|
||||
$this->assertSame($source->metadata['foo'], 'bar');
|
||||
}
|
||||
|
||||
public function testSaveOwner()
|
||||
{
|
||||
$response = array(
|
||||
'id' => 'src_foo',
|
||||
'object' => 'source',
|
||||
'owner' => array(
|
||||
'name' => null,
|
||||
'address' => null,
|
||||
),
|
||||
);
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/sources/src_foo',
|
||||
array(),
|
||||
$response
|
||||
);
|
||||
|
||||
$response['owner'] = array(
|
||||
'name' => "Stripey McStripe",
|
||||
'address' => array(
|
||||
'line1' => "Test Address",
|
||||
'city' => "Test City",
|
||||
'postal_code' => "12345",
|
||||
'state' => "Test State",
|
||||
'country' => "Test Country",
|
||||
)
|
||||
);
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/sources/src_foo',
|
||||
array(
|
||||
'owner' => array(
|
||||
'name' => "Stripey McStripe",
|
||||
'address' => array(
|
||||
'line1' => "Test Address",
|
||||
'city' => "Test City",
|
||||
'postal_code' => "12345",
|
||||
'state' => "Test State",
|
||||
'country' => "Test Country",
|
||||
),
|
||||
),
|
||||
),
|
||||
$response
|
||||
);
|
||||
|
||||
$source = Source::retrieve('src_foo');
|
||||
$source->owner['name'] = "Stripey McStripe";
|
||||
$source->owner['address'] = array(
|
||||
'line1' => "Test Address",
|
||||
'city' => "Test City",
|
||||
'postal_code' => "12345",
|
||||
'state' => "Test State",
|
||||
'country' => "Test Country",
|
||||
);
|
||||
$source->save();
|
||||
$this->assertSame($source->owner['name'], "Stripey McStripe");
|
||||
$this->assertSame($source->owner['address']['line1'], "Test Address");
|
||||
$this->assertSame($source->owner['address']['city'], "Test City");
|
||||
$this->assertSame($source->owner['address']['postal_code'], "12345");
|
||||
$this->assertSame($source->owner['address']['state'], "Test State");
|
||||
$this->assertSame($source->owner['address']['country'], "Test Country");
|
||||
}
|
||||
|
||||
public function testDeleteAttached()
|
||||
{
|
||||
$response = array(
|
||||
'id' => 'src_foo',
|
||||
'object' => 'source',
|
||||
'customer' => 'cus_bar',
|
||||
);
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/sources/src_foo',
|
||||
array(),
|
||||
$response
|
||||
);
|
||||
|
||||
unset($response['customer']);
|
||||
$this->mockRequest(
|
||||
'DELETE',
|
||||
'/v1/customers/cus_bar/sources/src_foo',
|
||||
array(),
|
||||
$response
|
||||
);
|
||||
|
||||
$source = Source::retrieve('src_foo');
|
||||
$source->delete();
|
||||
$this->assertFalse(array_key_exists('customer', $source));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Stripe\Error\Api
|
||||
*/
|
||||
public function testDeleteUnattached()
|
||||
{
|
||||
$response = array(
|
||||
'id' => 'src_foo',
|
||||
'object' => 'source',
|
||||
);
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/sources/src_foo',
|
||||
array(),
|
||||
$response
|
||||
);
|
||||
|
||||
$source = Source::retrieve('src_foo');
|
||||
$source->delete();
|
||||
}
|
||||
|
||||
public function testVerify()
|
||||
{
|
||||
$response = array(
|
||||
'id' => 'src_foo',
|
||||
'object' => 'source',
|
||||
'verification' => array('status' => 'pending'),
|
||||
);
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/sources/src_foo',
|
||||
array(),
|
||||
$response
|
||||
);
|
||||
|
||||
$response['verification']['status'] = 'succeeded';
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/sources/src_foo/verify',
|
||||
array(
|
||||
'values' => array(32, 45),
|
||||
),
|
||||
$response
|
||||
);
|
||||
|
||||
$source = Source::retrieve('src_foo');
|
||||
$this->assertSame($source->verification->status, 'pending');
|
||||
$source->verify(array(
|
||||
'values' => array(32, 45),
|
||||
));
|
||||
$this->assertSame($source->verification->status, 'succeeded');
|
||||
}
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class StripeObjectTest extends TestCase
|
||||
{
|
||||
public function testArrayAccessorsSemantics()
|
||||
{
|
||||
$s = new StripeObject();
|
||||
$s['foo'] = 'a';
|
||||
$this->assertSame($s['foo'], 'a');
|
||||
$this->assertTrue(isset($s['foo']));
|
||||
unset($s['foo']);
|
||||
$this->assertFalse(isset($s['foo']));
|
||||
}
|
||||
|
||||
public function testNormalAccessorsSemantics()
|
||||
{
|
||||
$s = new StripeObject();
|
||||
$s->foo = 'a';
|
||||
$this->assertSame($s->foo, 'a');
|
||||
$this->assertTrue(isset($s->foo));
|
||||
unset($s->foo);
|
||||
$this->assertFalse(isset($s->foo));
|
||||
}
|
||||
|
||||
public function testArrayAccessorsMatchNormalAccessors()
|
||||
{
|
||||
$s = new StripeObject();
|
||||
$s->foo = 'a';
|
||||
$this->assertSame($s['foo'], 'a');
|
||||
|
||||
$s['bar'] = 'b';
|
||||
$this->assertSame($s->bar, 'b');
|
||||
}
|
||||
|
||||
public function testKeys()
|
||||
{
|
||||
$s = new StripeObject();
|
||||
$s->foo = 'a';
|
||||
$this->assertSame($s->keys(), array('foo'));
|
||||
}
|
||||
|
||||
public function testToArray()
|
||||
{
|
||||
$s = new StripeObject();
|
||||
$s->foo = 'a';
|
||||
|
||||
$converted = $s->__toArray();
|
||||
|
||||
$this->assertInternalType('array', $converted);
|
||||
$this->assertArrayHasKey('foo', $converted);
|
||||
$this->assertEquals('a', $converted['foo']);
|
||||
}
|
||||
|
||||
public function testRecursiveToArray()
|
||||
{
|
||||
$s = new StripeObject();
|
||||
$z = new StripeObject();
|
||||
|
||||
$s->child = $z;
|
||||
$z->foo = 'a';
|
||||
|
||||
$converted = $s->__toArray(true);
|
||||
|
||||
$this->assertInternalType('array', $converted);
|
||||
$this->assertArrayHasKey('child', $converted);
|
||||
$this->assertInternalType('array', $converted['child']);
|
||||
$this->assertArrayHasKey('foo', $converted['child']);
|
||||
$this->assertEquals('a', $converted['child']['foo']);
|
||||
}
|
||||
|
||||
public function testNonexistentProperty()
|
||||
{
|
||||
$s = new StripeObject();
|
||||
$this->assertNull($s->nonexistent);
|
||||
}
|
||||
|
||||
public function testPropertyDoesNotExists()
|
||||
{
|
||||
$s = new StripeObject();
|
||||
$this->assertNull($s['nonexistent']);
|
||||
}
|
||||
|
||||
public function testJsonEncode()
|
||||
{
|
||||
// We can only JSON encode our objects in PHP 5.4+. 5.3 must use ->__toJSON()
|
||||
if (version_compare(phpversion(), '5.4.0', '<')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$s = new StripeObject();
|
||||
$s->foo = 'a';
|
||||
|
||||
$this->assertEquals('{"foo":"a"}', json_encode($s->__toArray()));
|
||||
}
|
||||
|
||||
public function testReplaceNewNestedUpdatable()
|
||||
{
|
||||
StripeObject::init(); // Populate the $nestedUpdatableAttributes Set
|
||||
$s = new StripeObject();
|
||||
|
||||
$s->metadata = array('bar');
|
||||
$this->assertSame($s->metadata, array('bar'));
|
||||
$s->metadata = array('baz', 'qux');
|
||||
$this->assertSame($s->metadata, array('baz', 'qux'));
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class SubscriptionItemTest extends TestCase
|
||||
{
|
||||
public function testCreateUpdateRetrieveListCancel()
|
||||
{
|
||||
$plan0ID = 'gold-' . self::generateRandomString(20);
|
||||
self::retrieveOrCreatePlan($plan0ID);
|
||||
|
||||
$customer = self::createTestCustomer();
|
||||
$sub = Subscription::create(array('plan' => $plan0ID, 'customer' => $customer->id));
|
||||
|
||||
$plan1ID = 'gold-' . self::generateRandomString(20);
|
||||
self::retrieveOrCreatePlan($plan1ID);
|
||||
|
||||
$subItem = SubscriptionItem::create(array('plan' => $plan1ID, 'subscription' => $sub->id));
|
||||
$this->assertSame($subItem->plan->id, $plan1ID);
|
||||
|
||||
$subItem->quantity = 2;
|
||||
$subItem->save();
|
||||
|
||||
$subItem = SubscriptionItem::retrieve($subItem->id);
|
||||
$this->assertSame($subItem->quantity, 2);
|
||||
|
||||
// Update the quantity parameter one more time
|
||||
$subItem = SubscriptionItem::update($subItem->id, array('quantity' => 3));
|
||||
$this->assertSame($subItem->quantity, 3);
|
||||
|
||||
$subItems = SubscriptionItem::all(array('subscription'=>$sub->id, 'limit'=>3));
|
||||
$this->assertSame(get_class($subItems->data[0]), 'Stripe\SubscriptionItem');
|
||||
$this->assertSame(2, count($subItems->data));
|
||||
|
||||
$subItem->delete();
|
||||
$this->assertTrue($subItem->deleted);
|
||||
}
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class SubscriptionTest extends TestCase
|
||||
{
|
||||
|
||||
public function testCustomerCreateUpdateListCancel()
|
||||
{
|
||||
$planID = 'gold-' . self::generateRandomString(20);
|
||||
self::retrieveOrCreatePlan($planID);
|
||||
|
||||
$customer = self::createTestCustomer();
|
||||
|
||||
$sub = $customer->subscriptions->create(array('plan' => $planID));
|
||||
|
||||
$this->assertSame($sub->status, 'active');
|
||||
$this->assertSame($sub->plan->id, $planID);
|
||||
|
||||
$sub->quantity = 2;
|
||||
$sub->save();
|
||||
|
||||
$sub = $customer->subscriptions->retrieve($sub->id);
|
||||
$this->assertSame($sub->status, 'active');
|
||||
$this->assertSame($sub->plan->id, $planID);
|
||||
$this->assertSame($sub->quantity, 2);
|
||||
|
||||
$subs = $customer->subscriptions->all(array('limit'=>3));
|
||||
$this->assertSame(get_class($subs->data[0]), 'Stripe\Subscription');
|
||||
|
||||
$sub->cancel(array('at_period_end' => true));
|
||||
|
||||
$sub = $customer->subscriptions->retrieve($sub->id);
|
||||
$this->assertSame($sub->status, 'active');
|
||||
// @codingStandardsIgnoreStart
|
||||
$this->assertTrue($sub->cancel_at_period_end);
|
||||
// @codingStandardsIgnoreEnd
|
||||
}
|
||||
|
||||
public function testCreateUpdateListCancel()
|
||||
{
|
||||
$planID = 'gold-' . self::generateRandomString(20);
|
||||
self::retrieveOrCreatePlan($planID);
|
||||
|
||||
$customer = self::createTestCustomer();
|
||||
|
||||
$sub = Subscription::create(array('plan' => $planID, 'customer' => $customer->id));
|
||||
|
||||
$this->assertSame($sub->status, 'active');
|
||||
$this->assertSame($sub->plan->id, $planID);
|
||||
|
||||
$sub->quantity = 2;
|
||||
$sub->save();
|
||||
|
||||
$sub = Subscription::retrieve($sub->id);
|
||||
$this->assertSame($sub->status, 'active');
|
||||
$this->assertSame($sub->plan->id, $planID);
|
||||
$this->assertSame($sub->quantity, 2);
|
||||
|
||||
// Update the quantity parameter one more time
|
||||
$sub = Subscription::update($sub->id, array("quantity" => 3));
|
||||
$this->assertSame($sub->status, 'active');
|
||||
$this->assertSame($sub->plan->id, $planID);
|
||||
$this->assertSame($sub->quantity, 3);
|
||||
|
||||
$subs = Subscription::all(array('customer'=>$customer->id, 'plan'=>$planID, 'limit'=>3));
|
||||
$this->assertSame(get_class($subs->data[0]), 'Stripe\Subscription');
|
||||
|
||||
$sub->cancel(array('at_period_end' => true));
|
||||
|
||||
$sub = Subscription::retrieve($sub->id);
|
||||
$this->assertSame($sub->status, 'active');
|
||||
$this->assertTrue($sub->cancel_at_period_end);
|
||||
}
|
||||
|
||||
public function testCreateUpdateListCancelWithItems()
|
||||
{
|
||||
$plan0ID = 'gold-' . self::generateRandomString(20);
|
||||
self::retrieveOrCreatePlan($plan0ID);
|
||||
|
||||
$customer = self::createTestCustomer();
|
||||
|
||||
$sub = Subscription::create(array(
|
||||
'customer' => $customer->id,
|
||||
'items' => array(
|
||||
array('plan' => $plan0ID),
|
||||
),
|
||||
));
|
||||
|
||||
$this->assertSame(count($sub->items->data), 1);
|
||||
$this->assertSame($sub->items->data[0]->plan->id, $plan0ID);
|
||||
|
||||
$plan1ID = 'gold-' . self::generateRandomString(20);
|
||||
self::retrieveOrCreatePlan($plan1ID);
|
||||
|
||||
$sub = Subscription::update($sub->id, array(
|
||||
'items' => array(
|
||||
array('plan' => $plan1ID),
|
||||
),
|
||||
));
|
||||
|
||||
$this->assertSame(count($sub->items->data), 2);
|
||||
$this->assertSame($sub->items->data[0]->plan->id, $plan0ID);
|
||||
$this->assertSame($sub->items->data[1]->plan->id, $plan1ID);
|
||||
}
|
||||
|
||||
public function testDeleteDiscount()
|
||||
{
|
||||
$planID = 'gold-' . self::generateRandomString(20);
|
||||
self::retrieveOrCreatePlan($planID);
|
||||
|
||||
$couponID = '25off-' . self::generateRandomString(20);
|
||||
self::retrieveOrCreateCoupon($couponID);
|
||||
|
||||
$customer = self::createTestCustomer();
|
||||
|
||||
$sub = $customer->subscriptions->create(
|
||||
array(
|
||||
'plan' => $planID,
|
||||
'coupon' => $couponID
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertSame($sub->status, 'active');
|
||||
$this->assertSame($sub->plan->id, $planID);
|
||||
$this->assertSame($sub->discount->coupon->id, $couponID);
|
||||
|
||||
$sub->deleteDiscount();
|
||||
$sub = $customer->subscriptions->retrieve($sub->id);
|
||||
$this->assertNull($sub->discount);
|
||||
}
|
||||
}
|
||||
@@ -3,269 +3,175 @@
|
||||
namespace Stripe;
|
||||
|
||||
/**
|
||||
* Base class for Stripe test cases, provides some utility methods for creating
|
||||
* objects.
|
||||
* Base class for Stripe test cases.
|
||||
*/
|
||||
class TestCase extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
const API_KEY = 'tGN0bIwXnHdwOa85VABjPdSn8nWY7G7I';
|
||||
/** @var string original API base URL */
|
||||
protected $origApiBase;
|
||||
|
||||
private $mock;
|
||||
/** @var string original API key */
|
||||
protected $origApiKey;
|
||||
|
||||
protected static function authorizeFromEnv()
|
||||
{
|
||||
$apiKey = getenv('STRIPE_API_KEY');
|
||||
if (!$apiKey) {
|
||||
$apiKey = self::API_KEY;
|
||||
}
|
||||
/** @var string original client ID */
|
||||
protected $origClientId;
|
||||
|
||||
Stripe::setApiKey($apiKey);
|
||||
}
|
||||
/** @var string original API version */
|
||||
protected $origApiVersion;
|
||||
|
||||
/** @var string original account ID */
|
||||
protected $origAccountId;
|
||||
|
||||
/** @var object HTTP client mocker */
|
||||
protected $clientMock;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
// Save original values so that we can restore them after running tests
|
||||
$this->origApiBase = Stripe::$apiBase;
|
||||
$this->origApiKey = Stripe::getApiKey();
|
||||
$this->origClientId = Stripe::getClientId();
|
||||
$this->origApiVersion = Stripe::getApiVersion();
|
||||
$this->origAccountId = Stripe::getAccountId();
|
||||
|
||||
// Set up host and credentials for stripe-mock
|
||||
Stripe::$apiBase = "http://localhost:" . MOCK_PORT;
|
||||
Stripe::setApiKey("sk_test_123");
|
||||
Stripe::setClientId("ca_123");
|
||||
Stripe::setApiVersion(null);
|
||||
Stripe::setAccountId(null);
|
||||
|
||||
// Set up the HTTP client mocker
|
||||
$this->clientMock = $this->getMock('\Stripe\HttpClient\ClientInterface');
|
||||
|
||||
// By default, use the real HTTP client
|
||||
ApiRequestor::setHttpClient(HttpClient\CurlClient::instance());
|
||||
|
||||
// Peg the API version so that it can be varied independently of the
|
||||
// one set on the test account.
|
||||
Stripe::setApiVersion('2017-04-06');
|
||||
|
||||
$this->mock = null;
|
||||
$this->call = 0;
|
||||
}
|
||||
|
||||
protected function mockRequest($method, $path, $params = array(), $return = array('id' => 'myId'), $rcode = 200)
|
||||
protected function tearDown()
|
||||
{
|
||||
$mock = $this->setUpMockRequest();
|
||||
$mock->expects($this->at($this->call++))
|
||||
// Restore original values
|
||||
Stripe::$apiBase = $this->origApiBase;
|
||||
Stripe::setApiKey($this->origApiKey);
|
||||
Stripe::setClientId($this->origClientId);
|
||||
Stripe::setApiVersion($this->origApiVersion);
|
||||
Stripe::setAccountId($this->origAccountId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up a request expectation with the provided parameters. The request
|
||||
* will actually go through and be emitted.
|
||||
*
|
||||
* @param string $method HTTP method (e.g. 'post', 'get', etc.)
|
||||
* @param string $path relative path (e.g. '/v1/charges')
|
||||
* @param array|null $params array of parameters. If null, parameters will
|
||||
* not be checked.
|
||||
* @param string[]|null $headers array of headers. Does not need to be
|
||||
* exhaustive. If null, headers are not checked.
|
||||
* @param bool $hasFile Whether the request parameters contains a file.
|
||||
* Defaults to false.
|
||||
*/
|
||||
protected function expectsRequest(
|
||||
$method,
|
||||
$path,
|
||||
$params = null,
|
||||
$headers = null,
|
||||
$hasFile = false
|
||||
) {
|
||||
$this->prepareRequestMock($method, $path, $params, $headers, $hasFile)
|
||||
->will($this->returnCallback(
|
||||
function ($method, $absUrl, $headers, $params, $hasFile) {
|
||||
$curlClient = HttpClient\CurlClient::instance();
|
||||
ApiRequestor::setHttpClient($curlClient);
|
||||
return $curlClient->request($method, $absUrl, $headers, $params, $hasFile);
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up a request expectation with the provided parameters. The request
|
||||
* will not actually be emitted, instead the provided response parameters
|
||||
* will be returned.
|
||||
*
|
||||
* @param string $method HTTP method (e.g. 'post', 'get', etc.)
|
||||
* @param string $path relative path (e.g. '/v1/charges')
|
||||
* @param array|null $params array of parameters. If null, parameters will
|
||||
* not be checked.
|
||||
* @param string[]|null $headers array of headers. Does not need to be
|
||||
* exhaustive. If null, headers are not checked.
|
||||
* @param bool $hasFile Whether the request parameters contains a file.
|
||||
* Defaults to false.
|
||||
* @param array $response
|
||||
* @param integer $rcode
|
||||
* @param string|null $base
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function stubRequest(
|
||||
$method,
|
||||
$path,
|
||||
$params = null,
|
||||
$headers = null,
|
||||
$hasFile = false,
|
||||
$response = [],
|
||||
$rcode = 200,
|
||||
$base = null
|
||||
) {
|
||||
$this->prepareRequestMock($method, $path, $params, $headers, $hasFile, $base)
|
||||
->willReturn([json_encode($response), $rcode, []]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the client mocker for an invocation of the `request` method.
|
||||
* This helper method is used by both `expectsRequest` and `stubRequest` to
|
||||
* prepare the client mocker to expect an invocation of the `request` method
|
||||
* with the provided arguments.
|
||||
*
|
||||
* @param string $method HTTP method (e.g. 'post', 'get', etc.)
|
||||
* @param string $path relative path (e.g. '/v1/charges')
|
||||
* @param array|null $params array of parameters. If null, parameters will
|
||||
* not be checked.
|
||||
* @param string[]|null $headers array of headers. Does not need to be
|
||||
* exhaustive. If null, headers are not checked.
|
||||
* @param bool $hasFile Whether the request parameters contains a file.
|
||||
* Defaults to false.
|
||||
* @param string|null $base base URL (e.g. 'https://api.stripe.com')
|
||||
*
|
||||
* @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
|
||||
*/
|
||||
private function prepareRequestMock(
|
||||
$method,
|
||||
$path,
|
||||
$params = null,
|
||||
$headers = null,
|
||||
$hasFile = false,
|
||||
$base = null
|
||||
) {
|
||||
ApiRequestor::setHttpClient($this->clientMock);
|
||||
|
||||
if ($base === null) {
|
||||
$base = Stripe::$apiBase;
|
||||
}
|
||||
$absUrl = $base . $path;
|
||||
|
||||
return $this->clientMock
|
||||
->expects($this->once())
|
||||
->method('request')
|
||||
->with(strtolower($method), 'https://api.stripe.com' . $path, $this->anything(), $params, false)
|
||||
->willReturn(array(json_encode($return), $rcode, array()));
|
||||
->with(
|
||||
$this->identicalTo(strtolower($method)),
|
||||
$this->identicalTo($absUrl),
|
||||
// for headers, we only check that all of the headers provided in $headers are
|
||||
// present in the list of headers of the actual request
|
||||
$headers === null ? $this->anything() : $this->callback(function ($array) use ($headers) {
|
||||
foreach ($headers as $header) {
|
||||
if (!in_array($header, $array)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
private function setUpMockRequest()
|
||||
{
|
||||
if (!$this->mock) {
|
||||
self::authorizeFromEnv();
|
||||
$this->mock = $this->getMock('\Stripe\HttpClient\ClientInterface');
|
||||
ApiRequestor::setHttpClient($this->mock);
|
||||
}
|
||||
return $this->mock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a valid test charge.
|
||||
*/
|
||||
protected static function createTestCharge(array $attributes = array())
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
return Charge::create(
|
||||
$attributes + array(
|
||||
'amount' => 2000,
|
||||
'currency' => 'usd',
|
||||
'description' => 'Charge for test@example.com',
|
||||
'card' => array(
|
||||
'number' => '4242424242424242',
|
||||
'exp_month' => 5,
|
||||
'exp_year' => date('Y') + 3,
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a valid test transfer.
|
||||
*/
|
||||
protected static function createTestTransfer(array $attributes = array(), $opts = null)
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
$recipient = self::createTestRecipient();
|
||||
|
||||
return Transfer::create(
|
||||
$attributes + array(
|
||||
'amount' => 2000,
|
||||
'currency' => 'usd',
|
||||
'description' => 'Transfer to test@example.com',
|
||||
'recipient' => $recipient->id
|
||||
),
|
||||
$opts
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a valid test customer.
|
||||
*/
|
||||
protected static function createTestCustomer(array $attributes = array())
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
return Customer::create(
|
||||
$attributes + array(
|
||||
'card' => array(
|
||||
'number' => '4242424242424242',
|
||||
'exp_month' => 5,
|
||||
'exp_year' => date('Y') + 3,
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a valid test recipient
|
||||
*/
|
||||
protected static function createTestRecipient(array $attributes = array())
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
return Recipient::create(
|
||||
$attributes + array(
|
||||
'name' => 'PHP Test',
|
||||
'type' => 'individual',
|
||||
'tax_id' => '000000000',
|
||||
'bank_account' => array(
|
||||
'country' => 'US',
|
||||
'routing_number' => '110000000',
|
||||
'account_number' => '000123456789'
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a test account
|
||||
*/
|
||||
protected static function createTestAccount(array $attributes = array())
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
return Account::create(
|
||||
$attributes + array(
|
||||
'managed' => false,
|
||||
'country' => 'US',
|
||||
'email' => self::generateRandomEmail(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a test account
|
||||
*/
|
||||
protected static function createTestManagedAccount(array $attributes = array())
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
return Account::create(
|
||||
$attributes + array(
|
||||
'managed' => true,
|
||||
'country' => 'US',
|
||||
'external_account' => array(
|
||||
'object' => 'bank_account',
|
||||
'country' => 'US',
|
||||
'currency' => 'usd',
|
||||
'routing_number' => '110000000',
|
||||
'account_number' => '000123456789'
|
||||
),
|
||||
'legal_entity' => array(
|
||||
'type' => 'individual',
|
||||
'personal_id_number' => '000000000',
|
||||
'type' => 'individual',
|
||||
'dob' => array('year' => '1980', 'month' => '01', 'day' => '01'),
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address' => array(
|
||||
'line1' => '1234 Main Street',
|
||||
'postal_code' => '94110',
|
||||
'city' => 'San Francisco'
|
||||
),
|
||||
'personal_address' => array(
|
||||
'line1' => '1234 Main Street',
|
||||
'postal_code' => '94110',
|
||||
'city' => 'San Francisco'
|
||||
)
|
||||
),
|
||||
'tos_acceptance' => array('date' => time(), 'ip' => '127.0.0.1')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a plan with a given ID exists, or create a new one if it does
|
||||
* not.
|
||||
*/
|
||||
protected static function retrieveOrCreatePlan($id)
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
try {
|
||||
$plan = Plan::retrieve($id);
|
||||
} catch (Error\InvalidRequest $exception) {
|
||||
$plan = Plan::create(
|
||||
array(
|
||||
'id' => $id,
|
||||
'amount' => 0,
|
||||
'currency' => 'usd',
|
||||
'interval' => 'month',
|
||||
'name' => 'Gold Test Plan',
|
||||
)
|
||||
return true;
|
||||
}),
|
||||
$params === null ? $this->anything() : $this->identicalTo($params),
|
||||
$this->identicalTo($hasFile)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a coupon with a given ID exists, or create a new one if it
|
||||
* does not.
|
||||
*/
|
||||
protected static function retrieveOrCreateCoupon($id)
|
||||
{
|
||||
self::authorizeFromEnv();
|
||||
|
||||
try {
|
||||
$coupon = Coupon::retrieve($id);
|
||||
} catch (Error\InvalidRequest $exception) {
|
||||
$coupon = Coupon::create(
|
||||
array(
|
||||
'id' => $id,
|
||||
'duration' => 'forever',
|
||||
'percent_off' => 25,
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a semi-random string
|
||||
*/
|
||||
protected static function generateRandomString($length = 24)
|
||||
{
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU';
|
||||
$charactersLength = strlen($characters);
|
||||
$randomString = '';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
||||
}
|
||||
return $randomString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a semi-random email.
|
||||
*/
|
||||
protected static function generateRandomEmail()
|
||||
{
|
||||
return 'dev-platform-bots+php-'.self::generateRandomString(12).'@stripe.com';
|
||||
}
|
||||
|
||||
protected static function createTestBitcoinReceiver($email)
|
||||
{
|
||||
$receiver = BitcoinReceiver::create(
|
||||
array(
|
||||
'amount' => 100,
|
||||
'currency' => 'usd',
|
||||
'description' => 'some details',
|
||||
'email' => $email
|
||||
)
|
||||
);
|
||||
return $receiver;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class ThreeDSecureTest extends TestCase
|
||||
{
|
||||
public function testRetrieve()
|
||||
{
|
||||
$this->mockRequest(
|
||||
'GET',
|
||||
'/v1/3d_secure/tdsrc_test',
|
||||
array(),
|
||||
array(
|
||||
'id' => 'tdsrc_test',
|
||||
'object' => 'three_d_secure'
|
||||
)
|
||||
);
|
||||
$three_d_secure = ThreeDSecure::retrieve('tdsrc_test');
|
||||
$this->assertSame($three_d_secure->id, 'tdsrc_test');
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$this->mockRequest(
|
||||
'POST',
|
||||
'/v1/3d_secure',
|
||||
array(
|
||||
'card' => 'tok_test',
|
||||
'amount' => 1500,
|
||||
'currency' => 'usd',
|
||||
'return_url' => 'https://example.org/3d-secure-result'
|
||||
),
|
||||
array(
|
||||
'id' => 'tdsrc_test',
|
||||
'object' => 'three_d_secure'
|
||||
)
|
||||
);
|
||||
$three_d_secure = ThreeDSecure::create(array(
|
||||
'card' => 'tok_test',
|
||||
'amount' => 1500,
|
||||
'currency' => 'usd',
|
||||
'return_url' => 'https://example.org/3d-secure-result'
|
||||
));
|
||||
$this->assertSame($three_d_secure->id, 'tdsrc_test');
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class TokenTest extends TestCase
|
||||
{
|
||||
public function testUrls()
|
||||
{
|
||||
$this->assertSame(Token::classUrl(), '/v1/tokens');
|
||||
$token = new Token('abcd/efgh');
|
||||
$this->assertSame($token->instanceUrl(), '/v1/tokens/abcd%2Fefgh');
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class TransferReversalTest extends TestCase
|
||||
{
|
||||
// The resource that was traditionally called "transfer" became a "payout"
|
||||
// in API version 2017-04-06. We're testing traditional transfers here, so
|
||||
// we force the API version just prior anywhere that we need to.
|
||||
private $opts = array('stripe_version' => '2017-02-14');
|
||||
|
||||
public function testList()
|
||||
{
|
||||
$transfer = self::createTestTransfer(array(), $this->opts);
|
||||
$all = $transfer->reversals->all();
|
||||
$this->assertSame(false, $all['has_more']);
|
||||
$this->assertSame(0, count($all->data));
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class TransferTest extends TestCase
|
||||
{
|
||||
// The resource that was traditionally called "transfer" became a "payout"
|
||||
// in API version 2017-04-06. We're testing traditional transfers here, so
|
||||
// we force the API version just prior anywhere that we need to.
|
||||
private $opts = array('stripe_version' => '2017-02-14');
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$transfer = self::createTestTransfer(array(), $this->opts);
|
||||
$this->assertSame('transfer', $transfer->object);
|
||||
}
|
||||
|
||||
public function testRetrieve()
|
||||
{
|
||||
$transfer = self::createTestTransfer(array(), $this->opts);
|
||||
$reloaded = Transfer::retrieve($transfer->id, $this->opts);
|
||||
$this->assertSame($reloaded->id, $transfer->id);
|
||||
}
|
||||
|
||||
public function testTransferUpdateMetadata()
|
||||
{
|
||||
$transfer = self::createTestTransfer(array(), $this->opts);
|
||||
|
||||
$transfer->metadata['test'] = 'foo bar';
|
||||
$transfer->save();
|
||||
|
||||
$updatedTransfer = Transfer::retrieve($transfer->id, $this->opts);
|
||||
$this->assertSame('foo bar', $updatedTransfer->metadata['test']);
|
||||
}
|
||||
|
||||
public function testTransferUpdateMetadataAll()
|
||||
{
|
||||
$transfer = self::createTestTransfer(array(), $this->opts);
|
||||
|
||||
$transfer->metadata = array('test' => 'foo bar');
|
||||
$transfer->save();
|
||||
|
||||
$updatedTransfer = Transfer::retrieve($transfer->id, $this->opts);
|
||||
$this->assertSame('foo bar', $updatedTransfer->metadata['test']);
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stripe;
|
||||
|
||||
class UtilTest extends TestCase
|
||||
{
|
||||
public function testIsList()
|
||||
{
|
||||
$list = array(5, 'nstaoush', array());
|
||||
$this->assertTrue(Util\Util::isList($list));
|
||||
|
||||
$notlist = array(5, 'nstaoush', array(), 'bar' => 'baz');
|
||||
$this->assertFalse(Util\Util::isList($notlist));
|
||||
}
|
||||
|
||||
public function testThatPHPHasValueSemanticsForArrays()
|
||||
{
|
||||
$original = array('php-arrays' => 'value-semantics');
|
||||
$derived = $original;
|
||||
$derived['php-arrays'] = 'reference-semantics';
|
||||
|
||||
$this->assertSame('value-semantics', $original['php-arrays']);
|
||||
}
|
||||
|
||||
public function testConvertStripeObjectToArrayIncludesId()
|
||||
{
|
||||
$customer = self::createTestCustomer();
|
||||
$this->assertTrue(array_key_exists("id", $customer->__toArray(true)));
|
||||
}
|
||||
|
||||
public function testUtf8()
|
||||
{
|
||||
// UTF-8 string
|
||||
$x = "\xc3\xa9";
|
||||
$this->assertSame(Util\Util::utf8($x), $x);
|
||||
|
||||
// Latin-1 string
|
||||
$x = "\xe9";
|
||||
$this->assertSame(Util\Util::utf8($x), "\xc3\xa9");
|
||||
|
||||
// Not a string
|
||||
$x = true;
|
||||
$this->assertSame(Util\Util::utf8($x), $x);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../init.php';
|
||||
require_once __DIR__ . '/TestCase.php';
|
||||
|
||||
require_once __DIR__ . '/bootstrap.php';
|
||||
|
||||
@@ -1,3 +1,43 @@
|
||||
<?php
|
||||
|
||||
define("MOCK_MINIMUM_VERSION", "0.5.0");
|
||||
define("MOCK_PORT", getenv("STRIPE_MOCK_PORT") ?: 12111);
|
||||
|
||||
// Send a request to stripe-mock
|
||||
$ch = curl_init("http://localhost:" . MOCK_PORT . "/");
|
||||
curl_setopt($ch, CURLOPT_HEADER, 1);
|
||||
curl_setopt($ch, CURLOPT_NOBODY, 1);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
$resp = curl_exec($ch);
|
||||
|
||||
if (curl_errno($ch)) {
|
||||
echo "Couldn't reach stripe-mock at `localhost:" . MOCK_PORT . "`. Is " .
|
||||
"it running? Please see README for setup instructions.\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Retrieve the Stripe-Mock-Version header
|
||||
$version = null;
|
||||
$headers = explode("\n", $resp);
|
||||
foreach ($headers as $header) {
|
||||
$pair = explode(":", $header, 2);
|
||||
if ($pair[0] == "Stripe-Mock-Version") {
|
||||
$version = trim($pair[1]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($version === null) {
|
||||
echo "Could not retrieve Stripe-Mock-Version header. Are you sure " .
|
||||
"that the server at `localhost:" . MOCK_PORT . "` is a stripe-mock " .
|
||||
"instance?";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (version_compare($version, MOCK_MINIMUM_VERSION) == -1) {
|
||||
echo "Your version of stripe-mock (" . $version . ") is too old. The minimum " .
|
||||
"version to run this test suite is " . MOCK_MINIMUM_VERSION . ". " .
|
||||
"Please see its repository for upgrade instructions.\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/TestCase.php';
|
||||
|
||||
Reference in New Issue
Block a user