Response Validator

Response Validator is a component of the Magento 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 validators’ pool using dependency injection.

Useful implementations

Example

In the following example the a response validator is implemented and added to the pool of the Braintree payment provider request validators.

class 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);
    }
}

Pool of validators

The following sample demonstrates defining a validator’s pool and adding validators to this pool for the Braintree payment provider in di.xml:

...
<virtualType name="BraintreeValidatorPool" type="Magento\Payment\Gateway\Validator\ValidatorPool">
    <arguments>
        <argument name="validators" xsi:type="array">
            <item name="country" xsi:type="object">Magento\Payment\Gateway\Validator\CountryValidator</item>
            <item name="accept" xsi:type="string">AcceptValidator</item>
        </argument>
    </arguments>
</virtualType>
...

(This code sample was created for demonstration only, the real Braintree configuration is different).