Skip to content

Commit

Permalink
Added Order Status management (#80)
Browse files Browse the repository at this point in the history
- added order status management settings
- fixed issue with captured transaction on Fallback
- improved currency validation logic
- documentation update
  • Loading branch information
ed007m authored Nov 12, 2020
1 parent 84f10fa commit 9b77b44
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Controller/Payment/Fallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private function prepareResponse(Redirect $resultRedirect, Transaction $transact
{
if ($transaction->transactionWasCancelled()) {
$this->messageManager->addWarningMessage(__('Your order was cancelled in Vipps.'));
} elseif ($transaction->isTransactionReserved()) {
} elseif ($transaction->isTransactionReserved() || $transaction->isTransactionCaptured()) {
return $resultRedirect->setPath('checkout/onepage/success', ['_secure' => true]);
} elseif ($transaction->isTransactionExpired()) {
$this->messageManager->addErrorMessage(
Expand Down
3 changes: 1 addition & 2 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ If you have experienced any issue with Vipps try to enable `Request Profiling` a

`Stores -> Configuration -> Sales -> Payment Methods -> Vipps`

![Screenshot of Vipps Configuration Area](docs/images/vipps_basic.png)

After that, all information related to vipps payment module will be stored into two files `{project_root}/var/log/vipps_exception.log` or `{project_root}/var/log/vipps_debug.log`.

Requests Profiling is a page in Magento admin panel that helps you to track a communication between Vipps and Magento.
Expand Down Expand Up @@ -99,6 +97,7 @@ For information about how to find the above values, see the [Vipps Developer Por

**Environment** - Vipps API mode. Can be *production/develop*.
**Payment Action** - *Authorize*(process authorization transaction; funds are blocked on customer account, but not withdrawn) or *Capture* (withdraw previously authorized amount).
**Order Status** - default order status before redirecting back to Magento. Can be *Pending* or *Payment Review*.
**Debug** - log all actions with Vipps Payment module into `{project_root}/var/log/vipps_debug.log` file *(not recommended in production mode)*.
**Request/Response Profiling** - log all requests/responses to Vipps API into `vipps_profiling` table.

Expand Down
13 changes: 13 additions & 0 deletions Gateway/Transaction/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,19 @@ public function isTransactionReserved(): bool
return false;
}

/**
* @return bool
*/
public function isTransactionCaptured(): bool
{
$item = $this->transactionLogHistory->getLastSuccessItem();
if ($item && $item->getOperation() == self::TRANSACTION_OPERATION_CAPTURE) {
return true;
}

return false;
}

/**
* @return bool
* @throws \Exception
Expand Down
67 changes: 67 additions & 0 deletions Gateway/Validator/AvailabilityValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright 2020 Vipps
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
namespace Vipps\Payment\Gateway\Validator;

use Magento\Payment\Gateway\Validator\AbstractValidator;
use Magento\Payment\Gateway\Validator\ResultInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class InitiateValidator
* @package Vipps\Payment\Gateway\Validator
*/
class AvailabilityValidator extends AbstractValidator
{
const NORWEGIAN_CURRENCY = 'NOK';

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* CurrencyValidator constructor.
*
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ResultInterfaceFactory $resultFactory,
StoreManagerInterface $storeManager
) {
parent::__construct($resultFactory);
$this->storeManager = $storeManager;
}

/**
* @param array $validationSubject
*
* @return ResultInterface
* @throws NoSuchEntityException
*/
public function validate(array $validationSubject)
{
/** @var Store $store */
$store = $this->storeManager->getStore();

$isValid = self::NORWEGIAN_CURRENCY == $store->getBaseCurrencyCode();
$errorMessages = $isValid ? [] : [__('Not allowed currency. Please, contact store administrator.')];

return $this->createResult($isValid, $errorMessages);
}
}
1 change: 1 addition & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ For information about how to find the above values, see the [Vipps Developer Por

**Environment** - Vipps API mode. Can be *production/develop*.
**Payment Action** - *Authorize*(process authorization transaction; funds are blocked on customer account, but not withdrawn) or *Capture* (withdraw previously authorized amount).
**Order Status** - default order status before redirecting back to Magento. Can be *Pending* or *Payment Review*.
**Debug** - log all actions with Vipps Payment module into `{project_root}/var/log/vipps_debug.log` file *(not recommended in production mode)*.
**Request/Response Profiling** - log all requests/responses to Vipps API into `vipps_profiling` table.

Expand Down
53 changes: 53 additions & 0 deletions Model/Adminhtml/Source/OrderStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright 2020 Vipps
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
namespace Vipps\Payment\Model\Adminhtml\Source;

use Magento\Framework\Data\OptionSourceInterface;

/**
* Class PaymentAction
*/
class OrderStatus implements OptionSourceInterface
{
/**
* @var string
*/
const STATUS_PAYMENT_REVIEW = 'payment_review';

/**
* @var string
*/
const STATUS_PENDING = 'pending';

/**
* Possible actions on order place
*
* @return array
*/
public function toOptionArray()
{
return [
[
'value' => self::STATUS_PAYMENT_REVIEW,
'label' => __('Payment Review'),
],
[
'value' => self::STATUS_PENDING,
'label' => __('Pending'),
]
];
}
}
4 changes: 2 additions & 2 deletions Model/TransactionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ private function validateAmount(CartInterface $quote, Transaction $transaction)
*/
private function capture(OrderInterface $order)
{
if ($order->getState() !== Order::STATE_NEW) {
if (!in_array($order->getState(), [Order::STATE_NEW, Order::STATE_PAYMENT_REVIEW])) {
return;
}

Expand All @@ -431,7 +431,7 @@ private function capture(OrderInterface $order)
*/
private function authorize(OrderInterface $order, Transaction $transaction)
{
if ($order->getState() !== Order::STATE_NEW) {
if (!in_array($order->getState(), [Order::STATE_NEW, Order::STATE_PAYMENT_REVIEW])) {
return;
}

Expand Down
73 changes: 73 additions & 0 deletions Observer/OrderPaymentAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright 2020 Vipps
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

declare(strict_types=1);

namespace Vipps\Payment\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Payment\Gateway\ConfigInterface;
use Magento\Sales\Model\Order;
use Psr\Log\LoggerInterface;
use Vipps\Payment\Model\Adminhtml\Source\OrderStatus;

/**
* Class OrderPaymentAfter
* @package Vipps\Payment\Observer
*/
class OrderPaymentAfter implements ObserverInterface
{
/**
* @var ConfigInterface
*/
private $config;

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

/**
* OrderPaymentAfter constructor.
*
* @param ConfigInterface $config
* @param LoggerInterface $logger
*/
public function __construct(ConfigInterface $config, LoggerInterface $logger)
{
$this->config = $config;
$this->logger = $logger;
}

/**
* @param Observer $observer
*/
public function execute(Observer $observer)
{
/** @var Order\Payment $payment */
$payment = $observer->getPayment();
$order = $payment->getOrder();

$status = $this->config->getValue('order_status');
if ($status == OrderStatus::STATUS_PAYMENT_REVIEW) {
$order->setState(Order::STATE_PAYMENT_REVIEW);
$order->setStatus(Order::STATE_PAYMENT_REVIEW);
}

return;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "magento2-module",
"description": "Vipps Payment Method",
"license": "proprietary",
"version": "2.4.3",
"version": "2.4.4",
"require": {
"magento/framework": "103.0.*",
"magento/module-sales": "103.0.*",
Expand Down
Binary file modified docs/images/vipps_basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
<source_model>Vipps\Payment\Model\Adminhtml\Source\PaymentAction</source_model>
<attribute type="shared">1</attribute>
</field>
<field id="order_status" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Order status: order creation</label>
<tooltip>Status given to newly created orders before payment result confirmation via server notifications from Vipps.</tooltip>
<source_model>Vipps\Payment\Model\Adminhtml\Source\OrderStatus</source_model>
<config_path>payment/vipps/order_status</config_path>
</field>
<field id="merchant_serial_number" translate="label" type="obscure" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Merchant Serial number</label>
<config_path>payment/vipps/merchant_serial_number</config_path>
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<model>Vipps\Payment\Model\Method\Vipps</model>
<title>Vipps</title>
<active>1</active>
<order_status>processing</order_status>
<order_status>pending</order_status>
<is_gateway>1</is_gateway>
<can_use_checkout>1</can_use_checkout>
<can_initialize>0</can_initialize>
Expand Down
9 changes: 8 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@
<arguments>
<argument name="validators" xsi:type="array">
<item name="initiate" xsi:type="string">Vipps\Payment\Gateway\Validator\InitiateValidator</item>
</argument>
<item name="availability" xsi:type="string">Vipps\Payment\Gateway\Validator\AvailabilityValidator</item>
</argument>
</arguments>
</virtualType>
<virtualType name="VippsInitiateValidator" type="Magento\Payment\Gateway\Validator\ValidatorComposite">
Expand Down Expand Up @@ -401,4 +402,10 @@
<argument name="logger" xsi:type="object">Vipps\Payment\Model\Logger</argument>
</arguments>
</type>
<type name="Vipps\Payment\Observer\OrderPaymentAfter">
<arguments>
<argument name="config" xsi:type="object">Vipps\Payment\Gateway\Config\Config</argument>
<argument name="logger" xsi:type="object">Vipps\Payment\Model\Logger</argument>
</arguments>
</type>
</config>
3 changes: 3 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@
<event name="checkout_submit_all_after">
<observer name="vipps_checkout_submit_all_after" instance="Vipps\Payment\Observer\CheckoutSubmitAllAfter"/>
</event>
<event name="sales_order_payment_place_end">
<observer name="vipps_payment_place_after" instance="Vipps\Payment\Observer\OrderPaymentAfter"/>
</event>
</config>

0 comments on commit 9b77b44

Please sign in to comment.