-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: master
Are you sure you want to change the base?
Changes from all commits
8bda132
e3faa85
63df63f
db7b2c1
817f1f2
6b71837
743aa83
7a03f67
4faf5a1
57b92b8
2aa2796
b7c5bfa
574ef2a
a785c1c
95a7035
86d642f
764a94a
5c43d06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea/ | ||
vendor/ |
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); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
* Youama_OTP | ||
* | ||
* @author David Belicza <[email protected]> | ||
* @author Dominik Nsosso <[email protected]> | ||
* @license David Belicza e.v. (http://youama.hu) | ||
*/ | ||
|
||
|
@@ -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 | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -97,15 +116,13 @@ public function inProgress(string $orderIncrementId) | |
} | ||
|
||
/** | ||
* Put order to processing, notify customer about it. | ||
* | ||
* @param string $orderIncrementId | ||
* @param int $transactionId | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
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": "*" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.