AuthorizationValidator.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Payment\Gateway\Validator;
  17. use Magento\Payment\Gateway\Validator\AbstractValidator;
  18. use Magento\Payment\Gateway\Validator\ResultInterface;
  19. use Amazon\Payment\Domain\AmazonConstraint;
  20. /**
  21. * Class AuthorizationValidator
  22. * Validates authorization calls during gateway payment
  23. */
  24. class AuthorizationValidator extends AbstractValidator
  25. {
  26. /**
  27. * Performs validation of result code
  28. *
  29. * @param array $validationSubject
  30. * @return ResultInterface
  31. */
  32. public function validate(array $validationSubject)
  33. {
  34. $messages = [];
  35. $response = $validationSubject['response'];
  36. if (isset($response['sandbox']) && $response['sandbox']) {
  37. $bits = explode(':', $response['sandbox']);
  38. $messages[] = $bits[count($bits) - 1];
  39. return $this->createResult(false, $messages);
  40. }
  41. if (isset($response['status']) && $response['status']) {
  42. return $this->createResult(
  43. true,
  44. ['status' => $response['status']]
  45. );
  46. }
  47. if (isset($response['response_code']) && $response['response_code']) {
  48. $messages[] = $response['response_code'];
  49. } elseif (isset($response['constraints']) && $response['constraints']) {
  50. $messages[] = $this->getConstraint($response['constraints'][0]);
  51. }
  52. return $this->createResult(false, $messages);
  53. }
  54. /**
  55. * @param AmazonConstraint $constraint
  56. * @return string
  57. */
  58. private function getConstraint(AmazonConstraint $constraint)
  59. {
  60. return $constraint->getId();
  61. }
  62. }