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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"require-dev": {
"ext-openssl": "*",
"php-parallel-lint/php-parallel-lint": "^1.3.2",
"phpunit/phpunit": "^5.7.27 || ^9.6.10",
"phpunit/phpunit": "^5.7 || ^7.5 || ^8.5 || ^9.5",
"psx/cache": "^v1.0.2"
},
"autoload": {
Expand Down
24 changes: 24 additions & 0 deletions examples/Notifications/AllNotificationsExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use PSX\Cache\SimpleCache;
use Tpay\Example\ExamplesConfig;
use Tpay\OpenApi\Model\Objects\NotificationBody\BasicPayment;
use Tpay\OpenApi\Model\Objects\NotificationBody\BlikAliasRegister;
use Tpay\OpenApi\Model\Objects\NotificationBody\BlikAliasUnregister;
use Tpay\OpenApi\Model\Objects\NotificationBody\MarketplaceTransaction;
use Tpay\OpenApi\Model\Objects\NotificationBody\Tokenization;
use Tpay\OpenApi\Model\Objects\NotificationBody\TokenUpdate;
Expand Down Expand Up @@ -81,6 +83,28 @@ public function getVerifiedNotification()
// $marketplaceTransactionProcessor->process($notification)
exit('{"result":true}');
}
if ($notification instanceof BlikAliasRegister) {
// Notification about successful blik alias registered

$value = $notification->value->getValue();
// The above example will check the notification and return the value for future transactions,
// correlate this value with the payer/user of your system for subsequent payment handling
// You can access any notification field by $notification->fieldName

// $blikAliasRegisteredProcessor->process($notification)
exit('TRUE');
}

if ($notification instanceof BlikAliasUnregister) {
// Notification about successful blik alias unregistered

$value = $notification->value->getValue();
// The above example will check the notification and return the value of deleted token
// You can access any notification field by $notification->fieldName

// $blikAliasRegisteredProcessor->process($notification)
exit('TRUE');
}

// Ignore and silence other notification types if not expected
http_response_code(404);
Expand Down
11 changes: 11 additions & 0 deletions src/Model/Fields/Notification/BlikAlias/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tpay\OpenApi\Model\Fields\Notification\BlikAlias;

use Tpay\OpenApi\Model\Fields\Field;

class Event extends Field
{
protected $name = __CLASS__;
protected $type = self::STRING;
}
11 changes: 11 additions & 0 deletions src/Model/Fields/Notification/BlikAlias/ExpirationDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tpay\OpenApi\Model\Fields\Notification\BlikAlias;

use Tpay\OpenApi\Model\Fields\Field;

class ExpirationDate extends Field
{
protected $name = __CLASS__;
protected $type = self::STRING;
}
11 changes: 11 additions & 0 deletions src/Model/Fields/Notification/BlikAlias/Id.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tpay\OpenApi\Model\Fields\Notification\BlikAlias;

use Tpay\OpenApi\Model\Fields\Field;

class Id extends Field
{
protected $name = __CLASS__;
protected $type = self::INT;
}
11 changes: 11 additions & 0 deletions src/Model/Fields/Notification/BlikAlias/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tpay\OpenApi\Model\Fields\Notification\BlikAlias;

use Tpay\OpenApi\Model\Fields\Field;

class Type extends Field
{
protected $name = __CLASS__;
protected $type = self::STRING;
}
11 changes: 11 additions & 0 deletions src/Model/Fields/Notification/BlikAlias/Value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tpay\OpenApi\Model\Fields\Notification\BlikAlias;

use Tpay\OpenApi\Model\Fields\Field;

class Value extends Field
{
protected $name = __CLASS__;
protected $type = self::STRING;
}
44 changes: 44 additions & 0 deletions src/Model/Objects/NotificationBody/BlikAliasRegister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tpay\OpenApi\Model\Objects\NotificationBody;

use Tpay\OpenApi\Model\Fields\Notification\BlikAlias\ExpirationDate;
use Tpay\OpenApi\Model\Fields\Notification\BlikAlias\Type;
use Tpay\OpenApi\Model\Fields\Notification\BlikAlias\Value;
use Tpay\OpenApi\Model\Objects\Objects;

class BlikAliasRegister extends Objects
{
const OBJECT_FIELDS = [
'value' => Value::class,
'type' => Type::class,
'expirationDate' => ExpirationDate::class,
];

/** @var Value */
public $value;

/** @var Type */
public $type;

/** @var ExpirationDate */
public $expirationDate;

public function getRequiredFields()
{
return [
$this->value,
$this->type,
$this->expirationDate,
];
}

public function toArray()
{
return [
'value' => $this->value->getValue(),
'type' => $this->type->getValue(),
'expirationDate' => $this->expirationDate->getValue(),
];
}
}
37 changes: 37 additions & 0 deletions src/Model/Objects/NotificationBody/BlikAliasUnregister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Tpay\OpenApi\Model\Objects\NotificationBody;

use Tpay\OpenApi\Model\Fields\Notification\BlikAlias\Type;
use Tpay\OpenApi\Model\Fields\Notification\BlikAlias\Value;
use Tpay\OpenApi\Model\Objects\Objects;

