Skip to content

Commit

Permalink
SPRO-54: Display Google Pay & Apple Pay methods via Adyen
Browse files Browse the repository at this point in the history
Introduce Google and Apple payment methods
  • Loading branch information
Dmitry Pryshchepa committed Dec 14, 2021
1 parent ddacc9c commit 72b98e0
Show file tree
Hide file tree
Showing 15 changed files with 369 additions and 17 deletions.
59 changes: 59 additions & 0 deletions Model/Ui/Adminhtml/TokenUiComponentProviderAdyenHpp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Swarming\SubscribeProAdyen\Model\Ui\Adminhtml;

use Adyen\Payment\Helper\Data;
use Magento\Framework\View\Element\Template;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Model\Ui\TokenUiComponentInterfaceFactory;
use Magento\Vault\Model\Ui\TokenUiComponentProviderInterface;

class TokenUiComponentProviderAdyenHpp implements TokenUiComponentProviderInterface
{
/**
* @var TokenUiComponentInterfaceFactory
*/
private $componentFactory;

/**
* @var Data
*/
private $adyenHelper;

/**
* TokenUiComponentProvider constructor.
*
* @param TokenUiComponentInterfaceFactory $componentFactory
* @param Data $adyenHelper
*/
public function __construct(
TokenUiComponentInterfaceFactory $componentFactory,
Data $adyenHelper
) {
$this->componentFactory = $componentFactory;
$this->adyenHelper = $adyenHelper;
}

/**
* @inheritdoc
*/
public function getComponentForToken(PaymentTokenInterface $paymentToken)
{
$details = json_decode($paymentToken->getTokenDetails() ?: '{}', true);
$details['icon'] = $this->adyenHelper->getVariantIcon($details['type']);
$component = $this->componentFactory->create(
[
'config' => [
'code' => 'adyen_hpp_vault',
TokenUiComponentProviderInterface::COMPONENT_DETAILS => $details,
TokenUiComponentProviderInterface::COMPONENT_PUBLIC_HASH => $paymentToken->getPublicHash(),
'template' => 'Adyen_Payment::form/vault.phtml'
],
'name' => Template::class
]
);
return $component;
}
}
59 changes: 59 additions & 0 deletions Model/Ui/Adminhtml/TokenUiComponentProviderApplePay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Swarming\SubscribeProAdyen\Model\Ui\Adminhtml;

use Adyen\Payment\Helper\Data;
use Magento\Framework\View\Element\Template;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Model\Ui\TokenUiComponentInterfaceFactory;
use Magento\Vault\Model\Ui\TokenUiComponentProviderInterface;

class TokenUiComponentProviderApplePay implements TokenUiComponentProviderInterface
{
/**
* @var TokenUiComponentInterfaceFactory
*/
private $componentFactory;

/**
* @var Data
*/
private $adyenHelper;

/**
* TokenUiComponentProvider constructor.
*
* @param TokenUiComponentInterfaceFactory $componentFactory
* @param Data $adyenHelper
*/
public function __construct(
TokenUiComponentInterfaceFactory $componentFactory,
Data $adyenHelper
) {
$this->componentFactory = $componentFactory;
$this->adyenHelper = $adyenHelper;
}

/**
* @inheritdoc
*/
public function getComponentForToken(PaymentTokenInterface $paymentToken)
{
$details = json_decode($paymentToken->getTokenDetails() ?: '{}', true);
$details['icon'] = $this->adyenHelper->getVariantIcon($details['type']);
$component = $this->componentFactory->create(
[
'config' => [
'code' => 'adyen_apple_pay_vault',
TokenUiComponentProviderInterface::COMPONENT_DETAILS => $details,
TokenUiComponentProviderInterface::COMPONENT_PUBLIC_HASH => $paymentToken->getPublicHash(),
'template' => 'Adyen_Payment::form/vault.phtml'
],
'name' => Template::class
]
);
return $component;
}
}
60 changes: 60 additions & 0 deletions Model/Ui/Adminhtml/TokenUiComponentProviderGooglePay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Swarming\SubscribeProAdyen\Model\Ui\Adminhtml;

