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

Added change_plan_simulation method in Subscription class #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions lib/Iugu/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,36 @@ public function activate()
return false;
}

public function change_plan_simulation($identifier = null)
{
if ($this->is_new()) {
return false;
}

if ($identifier == null) {
return false;
}

try {
$response = self::API()->request(
'GET',
static::url($this).'/change_plan_simulation/'.$identifier
);

if (isset($response->errors)) {
return false;
}

$new_object = self::createFromResponse($response);

return $new_object;
} catch (Exception $e) {
return false;
}

return false;
}

public function change_plan($identifier = null)
{
if ($this->is_new()) {
Expand Down
51 changes: 51 additions & 0 deletions tests/Iugu/SubscriptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class Iugu_SubscriptionTest extends TestCase
{
protected function setUp()
{
Iugu::$endpoint = "http://api.desenvolvimento";
Iugu::setApiKey("development_api_token");
}

public function testShouldCreateASubscription()
{
\VCR\VCR::turnOn();
\VCR\VCR::insertCassette("iugu_subscription_should_create_a_subscription");

$subscription = Iugu_Subscription::create([
"plan_identifier" => "silver",
"customer_id" => "A11222C0E33445566E77A8899DD00B00"
]);

$this->assertEquals("silver", $subscription->plan_identifier);
$this->assertEquals("A11222C0E33445566E77A8899DD00B00", $subscription->customer_id);

\VCR\VCR::eject();
\VCR\VCR::turnOff();
}

public function testChangePlanSimulation()
{
\VCR\VCR::turnOn();
\VCR\VCR::insertCassette("iugu_subscription_should_create_a_subscription");

$subscription = Iugu_Subscription::create([
"plan_identifier" => "silver",
"customer_id" => "A11222C0E33445566E77A8899DD00B00"
]);

\VCR\VCR::insertCassette("iugu_subscription_change_plan_simulation");
$simulation = $subscription->change_plan_simulation("gold");

$this->assertEquals("silver", $simulation->old_plan);
$this->assertEquals("gold", $simulation->new_plan);

\VCR\VCR::eject();
\VCR\VCR::turnOff();
}
}
31 changes: 31 additions & 0 deletions tests/fixtures/iugu_subscription_change_plan_simulation
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

-
request:
method: GET
url: 'http://api.desenvolvimento/v1/subscriptions/C00B1A22CE8A8877A55C444FDED99999/change_plan_simulation/gold'
headers:
Host: api.desenvolvimento
Authorization: 'Basic ZGV2ZWxvcG1lbnRfYXBpX3Rva2VuOg=='
Accept: application/json
Accept-Charset: utf-8
User-Agent: 'Iugu PHPLibrary'
Accept-Language: 'pt-br;q=0.9,pt-BR'
response:
status:
http_version: '1.1'
code: '200'
message: OK
headers:
Server: nginx/1.13.5
Date: 'Wed, 24 Jan 2018 19:47:34 GMT'
Content-Type: 'application/json; charset=utf-8'
Transfer-Encoding: chunked
Connection: keep-alive
x-HandledBy: 'api/v1/subscription#change_plan_simulation'
X-UA-Compatible: IE=Edge
ETag: '"fa7ae1adec07ffe45ea2191e16d645c8"'
Cache-Control: 'max-age=0, private, must-revalidate'
X-Request-Id: 639ff4e9bd2221957b52d2ca3bf2e2cb
X-Runtime: '0.067239'
Vary: Origin
body: '{"cost":1000,"discount":1500,"cycles":1,"expires_at":"2018-11-20","new_plan":"gold","old_plan":"silver"}'
32 changes: 32 additions & 0 deletions tests/fixtures/iugu_subscription_should_create_a_subscription
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

-
request:
method: POST
url: 'http://api.desenvolvimento/v1/subscriptions'
headers:
Host: api.desenvolvimento
Authorization: 'Basic ZGV2ZWxvcG1lbnRfYXBpX3Rva2VuOg=='
Accept: application/json
Accept-Charset: utf-8
User-Agent: 'Iugu PHPLibrary'
Accept-Language: 'pt-br;q=0.9,pt-BR'
body: 'plan_identifier=silver&customer_id=A11222C0E33445566E77A8899DD00B00'
response:
status:
http_version: '1.1'
code: '200'
message: OK
headers:
Server: nginx/1.13.5
Date: 'Wed, 24 Jan 2018 19:47:34 GMT'
Content-Type: 'application/json; charset=utf-8'
Transfer-Encoding: chunked
Connection: keep-alive
x-HandledBy: 'api/v1/subscription#create'
X-UA-Compatible: IE=Edge
ETag: '"fa7ae1adec07ffe45ea2191e16d645c8"'
Cache-Control: 'max-age=0, private, must-revalidate'
X-Request-Id: 639ff4e9bd2221957b52d2ca3bf2e2cb
X-Runtime: '0.067239'
Vary: Origin
body: '{"id":"C00B1A22CE8A8877A55C444FDED99999","suspended":false,"plan_identifier":"silver","price_cents":1500,"currency":"BRL","features":{},"expires_at":"2019-04-20","created_at":"2018-10-20T21:59:42-03:00","updated_at":"2018-10-20T21:59:45-03:00","customer_name":"Martin Fowler","customer_email":"[email protected]","cycled_at":null,"credits_min":0,"credits_cycle":null,"payable_with":"all","ignore_due_email":null,"customer_id":"A11222C0E33445566E77A8899DD00B00","plan_name":"Silver","customer_ref":"Martin Fowler","plan_ref":"Silver","active":true,"in_trial":null,"credits":0,"credits_based":false,"recent_invoices":null,"subitems":[],"logs":[{"id":"9A8F732BE8054AB282AE915A00DB591B","description":"Fatura criada","notes":"Fatura criada com os items: 1x Ativa\u00e7\u00e3o de Assinatura: Silver = R$ 15,00;","created_at":"2018-10-20T21:59:44-03:00"},{"id":"9F21C27F154447CA97D96EB61EB76C64","description":"Assinatura Criada","notes":"Assinatura Criada ","created_at":"2018-10-20T21:59:42-03:00"}],"custom_variables":[]}'