diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e0caea8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +vendor/ \ No newline at end of file diff --git a/Api/Data/ConfigInterface.php b/Api/Data/ConfigInterface.php index 01bae5a..9c0999a 100644 --- a/Api/Data/ConfigInterface.php +++ b/Api/Data/ConfigInterface.php @@ -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'; } diff --git a/Helper/Config.php b/Helper/Config.php index 92caf51..6dd3de3 100644 --- a/Helper/Config.php +++ b/Helper/Config.php @@ -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. * @@ -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 { diff --git a/Model/Invoice/StatusUpdater.php b/Model/Invoice/StatusUpdater.php new file mode 100644 index 0000000..116b728 --- /dev/null +++ b/Model/Invoice/StatusUpdater.php @@ -0,0 +1,142 @@ + + * @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); + } + } +} diff --git a/Model/Order/StatusUpdater.php b/Model/Order/StatusUpdater.php index ad642b5..b0a289c 100644 --- a/Model/Order/StatusUpdater.php +++ b/Model/Order/StatusUpdater.php @@ -3,6 +3,7 @@ * Youama_OTP * * @author David Belicza <87.bdavid@gmail.com> + * @author Dominik Nsosso * @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 + * @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); } } diff --git a/Model/PaymentConfigProvider.php b/Model/PaymentConfigProvider.php index 39013c6..b428425 100644 --- a/Model/PaymentConfigProvider.php +++ b/Model/PaymentConfigProvider.php @@ -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() ], ]]; } diff --git a/composer.json b/composer.json index 888684a..82430c9 100644 --- a/composer.json +++ b/composer.json @@ -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": "*" + } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 08bac22..4dad3f1 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -60,6 +60,11 @@ Magento\Config\Model\Config\Source\Yesno Order can be edited + + + Magento\Config\Model\Config\Source\Yesno + Automatically create an invoice if the order contains only virtual items and the payment was successful. + @@ -87,8 +92,14 @@ Short text about the webshop or order on the OTP payment user interface. + + + Information to display about the payment method before redirect. + + + showInWebsite="1" showInStore="1"> Youama\OTP\Model\Source\LanguageCode This language will be used in OTP payment user interface diff --git a/etc/config.xml b/etc/config.xml index f4462f9..5084573 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -22,6 +22,7 @@ 1 0 1 + 1 1 0 0 @@ -43,6 +44,7 @@ 1 Your order has been paid. Your Authorization Code is: OTP_TR_ID Your order has been canceled because of unpaid products. + Once you placed your order, we redirect you to the OTP payment processing site to complete your purchase HUF hu 21117891 diff --git a/etc/module.xml b/etc/module.xml index 4a420a1..242d246 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -10,7 +10,7 @@ - + diff --git a/view/frontend/web/js/view/payment/method-renderer/otp-method.js b/view/frontend/web/js/view/payment/method-renderer/otp-method.js index 8fd769d..62d9e2e 100644 --- a/view/frontend/web/js/view/payment/method-renderer/otp-method.js +++ b/view/frontend/web/js/view/payment/method-renderer/otp-method.js @@ -51,6 +51,14 @@ define( return config['is_available']; }, + /** + * + * @returns {*} + */ + getFrontendComment: function() { + return config['frontend_comment']; + }, + /** * @param data * @param event diff --git a/view/frontend/web/template/payment/otp.html b/view/frontend/web/template/payment/otp.html index 0637e00..4560065 100644 --- a/view/frontend/web/template/payment/otp.html +++ b/view/frontend/web/template/payment/otp.html @@ -21,6 +21,7 @@ +