TokenFormatter.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Model\InstantPurchase\CreditCard;
  7. use Magento\InstantPurchase\PaymentMethodIntegration\PaymentTokenFormatterInterface;
  8. use Magento\Vault\Api\Data\PaymentTokenInterface;
  9. /**
  10. * Braintree stored credit card formatter.
  11. */
  12. class TokenFormatter implements PaymentTokenFormatterInterface
  13. {
  14. /**
  15. * Most used credit card types
  16. * @var array
  17. */
  18. public static $baseCardTypes = [
  19. 'AE' => 'American Express',
  20. 'VI' => 'Visa',
  21. 'MC' => 'MasterCard',
  22. 'DI' => 'Discover',
  23. 'JBC' => 'JBC',
  24. 'CUP' => 'China Union Pay',
  25. 'MI' => 'Maestro',
  26. ];
  27. /**
  28. * @inheritdoc
  29. */
  30. public function formatPaymentToken(PaymentTokenInterface $paymentToken): string
  31. {
  32. $details = json_decode($paymentToken->getTokenDetails() ?: '{}', true);
  33. if (!isset($details['type'], $details['maskedCC'], $details['expirationDate'])) {
  34. throw new \InvalidArgumentException('Invalid Braintree credit card token details.');
  35. }
  36. if (isset(self::$baseCardTypes[$details['type']])) {
  37. $ccType = self::$baseCardTypes[$details['type']];
  38. } else {
  39. $ccType = $details['type'];
  40. }
  41. $formatted = sprintf(
  42. '%s: %s, %s: %s (%s: %s)',
  43. __('Credit Card'),
  44. $ccType,
  45. __('ending'),
  46. $details['maskedCC'],
  47. __('expires'),
  48. $details['expirationDate']
  49. );
  50. return $formatted;
  51. }
  52. }