Skip to content

Commit

Permalink
Added API v0.3 support and plugin information headers (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
vytautasgimbutas authored Mar 24, 2021
1 parent f1fcdd3 commit c7f8157
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 57 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## [0.10.0] - 2021-03-19
- Added API v0.3 support
- Added support for specifying plugin integration information

## [0.9.3] - 2021-01-20
- Added option to choose API version.

Expand Down
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use Kevin\Client;
$clientId = 'my-client-id';
$clientSecret = 'my-client-secret';
$options = ['error' => 'array', 'version' => '0.1'];
$options = ['error' => 'array', 'version' => '0.3'];
$kevinClient = new Client($clientId, $clientSecret, $options);
```
Expand All @@ -53,7 +53,7 @@ $kevinClient = new Client($clientId, $clientSecret, $options);
>
> - `error` - Defines return type of error data. Possible values are: `array` - returns an array on error, `exception` - throws an exception on error, default value is `exception`.
>
> - `version` - Selects API versions to use. Default value is `0.2`. Possible values are `0.1` or `0.2`.
> - `version` - Selects API versions to use. Default value is `0.3`. Possible values are `0.1`, `0.2` or `0.3`.
### 1. Authentication

Expand Down Expand Up @@ -116,32 +116,53 @@ $response = $kevinClient->auth()->receiveTokenContent($attr);

### 2. Payment

### 2.1 Initiate payment
### 2.1 Initiate bank payment

```
$attr = [
'Redirect-URL' => 'https://redirect.getkevin.eu/payment.html',
'endToEndId' => '1',
'informationUnstructured' => 'Test',
'description' => 'Test',
'currencyCode' => 'EUR',
'amount' => '0.01',
'creditorName' => 'John Smith',
'creditorAccount' => [
'iban' => 'LT144010051005081586'
]
'bankPaymentMethod' => [
'endToEndId' => '1',
'creditorName' => 'John Smith',
'creditorAccount' => [
'iban' => 'LT144010051005081586'
],
],
];
$response = $kevinClient->payment()->initPayment($attr);
```

### 2.2 Get payment
### 2.2 Initiate card payment

```
$attr = [
'Redirect-URL' => 'https://redirect.getkevin.eu/payment.html',
'description' => 'Test',
'currencyCode' => 'EUR',
'amount' => '0.01',
'cardPaymentMethod' => [
'cvc' => '123',
'expMonth' => '01',
'expYear' => '2036',
'number' => '5555555555555555',
'holderName' => 'John Titor',
],
];
$response = $kevinClient->payment()->initPayment($attr);
```

### 2.3 Get payment

```
$paymentId = 'your-payment-id';
$attr = ['PSU-IP-Address' => 'user-ip-address'];
$response = $kevinClient->payment()->getPayment($paymentId, $attr);
```

### 2.3 Get payment status
### 2.4 Get payment status

```
$paymentId = 'your-payment-id';
Expand Down
3 changes: 2 additions & 1 deletion src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public function auth($attr = [])
}

$jsonData = $this->getAuthBodyAttr($attr);
$data = json_encode($jsonData);

$data = json_encode($jsonData, JSON_FORCE_OBJECT);

$header = array_merge($this->getAuthHeaderAttr($attr), $this->buildJsonHeader($data));

Expand Down
1 change: 1 addition & 0 deletions src/EndpointInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface EndpointInterface
const BASE_URL = 'https://api.getkevin.eu/platform';
const BASE_URL_V01 = self::BASE_URL . '/v0.1';
const BASE_URL_V02 = self::BASE_URL . '/v0.2';
const BASE_URL_V03 = self::BASE_URL . '/v0.3';

