AvsEmsCodeMapper.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Model;
  7. use Magento\Braintree\Gateway\Response\PaymentDetailsHandler;
  8. use Magento\Braintree\Model\Ui\ConfigProvider;
  9. use Magento\Payment\Api\PaymentVerificationInterface;
  10. use Magento\Sales\Api\Data\OrderPaymentInterface;
  11. /**
  12. * Processes AVS codes mapping from Braintree transaction to
  13. * electronic merchant systems standard.
  14. *
  15. * @see https://developers.braintreepayments.com/reference/response/transaction
  16. * @see http://www.emsecommerce.net/avs_cvv2_response_codes.htm
  17. */
  18. class AvsEmsCodeMapper implements PaymentVerificationInterface
  19. {
  20. /**
  21. * Default code for mismatching mapping.
  22. *
  23. * @var string
  24. */
  25. private static $unavailableCode = '';
  26. /**
  27. * List of mapping AVS codes
  28. *
  29. * @var array
  30. */
  31. private static $avsMap = [
  32. 'MM' => 'Y',
  33. 'NM' => 'A',
  34. 'MN' => 'Z',
  35. 'NN' => 'N',
  36. 'UU' => 'U',
  37. 'II' => 'U',
  38. 'AA' => 'E'
  39. ];
  40. /**
  41. * Gets payment AVS verification code.
  42. *
  43. * @param OrderPaymentInterface $orderPayment
  44. * @return string
  45. * @throws \InvalidArgumentException If specified order payment has different payment method code.
  46. */
  47. public function getCode(OrderPaymentInterface $orderPayment)
  48. {
  49. if ($orderPayment->getMethod() !== ConfigProvider::CODE) {
  50. throw new \InvalidArgumentException(
  51. 'The "' . $orderPayment->getMethod() . '" does not supported by Braintree AVS mapper.'
  52. );
  53. }
  54. $additionalInfo = $orderPayment->getAdditionalInformation();
  55. if (empty($additionalInfo[PaymentDetailsHandler::AVS_POSTAL_RESPONSE_CODE]) ||
  56. empty($additionalInfo[PaymentDetailsHandler::AVS_STREET_ADDRESS_RESPONSE_CODE])
  57. ) {
  58. return self::$unavailableCode;
  59. }
  60. $streetCode = $additionalInfo[PaymentDetailsHandler::AVS_STREET_ADDRESS_RESPONSE_CODE];
  61. $zipCode = $additionalInfo[PaymentDetailsHandler::AVS_POSTAL_RESPONSE_CODE];
  62. $key = $zipCode . $streetCode;
  63. return isset(self::$avsMap[$key]) ? self::$avsMap[$key] : self::$unavailableCode;
  64. }
  65. }