Skip to content

Develop #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 51 additions & 38 deletions Block/System/Config/GooglePayChannelId.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
namespace Unzer\PAPI\Block\System\Config;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Button;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Unzer\PAPI\Model\Config;
use UnzerSDK\Exceptions\UnzerApiException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Serialize\SerializerInterface;
use Unzer\PAPI\Controller\Adminhtml\Webhooks\AbstractAction;

/**
* Adminhtml Webhook Configuration Buttons Block
Expand All @@ -16,70 +20,79 @@
*/
class GooglePayChannelId extends Field
{
/**
* @var Config
*/
private Config $configHelper;
/** @var string */
protected $_template = 'Unzer_PAPI::system/config/googlepaychannelid.phtml';

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

/**
* @inheritDoc
*
* @throws UnzerApiException
* @throws LocalizedException
*/
protected function _getElementHtml(AbstractElement $element): string
{
if ($element->getValue() === '' || $element->getValue() === null) {
$element->setValue($this->fetchChannelId());
}
$button = $this->getGooglePayChannelIdButton();

return $element->getElementHtml();
$block = $this->_layout->createBlock(self::class);
$block->setTemplate('Unzer_PAPI::system/config/googlepaychannelid.phtml')
->setChild('button', $button);
return parent::_getElementHtml($element) . $block->toHtml();
}

/**
* Fetch Channel ID
* Get Google Pay Channel ID Button
*
* @return string
* @throws UnzerApiException
* @return Button
* @throws LocalizedException
*/
private function fetchChannelId(): string
public function getGooglePayChannelIdButton(): Button
{
$keyPair = $this->configHelper->getUnzerClient()->getResourceService()->fetchKeypair(true);

foreach ($keyPair->getPaymentTypes() as $paymentType) {
if (!property_exists($paymentType, 'type')) {
continue;
}
if ($paymentType->type === 'googlepay') {
if (!property_exists($paymentType, 'supports')) {
return '';
}
if (!is_array($paymentType->supports) || !array_key_exists(0, $paymentType->supports)) {
return '';
}
if (!property_exists($paymentType->supports[0], 'channel')) {
return '';
}
$button = $this->getLayout()->createBlock(Button::class);
$button->setData([
'id' => 'unzer_googlepay_channelid',
'label' => __('Fetch Gateway Merchant ID'),
]);
return $button;
}

return $paymentType->supports[0]->channel;
}
}
/**
* Get Channel ID Action
*
* @return string
* @throws NoSuchEntityException
*/
public function getChannelIdAction(): string
{
return $this->getUrl('unzer/googlepay/channelid', [
AbstractAction::URL_PARAM_STORE => $this->getStoreIdentifier()
]);
}

return '';
/**
* Get Store Identifier
*
* @return int
* @throws NoSuchEntityException
*/
protected function getStoreIdentifier(): int
{
/** @var int|string $storeIdentifier */
$storeIdentifier = $this->getRequest()->getParam(AbstractAction::URL_PARAM_STORE, 0);
return (int)$this->_storeManager->getStore($storeIdentifier)->getId();
}
}
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.1.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [3.2.0](https://github.com/unzerdev/magento2/compare/3.1.0..3.2.0)
### Added
* TWINT payment method
### Changed
* automatic fetching of Google Pay Gateway Merchant ID to manual fetching by adding a button for fetching of the ID
* Giropay to be deactivated in checkout and marked as deprecated
### Removed
* Discover and JCB from available Google Pay card list

## [3.1.0](https://github.com/unzerdev/magento2/compare/3.0.0..3.1.0)
### Added
* Google Pay
### Changed
* Terms and Conditions are now included in the input check for the "Place Order" button to get activated. Additionally to all required input fields, all terms need to be checked, too. See the [terms-checked.js file]( view/frontend/web/js/model/checkout/terms-checked.js)
* system.xml to include separate files for each payment method instead of keeping everything in one file
* refactored Apple Pay "supported networks" to allow "supported networks" for Google Pay, too
* refactored Apple Pay "supported networks" to allow "supported networks" for Google Pay, too
### Fixed
* Threat Metrix data was not correctly handled for some payment methods

Expand Down
92 changes: 92 additions & 0 deletions Controller/Adminhtml/Googlepay/ChannelId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);

namespace Unzer\Papi\Controller\Adminhtml\Googlepay;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Store\Model\StoreManagerInterface;
use UnzerSDK\Exceptions\UnzerApiException;
use Unzer\PAPI\Model\Config;

