From 9a3cda396a9df550c944dd811402a8442c7a29bd Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Sun, 10 Jun 2018 14:23:56 -0400 Subject: [PATCH 1/4] Add instant purchase formatter --- .../CreditCard/TokenFormatter.php | 66 +++++++++++++++++++ etc/config.xml | 1 + 2 files changed, 67 insertions(+) create mode 100644 Model/InstantPurchase/CreditCard/TokenFormatter.php diff --git a/Model/InstantPurchase/CreditCard/TokenFormatter.php b/Model/InstantPurchase/CreditCard/TokenFormatter.php new file mode 100644 index 0000000..a0858f7 --- /dev/null +++ b/Model/InstantPurchase/CreditCard/TokenFormatter.php @@ -0,0 +1,66 @@ + 'American Express', + 'VI' => 'Visa', + 'MC' => 'MasterCard', + 'DI' => 'Discover', + 'JBC' => 'JBC', + 'CUP' => 'China Union Pay', + 'MI' => 'Maestro', + ]; + + /** + * @inheritdoc + */ + public function formatPaymentToken(PaymentTokenInterface $paymentToken): string + { + $details = json_decode($paymentToken->getTokenDetails() ?: '{}', true); + if (!isset($details['type'], $details['maskedCC'], $details['expirationDate'])) { + throw new \InvalidArgumentException('Invalid credit card token details.'); + } + + if (isset(self::$baseCardTypes[$details['type']])) { + $ccType = self::$baseCardTypes[$details['type']]; + } else { + $ccType = $details['type']; + } + + $formatted = sprintf( + '%s: %s, %s: %s (%s: %s)', + __('Credit Card'), + $ccType, + __('ending'), + $details['maskedCC'], + __('expires'), + $details['expirationDate'] + ); + + return $formatted; + } +} diff --git a/etc/config.xml b/etc/config.xml index aba4958..01a891e 100755 --- a/etc/config.xml +++ b/etc/config.xml @@ -58,6 +58,7 @@ 1 1 + Pmclain\Stripe\Model\InstantPurchase\CreditCard\TokenFormatter From 094d0dd8f0306df1f3ed0a52b63ae9d70eeda651 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Sun, 10 Jun 2018 14:24:17 -0400 Subject: [PATCH 2/4] Bump current maagento version for travis --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 34dd93c..6750100 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,11 +27,11 @@ matrix: - TEST_SUITE=integration - php: 7.1 env: - - MAGENTO_VERSION=2.2.2 + - MAGENTO_VERSION=2.2.4 - TEST_SUITE=unit - php: 7.1 env: - - MAGENTO_VERSION=2.2.2 + - MAGENTO_VERSION=2.2.4 - TEST_SUITE=integration - php: 7.0 env: @@ -47,11 +47,11 @@ matrix: - TEST_SUITE=integration - php: 7.0 env: - - MAGENTO_VERSION=2.2.2 + - MAGENTO_VERSION=2.2.4 - TEST_SUITE=unit - php: 7.0 env: - - MAGENTO_VERSION=2.2.2 + - MAGENTO_VERSION=2.2.4 - TEST_SUITE=integration env: global: From 44cdaf0921d79461d28e284f1080deffecb81772 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Sun, 10 Jun 2018 14:27:40 -0400 Subject: [PATCH 3/4] Add Card Details to Info Block Adds dependency on JSON Serializer which requires Magento 2.2.x or greater. Displays the card type and last 4 digits anywhere the payment method is displayed: email, customer order detail, admin order detail, etc Fixes #51 --- Block/Info.php | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 +- composer.json | 6 +-- 3 files changed, 122 insertions(+), 4 deletions(-) diff --git a/Block/Info.php b/Block/Info.php index 73109d7..eb0b19c 100755 --- a/Block/Info.php +++ b/Block/Info.php @@ -17,10 +17,46 @@ namespace Pmclain\Stripe\Block; use Magento\Framework\Phrase; +use Magento\Framework\View\Element\Template\Context; use Magento\Payment\Block\ConfigurableInfo; +use Magento\Payment\Gateway\ConfigInterface; +use Magento\Payment\Model\InfoInterface; +use Magento\Vault\Api\PaymentTokenManagementInterface; +use Magento\Framework\Serialize\SerializerInterface; +use Pmclain\Stripe\Model\InstantPurchase\CreditCard\TokenFormatter; class Info extends ConfigurableInfo { + /** + * @var PaymentTokenManagementInterface + */ + private $paymentTokenManagement; + + /** + * @var SerializerInterface + */ + private $serializer; + + /** + * Info constructor. + * @param Context $context + * @param ConfigInterface $config + * @param PaymentTokenManagementInterface $paymentTokenManagement + * @param SerializerInterface $serializer + * @param array $data + */ + public function __construct( + Context $context, + ConfigInterface $config, + PaymentTokenManagementInterface $paymentTokenManagement, + SerializerInterface $serializer, + array $data = [] + ) { + parent::__construct($context, $config, $data); + $this->paymentTokenManagement = $paymentTokenManagement; + $this->serializer = $serializer; + } + /** * Returns label * @@ -31,4 +67,86 @@ protected function getLabel($field) { return __($field); } + + /** + * @param null $transport + * @return \Magento\Framework\DataObject|null + * @throws \Magento\Framework\Exception\LocalizedException + */ + protected function _prepareSpecificInformation($transport = null) + { + $transport = parent::_prepareSpecificInformation($transport); + $payment = $this->getInfo(); + + $this->setDataToTransfer( + $transport, + 'Card Type', + $this->getCcType($payment) + ); + $this->setDataToTransfer( + $transport, + 'Card Last 4 Digits', + $this->getCcLast4($payment) + ); + + return $transport; + } + + /** + * @param InfoInterface $payment + * @return string + */ + private function getCcLast4(InfoInterface $payment) + { + if ($payment->getCcLast4()) { + return $payment->getCcLast4(); + } + + $token = $this->getPaymentToken($payment); + $details = $this->serializer->unserialize($token->getTokenDetails()); + + $last4 = ''; + if (!empty($details['maskedCC'])) { + $last4 = $details['maskedCC']; + } + + return $last4; + } + + /** + * @param InfoInterface $payment + * @return string + */ + private function getCcType(InfoInterface $payment) + { + if ($payment->getCcType()) { + return $payment->getCcType(); + } + + $token = $this->getPaymentToken($payment); + $details = $this->serializer->unserialize($token->getTokenDetails()); + + $type = ''; + if (!empty($details['type'])) { + $type = $details['type']; + } + + if (!empty(TokenFormatter::$baseCardTypes[$type])) { + $type = TokenFormatter::$baseCardTypes[$type]; + } + + return $type; + } + + /** + * @param InfoInterface $payment + * @return \Magento\Vault\Api\Data\PaymentTokenInterface|null + */ + private function getPaymentToken(InfoInterface $payment) + { + return $this->paymentTokenManagement->getByPublicHash( + $payment->getAdditionalInformation('public_hash'), + $payment->getAdditionalInformation('customer_id') + ); + } } diff --git a/README.md b/README.md index d8b6f00..16cda4d 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ In your Magento 2 root directory run ## Magento Version Requirements | Release | Magento Version | | ------- | --------------- | -| 1.x.x | 2.2.x | +| 2.x.x | 2.2.x | | 1.x.x   | 2.1.x           | | None | 2.0.x | diff --git a/composer.json b/composer.json index 1d025b9..8c6ba4f 100755 --- a/composer.json +++ b/composer.json @@ -4,9 +4,9 @@ "type": "magento2-module", "license": "OSL-3.0", "require": { - "php": "~7.0.0||~7.1.0", - "magento/framework": "~100.1.0||~101.0.0||~100.3.0-dev", - "stripe/stripe-php": "~6.3.0" + "php": "~7.0.0||~7.1.0||~7.2.0", + "magento/framework": "~101.0.0||~100.3.0-dev", + "stripe/stripe-php": "~6.7.0" }, "autoload": { "files": [ From 39dc14f068d1b360bbe8c1940112326bcde42830 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Sun, 10 Jun 2018 14:36:21 -0400 Subject: [PATCH 4/4] Remove 2.3-develop from travis builds There are changes in upstream magento that will need to be addressed for builds to pass. --- .travis.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6750100..e2a9355 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,10 +13,6 @@ addons: language: php matrix: include: - - php: 7.1 - env: - - MAGENTO_VERSION=2.3-develop - - TEST_SUITE=unit - php: 7.1 env: - MAGENTO_VERSION=2.2-develop @@ -33,10 +29,6 @@ matrix: env: - MAGENTO_VERSION=2.2.4 - TEST_SUITE=integration - - php: 7.0 - env: - - MAGENTO_VERSION=2.3-develop - - TEST_SUITE=unit - php: 7.0 env: - MAGENTO_VERSION=2.2-develop