Skip to content
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
7 changes: 7 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
use \Finix\Settings;
use \Finix\Bootstrap;

$processing_url = getenv("PROCESSING_URL");
if ($processing_url == null) {
$processing_url = "https://api-staging.finix.io/";
}

Fixtures::$apiUrl = $processing_url;

Settings::configure([
"root_url" => Fixtures::$apiUrl
]);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "finix/processing-php",
"version": "1.0.3",
"version": "1.0.4",
"description": "A PHP HTTP Client conforming to the HAL hypermedia type for the Finix processing API",
"license": "Apache2",
"authors": [
Expand Down
3 changes: 3 additions & 0 deletions src/Finix/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ private static function initializeResources()
Resources\Processor::init();
Resources\Merchant::init();
Resources\PaymentInstrument::init();
Resources\PaymentCard::init();
Resources\BankAccount::init();
Resources\Authorization::init();
Resources\Transfer::init();
Resources\Reversal::init();
Expand All @@ -75,6 +77,7 @@ private static function initializeResources()
Resources\Verification::init();
Resources\Evidence::init();
Resources\Token::init();
Resources\InstrumentUpdate::init();

self::$initialized = true;
}
Expand Down
32 changes: 0 additions & 32 deletions src/Finix/Hal/HrefSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,4 @@ public function __construct($name, $idNames, $root = null, $override = null)
}
}
}

public function match($uri)
{
$parts = explode('/', rtrim($uri, "/"));

// collection
if ($parts[count($parts) - 1] == $this->name) {

return array(
'collection' => true,
);
}

// non-member
if (count($parts) < count($this->idNames) + 1 ||
$parts[count($parts) - 1 - count($this->idNames)] != $this->name
) {
return null;
}

// member
$ids = array_combine(
$this->idNames,
array_slice($parts, -count($this->idNames))
);
$result = array(
'collection' => false,
'ids' => $ids,
);

return $result;
}
}
5 changes: 5 additions & 0 deletions src/Finix/Hal/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public function getLink($rel)
return $link;
}

public function hasRel($rel)
{
return self::findByRel($this->links, $rel) != null;
}

/**
* Finds an array of links by their relation type.
* Note that there is no guarantees as to the order of the links.
Expand Down
40 changes: 39 additions & 1 deletion src/Finix/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Finix;

use Finix\Http\Request;
use Iterator;

class Pagination extends Resource
class Pagination extends Resource implements Iterator
{
private $resourceClass;

public function __construct(Hal\Resource $halResource, $class)
{
Expand All @@ -16,5 +19,40 @@ public function __construct(Hal\Resource $halResource, $class)
array_push($items, new $class($resource->getState(), $resource->getAllLinks()));
}
$this->state->items = $items;
$this->resourceClass = $class;
}

public function current()
{
return $this->state->items;
}

public function next()
{
if (!$this->resource->hasRel("next")) {
$this->state->items = array();
$page = $this->state->page;
$page["offset"] = $page["count"];
$this->page = $page;
return;
}
$link = $this->resource->getLink('next')->getHref();
$halResource = $this->client->sendRequest(new Request($link));
$this->__construct($halResource, $this->resourceClass);
}

public function key()
{
return $this->page["offset"];
}

public function valid()
{
return $this->page["offset"] < $this->page["count"];
}

public function rewind()
{
// TODO: Implement rewind() method.
}
}
29 changes: 18 additions & 11 deletions src/Finix/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@

abstract class Resource
{
/** @var Hal\Resource $resource */
protected $resource;
/** @var ArrayProxy $state */
protected $state;

protected static $href;
protected $client;
// protected static $client;
protected static $href;
protected static $registry;

/**
Expand Down Expand Up @@ -105,18 +101,29 @@ public function __isset($name)
* @throws Hal\Exception\HalRedirectionException
* @throws Hal\Exception\HalServerErrorException
*/
public static function retrieve($id)
public static function retrieve($id_or_url)
{
$uri = self::getHrefSpec()->collection_uri . '/' . $id;

$uri = filter_var($id_or_url, FILTER_VALIDATE_URL) || (strpos($id_or_url, '/') !== false) ?
$id_or_url : self::getHrefSpec()->collection_uri . '/' . $id_or_url;
$resource = Bootstrap::createClient()->sendRequest(new Request($uri));
$class = get_called_class();
return new $class($resource->getState(), $resource->getAllLinks());
$state = $resource->getState();
if (sizeof($resource->getAllEmbeddedResources()) > 0) {
$items = $resource->getAllEmbeddedResources();
$items = reset($items);
if (sizeof($items) == 1) {
$state = $items[0]->getState();
}
}
return new $class($state, $resource->getAllLinks());
}

