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

Write Card Metadata on order creation in magento #238

Merged
merged 2 commits into from
Jan 10, 2025
Merged
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
151 changes: 0 additions & 151 deletions Observer/Model/ProcessOrder/CardsMetadata.php

This file was deleted.

110 changes: 110 additions & 0 deletions Service/Card/CardMetaService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace Rvvup\Payments\Service\Card;

use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Api\Data\OrderStatusHistoryInterfaceFactory;
use Magento\Sales\Api\OrderManagementInterface;
use Psr\Log\LoggerInterface;
use Rvvup\Payments\Gateway\Method;

class CardMetaService
{

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

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

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

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

/**
* @param array $rvvupPaymentResponse
* @param OrderInterface $order
* @throws AlreadyExistsException
*/
public function process(array $rvvupPaymentResponse, OrderInterface $order)
{
if ($order->getPayment()->getMethod() != 'rvvup_CARD') {
return;
}
$data = [];
$keys = [
'cvvResponseCode',
'avsAddressResponseCode',
'avsPostCodeResponseCode',
'eci',
'cavv',
'acquirerResponseCode',
'acquirerResponseMessage'
];

$payment = $order->getPayment();
foreach ($keys as $key) {
if (isset($rvvupPaymentResponse[$key])) {
$value = $this->mapCardValue($rvvupPaymentResponse[$key]);
$payment->setAdditionalInformation(Method::PAYMENT_TITLE_PREFIX . $key, $rvvupPaymentResponse[$key]);
$data[$key] = $key . ': ' . $value;
}
}
if (!empty($data)) {
try {
$historyComment = $this->orderStatusHistoryFactory->create();
$historyComment->setParentId($order->getEntityId());
$historyComment->setIsCustomerNotified(0);
$historyComment->setIsVisibleOnFront(0);
$historyComment->setStatus($order->getStatus());
$message = __("<strong>Rvvup Card Data:</strong> <br />" . implode("<br />", $data));
$historyComment->setComment($message);
$this->orderManagement->addComment($order->getEntityId(), $historyComment);
} catch (\Exception $e) {
$this->logger->error('Rvvup cards metadata comment failed with exception: ' . $e->getMessage());
}
}
}

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

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

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

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

default:
return $value;
}
}
}
11 changes: 10 additions & 1 deletion Service/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\ResourceModel\Order\Payment;
use Magento\Store\Model\App\Emulation;
Expand All @@ -18,6 +19,7 @@
use Rvvup\Payments\Model\Payment\PaymentDataGetInterface;
use Rvvup\Payments\Model\ProcessOrder\Cancel;
use Rvvup\Payments\Model\ProcessOrder\ProcessorPool;
use Rvvup\Payments\Service\Card\CardMetaService;

class Result
{
Expand Down Expand Up @@ -53,6 +55,9 @@ class Result
/** @var Payment */
private $paymentResource;

/** @var CardMetaService */
private $cardMetaService;

/**
* @param ResultFactory $resultFactory
* @param SessionManagerInterface $checkoutSession
Expand All @@ -63,6 +68,7 @@ class Result
* @param Emulation $emulation
* @param LoggerInterface $logger
* @param Payment $paymentResource
* @param CardMetaService $cardMetaService
*/
public function __construct(
ResultFactory $resultFactory,
Expand All @@ -73,7 +79,8 @@ public function __construct(
OrderInterface $order,
Emulation $emulation,
LoggerInterface $logger,
Payment $paymentResource
Payment $paymentResource,
CardMetaService $cardMetaService
) {
$this->resultFactory = $resultFactory;
$this->checkoutSession = $checkoutSession;
Expand All @@ -84,6 +91,7 @@ public function __construct(
$this->emulation = $emulation;
$this->logger = $logger;
$this->paymentResource = $paymentResource;
$this->cardMetaService = $cardMetaService;
}

/**
Expand Down Expand Up @@ -139,6 +147,7 @@ public function processOrderResult(
$payment = $order->getPayment();
$dashboardUrl = $rvvupData['dashboardUrl'] ?? '';
$payment->setAdditionalInformation(Method::DASHBOARD_URL, $dashboardUrl);
$this->cardMetaService->process($rvvupData['payments'][0], $order);
$this->paymentResource->save($payment);

if (get_class($processor) == Cancel::class) {
Expand Down
2 changes: 1 addition & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@
</arguments>
</type>

<type name="Rvvup\Payments\Observer\Model\ProcessOrder\CardsMetadata">
<type name="Rvvup\Payments\Service\Card\CardMetaService">
<arguments>
<argument name="logger" xsi:type="object">Rvvup\Payments\Model\Logger</argument>
</arguments>
Expand Down
3 changes: 0 additions & 3 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
<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
Loading