TokenFormatter.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\InstantPurchase\Payflow\Pro;
  7. use Magento\InstantPurchase\PaymentMethodIntegration\PaymentTokenFormatterInterface;
  8. use Magento\Vault\Api\Data\PaymentTokenInterface;
  9. /**
  10. * 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['cc_type'], $details['cc_last_4'], $details['cc_exp_month'], $details['cc_exp_year'])) {
  34. throw new \InvalidArgumentException('Invalid PayPal Payflow Pro credit card token details.');
  35. }
  36. if (isset(self::$baseCardTypes[$details['cc_type']])) {
  37. $ccType = self::$baseCardTypes[$details['cc_type']];
  38. } else {
  39. $ccType = $details['cc_type'];
  40. }
  41. $formatted = sprintf(
  42. '%s: %s, %s: %s (%s: %02d/%04d)',
  43. __('Credit Card'),
  44. $ccType,
  45. __('ending'),
  46. $details['cc_last_4'],
  47. __('expires'),
  48. $details['cc_exp_month'],
  49. $details['cc_exp_year']
  50. );
  51. return $formatted;
  52. }
  53. }