use Adyen\Payment\Model\Ui\AdyenGooglePayConfigProvider;
use Adyen\Payment\Helper\Data;
use Magento\Framework\View\Element\Template;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Model\Ui\TokenUiComponentInterfaceFactory;
use Magento\Vault\Model\Ui\TokenUiComponentProviderInterface;

class TokenUiComponentProviderGooglePay implements TokenUiComponentProviderInterface
{
/**
* @var TokenUiComponentInterfaceFactory
*/
private $componentFactory;

/**
* @var Data
*/
private $adyenHelper;

/**
* TokenUiComponentProvider constructor.
*
* @param TokenUiComponentInterfaceFactory $componentFactory
* @param Data $adyenHelper
*/
public function __construct(
TokenUiComponentInterfaceFactory $componentFactory,
Data $adyenHelper
) {
$this->componentFactory = $componentFactory;
$this->adyenHelper = $adyenHelper;
}

/**
* @inheritdoc
*/
public function getComponentForToken(PaymentTokenInterface $paymentToken)
{
$details = json_decode($paymentToken->getTokenDetails() ?: '{}', true);
$details['icon'] = $this->adyenHelper->getVariantIcon($details['type']);
$component = $this->componentFactory->create(
[
'config' => [
'code' => AdyenGooglePayConfigProvider::GOOGLE_PAY_VAULT_CODE,
TokenUiComponentProviderInterface::COMPONENT_DETAILS => $details,
TokenUiComponentProviderInterface::COMPONENT_PUBLIC_HASH => $paymentToken->getPublicHash(),
'template' => 'Adyen_Payment::form/vault.phtml'
],
'name' => Template::class
]
);
return $component;
}
}
12 changes: 2 additions & 10 deletions Observer/AdyenCcDataAssignObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,20 @@ class AdyenCcDataAssignObserver extends AbstractDataAssignObserver
*/
private $generalConfig;

/**
* @var \Magento\Checkout\Model\Session
*/
private $checkoutSession;

/**
* @var \Swarming\SubscribePro\Helper\Quote
*/
private $quoteHelper;

/**
* @param \Swarming\SubscribePro\Model\Config\General $generalConfig
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Swarming\SubscribePro\Helper\Quote $quoteHelper
*/
public function __construct(
\Swarming\SubscribePro\Model\Config\General $generalConfig,
\Magento\Checkout\Model\Session $checkoutSession,
\Swarming\SubscribePro\Helper\Quote $quoteHelper
) {
$this->generalConfig = $generalConfig;
$this->checkoutSession = $checkoutSession;
$this->quoteHelper = $quoteHelper;
}

