Skip to content

Commit

Permalink
Merged PR 13951: Release 1.02
Browse files Browse the repository at this point in the history
Related work items: #20822, #20823, #20978
  • Loading branch information
pawel.karczmarczyk committed Oct 21, 2022
2 parents 25bd067 + 2839e73 commit 2f41ade
Show file tree
Hide file tree
Showing 10 changed files with 447 additions and 7 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nets/paymentcore",
"description": "Core for EasyNets and NextAccept payments plugin",
"type": "package",
"type": "library",
"license": "MIT",
"version": "0.0.3",
"autoload": {
Expand All @@ -17,7 +17,7 @@
"require": {
"php": ">=7.4",
"guzzlehttp/guzzle": "^7.4.5",
"myclabs/php-enum": "dev-master",
"myclabs/php-enum": "^1.8.4",
"ext-json": "*"
},
"minimum-stability": "dev",
Expand Down
67 changes: 67 additions & 0 deletions src/Auth/NetaxeptSandboxAPIAuthService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace NetsCore\Auth;

use NetsCore\Enums\ApiUrlsEnum;
use NetsCore\Interfaces\APIAuthServiceInterface;
use NetsCore\Interfaces\ConfigurationInterface;
use NetsCore\Services\ApiService;

class NetaxeptSandboxAPIAuthService implements APIAuthServiceInterface
{
protected ConfigurationInterface $configuration;
private ApiService $apiService;

/**
* @param ConfigurationInterface $configuration
* @param ApiService|null $apiService
*/
public function __construct(ConfigurationInterface $configuration, ApiService $apiService = null)
{
$this->apiService = $apiService ?: new ApiService();
$this->configuration = $configuration;
}

/**
* @return mixed
*/
public function authorize()
{
return $this->apiService->post(
$this->configuration->getAuthUrl(),
$this->generateHeaders(),
$this->getOptions()
);
}

/**
* @return string[]
*/
private function generateHeaders(): array
{
return [
'Authorization' => 'Basic ' . base64_encode(
$this->configuration->getUsername() . ':' . $this->configuration->getPassword()
),
];
}

/**
* @return array[]
*/
private function getOptions(): array
{
return [
'multipart' => [
[
'name' => 'grant_type',
'contents' => 'client_credentials',
],
[
'name' => 'scope',
'contents' => ApiUrlsEnum::NETAXEPT_SANDBOX_API_SCOPE_PAYMENT_SERVICE,
],
],
];
}
}
179 changes: 179 additions & 0 deletions src/Clients/NetaxeptSandboxAPIClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php

namespace NetsCore\Clients;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use NetsCore\Enums\ApiUrlsEnum;
use NetsCore\Exceptions\ApiResponseException;
use NetsCore\Interfaces\APIClientInterface;
use NetsCore\Interfaces\PaymentRequestInterface;
use NetsCore\Interfaces\PaymentObjectInterface;

class NetaxeptSandboxAPIClient implements APIClientInterface
{
protected array $authData;
private Client $httpClient;

/**
* @param array $authData
* @param Client|null $client
*/
public function __construct(array $authData, Client $client = null)
{
$this->authData = $authData;
$this->httpClient = $client ?: new Client();
}

/**
* @param PaymentObjectInterface $paymentObject
*
* @return mixed
* @throws ApiResponseException
*/
public function createPayment(PaymentObjectInterface $paymentObject)
{
$request = new Request(
'POST',
ApiUrlsEnum::NETAXEPT_SANDBOX_PAYMENT_SERVICE,
$this->generateHeader(),
json_encode($paymentObject)
);
try {
$res = $this->httpClient->sendAsync($request)->wait();

return $res->getBody();
} catch (RequestException $e) {
throw new ApiResponseException();
}
}

/**
* @param PaymentRequestInterface $authorizationObject
*
* @return mixed
* @throws ApiResponseException
*/
public function authorizePayment(PaymentRequestInterface $authorizationObject)
{
$request = new Request(
'POST',
ApiUrlsEnum::NETAXEPT_SANDBOX_PAYMENT_SERVICE . $authorizationObject->getPaymentId(
) . ApiUrlsEnum::NETAXEPT_API_PAYMENT_AUTHORIZATION,
$this->generateHeader(),
json_encode($authorizationObject->getBodyRequest())
);
try {
$res = $this->httpClient->sendAsync($request)->wait();

return $res->getBody();
} catch (RequestException $e) {
throw new ApiResponseException();
}
}

/**
* @param PaymentRequestInterface $paymentObject
*
* @return mixed
* @throws ApiResponseException
*/
public function cancelPayment(PaymentRequestInterface $paymentObject)
{
$request = new Request(
'POST',
ApiUrlsEnum::NETAXEPT_SANDBOX_PAYMENT_SERVICE . $paymentObject->getPaymentId(
) . ApiUrlsEnum::NETAXEPT_API_CANCEL,
$this->generateHeader(),
json_encode($paymentObject->getBodyRequest())
);
try {
$res = $this->httpClient->sendAsync($request)->wait();

return $res->getBody();
} catch (RequestException $e) {
throw new ApiResponseException();
}
}

/**
* @throws ApiResponseException
*/
public function capturePayment(PaymentRequestInterface $capturePayment)
{
$request = new Request(
'POST',
ApiUrlsEnum::NETAXEPT_SANDBOX_PAYMENT_SERVICE . $capturePayment->getPaymentId(
) . ApiUrlsEnum::NETAXEPT_API_CAPTURE,
$this->generateHeader(),
json_encode($capturePayment->getBodyRequest())
);
try {
$res = $this->httpClient->sendAsync($request)->wait();

return $res->getBody();
} catch (RequestException $e) {
throw new ApiResponseException();
}
}

/**
* @throws ApiResponseException
*/
public function getPaymentDetails(string $paymentId)
{
$request = new Request(
'GET',
ApiUrlsEnum::NETAXEPT_SANDBOX_PAYMENT_SERVICE . $paymentId,
$this->generateHeader()
);
try {
$res = $this->httpClient->sendAsync($request)->wait();

return $res->getBody();
} catch (RequestException $e) {
throw new ApiResponseException();
}
}

/**
* @param PaymentRequestInterface $refundObject
*
* @return mixed
* @throws ApiResponseException
*/
public function refundPayment(PaymentRequestInterface $refundObject)
{
$request = new Request(
'POST',
ApiUrlsEnum::NETAXEPT_SANDBOX_PAYMENT_SERVICE . $refundObject->getPaymentId(
) . ApiUrlsEnum::NETAXEPT_API_REFUND,
$this->generateHeader(),
json_encode($refundObject->getBodyRequest())
);
try {
$res = $this->httpClient->sendAsync($request)->wait();

return $res->getBody();
} catch (RequestException $e) {
throw new ApiResponseException();
}
}

public function salePayment()
{
//TODO: Implement sale payment request
}

/**
* @return string[]
*/
private function generateHeader(): array
{
return [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->authData['token'],
];
}
}
71 changes: 71 additions & 0 deletions src/Configuration/NetaxeptSandboxConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace NetsCore\Configuration;

use NetsCore\Enums\ApiUrlsEnum;
use NetsCore\Enums\ClientTypeEnum;
use NetsCore\Interfaces\ConfigurationInterface;

class NetaxeptSandboxConfiguration implements ConfigurationInterface
{
protected string $clientType = ClientTypeEnum::NETAXEPT_SANDBOX;
private string $username;
private string $password;

/**
* @return string
*/
public function getClientType(): string
{
return $this->clientType;
}


/**
* @return string
*/
public function getUsername(): string
{
return $this->username;
}

/**
* @param string $username
*
* @return $this
*/
public function setUsername(string $username): NetaxeptSandboxConfiguration
{
$this->username = $username;

return $this;
}

/**
* @return string
*/
public function getPassword(): string
{
return $this->password;
}

/**
* @param string $password
*
* @return $this
*/
public function setPassword(string $password): NetaxeptSandboxConfiguration
{
$this->password = $password;

return $this;
}

/**
* @return string
*/
public function getAuthUrl(): string
{
return ApiUrlsEnum::NETAXEPT_SANDBOX_O_AUTH_AUTHORIZATION;
}
}
16 changes: 14 additions & 2 deletions src/Dto/Netaxept/Request/PaymentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use NetsCore\Dto\Netaxept\BasketItemDto;
use NetsCore\Dto\Netaxept\PayPageConfigurationDto;
use NetsCore\Dto\Netaxept\RedirectUrlDto;
use NetsCore\Enums\LanguageEnum;
use NetsCore\Interfaces\CustomerInterface;
use NetsCore\Interfaces\PaymentMethodDetailsInterface;
use NetsCore\Interfaces\PaymentObjectInterface;
Expand All @@ -30,8 +31,19 @@ class PaymentObject implements PaymentObjectInterface
*/
public array $basket;

public function getPaymentId()
public function setLanguage(string $language)
{
// TODO: Implement getPaymentId() method.
$this->payPageConfiguration = new PayPageConfigurationDto();
switch ($language)
{
case 'English':
$this->payPageConfiguration->language = LanguageEnum::EN;
break;
case 'Deutsch':
$this->payPageConfiguration->language = LanguageEnum::DE;
break;
default:
$this->payPageConfiguration->language = LanguageEnum::EN;
}
}
}
3 changes: 3 additions & 0 deletions src/Enums/ApiUrlsEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

