-
Notifications
You must be signed in to change notification settings - Fork 214
[ECP-9482] Refactor HeaderDataBuilder and TransactionClient #2769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dc1b659
06aa3c6
7cc3e2a
cd9f4d0
3b0ca36
ae841e0
59dddd0
2333fff
5088018
01c741b
eb63a59
bad7cbc
86b406b
0bfed34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Adyen\Payment\Gateway\Http\Client; | ||
|
||
use Adyen\Payment\Helper\Data; | ||
use Adyen\Payment\Gateway\Request\Header\HeaderDataBuilder; | ||
use Magento\Payment\Gateway\Http\TransferInterface; | ||
use Magento\Payment\Gateway\Http\ClientInterface; | ||
|
||
abstract class BaseTransaction implements ClientInterface | ||
{ | ||
|
||
/** | ||
* @var Data | ||
*/ | ||
protected Data $adyenHelper; | ||
|
||
/** | ||
* BaseTransactionClient constructor. | ||
* @param Data $adyenHelper | ||
*/ | ||
public function __construct(Data $adyenHelper) | ||
{ | ||
$this->adyenHelper = $adyenHelper; | ||
} | ||
|
||
/** | ||
* Builds the headers if they are not provided. | ||
* | ||
* @param TransferInterface $transferObject | ||
* @return array | ||
*/ | ||
protected function requestHeaders(TransferInterface $transferObject): array | ||
{ | ||
$headers = $transferObject->getHeaders(); | ||
|
||
// If no headers exist, build them using HeaderDataBuilder | ||
if (empty($headers)) { | ||
$headerBuilder = new HeaderDataBuilder($this->adyenHelper); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
However, if you strictly want to use a class' new instance created during the runtime, you can use factory design pattern. |
||
$headers = $headerBuilder->buildRequestHeaders(); | ||
} | ||
|
||
return $headers; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,15 +17,14 @@ | |
use Adyen\Payment\Helper\Data; | ||
use Adyen\Payment\Helper\Idempotency; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Payment\Gateway\Http\ClientInterface; | ||
use Magento\Payment\Gateway\Http\TransferInterface; | ||
|
||
class TransactionCancel implements ClientInterface | ||
class TransactionCancel extends BaseTransaction | ||
{ | ||
/** | ||
* @var Data | ||
*/ | ||
private Data $adyenHelper; | ||
protected Data $adyenHelper; | ||
|
||
/** | ||
* @var Idempotency | ||
|
@@ -40,7 +39,7 @@ public function __construct( | |
Data $adyenHelper, | ||
Idempotency $idempotencyHelper | ||
) { | ||
$this->adyenHelper = $adyenHelper; | ||
parent::__construct($adyenHelper); | ||
$this->idempotencyHelper = $idempotencyHelper; | ||
} | ||
|
||
|
@@ -53,20 +52,20 @@ public function __construct( | |
public function placeRequest(TransferInterface $transferObject): array | ||
{ | ||
$requests = $transferObject->getBody(); | ||
$headers = $transferObject->getHeaders(); | ||
$clientConfig = $transferObject->getClientConfig(); | ||
$requestOptions['headers'] = $this->requestHeaders($transferObject); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally |
||
|
||
$clientConfig = $transferObject->getClientConfig(); | ||
$client = $this->adyenHelper->initializeAdyenClientWithClientConfig($clientConfig); | ||
$service = $this->adyenHelper->initializeModificationsApi($client); | ||
$responseData = []; | ||
|
||
foreach ($requests as $request) { | ||
$idempotencyKey = $this->idempotencyHelper->generateIdempotencyKey( | ||
$request, | ||
$headers['idempotencyExtraData'] ?? null | ||
$requestOptions['headers']['idempotencyExtraData'] ?? null | ||
); | ||
|
||
$requestOptions['idempotencyKey'] = $idempotencyKey; | ||
$requestOptions['headers'] = $this->adyenHelper->buildRequestHeaders(); | ||
$this->adyenHelper->logRequest($request, Client::API_CHECKOUT_VERSION, '/cancels'); | ||
$request['applicationInfo'] = $this->adyenHelper->buildApplicationInfo($client); | ||
$paymentCancelRequest = new PaymentCancelRequest($request); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,9 @@ | |
use Adyen\Payment\Logger\AdyenLogger; | ||
use Adyen\Service\Checkout\ModificationsApi; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Payment\Gateway\Http\ClientInterface; | ||
use Magento\Payment\Gateway\Http\TransferInterface; | ||
|
||
class TransactionCapture implements ClientInterface | ||
class TransactionCapture extends BaseTransaction | ||
{ | ||
const MULTIPLE_AUTHORIZATIONS = 'multiple_authorizations'; | ||
const FORMATTED_CAPTURE_AMOUNT = 'formatted_capture_amount'; | ||
|
@@ -35,7 +34,7 @@ class TransactionCapture implements ClientInterface | |
/** | ||
* @var Data | ||
*/ | ||
private Data $adyenHelper; | ||
protected Data $adyenHelper; | ||
|
||
/** | ||
* @var AdyenLogger | ||
|
@@ -57,7 +56,7 @@ public function __construct( | |
AdyenLogger $adyenLogger, | ||
Idempotency $idempotencyHelper | ||
) { | ||
$this->adyenHelper = $adyenHelper; | ||
parent::__construct($adyenHelper); | ||
$this->adyenLogger = $adyenLogger; | ||
$this->idempotencyHelper = $idempotencyHelper; | ||
} | ||
|
@@ -71,13 +70,12 @@ public function __construct( | |
public function placeRequest(TransferInterface $transferObject): array | ||
{ | ||
$request = $transferObject->getBody(); | ||
$headers = $transferObject->getHeaders(); | ||
$clientConfig = $transferObject->getClientConfig(); | ||
$requestOptions['headers'] = $this->requestHeaders($transferObject); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally |
||
|
||
$clientConfig = $transferObject->getClientConfig(); | ||
$client = $this->adyenHelper->initializeAdyenClientWithClientConfig($clientConfig); | ||
$service = $this->adyenHelper->initializeModificationsApi($client); | ||
|
||
$requestOptions['headers'] = $this->adyenHelper->buildRequestHeaders(); | ||
$request['applicationInfo'] = $this->adyenHelper->buildApplicationInfo($client); | ||
|
||
if (array_key_exists(self::MULTIPLE_AUTHORIZATIONS, $request)) { | ||
|
@@ -86,10 +84,10 @@ public function placeRequest(TransferInterface $transferObject): array | |
|
||
$idempotencyKey = $this->idempotencyHelper->generateIdempotencyKey( | ||
$request, | ||
$headers['idempotencyExtraData'] ?? null | ||
$requestOptions['headers']['idempotencyExtraData'] ?? null | ||
); | ||
$requestOptions['idempotencyKey'] = $idempotencyKey; | ||
|
||
$requestOptions['idempotencyKey'] = $idempotencyKey; | ||
$this->adyenHelper->logRequest($request, Client::API_CHECKOUT_VERSION, '/captures'); | ||
$paymentCaptureRequest = new PaymentCaptureRequest($request); | ||
$responseData = []; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please cover all the lines in the unit tests? It looks like some coverage is missing in this report. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,9 @@ | |
use Adyen\Payment\Helper\Idempotency; | ||
use Adyen\Service\Checkout\DonationsApi; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Payment\Gateway\Http\ClientInterface; | ||
use Magento\Payment\Gateway\Http\TransferInterface; | ||
|
||
class TransactionDonate implements ClientInterface | ||
class TransactionDonate extends BaseTransaction | ||
{ | ||
/** | ||
* @var Client | ||
|
@@ -32,7 +31,7 @@ class TransactionDonate implements ClientInterface | |
/** | ||
* @var Data | ||
*/ | ||
private Data $adyenHelper; | ||
protected Data $adyenHelper; | ||
|
||
/** | ||
* @var Idempotency | ||
|
@@ -49,9 +48,8 @@ public function __construct( | |
Data $adyenHelper, | ||
Idempotency $idempotencyHelper | ||
) { | ||
$this->adyenHelper = $adyenHelper; | ||
parent::__construct($adyenHelper); | ||
$this->idempotencyHelper = $idempotencyHelper; | ||
|
||
$this->client = $this->adyenHelper->initializeAdyenClient(); | ||
} | ||
|
||
|
@@ -63,17 +61,15 @@ public function __construct( | |
public function placeRequest(TransferInterface $transferObject): array | ||
{ | ||
$request = $transferObject->getBody(); | ||
$headers = $transferObject->getHeaders(); | ||
|
||
$requestOptions['headers'] = $this->requestHeaders($transferObject); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally |
||
$service = new DonationsApi($this->client); | ||
|
||
$idempotencyKey = $this->idempotencyHelper->generateIdempotencyKey( | ||
$request, | ||
$headers['idempotencyExtraData'] ?? null | ||
$requestOptions['headers']['idempotencyExtraData'] ?? null | ||
); | ||
|
||
$requestOptions['idempotencyKey'] = $idempotencyKey; | ||
$requestOptions['headers'] = $this->adyenHelper->buildRequestHeaders(); | ||
$request['applicationInfo'] = $this->adyenHelper->buildApplicationInfo($this->client); | ||
|
||
$this->adyenHelper->logRequest($request, Client::API_CHECKOUT_VERSION, 'donations'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,16 +26,15 @@ | |
use Adyen\Service\Checkout\PaymentsApi; | ||
use Magento\Framework\Exception\AlreadyExistsException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Payment\Gateway\Http\ClientInterface; | ||
use Magento\Payment\Gateway\Http\TransferInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
class TransactionPayment implements ClientInterface | ||
class TransactionPayment extends BaseTransaction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Abstract |
||
{ | ||
/** | ||
* @var Data | ||
*/ | ||
private Data $adyenHelper; | ||
protected Data $adyenHelper; | ||
|
||
/** | ||
* @var PaymentResponseFactory | ||
|
@@ -87,7 +86,7 @@ public function __construct( | |
StoreManagerInterface $storeManager, | ||
GiftcardPayment $giftcardPaymentHelper, | ||
) { | ||
$this->adyenHelper = $adyenHelper; | ||
parent::__construct($adyenHelper); | ||
$this->paymentResponseFactory = $paymentResponseFactory; | ||
$this->paymentResponseResourceModel = $paymentResponseResourceModel; | ||
$this->idempotencyHelper = $idempotencyHelper; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please cover all the lines in the unit tests? It looks like some coverage is missing in this report. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,15 +18,14 @@ | |
use Adyen\Payment\Helper\Idempotency; | ||
use Adyen\Service\Checkout\PaymentLinksApi; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Payment\Gateway\Http\ClientInterface; | ||
use Magento\Payment\Gateway\Http\TransferInterface; | ||
|
||
class TransactionPaymentLinks implements ClientInterface | ||
class TransactionPaymentLinks extends BaseTransaction | ||
{ | ||
/** | ||
* @var Data | ||
*/ | ||
private Data $adyenHelper; | ||
protected Data $adyenHelper; | ||
|
||
/** | ||
* @var Idempotency | ||
|
@@ -41,7 +40,7 @@ public function __construct( | |
Data $adyenHelper, | ||
Idempotency $idempotencyHelper | ||
) { | ||
$this->adyenHelper = $adyenHelper; | ||
parent::__construct($adyenHelper); | ||
$this->idempotencyHelper = $idempotencyHelper; | ||
} | ||
|
||
|
@@ -54,9 +53,9 @@ public function __construct( | |
public function placeRequest(TransferInterface $transferObject): array | ||
{ | ||
$request = $transferObject->getBody(); | ||
$headers = $transferObject->getHeaders(); | ||
$clientConfig = $transferObject->getClientConfig(); | ||
$requestOptions['headers'] = $this->requestHeaders($transferObject); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally |
||
|
||
$clientConfig = $transferObject->getClientConfig(); | ||
$client = $this->adyenHelper->initializeAdyenClientWithClientConfig($clientConfig); | ||
$service = new PaymentLinksApi($client); | ||
|
||
|
@@ -68,11 +67,11 @@ public function placeRequest(TransferInterface $transferObject): array | |
|
||
$idempotencyKey = $this->idempotencyHelper->generateIdempotencyKey( | ||
$request, | ||
$headers['idempotencyExtraData'] ?? null | ||
$requestOptions['headers']['idempotencyExtraData'] ?? null | ||
); | ||
|
||
$requestOptions['idempotencyKey'] = $idempotencyKey; | ||
$requestOptions['headers'] = $this->adyenHelper->buildRequestHeaders(); | ||
|
||
$request['applicationInfo'] = $this->adyenHelper->buildApplicationInfo($client); | ||
|
||
$this->adyenHelper->logRequest($request, Client::API_CHECKOUT_VERSION, '/paymentLinks'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please cover all the lines in the unit tests? It looks like some coverage is missing in this report. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,10 @@ | |
use Adyen\Payment\Helper\Config; | ||
use Adyen\Payment\Helper\Data; | ||
use Adyen\Payment\Logger\AdyenLogger; | ||
use Magento\Payment\Gateway\Http\ClientInterface; | ||
use Magento\Payment\Gateway\Http\TransferInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
class TransactionPosCloudSync implements ClientInterface | ||
class TransactionPosCloudSync extends BaseTransaction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Abstract |
||
{ | ||
protected int $storeId; | ||
protected mixed $timeout; | ||
|
@@ -36,7 +35,7 @@ public function __construct( | |
StoreManagerInterface $storeManager, | ||
Config $configHelper | ||
) { | ||
$this->adyenHelper = $adyenHelper; | ||
parent::__construct($adyenHelper); | ||
$this->adyenLogger = $adyenLogger; | ||
$this->configHelper = $configHelper; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,12 +22,12 @@ | |
/** | ||
* Class TransactionSale | ||
*/ | ||
class TransactionRefund implements TransactionRefundInterface | ||
class TransactionRefund extends BaseTransaction implements TransactionRefundInterface | ||
{ | ||
/** | ||
* @var Data | ||
*/ | ||
private Data $adyenHelper; | ||
protected Data $adyenHelper; | ||
|
||
/** | ||
* @var Idempotency | ||
|
@@ -42,7 +42,7 @@ public function __construct( | |
Data $adyenHelper, | ||
Idempotency $idempotencyHelper | ||
) { | ||
$this->adyenHelper = $adyenHelper; | ||
parent::__construct($adyenHelper); | ||
$this->idempotencyHelper = $idempotencyHelper; | ||
} | ||
|
||
|
@@ -55,9 +55,9 @@ public function __construct( | |
public function placeRequest(TransferInterface $transferObject): array | ||
{ | ||
$requests = $transferObject->getBody(); | ||
$headers = $transferObject->getHeaders(); | ||
$clientConfig = $transferObject->getClientConfig(); | ||
$requestOptions['headers'] = $this->requestHeaders($transferObject); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally |
||
|
||
$clientConfig = $transferObject->getClientConfig(); | ||
$client = $this->adyenHelper->initializeAdyenClientWithClientConfig($clientConfig); | ||
$service = $this->adyenHelper->initializeModificationsApi($client); | ||
$responses = []; | ||
|
@@ -66,10 +66,10 @@ public function placeRequest(TransferInterface $transferObject): array | |
$responseData = []; | ||
$idempotencyKey = $this->idempotencyHelper->generateIdempotencyKey( | ||
$request, | ||
$headers['idempotencyExtraData'] ?? null | ||
$requestOptions['headers']['idempotencyExtraData'] ?? null | ||
); | ||
|
||
$requestOptions['idempotencyKey'] = $idempotencyKey; | ||
$requestOptions['headers'] = $this->adyenHelper->buildRequestHeaders(); | ||
$this->adyenHelper->logRequest($request, Client::API_CHECKOUT_VERSION, '/refunds'); | ||
$request['applicationInfo'] = $this->adyenHelper->buildApplicationInfo($client); | ||
$paymentRefundRequest = new PaymentRefundRequest($request); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If any of the request builders add header
H
, this method will not be called. Thus, this will result in not adding desired headers. I would recommend adding header data builder to command chain of the transaction clients indi.xml
. Let's have a catch-up if this is not clear.