Skip to content

Commit

Permalink
Merge pull request #33 from checkout/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
aquila-freitas-cko authored Jul 5, 2019
2 parents 095a097 + 8b2886b commit 729fb64
Show file tree
Hide file tree
Showing 37 changed files with 550 additions and 191 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ vendor/
nbproject/

index.php
phpunit/

*.log
3 changes: 2 additions & 1 deletion examples/Events/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* Used namespaces.
*/

use Checkout\CheckoutApi;

/**
Expand All @@ -27,6 +28,6 @@
/**
* Get all events
*/
$events = $checkout->events()->retrieve();
$events = $checkout->events()->retrieve(array('skip' => 5, 'limit' => 1));

var_dump($events);
21 changes: 16 additions & 5 deletions examples/Payments/alternative_payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@
*/

use Checkout\CheckoutApi;
use Checkout\Models\Payments\Payment;
use Checkout\Models\Payments\EpsSource;
use Checkout\Models\Payments\PoliSource;
use Checkout\Models\Payments\IdealSource;
use Checkout\Models\Payments\AlipaySource;
use Checkout\Models\Payments\BancontactSource;
use Checkout\Models\Payments\BoletoSource;
use Checkout\Models\Payments\SofortSource;
use Checkout\Models\Payments\EpsSource;
use Checkout\Models\Payments\FawrySource;
use Checkout\Models\Payments\GiropaySource;
use Checkout\Models\Payments\IdealSource;
use Checkout\Models\Payments\KnetSource;
use Checkout\Models\Payments\Payment;
use Checkout\Models\Payments\PoliSource;
use Checkout\Models\Payments\QpaySource;
use Checkout\Models\Payments\SofortSource;
use Checkout\Models\Product;

/**
* Create new instance of Checkout
Expand Down Expand Up @@ -108,3 +113,9 @@
$knet = new Payment(new KnetSource('en'), 'KWD');
$knet->amount = 999;
$payment = $checkout->payments()->request($knet);


// QPay
$qpay = new Payment(new QpaySource('description'), 'KWD');
$qpay->amount = 999;
$payment = $checkout->payments()->request($qpay);
6 changes: 3 additions & 3 deletions examples/Sources/klarna.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@




$source = new KlarnaSource(/* `authorization_token` from Klarna JS SDK*/, 'GB', 'en-GB', $address, 1, array($product));
$payment = new Payment($source, 'GBP');
/* `authorization_token` from Klarna JS SDK*/
$method = new KlarnaSource($klarnaAuthToken, 'GB', 'en-GB', $address, 1, array($product));
$payment = new Payment($method, 'GBP');
$payment->amount = 999;

$res = $checkout->payments()->request($payment);
Expand Down
2 changes: 1 addition & 1 deletion src/CheckoutApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class CheckoutApi
*
* @var string
*/
const VERSION = '1.0.6';
const VERSION = '1.0.7';

/**
* Channel section.
Expand Down
35 changes: 35 additions & 0 deletions src/Controllers/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Checkout\Models\Payments\Payment;
use Checkout\Models\Payments\Refund;
use Checkout\Models\Payments\Voids;
use Checkout\Models\Response;

/**
* Make payments.
Expand Down Expand Up @@ -161,6 +162,40 @@ public function request(Payment $payment, $mode = HttpHandler::MODE_EXECUTE)

return parent::response($response, Payment::QUALIFIED_NAME, $mode);
}

/**
* Extra methods.
*/

/**
* Retrieve supported banks.
*
* @param string $class Qualified name of the class.
* @return Response
*/
public function banks($class, $mode = HttpHandler::MODE_EXECUTE)
{

$banks = new Response();
$url = $class::MODEL_REQUEST_BANKS_URL;

if($url) {
$response = $this->requestAPI($url);
$banks = $this->response($response, Response::QUALIFIED_NAME, $mode);
}

return $banks;
}

/**
* Retrieve supported issuers. Alias for $this->banks().
*
* @param string $model
* @return array
*/
public function issuers($class, $mode = HttpHandler::MODE_EXECUTE) {
return $this->banks($class, $mode);
}

