Skip to content
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

Extend the functionality #1

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
vendor/
1 change: 1 addition & 0 deletions Api/Data/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ interface ConfigInterface
const LANGUAGE_CODE = 'language_code';
const TRANSACTION_ID_PREFIX = 'transaction_id_prefix';
const SHOP_COMMENT = 'shop_comment';
const FRONTEND_COMMENT = 'frontend_comment';
}
10 changes: 9 additions & 1 deletion Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public function __construct(
$this->componentRegistrar = $componentRegistrar;
}

/**
* @return string
*/
public function getFrontendComment(): string
{
return nl2br($this->getValue(ConfigInterface::FRONTEND_COMMENT));
}

/**
* It retrieves the Payment's method code.
*
Expand Down Expand Up @@ -317,7 +325,7 @@ public function getPrivateKeyPath(): string

if (strlen(trim($privateKeyPath)) <= 0) {
$privateKeyPath = $this->componentRegistrar
->getPath(ComponentRegistrar::MODULE, 'Youama_OTP')
->getPath(ComponentRegistrar::MODULE, 'Youama_OTP')
. DIRECTORY_SEPARATOR . 'key'
. DIRECTORY_SEPARATOR . 'demoPrivateKey.pem';
} else {
Expand Down
142 changes: 142 additions & 0 deletions Model/Invoice/StatusUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/**
* Youama_OTP
*
* @author Dominik Nsosso <[email protected]>
* @license David Belicza e.v. (http://youama.hu)
*/

declare(strict_types=1);

namespace Youama\OTP\Model\Invoice;

use Magento\Sales\Api\Data\OrderStatusHistoryInterfaceFactory;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\InvoiceRepositoryInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use Magento\Sales\Model\Service\InvoiceService;
use Magento\Framework\DB\Transaction;
use Magento\Sales\Model\Order\Status\History;
use Youama\OTP\Helper\Config;

