The Payment Provider Gateway documentation uses the Magento 2.3 (now Adobe Commerce and Magento Open Source) of the Braintree module as a reference application. The Braintree module was removed in version 2.4.0. The concepts described in this guide are still applicable to version 2.4.x, but the code samples are not supported.
Response Validator
Response Validator is a component of the Adobe Commerce payment provider gateway that performs gateway response verification. This may include low level data formatting, security verification, and even execution of some business logic required by the store configuration.
Response Validator returns a Result object, containing validation result as Boolean value and errors description as a list of Phrase.
Interfaces
Response Validator must implement Magento\Payment\Gateway\Validator\ValidatorInterface
Result class must implement Magento\Payment\Gateway\Validator\ResultInterface
A payment provider integration can have multiple response validators, that should be added to the provider's validator's pool using dependency injection.
Useful implementations
\Magento\Payment\Gateway\Validator\AbstractValidator: an abstract class with ability to create a Result object. Can be inherited from by particular response validator implementations.
\Magento\Payment\Gateway\Validator\ValidatorComposite: a chain of Validator objects, which are executed one by one and the result gets aggregated into one Result object. This chain can be configured to stop when certain validators fail.
\Magento\Payment\Gateway\Validator\Result: base class for Result object. You still have an ability to create a Result of your own, but the default one covers the most amount of cases.
Example
In the following example a response validator is implemented and added to the pool of the Braintree payment provider request validators.
Copied to your clipboardclass AcceptValidator extends AbstractValidator{/*** Performs domain-related validation for business object** @param array $validationSubject* @return ResultInterface*/public function validate(array $validationSubject){$response = SubjectReader::readResponse($validationSubject);$paymentDO = SubjectReader::readPayment($validationSubject);$isValid = true;$fails = [];$statements = [[$paymentDO->getOrder()->getCurrencyCode() === $response['authCurrency'],__('Currency doesn\'t match.')],[sprintf('%.2F',$paymentDO->getOrder()->getGrandTotalAmount()) === $response['authCost'],__('Amount doesn\'t match.')],[in_array($response['authMode'], ['A', 'E']),__('Not supported response.')]];foreach ($statements as $statementResult) {if (!$statementResult[0]) {$isValid = false;$fails[] = $statementResult[1];}}return $this->createResult($isValid, $fails);}}
Now, the newly added validator should be specified for a specific command. Below is an example of specifying a validator for an authorization command:
Copied to your clipboard...<virtualType name="BraintreeAuthorizeCommand" type="Magento\Payment\Gateway\Command\GatewayCommand"><arguments>...<argument name="validator" xsi:type="object">Magento\Braintree\Gateway\Validator\AcceptValidator</argument></arguments></virtualType>...
(This code sample was created for demonstration purposes only, it differs from the actual Braintree configuration).