Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add support for exchange_rates API requests #386

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
require(dirname(__FILE__) . '/lib/Dispute.php');
require(dirname(__FILE__) . '/lib/EphemeralKey.php');
require(dirname(__FILE__) . '/lib/Event.php');
require(dirname(__FILE__) . '/lib/ExchangeRate.php');
require(dirname(__FILE__) . '/lib/FileUpload.php');
require(dirname(__FILE__) . '/lib/Invoice.php');
require(dirname(__FILE__) . '/lib/InvoiceItem.php');
Expand Down
44 changes: 44 additions & 0 deletions lib/ExchangeRate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Stripe;

/**
* Class ExchangeRate
*
* @package Stripe
*/
class ExchangeRate extends ApiResource
{
/**
* This is a special case because the exchange rates endpoint has an
* underscore in it. The parent `className` function strips underscores.
*
* @return string The name of the class.
*/
public static function className()
{
return 'exchange_rate';
}

/**
* @param array|string $currency
* @param array|string|null $opts
*
* @return ExchangeRate
*/
public static function retrieve($currency, $opts = null)
{
return self::_retrieve($currency, $opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return ExchangeRate
*/
public static function all($params = null, $opts = null)
{
return self::_all($params, $opts);
}
}
1 change: 1 addition & 0 deletions lib/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public static function convertToStripeObject($resp, $opts)
'customer' => 'Stripe\\Customer',
'dispute' => 'Stripe\\Dispute',
'ephemeral_key' => 'Stripe\\EphemeralKey',
'exchange_rate' => 'Stripe\\ExchangeRate',
'list' => 'Stripe\\Collection',
'login_link' => 'Stripe\\LoginLink',
'invoice' => 'Stripe\\Invoice',
Expand Down
52 changes: 52 additions & 0 deletions tests/ExchangeRateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Stripe;

class ExchangeRateTest extends TestCase
{
public function testRetrieve()
{
$this->mockRequest(
'GET',
'/v1/exchange_rates/usd',
array(),
array(
'id' => 'usd',
'object' => 'exchange_rate',
'rates' => array('eur' => 0.845876),
)
);

$currency = "usd";
$rates = ExchangeRate::retrieve($currency);
$this->assertEquals('exchange_rate', $rates->object);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I really wish we could do $this->assertInstanceOf(ExchangeRate::class, $rates); here instead, but unfortunately that doesn't fly with PHP <= 5.4 😢

Copy link
Contributor

Choose a reason for hiding this comment

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

Ugh ...

Well, it's worth noting that 5.4 has been EOLed for more than two years (since mid-2015) and hasn't even been receiving security fixes for more than a year, which means that it's not safe to use. This approach works fine for now, but it's totally plausible to drop support for it at this point. (If you're not updating off of PHP 5.4, you're also probably not bothering to update your version of stripe-php.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I misread that. It's 5.5 has been EOLed for two years and hasn't been receiving updates for a year.

5.4 has been EOLed for three years (mid-2014) and hasn't been receiving updates for two years!

Copy link
Contributor

@arcanedev-maroc arcanedev-maroc Oct 31, 2017

Choose a reason for hiding this comment

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

http://php.net/supported-versions.php

Version specific notes
PHP 5.6
As it is the final PHP 5 release, support for PHP 5.6 has been extended: active support will run for an additional four months, and the security fix period has been doubled from one to two years. Other releases are unaffected.

}

public function testList()
{
$this->mockRequest(
'GET',
'/v1/exchange_rates',
array(),
array(
'object' => 'list',
'data' => array(
array(
'id' => 'eur',
'object' => 'exchange_rate',
'rates' => array('usd' => 1.18221),
),
array(
'id' => 'usd',
'object' => 'exchange_rate',
'rates' => array('eur' => 0.845876),
),
),
)
);

$listRates = ExchangeRate::all();
$this->assertTrue(is_array($listRates->data));
$this->assertEquals('exchange_rate', $listRates->data[0]->object);
}
}