/**
* Class StatusUpdater
*
* This class manage to create an invoice for the order if all the items are virtual and the auto-invoicing option is
* enabled in the admin
* History and Comment changes.
*
* @package Youama\OTP\Model\Order
*/
class StatusUpdater
{
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* @var OrderStatusHistoryInterfaceFactory
*/
private $orderStatusHistoryInterfaceFactory;

/**
* @var Config
*/
private $configHelper;

/**
* @var InvoiceService
*/
private $invoiceService;

/**
* @var Transaction
*/
protected $transaction;

/**
* @var InvoiceSender
*/
protected $invoiceSender;

/**
* @var InvoiceRepositoryInterface
*/
protected $invoiceRepository;

/**
* StatusUpdater constructor.
* @param OrderRepositoryInterface $orderRepository
* @param OrderStatusHistoryInterfaceFactory $orderStatusHistoryInterfaceFactory
* @param Config $configHelper
* @param InvoiceService $invoiceService
* @param Transaction $transaction
* @param InvoiceSender $invoiceSender
* @param InvoiceRepositoryInterface $invoiceRepository
*/
public function __construct(
OrderRepositoryInterface $orderRepository,
OrderStatusHistoryInterfaceFactory $orderStatusHistoryInterfaceFactory,
Config $configHelper,
InvoiceService $invoiceService,
Transaction $transaction,
InvoiceSender $invoiceSender,
InvoiceRepositoryInterface $invoiceRepository
) {
$this->orderRepository = $orderRepository;
$this->orderStatusHistoryInterfaceFactory = $orderStatusHistoryInterfaceFactory;
$this->configHelper = $configHelper;
$this->invoiceService = $invoiceService;
$this->transaction = $transaction;
$this->invoiceSender = $invoiceSender;
$this->invoiceRepository = $invoiceRepository;
}

/**
* Determinate the order is virtual and the auto invoice is setting is enabled
* @param Order $order
* @return bool
*/
private function canInvoice(Order $order): bool
{
return (
$order->getIsVirtual()
&& $order->canInvoice()
&& $this->configHelper->getValue('can_auto_invoice_virtual')
);
}

/**
* @param Order $order
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function createInvoice(Order $order)
{
if ($this->canInvoice($order)) {
$invoice = $this->invoiceService->prepareInvoice($order);
$invoice->register();
$invoice->setState(Order\Invoice::STATE_PAID);
$this->invoiceRepository->save($invoice);
$transactionSave = $this->transaction->addObject(
$invoice
)->addObject(
$invoice->getOrder()
);
$transactionSave->save();
$this->invoiceSender->send($invoice);

/** @var OrderRepositoryInterface|History $history */
$history = $this->orderStatusHistoryInterfaceFactory->create();
$history->setComment(__('Notified customer about invoice #%1.', $invoice->getId()));
$history->setIsVisibleOnFront(0);
$history->setIsCustomerNotified(1);
$history->setStatus(Order::STATE_COMPLETE);

$order->addStatusHistory($history);
$order->setStatus(Order::STATE_COMPLETE);
$this->orderRepository->save($order);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point this is a second order saving in the success flow. This method createInvoice has called at the end of the Order's StatusUpdater::success where the order already has been saved.

Is there any reason why is get two saves? If order save is required after invoice saving then I think this createInvoice could be easily before the order saving.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good point. shall I pass the order as a pointer?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not necessary, the $order already points back because objects are reference types.

}
}
}
41 changes: 30 additions & 11 deletions Model/Order/StatusUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Youama_OTP
*
* @author David Belicza <[email protected]>
* @author Dominik Nsosso <[email protected]>
* @license David Belicza e.v. (http://youama.hu)
*/

Expand All @@ -15,8 +16,11 @@
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\OrderCommentSender;
use Magento\Sales\Model\Order\Status\History;
use Magento\Framework\DB\Transaction;
use Youama\OTP\Api\OrderFinderInterface;
use Youama\OTP\Helper\Config;
use Youama\OTP\Model\Invoice\StatusUpdater as InvoiceStatusUpdater;
use Magento\Framework\Exception\LocalizedException;

/**
* Class Status
Expand Down Expand Up @@ -59,26 +63,41 @@ class StatusUpdater
private $configHelper;

/**
* Status constructor.
*
* @param OrderRepositoryInterface $orderRepository
* @param OrderFinderInterface $orderFinder
* @var Transaction
*/
protected $transaction;

/**
* @var InvoiceStatusUpdater
*/
private $invoiceStatusUpdater;

/**
* StatusUpdater constructor.
* @param OrderRepositoryInterface $orderRepository
* @param OrderFinderInterface $orderFinder
* @param OrderStatusHistoryInterfaceFactory $orderStatusHistoryInterfaceFactory
* @param OrderCommentSender $orderCommentSender
* @param Config $configHelper
* @param OrderCommentSender $orderCommentSender
* @param Config $configHelper
* @param Transaction $transaction
* @param InvoiceStatusUpdater $invoiceStatusUpdater
*/
public function __construct(
OrderRepositoryInterface $orderRepository,
OrderFinderInterface $orderFinder,
OrderStatusHistoryInterfaceFactory $orderStatusHistoryInterfaceFactory,
OrderCommentSender $orderCommentSender,
Config $configHelper
Config $configHelper,
Transaction $transaction,
InvoiceStatusUpdater $invoiceStatusUpdater
) {
$this->orderRepository = $orderRepository;
$this->orderFinder = $orderFinder;
$this->orderStatusHistoryInterfaceFactory = $orderStatusHistoryInterfaceFactory;
$this->orderCommentSender = $orderCommentSender;
$this->configHelper = $configHelper;
$this->transaction = $transaction;
$this->invoiceStatusUpdater = $invoiceStatusUpdater;
}

/**
Expand All @@ -97,15 +116,13 @@ public function inProgress(string $orderIncrementId)
}

/**
* Put order to processing, notify customer about it.
*
* @param string $orderIncrementId
* @param int $transactionId
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dockblock param list should be aligned to each other.

* @param int $transactionId
* @throws LocalizedException
*/
public function success(string $orderIncrementId, int $transactionId)
{
$order = $this->orderFinder->getOrderByOrderIncrementId($orderIncrementId);

if ($order->getStatus() == Order::STATE_PENDING_PAYMENT) {
$comment = str_replace(
self::PLACEHOLDER,
Expand All @@ -126,6 +143,8 @@ public function success(string $orderIncrementId, int $transactionId)

$this->orderRepository->save($order);
$this->orderCommentSender->send($order, true, $comment);

$this->invoiceStatusUpdater->createInvoice($order);
}
}

Expand Down
3 changes: 2 additions & 1 deletion Model/PaymentConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public function getConfig(): array
$this->methodCode => [
'is_active' => $this->configHelper->isActive(),
'is_available' => true,
'request_url' => $this->configHelper->getRequestUrl()
'request_url' => $this->configHelper->getRequestUrl(),
'frontend_comment' => $this->configHelper->getFrontendComment()
],
]];
}
Expand Down
35 changes: 22 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
{
"name": "youama/module-otp-2",
"description": "OTP Payment integration for Magento 2",
"type": "magento2-module",
"version": "1.0.5",
"license": [
"proprietary"
"name": "youama/module-otp-2",
"description": "OTP Payment integration for Magento 2",
"type": "magento2-module",
"version": "1.1.0",
"license": [
"proprietary"
],
"repositories": {
"0": {
"type": "composer",
"url": "https://repo.magento.com/"
}
},
"autoload": {
"files": [
"registration.php"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Youama\\OTP\\": ""
}
"psr-4": {
"Youama\\OTP\\": ""
}
},
"require": {
"magento/module-sales": "*"
}
}
13 changes: 12 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Order can be edited</comment>
</field>
<field id="can_auto_invoice_virtual" translate="label" type="select" sortOrder="65" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Auto Invoice Virtual Order</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Automatically create an invoice if the order contains only virtual items and the payment was successful.</comment>
</field>
<field id="checkout_success_url" translate="label" type="text" sortOrder="70" showInDefault="1"
showInWebsite="1" showInStore="0">
<label>Success URL</label>
Expand Down Expand Up @@ -87,8 +92,14 @@
<label>Shop Comment</label>
<comment>Short text about the webshop or order on the OTP payment user interface.</comment>
</field>
<field id="frontend_comment" translate="label" type="textarea" sortOrder="90" showInDefault="1"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be defined in config.xml too because of when admin does not click on save button in module settings area after module installation, there will be no default value. Config.xml contains default values.

showInWebsite="1" showInStore="1">
<label>Storefront Comment</label>
<comment>Information to display about the payment method before redirect.
</comment>
</field>
<field id="language_code" translate="label" type="select" sortOrder="120" showInDefault="1"
showInWebsite="1" showInStore="0">
showInWebsite="1" showInStore="1">
<label>Language Code</label>
<source_model>Youama\OTP\Model\Source\LanguageCode</source_model>
<comment>This language will be used in OTP payment user interface</comment>
Expand Down
2 changes: 2 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<can_edit>1</can_edit>
<can_use_internal>0</can_use_internal>
<can_use_checkout>1</can_use_checkout>
<can_auto_invoice_virtual>1</can_auto_invoice_virtual>
<is_gateway>1</is_gateway>
<is_offline>0</is_offline>
<can_initialize>0</can_initialize>
Expand All @@ -43,6 +44,7 @@
<order_cancel_is_allowed>1</order_cancel_is_allowed>
<paid_message>Your order has been paid. Your Authorization Code is: OTP_TR_ID</paid_message>
<unpaid_message>Your order has been canceled because of unpaid products.</unpaid_message>
<frontend_comment>Once you placed your order, we redirect you to the OTP payment processing site to complete your purchase</frontend_comment>
<currency_code>HUF</currency_code>
<language_code>hu</language_code>
<transaction_id_prefix>21117891</transaction_id_prefix>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

<module name="Youama_OTP" setup_version="1.0.5">
<module name="Youama_OTP" setup_version="1.1.0">
<sequence>
<module name="Magento_Sales" />
<module name="Magento_Payment" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ define(
return config['is_available'];
},

/**
*
* @returns {*}
*/
getFrontendComment: function() {
return config['frontend_comment'];
},

/**
* @param data
* @param event
Expand Down
1 change: 1 addition & 0 deletions view/frontend/web/template/payment/otp.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<!-- ko template: getTemplate() --><!-- /ko -->
<!--/ko-->
</div>
<p data-bind="html: getFrontendComment(), visible: getFrontendComment()"></p>
<div class="checkout-agreements-block">
<!-- ko foreach: $parent.getRegion('before-place-order') -->
<!-- ko template: getTemplate() --><!-- /ko -->
Expand Down