Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from pmclain/feature/save-payment-info
Browse files Browse the repository at this point in the history
Feature/save payment info
  • Loading branch information
pmclain authored Jun 16, 2017
2 parents 4094ba1 + a19d010 commit 8a4b791
Show file tree
Hide file tree
Showing 49 changed files with 2,659 additions and 78 deletions.
74 changes: 74 additions & 0 deletions Block/Customer/CardRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Pmclain_Stripe extension
* NOTICE OF LICENSE
*
* This source file is subject to the GPL v3 License
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://www.gnu.org/licenses/gpl.txt
*
* @category Pmclain
* @package Pmclain_Stripe
* @copyright Copyright (c) 2017
* @license https://www.gnu.org/licenses/gpl.txt GPL v3 License
*/
namespace Pmclain\Stripe\Block\Customer;

use Pmclain\Stripe\Model\Ui\ConfigProvider;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Block\AbstractCardRenderer;

class CardRenderer extends AbstractCardRenderer
{
/**
* Can render specified token
*
* @param PaymentTokenInterface $token
* @return boolean
*/
public function canRender(PaymentTokenInterface $token)
{
return $token->getPaymentMethodCode() === ConfigProvider::CODE;
}

/**
* @return string
*/
public function getNumberLast4Digits()
{
return $this->getTokenDetails()['maskedCC'];
}

/**
* @return string
*/
public function getExpDate()
{
return $this->getTokenDetails()['expirationDate'];
}

/**
* @return string
*/
public function getIconUrl()
{
return $this->getIconForType($this->getTokenDetails()['type'])['url'];
}

/**
* @return int
*/
public function getIconHeight()
{
return $this->getIconForType($this->getTokenDetails()['type'])['height'];
}

/**
* @return int
*/
public function getIconWidth()
{
return $this->getIconForType($this->getTokenDetails()['type'])['width'];
}
}
29 changes: 28 additions & 1 deletion Block/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,53 @@
namespace Pmclain\Stripe\Block;

use Pmclain\Stripe\Gateway\Config\Config as GatewayConfig;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Element\Template\Context;
use Magento\Payment\Block\Form\Cc;
use Magento\Payment\Model\Config;
use Magento\Payment\Helper\Data as Helper;
use Pmclain\Stripe\Model\Ui\ConfigProvider;

class Form extends Cc
{
/** @var GatewayConfig $gatewayConfig */
protected $gatewayConfig;

/** @var Helper $paymentDataHelper */
private $paymentDataHelper;

public function __construct(
Context $context,
Config $paymentConfig,
GatewayConfig $gatewayConfig,
Helper $helper,
array $data = []
) {
parent::__construct($context, $paymentConfig, $data);
$this->gatewayConfig = $gatewayConfig;
$this->paymentDataHelper = $helper;
}

public function useCcv() {
return $this->gatewayConfig->isCcvEnabled();
}

/**
* Check if vault enabled
* @return bool
*/
public function isVaultEnabled()
{
$storeId = $this->_storeManager->getStore()->getId();
$vaultPayment = $this->getVaultPayment();
return $vaultPayment->isActive($storeId);
}

/**
* Get configured vault payment for Braintree
* @return VaultPaymentInterface
*/
private function getVaultPayment()
{
return $this->paymentDataHelper->getMethodInstance(ConfigProvider::CC_VAULT_CODE);
}
}
67 changes: 67 additions & 0 deletions Block/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Pmclain_Stripe extension
* NOTICE OF LICENSE
*
* This source file is subject to the GPL v3 License
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://www.gnu.org/licenses/gpl.txt
*
* @category Pmclain
* @package Pmclain_Stripe
* @copyright Copyright (c) 2017
* @license https://www.gnu.org/licenses/gpl.txt GPL v3 License
*/

namespace Pmclain\Stripe\Block;

use Pmclain\Stripe\Model\Ui\ConfigProvider;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

/**
* Class Payment
*/
class Payment extends Template
{
/**
* @var ConfigProvider
*/
private $config;

/**
* Constructor
*
* @param Context $context
* @param ConfigProvider $config
* @param array $data
*/
public function __construct(
Context $context,
ConfigProvider $config,
array $data = []
) {
parent::__construct($context, $data);
$this->config = $config;
}

/**
* @return string
*/
public function getPaymentConfig()
{
$payment = $this->config->getConfig()['payment'];
$config = $payment[$this->getCode()];
$config['code'] = $this->getCode();
return json_encode($config, JSON_UNESCAPED_SLASHES);
}

/**
* @return string
*/
public function getCode()
{
return ConfigProvider::CODE;
}
}
27 changes: 27 additions & 0 deletions Gateway/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ class Config extends \Magento\Payment\Gateway\Config\Config
const KEY_ALLOW_SPECIFIC = 'allowspecific';
const KEY_SPECIFIC_COUNTRY = 'specificcountry';

/**
* @return array
*/
public function getAvailableCardTypes() {
$ccTypes = $this->getValue(self::KEY_CC_TYPES);

return !empty($ccTypes) ? explode(',', $ccTypes) : [];
}

/**
* @return array|mixed
*/
public function getCcTypesMapper() {
$result = json_decode(
$this->getValue(self::KEY_CC_TYPES_STRIPE_MAPPER),
Expand All @@ -45,36 +51,57 @@ public function getCcTypesMapper() {
return is_array($result) ? $result : [];
}

/**
* @return mixed
*/
public function getCurrency() {
return $this->getValue(self::KEY_CURRENCY);
}

/**
* @return bool
*/
public function isCcvEnabled() {
return (bool) $this->getValue(self::KEY_USE_CCV);
}

/**
* @return mixed
*/
public function getEnvironment() {
return $this->getValue(Config::KEY_ENVIRONMENT);
}

/**
* @return bool
*/
public function isActive() {
return (bool) $this->getValue(self::KEY_ACTIVE);
}

/**
* @return mixed
*/
public function getPublishableKey() {
if($this->isTestMode()) {
return $this->getValue(self::KEY_TEST_PUBLISHABLE_KEY);
}
return $this->getValue(self::KEY_LIVE_PUBLISHABLE_KEY);
}

/**
* @return mixed
*/
public function getSecretKey() {
if($this->isTestMode()) {
return $this->getValue(self::KEY_TEST_SECRET_KEY);
}
return $this->getValue(self::KEY_LIVE_SECRET_KEY);
}

/**
* @return bool
*/
public function isTestMode() {
return (bool) $this->getValue(self::KEY_ENVIRONMENT);
}
Expand Down
13 changes: 11 additions & 2 deletions Gateway/Helper/SubjectReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,28 @@
*/
namespace Pmclain\Stripe\Gateway\Helper;

use Magento\Quote\Model\Quote;
use Magento\Payment\Gateway\Helper;
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;

class SubjectReader
{
/**
* @param array $subject
* @return array
*/
public function readResponseObject(array $subject) {
$response = Helper\SubjectReader::readResponse($subject);

if(!is_object($response['object'])) {
throw new \InvalidArgumentException('Response object does not exist');
}

if($response['object'] instanceof \Stripe\Error\Card) {
return [
'error' => true,
'message' => __($response['object']->getMessage())
];
}

return $response['object']->__toArray();
}

Expand Down
Loading

0 comments on commit 8a4b791

Please sign in to comment.