CvvEmsCodeMapper.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 CVV 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 CvvEmsCodeMapper implements PaymentVerificationInterface
  19. {
  20. /**
  21. * Default code for mismatch mapping
  22. *
  23. * @var string
  24. */
  25. private static $notProvidedCode = 'P';
  26. /**
  27. * List of mapping CVV codes
  28. *
  29. * @var array
  30. */
  31. private static $cvvMap = [
  32. 'M' => 'M',
  33. 'N' => 'N',
  34. 'U' => 'P',
  35. 'I' => 'P',
  36. 'S' => 'S',
  37. 'A' => ''
  38. ];
  39. /**
  40. * Gets payment CVV verification code.
  41. *
  42. * @param OrderPaymentInterface $orderPayment
  43. * @return string
  44. * @throws \InvalidArgumentException If specified order payment has different payment method code.
  45. */
  46. public function getCode(OrderPaymentInterface $orderPayment)
  47. {
  48. if ($orderPayment->getMethod() !== ConfigProvider::CODE) {
  49. throw new \InvalidArgumentException(
  50. 'The "' . $orderPayment->getMethod() . '" does not supported by Braintree CVV mapper.'
  51. );
  52. }
  53. $additionalInfo = $orderPayment->getAdditionalInformation();
  54. if (empty($additionalInfo[PaymentDetailsHandler::CVV_RESPONSE_CODE])) {
  55. return self::$notProvidedCode;
  56. }
  57. $cvv = $additionalInfo[PaymentDetailsHandler::CVV_RESPONSE_CODE];
  58. return isset(self::$cvvMap[$cvv]) ? self::$cvvMap[$cvv] : self::$notProvidedCode;
  59. }
  60. }