AvsEmsCodeMapper.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\Payflow;
  7. use Magento\Payment\Api\PaymentVerificationInterface;
  8. use Magento\Paypal\Model\Config;
  9. use Magento\Paypal\Model\Info;
  10. use Magento\Sales\Api\Data\OrderPaymentInterface;
  11. /**
  12. * Processes AVS codes mapping from PayPal Payflow transaction to
  13. * electronic merchant systems standard.
  14. *
  15. * @see https://developer.paypal.com/docs/classic/payflow/integration-guide/#credit-card-transaction-responses
  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. 'YY' => 'Y',
  33. 'NY' => 'A',
  34. 'YN' => 'Z',
  35. 'NN' => 'N'
  36. ];
  37. /**
  38. * Gets payment AVS verification code.
  39. *
  40. * @param OrderPaymentInterface $orderPayment
  41. * @return string
  42. * @throws \InvalidArgumentException If specified order payment has different payment method code.
  43. */
  44. public function getCode(OrderPaymentInterface $orderPayment)
  45. {
  46. if ($orderPayment->getMethod() !== Config::METHOD_PAYFLOWPRO) {
  47. throw new \InvalidArgumentException(
  48. 'The "' . $orderPayment->getMethod() . '" does not supported by Payflow AVS mapper.'
  49. );
  50. }
  51. $additionalInfo = $orderPayment->getAdditionalInformation();
  52. if (empty($additionalInfo[Info::PAYPAL_AVSADDR]) ||
  53. empty($additionalInfo[Info::PAYPAL_AVSZIP])
  54. ) {
  55. return self::$unavailableCode;
  56. }
  57. $streetCode = $additionalInfo[Info::PAYPAL_AVSADDR];
  58. $zipCode = $additionalInfo[Info::PAYPAL_AVSZIP];
  59. $key = $zipCode . $streetCode;
  60. return isset(self::$avsMap[$key]) ? self::$avsMap[$key] : self::$unavailableCode;
  61. }
  62. }