Skip to content

Commit

Permalink
Release 0.8.1
Browse files Browse the repository at this point in the history
- Product type restriction bugfix
- Pending payments bugfix
- Load assets from rvvup backend
- Rvvup messaging bugfix

ff588f4fb68e2547840a463b6a3d5cdf3a5a4dbf
  • Loading branch information
jay-rvvup committed Dec 15, 2022
1 parent 78a05f3 commit 0ffed01
Show file tree
Hide file tree
Showing 17 changed files with 231 additions and 53 deletions.
14 changes: 14 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,18 @@ private function getJwt(string $scopeType = ScopeInterface::SCOPE_STORE, string

return $this->jwt;
}

/**
* @param string $scopeType
* @param string|null $scopeCode
* @return bool
*/
public function getValidProductTypes(
string $scopeType = ScopeInterface::SCOPE_STORE,
string $scopeCode = null
): array {
$path = self::RVVUP_CONFIG . self::PRODUCT_RESTRICTIONS . self::XML_PATH_PRODUCT_TYPES_ENABLED;
$types = $this->scopeConfig->getValue($path, $scopeType, $scopeCode);
return explode(',', $types);
}
}
13 changes: 13 additions & 0 deletions Model/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
interface ConfigInterface
{
public const RVVUP_CONFIG = 'payment/rvvup/';
public const PRODUCT_RESTRICTIONS = 'product_restrictions/';
public const XML_PATH_ACTIVE = 'active';
public const XML_PATH_DEBUG = 'debug';
public const XML_PATH_JWT = 'jwt';
public const XML_PATH_PRODUCT_TYPES_ENABLED = 'enabled_product_types';

/**
* Validate whether Rvvup module & payment methods are active.
Expand Down Expand Up @@ -75,4 +77,15 @@ public function getAuthToken(string $scopeType = ScopeInterface::SCOPE_STORE, st
* @return bool
*/
public function isDebugEnabled(string $scopeType = ScopeInterface::SCOPE_STORE, string $scopeCode = null): bool;

/**
* Get valid product types
* @param string $scopeType
* @param string|null $scopeCode
* @return array
*/
public function getValidProductTypes(
string $scopeType = ScopeInterface::SCOPE_STORE,
string $scopeCode = null
): array;
}
3 changes: 2 additions & 1 deletion Model/PaymentMethodsAssetsGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Exception;
use Psr\Log\LoggerInterface;
use Rvvup\Payments\Api\PaymentMethodsAssetsGetInterface;
use Rvvup\Payments\Gateway\Method;

