Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GeneralResponseValidator.php
Go to the documentation of this file.
1 <?php
7 
8 use Braintree\Result\Error;
9 use Braintree\Result\Successful;
12 use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;
13 
15 {
19  protected $subjectReader;
20 
24  private $errorCodeProvider;
25 
33  public function __construct(
34  ResultInterfaceFactory $resultFactory,
36  ErrorCodeProvider $errorCodeProvider
37  ) {
38  parent::__construct($resultFactory);
39  $this->subjectReader = $subjectReader;
40  $this->errorCodeProvider = $errorCodeProvider;
41  }
42 
46  public function validate(array $validationSubject)
47  {
49  $response = $this->subjectReader->readResponseObject($validationSubject);
50 
51  $isValid = true;
52  $errorMessages = [];
53 
54  foreach ($this->getResponseValidators() as $validator) {
55  $validationResult = $validator($response);
56 
57  if (!$validationResult[0]) {
58  $isValid = $validationResult[0];
59  $errorMessages = array_merge($errorMessages, $validationResult[1]);
60  }
61  }
62  $errorCodes = $this->errorCodeProvider->getErrorCodes($response);
63 
64  return $this->createResult($isValid, $errorMessages, $errorCodes);
65  }
66 
70  protected function getResponseValidators()
71  {
72  return [
73  function ($response) {
74  return [
75  property_exists($response, 'success') && $response->success === true,
76  [$response->message ?? __('Braintree error response.')]
77  ];
78  }
79  ];
80  }
81 }
$response
Definition: 404.php:11
createResult($isValid, array $fails=[], array $errorCodes=[])
__()
Definition: __.php:13
__construct(ResultInterfaceFactory $resultFactory, SubjectReader $subjectReader, ErrorCodeProvider $errorCodeProvider)