Skip to content

Commit

Permalink
Added card metadata (#72)
Browse files Browse the repository at this point in the history
* Added card metadata to order notes
  • Loading branch information
andrii-onufriichuk authored Feb 5, 2024
1 parent 76a9cb1 commit 8ae0fe6
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 1 deletion.
132 changes: 132 additions & 0 deletions Observer/Model/ProcessOrder/CardsMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);

namespace Rvvup\Payments\Observer\Model\ProcessOrder;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\Data\OrderStatusHistoryInterfaceFactory;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Psr\Log\LoggerInterface;

class CardsMetadata implements ObserverInterface
{
/** @var OrderRepositoryInterface $orderRepository */
private $orderRepository;

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

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

/** @var OrderManagementInterface $orderManagement */
private $orderManagement;

/**
* @param OrderRepositoryInterface $orderRepository
* @param OrderStatusHistoryInterfaceFactory $orderStatusHistoryFactory
* @param OrderManagementInterface $orderManagement
* @param LoggerInterface $logger
*/
public function __construct(
OrderRepositoryInterface $orderRepository,
OrderStatusHistoryInterfaceFactory $orderStatusHistoryFactory,
OrderManagementInterface $orderManagement,
LoggerInterface $logger
) {
$this->orderRepository = $orderRepository;
$this->orderStatusHistoryFactory = $orderStatusHistoryFactory;
$this->orderManagement = $orderManagement;
$this->logger = $logger;
}

/**
* Send order confirmation & invoice emails.
*
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
$orderId = $observer->getData('order_id');
$rvvupData = $observer->getData('rvvup_data');
$payment = $rvvupData['payments'][0];
$order = $this->orderRepository->get($orderId);
if ($order->getPayment()->getMethod() == 'rvvup_CARD') {
$data = [];
$keys = [
'cvvResponseCode',
'avsAddressResponseCode',
'avsPostCodeResponseCode',
'eci',
'cavv',
'acquirerResponseCode',
'acquirerResponseMessage'
];

foreach ($keys as $key) {
$this->populateCardData($data, $payment, $key);
}

if (!empty($data)) {
try {
$historyComment = $this->orderStatusHistoryFactory->create();
$historyComment->setParentId($order->getEntityId());
$historyComment->setIsCustomerNotified(0);
$historyComment->setIsVisibleOnFront(0);
$historyComment->setStatus($order->getStatus());
$status = nl2br("Rvvup payment status " . $payment['status'] . "\n card data: \n");
$message = __($status . nl2br(implode("\n", $data)));
$historyComment->setComment($message);
$this->orderManagement->addComment($order->getEntityId(), $historyComment);
} catch (\Exception $e) {
$this->logger->error('Rvvup cards metadata comment fails with exception: ' . $e->getMessage());
}
}
}
}

/**
* @param array $data
* @param array $payment
* @param string $key
* @return void
*/
private function populateCardData(array &$data, array $payment, string $key): void
{
if (isset($payment[$key])) {
$value = $this->mapCardValue($payment[$key]);
$data[$key] = $key . ': ' . $value;
}
}

/**
* @param string $value
* @return string
*/
private function mapCardValue(string $value): string
{
switch ($value) {
case "0":
if ($value !== '0') {
return $value;
}
return '0 - Not Given';

case "1":
return '1 - Not Checked';

case "2":
return '2 - Match';

case "4":
return '4 - Not Matched';

default:
return $value;
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"guzzlehttp/guzzle": ">=6",
"magento/module-catalog": "^103.0 || ^104.0",
"magento/module-grouped-product": ">=100.1",
"rvvup/sdk": "0.13.8",
"rvvup/sdk": "0.13.9",
"ext-json": "*",
"php": "^7.3 || ^8.0"
},
Expand Down
6 changes: 6 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@
</arguments>
</type>

<type name="Rvvup\Payments\Observer\Model\ProcessOrder\CardsMetadata">
<arguments>
<argument name="logger" xsi:type="object">RvvupLog</argument>
</arguments>
</type>

<type name="Rvvup\Payments\Observer\Quote\Model\Quote\Item\RemoveExpressPaymentDataObserver">
<arguments>
<argument name="logger" xsi:type="object">RvvupLog</argument>
Expand Down
3 changes: 3 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<observer name="Rvvup_Payments::Model\ProcessOrder\Complete\EmailSenderObserver"
instance="Rvvup\Payments\Observer\Model\ProcessOrder\EmailSenderObserver"
shared="false" />
<observer name="Rvvup_Payments::Model\ProcessOrder\Complete\CardsMetadata"
instance="Rvvup\Payments\Observer\Model\ProcessOrder\CardsMetadata"
shared="false" />
</event>

<event name="rvvup_payments_process_order_processing_after">
Expand Down

0 comments on commit 8ae0fe6

Please sign in to comment.