class ApiUrlsEnum extends Enum
{
public const NETAXEPT_SANDBOX_PAYMENT_SERVICE = 'https://sandbox.unifiedapi.nets.eu/api/v1/Payments/';
public const NETAXEPT_PAYMENT_SERVICE = 'https://unifiedapi.netsdev.eu/api/v1/Payments/';
public const NETAXEPT_O_AUTH_AUTHORIZATION = 'https://login.microsoftonline.com/2b8c81c1-69c3-4fcd-9de2-4579ced9d137/oauth2/v2.0/token';
public const NETAXEPT_SANDBOX_O_AUTH_AUTHORIZATION = 'https://login.microsoftonline.com/unifiedcommercesandbox.onmicrosoft.com/oauth2/v2.0/token';
public const NETAXEPT_API_SCOPE_PAYMENT_SERVICE = 'https://unifiedcommercedev.onmicrosoft.com/unifiedapi/paymentservice/.default';
public const NETAXEPT_SANDBOX_API_SCOPE_PAYMENT_SERVICE = 'https://unifiedcommercesandbox.onmicrosoft.com/unifiedapi/paymentservice/.default';
public const NETAXEPT_API_CANCEL = '/Cancel';
public const NETAXEPT_API_CAPTURE = '/Capture';
public const NETAXEPT_API_REFUND = '/Refund';
Expand Down
1 change: 1 addition & 0 deletions src/Enums/ClientTypeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ final class ClientTypeEnum extends Enum
{
public const EASY_NETS = 'NetsEasy';
public const NETAXEPT = 'Netaxept';
public const NETAXEPT_SANDBOX = 'NetaxeptSandbox';
}
Loading

0 comments on commit 2f41ade

Please sign in to comment.