class PaymentMethodsAssetsGet implements PaymentMethodsAssetsGetInterface
{
Expand Down Expand Up @@ -60,7 +61,7 @@ public function execute(string $value, string $currency, array $methodCodes = []
continue;
}

$assets['rvvup_' . $methodName] = $method['assets'] ?? [];
$assets[Method::PAYMENT_TITLE_PREFIX . $methodName] = $method['settings']['assets'] ?? [];
}
} catch (Exception $ex) {
$this->logger->error(
Expand Down
13 changes: 11 additions & 2 deletions Plugin/LoadMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,25 @@ public function __construct(
*/
public function afterGetPaymentMethods(Data $subject, array $result): array
{
$quote = $this->checkoutSession->getQuote();
if (isset($result['rvvup'])) {
if (!$this->config->isActive()) {
return $result;
}
$productTypes = $this->config->getValidProductTypes();

foreach ($quote->getItems() as $item) {
if (!in_array($item->getProductType(), $productTypes)) {
return $result;
}
}

$this->template = $result['rvvup'];
unset($result['rvvup']);
}
if (!$this->methods) {
$total = $this->checkoutSession->getQuote()->getGrandTotal();
$currency = $this->checkoutSession->getQuote()->getQuoteCurrencyCode();
$total = $quote->getGrandTotal();
$currency = $quote->getQuoteCurrencyCode();
$this->methods = $this->sdkProxy->getMethods((string) $total, $currency ?? '');
}
return array_merge(
Expand Down
114 changes: 114 additions & 0 deletions Plugin/PreventOrderClean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace Rvvup\Payments\Plugin;

use Magento\Sales\Model\CronJob\CleanExpiredOrders;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Rvvup\Payments\Gateway\Method;
use Psr\Log\LoggerInterface;
use Magento\Store\Model\StoresConfig;

class PreventOrderClean
{
private const METHOD = 'method';
private const DELETE_PENDING_AFTER = 'sales/orders/delete_pending_after';

/**
* @var CollectionFactory
*/
private CollectionFactory $collectionFactory;

/**
* @var LoggerInterface
*/
private LoggerInterface $logger;

/**
* @var StoresConfig
*/
private StoresConfig $storesConfig;

/**
* @param CollectionFactory $collectionFactory
* @param StoresConfig $storesConfig
* @param LoggerInterface $logger
*/
public function __construct(
CollectionFactory $collectionFactory,
StoresConfig $storesConfig,
LoggerInterface $logger
) {
$this->collectionFactory = $collectionFactory;
$this->storesConfig = $storesConfig;
$this->logger = $logger;
}

/**
* @param CleanExpiredOrders $subject
* @return array
*/
public function beforeExecute(CleanExpiredOrders $subject): array
{
$ids = [];
$lifetimes = $this->storesConfig->getStoresConfigByPath(self::DELETE_PENDING_AFTER);
foreach ($lifetimes as $storeId => $lifetime) {
$collection = $this->collectionFactory->create();
$collection->addFieldToFilter('status', Order::STATE_PENDING_PAYMENT);
$collection->addFieldToFilter('store_id', $storeId);

$time = $lifetime * 60;
$expression = new \Zend_Db_Expr(
'TIME_TO_SEC(TIMEDIFF(CURRENT_TIMESTAMP, `updated_at`)) >= ' . $time
);

$collection->getSelect()->where($expression);
$ids[] = $collection->getAllIds();
}

$ids = array_unique(array_merge(...$ids));

if (empty($ids)) {
return [];
}

$collection = $this->collectionFactory->create();
$collection->join(
["sop" => "sales_order_payment"],
'main_table.entity_id = sop.parent_id',
[self::METHOD]
);

$collection->addFieldToFilter(
self::METHOD,
['like' => Method::PAYMENT_TITLE_PREFIX . '%']
);

$collection->addFieldToFilter('entity_id', ['in' => $ids]);

$connection = $collection->getConnection();

try {
$connection->beginTransaction();

$condition = ["entity_id in (?)" => $collection->getAllIds()];
$value = ['updated_at' => new \Zend_Db_Expr('CURRENT_TIMESTAMP')];

$connection->update('sales_order', $value, $condition);

$connection->commit();
} catch (\Exception $exception) {
$this->logger->debug(
sprintf(
'Exception caught while preventing order cleaning, %s',
$exception->getMessage()
)
);
$connection->rollBack();
}

return [];
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"magento/framework": "^102.0 || ^103.0",
"guzzlehttp/guzzle": ">=6",
"magento/module-catalog": "^103.0 || ^104.0",
"rvvup/sdk": "0.7.0",
"magento/module-grouped-product": ">=100.1",
"rvvup/sdk": "0.9.1",
"ext-json": "*",
"php": "^7.3 || ^8.0"
},
Expand Down
4 changes: 4 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
<field id="checkout_message" type="text" sortOrder="20" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
<label>Checkout Restriction Message</label>
</field>
<field id="enabled_product_types" type="multiselect" sortOrder="30" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
<label>Enable product types</label>
<source_model>Magento\Catalog\Model\Product\Type</source_model>
</field>
</group>
<group id="clearpay_messaging" sortOrder="200" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
<label>Clearpay Messaging</label>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<product_restrictions>
<pdp_message>This item has restrictions so not all payment methods may be available</pdp_message>
<checkout_message>You have some restricted items in your cart, not all payment methods may be available</checkout_message>
<enabled_product_types>simple,virtual,downloadable,bundle,grouped,configurable</enabled_product_types>
</product_restrictions>
<clearpay_messaging>
<active>1</active>
Expand Down
15 changes: 15 additions & 0 deletions etc/crontab/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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="Rvvup\Payments\Plugin\PreventOrderClean">
<arguments>
<argument name="logger" xsi:type="object">RvvupLog</argument>
</arguments>
</type>

<type name="Magento\Sales\Model\CronJob\CleanExpiredOrders">
<plugin name="prevent_cleaning_rvvup_orders"
type="Rvvup\Payments\Plugin\PreventOrderClean"
sortOrder="1"/>
</type>
</config>
3 changes: 0 additions & 3 deletions view/frontend/layout/catalog_product_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
<arguments>
<argument name="rvvup_payments_assets_view_model"
xsi:type="object">Rvvup\Payments\ViewModel\Assets</argument>
<argument name="rvvup_payments_method_codes" xsi:type="array">
<item name="0" xsi:type="string">paypal</item>
</argument>
</arguments>
</block>
</referenceBlock>
Expand Down
11 changes: 11 additions & 0 deletions view/frontend/layout/checkout_cart_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="head.additional">
<block class="Magento\Framework\View\Element\Template"
name="rvvup_payments.checkout_index_index.head.additional.assets"
template="Rvvup_Payments::head/additional/assets.phtml"
ifconfig="payment/rvvup/active">
<arguments>
<argument name="rvvup_payments_assets_view_model"
xsi:type="object">Rvvup\Payments\ViewModel\Assets</argument>
</arguments>
</block>
</referenceBlock>
<referenceContainer name="cart.summary">
<block class="Magento\Checkout\Block\Cart"
name="rvvup_payments.checkout_cart_index.methods"
Expand Down
12 changes: 0 additions & 12 deletions view/frontend/layout/default.xml

This file was deleted.

14 changes: 7 additions & 7 deletions view/frontend/templates/head/additional/assets.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@
* Load available assets on any template, currently used for PayPal only
*
* @var \Magento\Framework\View\Element\Template $block
* @var \Rvvup\Payments\ViewModel\Assets $assetsViewModel
* @var null|array|string[] $paymentMethodCodes
* @var \Magento\Framework\Escaper $escaper
*/
/** Magento 2.3.5 backward compatibility fix */
if (!isset($escaper)) {
$escaper = $block;
}

/** @var \Rvvup\Payments\ViewModel\Assets $assetsViewModel */
$assetsViewModel = $block->getData('rvvup_payments_assets_view_model');
$paymentMethodCodes = $block->getData('rvvup_payments_method_codes');

// Load specific payment methods if requested.
$paymentMethodsScriptAssets = $assetsViewModel !== null
? $assetsViewModel->getPaymentMethodsScriptAssets(is_array($paymentMethodCodes) ? $paymentMethodCodes: [])
: [];
$paymentMethodsScriptAssets = $assetsViewModel !== null ? $assetsViewModel->getPaymentMethodsScriptAssets([]) : [];
?>
<?php foreach ($paymentMethodsScriptAssets as $method => $scripts): ?>
<?php foreach ($scripts as $index => $script): ?>
Expand All @@ -29,7 +25,11 @@ $paymentMethodsScriptAssets = $assetsViewModel !== null
<?php // phpcs:enable ?>
<script id="<?= $escaper->escapeHtmlAttr($assetsViewModel->getScriptElementId($method, (string) $index)) ?>"
<?php foreach ($assetsViewModel->getScriptDataAttributes($script) as $dataAttribute => $value): ?>
<?= $escaper->escapeHtmlAttr($dataAttribute) . '="' . $escaper->escapeHtmlAttr($value) . '"' ?>
<?php if ($value === null): ?>
<?= $escaper->escapeHtmlAttr($dataAttribute) ?>
<?php else: ?>
<?= $escaper->escapeHtmlAttr($dataAttribute) . '="' . $escaper->escapeHtmlAttr($value) . '"' ?>
<?php endif; ?>
<?php endforeach; ?>
src="<?= $escaper->escapeUrl($assetsViewModel->getScriptElementSrc($script)) ?>"></script>
<?php endforeach; ?>
Expand Down
2 changes: 0 additions & 2 deletions view/frontend/templates/head/additional/clearpay-script.phtml

This file was deleted.

28 changes: 14 additions & 14 deletions view/frontend/web/js/bundle/price-bundle-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ define([
dataPriceTypeContainer = priceBox.find('[data-price-type]').first();

/* Get the price type loaded for the element on the bundle product page */
priceType = dataPriceTypeContainer.length === 1 && dataPriceTypeContainer.data('price-type') !== null ?
dataPriceTypeContainer.data('price-type')
priceType = dataPriceTypeContainer.length === 1 && dataPriceTypeContainer.data('price-type') !== null
? dataPriceTypeContainer.data('price-type')
: 'finalPrice';

priceBox.on('priceUpdated', this._updateClearpayPriceData.bind(this));
Expand All @@ -31,25 +31,25 @@ define([
rvvupMaxPrice = $widget.options.rvvupMax;

/* If price is not set, no action */
if (
!data ||
!data[priceType] ||
!data[priceType].amount
) {
if (!data || !data[priceType] || !data[priceType].amount) {
return this;
}

if (
data[priceType].amount <= rvvupMinPrice ||
data[priceType].amount >= rvvupMaxPrice
) {
$('.clearpay').hide();
let clearpayElement = $('.clearpay');

if (data[priceType].amount <= rvvupMinPrice || data[priceType].amount >= rvvupMaxPrice) {
clearpayElement.hide();

return this;
}

document.getElementById('clearpay-summary').dataset.amount = data[priceType].amount;
$('.clearpay').show();
let clearpaySummaryElement = document.getElementById('clearpay-summary');

if (clearpaySummaryElement !== null && clearpaySummaryElement.length !== 0) {
clearpaySummaryElement.dataset.amount = data[priceType].amount;
}

clearpayElement.show();

return this;
}
Expand Down
Loading

0 comments on commit 0ffed01

Please sign in to comment.