Expand All @@ -49,15 +41,15 @@ public function __construct(
*/
public function execute(Observer $observer): void
{
$quote = $this->checkoutSession->getQuote();
$paymentInfo = $this->readPaymentModelArgument($observer);
$quote = $paymentInfo->getQuote();

$websiteCode = $quote->getStore()->getWebsite()->getCode();
if (!$this->generalConfig->isEnabled($websiteCode) || !$this->quoteHelper->hasSubscription($quote)) {
return;
}

$data = $this->readDataArgument($observer);
$paymentInfo = $this->readPaymentModelArgument($observer);

$additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
if (!is_array($additionalData) || !empty($additionalData[PaymentTokenInterface::PUBLIC_HASH])) {
Expand Down
27 changes: 27 additions & 0 deletions Observer/Payment/AdyenHppTokenAssigner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Swarming\SubscribeProAdyen\Observer\Payment;

use Magento\Vault\Api\Data\PaymentTokenInterface;
use Adyen\Payment\Model\Ui\AdyenHppConfigProvider;

class AdyenHppTokenAssigner extends TokenAssigner
{
/**
* @param string $paymentMethodToken
* @param int $customerId
* @return \Magento\Vault\Api\Data\PaymentTokenInterface|null
*/
protected function getPaymentToken(
string $paymentMethodToken,
int $customerId
): ?PaymentTokenInterface {
return $this->paymentTokenManagement->getByGatewayToken(
$paymentMethodToken,
AdyenHppConfigProvider::CODE,
$customerId
);
}
}
26 changes: 26 additions & 0 deletions Observer/Payment/ApplePayTokenAssigner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Swarming\SubscribeProAdyen\Observer\Payment;

use Magento\Vault\Api\Data\PaymentTokenInterface;

class ApplePayTokenAssigner extends TokenAssigner
{
/**
* @param string $paymentMethodToken
* @param int $customerId
* @return \Magento\Vault\Api\Data\PaymentTokenInterface|null
*/
protected function getPaymentToken(
string $paymentMethodToken,
int $customerId
): ?PaymentTokenInterface {
return $this->paymentTokenManagement->getByGatewayToken(
$paymentMethodToken,
'adyen_apple_pay',
$customerId
);
}
}
26 changes: 26 additions & 0 deletions Observer/Payment/GooglePayTokenAssigner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Swarming\SubscribeProAdyen\Observer\Payment;

use Magento\Vault\Api\Data\PaymentTokenInterface;

class GooglePayTokenAssigner extends TokenAssigner
{
/**
* @param string $paymentMethodToken
* @param int $customerId
* @return \Magento\Vault\Api\Data\PaymentTokenInterface|null
*/
protected function getPaymentToken(
string $paymentMethodToken,
int $customerId
): ?PaymentTokenInterface {
return $this->paymentTokenManagement->getByGatewayToken(
$paymentMethodToken,
'adyen_google_pay',
$customerId
);
}
}
22 changes: 16 additions & 6 deletions Observer/Payment/TokenAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TokenAssigner extends \Magento\Payment\Observer\AbstractDataAssignObserver
/**
* @var \Magento\Vault\Api\PaymentTokenManagementInterface
*/
private $paymentTokenManagement;
protected $paymentTokenManagement;

/**
* @param \Magento\Vault\Api\PaymentTokenManagementInterface $paymentTokenManagement
Expand Down Expand Up @@ -54,11 +54,7 @@ public function execute(Observer $observer)
return;
}

$paymentToken = $this->paymentTokenManagement->getByGatewayToken(
$paymentMethodToken,
AdyenCcConfigProvider::CODE,
$customerId
);
$paymentToken = $this->getPaymentToken($paymentMethodToken, (int)$customerId);
if ($paymentToken === null) {
return;
}
Expand All @@ -80,4 +76,18 @@ public function execute(Observer $observer)
);
}
}

/**
* @param string $paymentMethodToken
* @param int $customerId
* @return \Magento\Vault\Api\Data\PaymentTokenInterface|null
*/
protected function getPaymentToken(string $paymentMethodToken, int $customerId): ?PaymentTokenInterface
{
return $this->paymentTokenManagement->getByGatewayToken(
$paymentMethodToken,
AdyenCcConfigProvider::CODE,
$customerId
);
}
}
12 changes: 12 additions & 0 deletions etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Vault\Model\Ui\Adminhtml\TokensConfigProvider">
<arguments>
<argument name="tokenUiComponentProviders" xsi:type="array">
<item name="adyen_apple_pay" xsi:type="object">Swarming\SubscribeProAdyen\Model\Ui\Adminhtml\TokenUiComponentProviderApplePay</item>
<item name="adyen_google_pay" xsi:type="object">Swarming\SubscribeProAdyen\Model\Ui\Adminhtml\TokenUiComponentProviderGooglePay</item>
<item name="adyen_hpp" xsi:type="object">Swarming\SubscribeProAdyen\Model\Ui\Adminhtml\TokenUiComponentProviderAdyenHpp</item>
</argument>
</arguments>
</type>
</config>
14 changes: 14 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@
<swarming_subscribepro>
<third_party_payment>
<adyen_cc>adyen_cc_vault</adyen_cc>
<adyen_hpp>adyen_hpp_vault</adyen_hpp>
<adyen_apple_pay>adyen_apple_pay_vault</adyen_apple_pay>
<adyen_google_pay>adyen_google_pay_vault</adyen_google_pay>
</third_party_payment>
</swarming_subscribepro>
<payment>
<adyen_google_pay>
<can_use_internal>1</can_use_internal>
</adyen_google_pay>
<adyen_apple_pay>
<can_use_internal>1</can_use_internal>
</adyen_apple_pay>
<adyen_hpp>
<can_use_internal>1</can_use_internal>
</adyen_hpp>
</payment>
</default>
</config>
Loading

0 comments on commit 72b98e0

Please sign in to comment.