/**
* List of Auth related endpoints.
Expand Down
61 changes: 40 additions & 21 deletions src/PaymentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,46 @@ private function getPaymentQueryAttr($attr = [])
private function getInitPaymentBodyAttr($attr)
{
$schema = [
'creditorName',
'creditorName' => '',
'creditorAccount' => [
'iban',
'bban',
'sortCodeAccountNumber'
'iban' => '',
'bban' => '',
'sortCodeAccountNumber' => '',
],
'debtorAccount' => [
'iban',
'bban',
'sortCodeAccountNumber'
'iban' => '',
'bban' => '',
'sortCodeAccountNumber' => '',
],
'amount',
'currencyCode',
'endToEndId',
'informationUnstructured',
'bankPaymentMethod' => [
'creditorName' => '',
'endToEndId' => '',
'informationStructured' => [
'reference' => '',
],
'creditorAccount' => [
'iban' => '',
]
],
'cardPaymentMethod' => [
'cvc' => '',
'expMonth' => '',
'expYear' => '',
'number' => '',
'holderName' => '',
],
'amount' => '',
'currencyCode' => '',
'description' => '',
'endToEndId' => '',
'informationUnstructured' => '',
'informationStructured' => [
'reference',
'referenceType'
'reference' => '',
'referenceType' => '',
],
'requestedExecutionDate',
'requestedExecutionDate' => '',
'identifier' => [
'email'
'email' => '',
]
];

Expand All @@ -77,12 +95,13 @@ private function getInitPaymentBodyAttr($attr)
*/
private function getInitPaymentHeaderAttr($attr = [])
{
$data = [];

if (isset($attr['Authorization'])) {
$data[] = 'Authorization: ' . $this->unifyBearerToken($attr['Authorization']);
$data = array_merge(
['Authorization: ' . $this->unifyBearerToken($attr['Authorization'])],
$this->buildPluginInformationHeader()
);
} else {
$data = array_merge($this->buildHeader(), $data);
$data = $this->buildHeader();
}

if (isset($attr['Redirect-URL'])) {
Expand Down Expand Up @@ -110,7 +129,7 @@ private function getPaymentHeaderAttr($attr = [])
$data[] = 'PSU-IP-Address: ' . $attr['PSU-IP-Address'];
}

if ($this->getOption('version') == '0.2') {
if (in_array($this->getOption('version'), ['0.2', '0.3'])) {
if (isset($attr['PSU-IP-Port'])) {
$data[] = 'PSU-IP-Port: ' . $attr['PSU-IP-Port'];
}
Expand Down Expand Up @@ -139,7 +158,7 @@ private function getPaymentStatusHeaderAttr($attr = [])
$data[] = 'PSU-IP-Address: ' . $attr['PSU-IP-Address'];
}

if ($this->getOption('version') == '0.2') {
if (in_array($this->getOption('version'), ['0.2', '0.3'])) {
if (isset($attr['PSU-User-Agent'])) {
$data[] = 'PSU-User-Agent: ' . $attr['PSU-User-Agent'];
}
Expand Down
89 changes: 65 additions & 24 deletions src/UtilityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,34 @@ trait UtilityTrait
*/
private function buildHeader()
{
return [
$data = [
'Client-Id: ' . $this->clientId,
'Client-Secret: ' . $this->clientSecret
'Client-Secret: ' . $this->clientSecret,
];

return array_merge($data, $this->buildPluginInformationHeader());
}

private function buildPluginInformationHeader() {
$data = [];

$pluginVersion = $this->getOption('pluginVersion');
$pluginPlatform = $this->getOption('pluginPlatform');
$pluginPlatformVersion = $this->getOption('pluginPlatformVersion');

if (isset($pluginVersion)) {
$data[] = 'Plugin-Version: ' . $pluginVersion;
}

if (isset($pluginPlatform)) {
$data[] = 'Plugin-Platform: ' . $pluginPlatform;
}

if (isset($pluginPlatformVersion)) {
$data[] = 'Plugin-Platform-Version: ' . $pluginPlatformVersion;
}

return $data;
}

/**
Expand Down Expand Up @@ -109,7 +133,6 @@ private function buildRequest($url, $type, $jsonData, $header)
$data = array_merge($default_headers, $header);
$data[] = "Connection: Close";
$data[] = ""; // Separator

if ($type === 'POST') {
$data[] = "$jsonData\r\n";
}
Expand All @@ -130,7 +153,6 @@ private function buildRequest($url, $type, $jsonData, $header)
$result = trim($parts[1]);

$header = explode("\r\n", $header);

$code = -1;
foreach ($header as $value) {
if (substr($value, 0, 4) === 'HTTP') {
Expand Down Expand Up @@ -246,24 +268,7 @@ private function gluePath($url, ...$parameters)
*/
private function processSchemaAttributes(array $schema, array $attr)
{
$data = [];

foreach ($schema as $item => $value) {
if (is_string($value)) {
if (isset($attr[$value])) {
$data[$value] = strval($attr[$value]);
}
} else if (is_array($value)) {
foreach ($value as $sub_value) {
if (isset($attr[$item][$sub_value])) {
$data[$item] = [];
$data[$item][$sub_value] = strval($attr[$item][$sub_value]);
}
}
}
}

return $data;
return $this->intersectArrayRecursively($attr, $schema);
}

/**
Expand Down Expand Up @@ -315,19 +320,29 @@ private function processOptionsAttributes(array $options)
{
$data = [
'error' => 'exception',
'version' => '0.2'
'version' => '0.3'
];

$option_error = ['exception', 'array'];
if (isset($options['error']) && in_array($options['error'], $option_error)) {
$data['error'] = $options['error'];
}

$option_version = ['0.1', '0.2'];
$option_version = ['0.1', '0.2', '0.3'];
if (isset($options['version']) && in_array($options['version'], $option_version)) {
$data['version'] = $options['version'];
}

if (isset($options['pluginVersion'])) {
$data['pluginVersion'] = $options['pluginVersion'];
}
if (isset($options['pluginPlatform'])) {
$data['pluginPlatform'] = $options['pluginPlatform'];
}
if (isset($options['pluginPlatformVersion'])) {
$data['pluginPlatformVersion'] = $options['pluginPlatformVersion'];
}

return $data;
}

Expand Down Expand Up @@ -382,6 +397,9 @@ private function getBaseUrl()
case '0.2':
$base_url = self::BASE_URL_V02;
break;
case '0.3':
$base_url = self::BASE_URL_V03;
break;
default:
$base_url = self::BASE_URL_V02;
}
Expand All @@ -397,4 +415,27 @@ private function getEndpointUrl($path = '')
{
return $this->getBaseUrl() . $path;
}

/**
* @param array $master
* @param array $mask
* @return array
*/
private function intersectArrayRecursively($master, $mask)
{
if (!is_array($master)) {
return $master;
}

foreach ($master as $k => $v) {
if (!isset($mask[$k])) {
unset ($master[$k]);
continue;
}
if (is_array($mask[$k])) {
$master[$k] = $this->intersectArrayRecursively($master[$k], $mask[$k]);
}
}
return $master;
}
}

0 comments on commit c7f8157

Please sign in to comment.