class ChannelId implements HttpGetActionInterface
{
/** @var Config */
private Config $configHelper;

/** @var JsonFactory */
private JsonFactory $jsonFactory;

/** @var RequestInterface */
private RequestInterface $request;

/**
* Index constructor.
*
* @param Config $configHelper
* @param JsonFactory $jsonFactory
* @param RequestInterface $request
*/
public function __construct(
Config $configHelper,
JsonFactory $jsonFactory,
RequestInterface $request
) {
$this->configHelper = $configHelper;
$this->jsonFactory = $jsonFactory;
$this->request = $request;
}

/**
* Controller execution.
*
* @return Json
* @throws UnzerApiException
*/
public function execute(): Json
{
$json = $this->jsonFactory->create();
$data = [
'channel_id' => $this->fetchChannelId(),
];
$json->setData($data);

return $json;
}

/**
* Fetch Channel ID
*
* @return string
* @throws UnzerApiException
*/
private function fetchChannelId(): string
{
$storeId = (string) $this->request->getParam('store', 0);

$keyPair = $this->configHelper->getUnzerClient($storeId)->getResourceService()->fetchKeypair(true);

foreach ($keyPair->getPaymentTypes() as $paymentType) {
if (!property_exists($paymentType, 'type')) {
continue;
}
if ($paymentType->type === 'googlepay') {
if (!property_exists($paymentType, 'supports')) {
return '';
}
if (!is_array($paymentType->supports) || !array_key_exists(0, $paymentType->supports)) {
return '';
}
if (!property_exists($paymentType->supports[0], 'channel')) {
return '';
}

return $paymentType->supports[0]->channel;
}
}

return '';
}
}
1 change: 1 addition & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Config extends \Magento\Payment\Gateway\Config\Config
public const METHOD_PREPAYMENT = 'unzer_prepayment';
public const METHOD_APPLEPAY = 'unzer_applepay';
public const METHOD_GOOGLEPAY = 'unzer_googlepay';
public const METHOD_TWINT = 'unzer_twint';

/**
* @var DebugHandler
Expand Down
1 change: 1 addition & 0 deletions Model/Config/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Provider implements ConfigProviderInterface
Config::METHOD_PREPAYMENT,
Config::METHOD_APPLEPAY,
Config::METHOD_GOOGLEPAY,
Config::METHOD_TWINT,
];

/**
Expand Down
2 changes: 2 additions & 0 deletions Model/Method/Giropay.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Giropay payment method
*
* @link https://docs.unzer.com/
* @deprecated
* @see https://docs.unzer.com/payment-methods/giropay/
*/
class Giropay extends Base
{
Expand Down
20 changes: 20 additions & 0 deletions Model/Method/Twint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace Unzer\PAPI\Model\Method;

/**
* Twint payment method
*
* @link https://docs.unzer.com/
*/
class Twint extends Base
{
/**
* @inheritDoc
*/
public function hasRedirect(): bool
{
return true;
}
}
2 changes: 0 additions & 2 deletions Model/Source/Googlepay/SupportedNetworks.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class SupportedNetworks implements OptionSourceInterface
* @var array
*/
protected static array $networks = [
"DISCOVER",
"JCB",
"MASTERCARD",
"VISA"
];
Expand Down
62 changes: 62 additions & 0 deletions Model/Validator/CurrencyRestrictionValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);

namespace Unzer\PAPI\Model\Validator;

use Exception;
use Magento\Payment\Gateway\Validator\ResultInterface;
use Unzer\PAPI\Model\Config;
use Magento\Payment\Gateway\Validator\AbstractValidator;
use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;

/**
* Validator for per payment method currency restrictions
*
* @link https://docs.unzer.com/
*/
class CurrencyRestrictionValidator extends AbstractValidator
{
/**
* @var Config
*/
private Config $config;

/**
* @param ResultInterfaceFactory $resultFactory
* @param Config $config
*/
public function __construct(
ResultInterfaceFactory $resultFactory,
Config $config
) {
parent::__construct($resultFactory);

$this->config = $config;
}

/**
* Validate
*
* @param array $validationSubject
* @return bool|ResultInterface
* @throws Exception
*/
public function validate(array $validationSubject)
{
$isValid = true;

/** @var string|null $currencyString */
$currencyString = $this->config->getValue('currency_restrictions', $validationSubject['storeId']);

if (!empty($currencyString)) {
/** @var string[] $currencies */
$currencies = preg_split('/\s*,\s*/', $currencyString);

if (!in_array($validationSubject['currency'], $currencies)) {
$isValid = false;
}
}

return $this->createResult($isValid);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "unzerdev/magento2",
"description": "This extension for Magento 2 provides a direct integration of the Unzer payment types to your Magento 2 shop via the Unzer Payment API (PAPI).",
"type": "magento2-module",
"version": "3.1.0",
"version": "3.2.0",
"license": "Apache-2.0",
"require": {
"php": "~7.4.0|~8.1.0|~8.2.0|~8.3.0",
Expand Down
1 change: 1 addition & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<include path="Unzer_PAPI::system/prepayment.xml"/>
<include path="Unzer_PAPI::system/applepay.xml"/>
<include path="Unzer_PAPI::system/googlepay.xml"/>
<include path="Unzer_PAPI::system/twint.xml"/>
</group>
</section>
</system>
Expand Down
2 changes: 1 addition & 1 deletion etc/adminhtml/system/googlepay.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<label>Gateway Merchant ID</label>
<config_path>payment/unzer_googlepay/unzer_channel_id</config_path>
<frontend_model>Unzer\PAPI\Block\System\Config\GooglePayChannelId</frontend_model>
<comment><![CDATA[The Gateway Merchant ID will be fetched automatically on page load. If you have changed the Public/Private Keys, please save again. If the field stays empty, please contact the Unzer Support.]]></comment>
<comment><![CDATA[Fetch the Gateway Merchant ID from Unzer API. If the field stays empty, please contact the Unzer Support.]]></comment>
</field>
<field id="merchant_id" translate="label" type="text" sortOrder="100" showInDefault="1"
showInWebsite="1" showInStore="1">
Expand Down
Loading
Loading