public static function getPagination($href)
public static function getPagination($resource)
{
$resource = Bootstrap::createClient()->sendRequest(new Request($href));
return new Pagination($resource, get_called_class());
$cls = get_called_class();
$halResource = Bootstrap::createClient()->sendRequest(new Request($resource->getHref($cls::getHrefSpec()->name)));
return new Pagination($halResource, $cls);
}

public function refresh()
Expand Down
9 changes: 5 additions & 4 deletions src/Finix/Resources/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ public static function init()
self::getRegistry()->add(get_called_class(), new HrefSpec('authorizations', 'id', '/'));
}

public function capture($amount, $fee = 0)
public function capture(array $cap = [])
{
$this->state["capture_amount"] = $amount;
$this->state["fee"] = $fee;
foreach ($cap as $key => $value) {
$this->state[$key] = $value;
}
return $this->save();
}

public function void($voidMe)
public function void($voidMe = true)
{
$this->state["void_me"] = $voidMe;
return $this->save();
Expand Down
6 changes: 6 additions & 0 deletions src/Finix/Resources/BankAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Finix\Resources;

use Finix\Hal\HrefSpec;

class BankAccount extends PaymentInstrument
{
Expand All @@ -10,4 +11,9 @@ public function __construct(array $state = [], array $links = null)
$state["type"] = "BANK_ACCOUNT";
parent::__construct($state, $links);
}

public static function init()
{
self::getRegistry()->add(get_called_class(), new HrefSpec('payment_instruments', 'id', '/'));
}
}
5 changes: 5 additions & 0 deletions src/Finix/Resources/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public static function init()
self::getRegistry()->add(get_called_class(), new HrefSpec('identities', 'id', '/'));
}

public function createMerchantUser(User $user)
{
return $user->create($this->resource->getLink("users")->getHref());
}

public function provisionMerchantOn(Merchant $merchant)
{
return $merchant->create($this->resource->getLink("merchants")->getHref());
Expand Down
14 changes: 14 additions & 0 deletions src/Finix/Resources/InstrumentUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Finix\Resources;

use Finix\Hal\HrefSpec;
use Finix\Resource;

class InstrumentUpdate extends Resource
{
public static function init()
{
self::getRegistry()->add(get_called_class(), new HrefSpec('updates', 'id', '/'));
}
}
18 changes: 18 additions & 0 deletions src/Finix/Resources/PaymentCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Finix\Resources;

use Finix\Hal\HrefSpec;


class PaymentCard extends PaymentInstrument
{
Expand All @@ -11,4 +13,20 @@ public function __construct(array $state = [], array $links = null)
$state["type"] = "PAYMENT_CARD";
parent::__construct($state, $links);
}

public static function init()
{
self::getRegistry()->add(get_called_class(), new HrefSpec('payment_instruments', 'id', '/'));
}

public function createUpdate(InstrumentUpdate $action)
{
return $action->create($this->resource->getLink("updates")->getHref());
}

public static function getUpdateUri($card_id, $update_id)
{
// TODO move this to Registry
return "/" . self::getHrefSpec()->name . "/" . $card_id . "/" . InstrumentUpdate::getHrefSpec()->name . "?id=" . $update_id;
}
}
4 changes: 2 additions & 2 deletions src/Finix/Resources/PaymentInstrument.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

abstract class PaymentInstrument extends Resource
{

public static function init()
{
self::getRegistry()->add(get_called_class(),
new HrefSpec('payment_instruments', 'id', '/'));
self::getRegistry()->add(get_called_class(), new HrefSpec('payment_instruments', 'id', '/'));
}
}
4 changes: 2 additions & 2 deletions tests/Finix/Fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

class Fixtures extends \PHPUnit_Framework_TestCase
{
public static $apiUrl = "https://api-staging.finix.io/";
public static $apiUrl;
public static $disputeAmount = 888888;

public static function createAdminUser()
{
$user = new User(["enabled" => true]);
$user = new User(["enabled" => true, "role" => "ROLE_ADMIN"]);
$user = $user->save();
self::assertNotEmpty($user->id);
self::assertNotEmpty($user->password);
Expand Down
Loading