Cc.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Block\Info;
  7. /**
  8. * Credit card generic payment info
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Cc extends \Magento\Payment\Block\Info
  14. {
  15. /**
  16. * Payment config model
  17. *
  18. * @var \Magento\Payment\Model\Config
  19. */
  20. protected $_paymentConfig;
  21. /**
  22. * @param \Magento\Framework\View\Element\Template\Context $context
  23. * @param \Magento\Payment\Model\Config $paymentConfig
  24. * @param array $data
  25. */
  26. public function __construct(
  27. \Magento\Framework\View\Element\Template\Context $context,
  28. \Magento\Payment\Model\Config $paymentConfig,
  29. array $data = []
  30. ) {
  31. parent::__construct($context, $data);
  32. $this->_paymentConfig = $paymentConfig;
  33. }
  34. /**
  35. * Retrieve credit card type name
  36. *
  37. * @return string
  38. */
  39. public function getCcTypeName()
  40. {
  41. $types = $this->_paymentConfig->getCcTypes();
  42. $ccType = $this->getInfo()->getCcType();
  43. if (isset($types[$ccType])) {
  44. return $types[$ccType];
  45. }
  46. return empty($ccType) ? __('N/A') : $ccType;
  47. }
  48. /**
  49. * Whether current payment method has credit card expiration info
  50. *
  51. * @return bool
  52. */
  53. public function hasCcExpDate()
  54. {
  55. return (int)$this->getInfo()->getCcExpMonth() || (int)$this->getInfo()->getCcExpYear();
  56. }
  57. /**
  58. * Retrieve CC expiration month
  59. *
  60. * @return string
  61. */
  62. public function getCcExpMonth()
  63. {
  64. $month = $this->getInfo()->getCcExpMonth();
  65. if ($month < 10) {
  66. $month = '0' . $month;
  67. }
  68. return $month;
  69. }
  70. /**
  71. * Retrieve CC expiration date
  72. *
  73. * @return \DateTime
  74. */
  75. public function getCcExpDate()
  76. {
  77. $date = new \DateTime('now', new \DateTimeZone($this->_localeDate->getConfigTimezone()));
  78. $date->setDate($this->getInfo()->getCcExpYear(), $this->getInfo()->getCcExpMonth() + 1, 0);
  79. return $date;
  80. }
  81. /**
  82. * Prepare credit card related payment info
  83. *
  84. * @param \Magento\Framework\DataObject|array $transport
  85. * @return \Magento\Framework\DataObject
  86. */
  87. protected function _prepareSpecificInformation($transport = null)
  88. {
  89. if (null !== $this->_paymentSpecificInformation) {
  90. return $this->_paymentSpecificInformation;
  91. }
  92. $transport = parent::_prepareSpecificInformation($transport);
  93. $data = [];
  94. if ($ccType = $this->getCcTypeName()) {
  95. $data[(string)__('Credit Card Type')] = $ccType;
  96. }
  97. if ($this->getInfo()->getCcLast4()) {
  98. $data[(string)__('Credit Card Number')] = sprintf('xxxx-%s', $this->getInfo()->getCcLast4());
  99. }
  100. if (!$this->getIsSecureMode()) {
  101. if ($ccSsIssue = $this->getInfo()->getCcSsIssue()) {
  102. $data[(string)__('Switch/Solo/Maestro Issue Number')] = $ccSsIssue;
  103. }
  104. $year = $this->getInfo()->getCcSsStartYear();
  105. $month = $this->getInfo()->getCcSsStartMonth();
  106. if ($year && $month) {
  107. $data[(string)__('Switch/Solo/Maestro Start Date')] = $this->_formatCardDate($year, $month);
  108. }
  109. }
  110. return $transport->setData(array_merge($data, $transport->getData()));
  111. }
  112. /**
  113. * Format year/month on the credit card
  114. *
  115. * @param string $year
  116. * @param string $month
  117. * @return string
  118. */
  119. protected function _formatCardDate($year, $month)
  120. {
  121. return sprintf('%s/%s', sprintf('%02d', $month), $year);
  122. }
  123. }