Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
SiarheyUchukhlebau committed Aug 8, 2022
0 parents commit d99678d
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Gateway/Command/CaptureStrategyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/
declare(strict_types=1);

namespace MageWorx\OrderEditorUSAePay\Gateway\Command;

use Magento\Framework\Exception\NotFoundException;
use Magento\Payment\Gateway\Command\CommandPoolInterface;
use Magento\Payment\Gateway\CommandInterface;
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use MageWorx\OrderEditorUSAePay\Helper\Data as Helper;
use Magento\Payment\Gateway\Helper\SubjectReader;

class CaptureStrategyCommand extends \Rootways\USAePay\Gateway\Command\CaptureStrategyCommand
implements CommandInterface
{
/**
* @var Helper
*/
protected $helper;

/**
* @var CommandPoolInterface
*/
protected $_commandPool;

/**
* We use factory to support both old and new versions of the braintree payment.
* Old namespace was \Magento\Braintree, new - \PayPal\Braintree.
*
* @param Helper $helper
* @param CommandPoolInterface $commandPool
*/
public function __construct(
Helper $helper,
CommandPoolInterface $commandPool
) {
$this->helper = $helper;
$this->_commandPool = $commandPool;
parent::__construct($commandPool);
}

/**
* @param array $commandSubject
* @return \Magento\Payment\Gateway\Command\ResultInterface|void|null
* @throws NotFoundException
* @throws \Magento\Payment\Gateway\Command\CommandException
*/
public function execute(array $commandSubject)
{
/** @var PaymentDataObjectInterface $paymentDO */
$paymentDO = SubjectReader::readPayment($commandSubject);

/** @var OrderPaymentInterface $payment */
$paymentInfo = $paymentDO->getPayment();

$useVault = $paymentInfo->getAdditionalInformation('mw_use_vault');
$needReauthorization = $paymentInfo->getAdditionalInformation('mw_reauthorization_required');
if ($this->helper->isEnabled() && $needReauthorization && $useVault) {
$command = 'adjust_positive';
$this->_commandPool->get($command)->execute($commandSubject);
} else {
return parent::execute($commandSubject);
}
}
}
67 changes: 67 additions & 0 deletions Gateway/Request/AdjustPositiveTransactionDataBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* USAePay Payment Module.
*
* @category Payment Integration
* @package Rootways_USAePay
* @author Developer RootwaysInc <developer@rootways.com>
* @copyright 2021 Rootways Inc. (https://www.rootways.com)
* @license Rootways Custom License
* @link https://www.rootways.com/pub/media/extension_doc/license_agreement.pdf
*/

namespace MageWorx\OrderEditorUSAePay\Gateway\Request;

/**
* Class AdjustTransactionDataBuilder
*/
class AdjustPositiveTransactionDataBuilder extends \Rootways\USAePay\Gateway\Request\GeneralDataBuilder
{
/**
* @param array $buildSubject
* @return array
*/
public function build(array $buildSubject): array
{
$amount = $this->subjectReader->readAmount($buildSubject);

$paymentDO = $this->subjectReader->readPayment($buildSubject);
$payment = $paymentDO->getPayment();
/** @var \Magento\Sales\Model\Order $order */
$order = $payment->getOrder();

$taxAmt = $discountAmt = $shippingAmt = 0.00;
$isTaxable = 0;

if (!empty($order->getBaseTaxAmount())) {
$taxAmt = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced();
$isTaxable = 1;
}

if (!empty($order->getBaseShippingAmount())) {
$shippingAmt = $order->getBaseShippingAmount() - $order->getBaseShippingInvoiced();
}
if (!empty($order->getBaseDiscountAmount())) {
$discountAmt = abs($order->getBaseDiscountAmount()) - abs($order->getBaseDiscountInvoiced());
}

$result = [
"amount" => $amount,
"command" => 'sale',
"comments" => 'MageWorx OrderEditor: Automatic Payment After Editing'
];

$subtotal = $amount - $taxAmt - $shippingAmt + $discountAmt;

// Overwrite original amount details
$result['amount_detail'] = [
"subtotal" => $subtotal,
"tax" => $taxAmt,
"nontaxable" => $isTaxable,
"discount" => $discountAmt,
"shipping" => $shippingAmt
];

return $result;
}
}
44 changes: 44 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/
declare(strict_types=1);

namespace MageWorx\OrderEditorUSAePay\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use MageWorx\OrderEditor\Helper\Data as OrderEditorHelper;

class Data extends AbstractHelper
{
const XML_PATH_IS_ENABLED =
'mageworx_order_management/order_editor/invoice_shipment_refund/enable_usaepay_reauthorization';

/**
* @var OrderEditorHelper
*/
protected $orderEditorHelper;

/**
* @param Context $context
* @param OrderEditorHelper $orderEditorHelper
*/
public function __construct(
Context $context,
OrderEditorHelper $orderEditorHelper
) {
$this->orderEditorHelper = $orderEditorHelper;
parent::__construct($context);
}

/**
* @return bool
*/
public function isEnabled(): bool
{
return $this->scopeConfig->isSetFlag(static::XML_PATH_IS_ENABLED)
&& $this->orderEditorHelper->isReauthorizationAllowed();
}
}
64 changes: 64 additions & 0 deletions Model/Invoice/PaymentMethodProcessor/USAePayProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/

declare(strict_types=1);

namespace MageWorx\OrderEditorUSAePay\Model\Invoice\PaymentMethodProcessor;

use MageWorx\OrderEditor\Api\PaymentMethodProcessorInterface;
use MageWorx\OrderEditor\Model\Invoice\PaymentMethodProcessor\DefaultProcessor;
use Rootways\USAePay\Observer\DataAssignObserver;

class USAePayProcessor extends DefaultProcessor
implements PaymentMethodProcessorInterface
{
/**
* @inheritDoc
*/
public function isReauthorizationRequired(): bool
{
$order = $this->getOrder();
$payment = $order->getPayment();

$baseAmountAuthorized = (float)$payment->getBaseAmountAuthorized();
$baseOrderGrandTotal = (float)$order->getBaseGrandTotal();

$result = $baseAmountAuthorized < $baseOrderGrandTotal;

$payment->setAdditionalInformation('mw_reauthorization_required', $result);

return $result;
}

/**
* @inheritDoc
*/
public function isVaultAvailable(): bool
{
$order = $this->getOrder();
$payment = $order->getPayment();
$paymentExtensionAttributes = $payment->getExtensionAttributes();
$vaultPaymentToken = $paymentExtensionAttributes->getVaultPaymentToken();

if ($vaultPaymentToken && $vaultPaymentToken->getEntityId()) {
return true;
} else {
return false;
}
}

/**
* @inheritDoc
*/
public function setUseVaultForReauthorizationFlag(): PaymentMethodProcessorInterface
{
$order = $this->getOrder();
$payment = $order->getPayment();
$payment->setAdditionalInformation('mw_use_vault', true);

return $this;
}
}
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "mageworx/module-ordereditor-usa-epay",
"description": "Order Editor USA ePay payments reauthorization Plugin",
"require": {
"mageworx/module-ordereditorbase": ">=2.11.4",
"rootways/module-usaepay": "^1.0"
},
"type": "magento2-module",
"version": "1.0.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"MageWorx\\OrderEditorUSAePay\\": ""
}
}
}
25 changes: 25 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="mageworx_order_management">
<group id="order_editor">
<group id="invoice_shipment_refund">
<field id="enable_usaepay_reauthorization" translate="label comment" type="select" sortOrder="110" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Enable USA ePay Payment Reauthorization</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>If this setting is enabled, all changes made to the order will attempt to be processed using the payment details stored in the vault.</comment>
<depends>
<field id="sales_processor">keep_untouched</field>
</depends>
</field>
</group>
</group>
</section>
</system>
</config>
17 changes: 17 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!--
Copyright © MageWorx. All rights reserved.
See LICENSE.txt for license details.
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<mageworx_order_management>
<order_editor>
<invoice_shipment_refund>
<enable_usaepay_reauthorization>0</enable_usaepay_reauthorization>
</invoice_shipment_refund>
</order_editor>
</mageworx_order_management>
</default>
</config>
72 changes: 72 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- Performing capture operations for a payment which has been captured. Using vault if available. -->
<preference for="USAePayCaptureStrategyCommand"
type="MageWorx\OrderEditorUSAePay\Gateway\Command\CaptureStrategyCommand" />
<type name="MageWorx\OrderEditorUSAePay\Gateway\Command\CaptureStrategyCommand">
<arguments>
<argument name="commandPool" xsi:type="object">USAePayCommandPool</argument>
</arguments>
</type>