/**
* Handle the responses
Expand Down
22 changes: 14 additions & 8 deletions src/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Checkout\Library\Controller;
use Checkout\Library\Exceptions\CheckoutModelException;
use Checkout\Library\HttpHandler;
use Checkout\Library\Utilities;
use Checkout\Models\Response;
use Checkout\Models\Webhooks\Webhook;

Expand Down Expand Up @@ -108,16 +109,14 @@ public function retrieve($mode = HttpHandler::MODE_EXECUTE)
public function register(Webhook $webhook, array $events = array(), $mode = HttpHandler::MODE_EXECUTE)
{
$body = $webhook->getValues();

if ((!isset($body[static::FIELD_EVENTS]) || (isset($body[static::FIELD_EVENTS]) && !$body[static::FIELD_EVENTS])) && !$events) {
$body[static::FIELD_EVENTS] = Utilities::getValueFromArray($body, static::FIELD_EVENTS, array());

if(!$events && !$body[static::FIELD_EVENTS]) {
throw new CheckoutModelException('Field "event_types" is required to register a new webhook.');
} elseif (!isset($body[static::FIELD_EVENTS])) {
$body[static::FIELD_EVENTS] = array();
} else {
$body[static::FIELD_EVENTS] = $body[static::FIELD_EVENTS] + $events;
}

$merged = array_merge($body[static::FIELD_EVENTS], $events);
$body[static::FIELD_EVENTS] = array_values($merged);

unset($body[static::FIELD_ID]); // Remove ID from the body.
$response = $this->requestAPI($webhook->getEndpoint())
->setBody($body);
Expand All @@ -136,9 +135,16 @@ public function register(Webhook $webhook, array $events = array(), $mode = Http
public function update(Webhook $webhook, $partially = false, $mode = HttpHandler::MODE_EXECUTE)
{
$body = $webhook->getValues();
if (!isset($body[static::FIELD_ID]) || !$body[static::FIELD_ID]) {
$body[static::FIELD_EVENTS] = Utilities::getValueFromArray($body, static::FIELD_EVENTS, array());
$body[static::FIELD_ID] = Utilities::getValueFromArray($body, static::FIELD_ID, 0);

if(!$body[static::FIELD_EVENTS]) {
throw new CheckoutModelException('Field "event_types" is required to register a new webhook.');
}
if (!$body[static::FIELD_ID]) {
throw new CheckoutModelException('Field "id" is required for webhook update.');
}

unset($body[static::FIELD_ID]); // Remove id from the body.
$response = $this->requestAPI($webhook->getEndpoint())
->setBody($body)
Expand Down
11 changes: 7 additions & 4 deletions src/Library/HttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,13 @@ protected function headers()
}

/**
* Serialise http client.
* Serialise HTTP client.
*
* @return array
*/
public function serialize()
{
return array('url' => $this->getUrl(),
return array('url' => $this->getUrl() . $this->getQueryParameters(true),
'header' => $this->getHeaders(),
'method' => $this->method,
'body' => $this->body);
Expand Down Expand Up @@ -616,9 +616,12 @@ public function setQueryParameters(array $params)
*/
public function getQueryParameters($query = false)
{
if ($query) {
$result = '';
if($query && $this->params) {
$result = '?' . http_build_query($this->params);
} else {
}

if(!$query) {
$result = $this->params;
}

Expand Down
61 changes: 45 additions & 16 deletions src/Library/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ abstract class Model
* @var string
*/
const MODEL_REQUEST_URL = '';

/**
* API Request banks URL.
*
* @var string
*/
const MODEL_REQUEST_BANKS_URL = '';

/**
* API Request Method.
Expand Down Expand Up @@ -144,26 +151,49 @@ public function getValues()
/**
* Get a specific field.
*
* @param string $key
* @param mixed $key
* @param array $values For recursion purposes.
* @return mixed
*/
public function getValue($key)
public function getValue($key, array $values = array())
{
$arr = (array) $key;
$index = array_shift($arr);

if($values) {
$value = isset($values[$index]) ? $values[$index] : $this->getValueAliased($index, $values);
} else {
$value = isset($this->{$index}) ? $this->{$index} : $this->getValueAliased($index);
}

if($arr) {
$value = $this->getValue($arr, (array) $value);
}

return $value;
}

/**
* Gets the value aliased.
*
* @param string $key The key
* @param array $values The values
*
* @return mixed The value aliased.
*/
protected function getValueAliased($key, array &$values = array())
{
$value = null;

if (isset($this->{$key})) {
$value = $this->{$key};
} else {
$index = array_search($key, static::$aliases);
if ($index && isset($this->{$index})) {
$index = array_search($key, static::$aliases);
if($index !== false) {
if($values && isset($values[$key])) {
$value = $value[$key];
} else if(isset($this->{$index})) {
$value = $this->{$index};
}
}

if ($value instanceof Model) {
$value = $value->getValues();
}

return $value;
}

Expand Down Expand Up @@ -266,11 +296,10 @@ public function getCode()
*/
public function getLink($key)
{
$link = '';
$types = $this->getValue('_links');
if (isset($types[$key])) {
$link = $types[$key]['href'];
}
$link = $this->getValue(array('_links', $key, 'href'));
if(!$link) {
$link = '';
}

return $link;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Models/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ public function __construct($id)
* @return Model
*/
protected static function create(array $response)
{
{
$code = Utilities::getValueFromArray($response, 'http_code', 0);
if ($code === 204 || Utilities::getValueFromArray($response, 'total_count', false)) { // List of Events
if ($code === 204 || Utilities::getValueFromArray($response, 'total_count', 0)) {

$obj = new Response();
foreach ($response['data'] as &$event) {
$obj->list = array();

foreach (Utilities::getValueFromArray($response, 'data', array()) as &$event) {
$obj->list []= static::arrayToModel($event, static::QUALIFIED_NAME);
}

Expand Down
9 changes: 1 addition & 8 deletions src/Models/Payments/AlipaySource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @license https://opensource.org/licenses/mit-license.html MIT License
* @link https://docs.checkout.com/
*/
class AlipaySource extends IdSource
class AlipaySource extends Source
{

/**
Expand All @@ -36,13 +36,6 @@ class AlipaySource extends IdSource
*/
const QUALIFIED_NAME = __CLASS__;

/**
* Qualified namespace of the class.
*
* @var string
*/
const QUALIFIED_NAMESPACE = __NAMESPACE__;

/**
* Name of the model.
*
Expand Down
9 changes: 1 addition & 8 deletions src/Models/Payments/BancontactSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @license https://opensource.org/licenses/mit-license.html MIT License
* @link https://docs.checkout.com/
*/
class BancontactSource extends IdSource
class BancontactSource extends Source
{

/**
Expand All @@ -36,13 +36,6 @@ class BancontactSource extends IdSource
*/
const QUALIFIED_NAME = __CLASS__;

/**
* Qualified namespace of the class.
*
* @var string
*/
const QUALIFIED_NAMESPACE = __NAMESPACE__;

/**
* Name of the model.
*
Expand Down
15 changes: 4 additions & 11 deletions src/Models/Payments/BoletoSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @license https://opensource.org/licenses/mit-license.html MIT License
* @link https://docs.checkout.com/
*/
class BoletoSource extends IdSource
class BoletoSource extends Source
{

/**
Expand All @@ -36,13 +36,6 @@ class BoletoSource extends IdSource
*/
const QUALIFIED_NAME = __CLASS__;

/**
* Qualified namespace of the class.
*
* @var string
*/
const QUALIFIED_NAMESPACE = __NAMESPACE__;

/**
* Name of the model.
*
Expand All @@ -58,9 +51,9 @@ class BoletoSource extends IdSource
/**
* Initialise Boleto source.
*
* @param string $name
* @param string $birthdate
* @param string $cpf
* @param string $name The customer's name.
* @param string $birthdate The date of birth (YYYY-MM-DD).
* @param string $cpf The Brazilian personal tax identifier (Cadastro de Pessoas Físicas).
*/
public function __construct($name, $birthdate, $cpf)
{
Expand Down
Loading

0 comments on commit 729fb64

Please sign in to comment.