-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related work items: #20822, #20823, #20978
- Loading branch information
Showing
10 changed files
with
447 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.