<!-- Performing adjustment operations for a payment that has not been captured yet. -->
<type name="MageWorx\OrderEditorUSAePay\Observer\UpdateAmountsWithoutInvoice">
<arguments>
<argument name="commandPool" xsi:type="object">USAePayCommandPool</argument>
</arguments>
</type>

<!-- Add new commands to the command pool -->
<virtualType name="USAePayCommandPool" type="Magento\Payment\Gateway\Command\CommandPool">
<arguments>
<argument name="commands" xsi:type="array">
<item name="adjust_positive" xsi:type="string">USAePayAdjustPositiveCommand</item>
</argument>
</arguments>
</virtualType>

<!-- Adjust positive command -->
<virtualType name="USAePayAdjustPositiveCommand" type="USAePayAuthorizeCommand">
<arguments>
<argument name="requestBuilder" xsi:type="object">USAePayAdjustPositiveRequest</argument>
<argument name="handler" xsi:type="object">USAePayAdjustHandler</argument>
</arguments>
</virtualType>
<virtualType name="USAePayAdjustPositiveRequest" type="Magento\Payment\Gateway\Request\BuilderComposite">
<arguments>
<argument name="builders" xsi:type="array">
<item name="commondata" xsi:type="string">Rootways\USAePay\Gateway\Request\CommonDataBuilder</item>
<item name="order" xsi:type="string">Rootways\USAePay\Gateway\Request\OrderDataBuilder</item>
<item name="transactiondata" xsi:type="string">Rootways\USAePay\Gateway\Request\Vault\TransactionDataBuilder</item>
<item name="addressdata" xsi:type="string">Rootways\USAePay\Gateway\Request\AddressDataBuilder</item>
<item name="level2data" xsi:type="string">Rootways\USAePay\Gateway\Request\LineItemDataBuilder</item>
<item name="customerip" xsi:type="string">Rootways\USAePay\Gateway\Request\CustomerIpDataBuilder</item>
<item name="adjustTransactionData" xsi:type="string">MageWorx\OrderEditorUSAePay\Gateway\Request\AdjustPositiveTransactionDataBuilder</item>
</argument>
</arguments>
</virtualType>

<!-- Adjustment commands general rules -->
<virtualType name="USAePayAdjustHandler" type="Magento\Payment\Gateway\Response\HandlerChain">
<arguments>
<argument name="handlers" xsi:type="array">
<item name="payment_details" xsi:type="string">Rootways\USAePay\Gateway\Response\PaymentDetailsHandler</item>
</argument>
</arguments>
</virtualType>

<!-- Add USA ePay payment processor for reauthorization purpose -->
<type name="MageWorx\OrderEditor\Model\Invoice\PaymentMethodProcessorFactory">
<arguments>
<argument name="map" xsi:type="array">
<item name="rootways_usaepay_option" xsi:type="string">MageWorx\OrderEditorUSAePay\Model\Invoice\PaymentMethodProcessor\USAePayProcessor</item>
</argument>
</arguments>
</type>
</config>
Loading

0 comments on commit d99678d

Please sign in to comment.