class BlikAliasUnregister extends Objects
{
const OBJECT_FIELDS = [
'value' => Value::class,
'type' => Type::class,
];

/** @var Value */
public $value;

/** @var Type */
public $type;

public function getRequiredFields()
{
return [
$this->value,
$this->type,
];
}

public function toArray()
{
return [
'value' => $this->value->getValue(),
'type' => $this->type->getValue(),
];
}
}
25 changes: 20 additions & 5 deletions src/Utilities/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

class RequestParser
{
/** @var null|string */
private $rawBody;

/** @return string */
public function getContentType()
{
return $_SERVER['CONTENT_TYPE'] ?: '';
return isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
}

/**
Expand All @@ -17,11 +20,14 @@ public function getContentType()
*/
public function getParsedContent()
{
if ('application/json' === $this->getContentType()) {
$body = file_get_contents('php://input');
if (false !== strpos($this->getContentType(), 'application/json')) {
$body = $this->getRawBody();
$jsonData = json_decode($body, true);

if (is_null($jsonData)) {
throw new TpayException('Invalid JSON body. Json Error: '.json_last_error_msg().' Body: '.$body);
throw new TpayException(
'Invalid JSON body. Json Error: '.json_last_error_msg().' Body: '.$body
);
}

return $jsonData;
Expand All @@ -33,7 +39,7 @@ public function getParsedContent()
/** @return string */
public function getPayload()
{
return file_get_contents('php://input');
return $this->getRawBody();
}

/**
Expand All @@ -50,4 +56,13 @@ public function getSignature()

return $jws;
}

private function getRawBody()
{
if (null === $this->rawBody) {
$this->rawBody = file_get_contents('php://input');
}

return $this->rawBody;
}
}
82 changes: 63 additions & 19 deletions src/Webhook/JWSVerifiedPaymentNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Tpay\OpenApi\Webhook;

use Tpay\OpenApi\Model\Fields\Field;
use Tpay\OpenApi\Model\Objects\NotificationBody\BasicPayment;
use Tpay\OpenApi\Model\Objects\NotificationBody\BlikAliasRegister;
use Tpay\OpenApi\Model\Objects\NotificationBody\BlikAliasUnregister;
use Tpay\OpenApi\Model\Objects\NotificationBody\MarketplaceTransaction;
use Tpay\OpenApi\Model\Objects\NotificationBody\Tokenization;
use Tpay\OpenApi\Model\Objects\NotificationBody\TokenUpdate;
Expand Down Expand Up @@ -164,12 +167,12 @@ private function getResourcePrefix()
*/
private function getNotificationObject()
{
if ('application/json' === $this->requestParser->getContentType()) {
$jsonData = $this->requestParser->getParsedContent();
if (!isset($jsonData['type'])) {
throw new TpayException('Not recognised or invalid notification type. JSON: '.json_encode($jsonData));
}
switch ($jsonData['type']) {
$source = $this->requestParser->getParsedContent();

if (isset($source['tr_id'])) {
$requestBody = new BasicPayment();
} elseif (isset($source['type'])) {
switch ($source['type']) {
case 'tokenization':
$requestBody = new Tokenization();
break;
Expand All @@ -180,28 +183,69 @@ private function getNotificationObject()
$requestBody = new MarketplaceTransaction();
break;
default:
throw new TpayException('Not recognised or invalid notification type. JSON: '.json_encode($jsonData));
throw new TpayException(
'Not recognised or invalid notification type: '.$source['type']
);
}
if (!isset($jsonData['data'])) {
throw new TpayException('Not recognised or invalid notification type. JSON: '.json_encode($jsonData));

if (!isset($source['data'])) {
throw new TpayException('Not recognised or invalid notification type: '.json_encode($source));
}
$source = $jsonData['data'];
} else {
$source = $this->requestParser->getParsedContent();
if (!isset($source['tr_id'])) {
throw new TpayException('Not recognised or invalid notification type. POST: '.json_encode($source));

$source = $source['data'];
} elseif (isset($source['event'])) {
switch ($source['event']) {
case 'ALIAS_REGISTER':
$requestBody = new BlikAliasRegister();
break;
case 'ALIAS_UNREGISTER':
$requestBody = new BlikAliasUnregister();
break;
default:
throw new TpayException(
'Not recognised or invalid notification event: '.$source['event']
);
}
$requestBody = new BasicPayment();
}
foreach ($source as $parameter => $value) {
if (isset($requestBody->{$parameter})) {
$source[$parameter] = Util::cast($value, $requestBody->{$parameter}->getType());
if (!isset($source['msg_value']) || !is_array($source['msg_value'])) {
throw new TpayException('Not recognised or invalid notification event: '.json_encode($source));
}
$source = $source['msg_value'];
} else {
throw new TpayException(
'Cannot determine notification type. POST payload: '.json_encode($source)
);
}

$source = $this->castRequestBody($source, $requestBody);

$this->Manager
->setRequestBody($requestBody)
->setFields($source, false);

return $this->Manager->getRequestBody();
}

private function castRequestBody($source, $requestBody)
{
$fields = [];
$definitions = $requestBody::OBJECT_FIELDS;

foreach ($source as $parameter => $value) {
if (!isset($definitions[$parameter])) {
continue;
}

$definition = $definitions[$parameter];

/** @var Field $field */
$field = new $definition();

$fields[$parameter] = Util::cast(
$value,
$field->getType()
);
}

return $fields;
}
}
Loading