AmazonAuthorization.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Domain\Validator;
  17. use Amazon\Payment\Domain\AmazonAuthorizationStatus;
  18. use Amazon\Payment\Domain\Details\AmazonAuthorizationDetails;
  19. /**
  20. * Class AmazonAuthorization
  21. * validates Amazon Pay status during cron updates.
  22. */
  23. class AmazonAuthorization
  24. {
  25. /**
  26. * @param AmazonAuthorizationDetails $details
  27. * @return array
  28. */
  29. public function validate(AmazonAuthorizationDetails $details)
  30. {
  31. $status = $details->getStatus();
  32. switch ($status->getState()) {
  33. case AmazonAuthorizationStatus::STATE_CLOSED:
  34. switch ($status->getReasonCode()) {
  35. case AmazonAuthorizationStatus::REASON_MAX_CAPTURES_PROCESSED:
  36. return [
  37. 'result' => true,
  38. 'reason' => AmazonAuthorizationStatus::REASON_MAX_CAPTURES_PROCESSED
  39. ];
  40. }
  41. break;
  42. case AmazonAuthorizationStatus::STATE_OPEN:
  43. case AmazonAuthorizationStatus::STATE_PENDING:
  44. return ['result' => true, 'reason' => $status->getState()];
  45. case AmazonAuthorizationStatus::STATE_DECLINED:
  46. return ['result' => false, 'reason' => $this->getReasonCode($status)];
  47. }
  48. return ['result' => false, 'reason' => $status->getState()];
  49. }
  50. /**
  51. * Need to ensure three specific reason codes come through during processing.
  52. *
  53. * @param AmazonAuthorizationStatus $status
  54. * @return null|string
  55. */
  56. protected function getReasonCode(AmazonAuthorizationStatus $status)
  57. {
  58. switch ($status->getReasonCode()) {
  59. case AmazonAuthorizationStatus::REASON_TRANSACTION_TIMEOUT:
  60. return 'timeout';
  61. case AmazonAuthorizationStatus::REASON_AMAZON_REJECTED:
  62. case AmazonAuthorizationStatus::REASON_PROCESSING_FAILURE:
  63. return 'hard_decline';
  64. case AmazonAuthorizationStatus::REASON_INVALID_PAYMENT_METHOD:
  65. return 'soft_decline';
  66. }
  67. return $status->getReasonCode();
  68. }
  69. }