Skip to content
Closed
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
11 changes: 11 additions & 0 deletions app/code/Riskified/Decider/Api/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public function post($order, $action)
$orderForTransport = $this->_orderHelper->getOrderFulfillments();
$response = $transport->fulfillOrder($orderForTransport);
break;
case Api::ACTION_REFUND:
$orderForTransport = $this->load($order);
$response = $transport->refundOrder($orderForTransport);
break;
}
$eventData['response'] = $response;

Expand Down Expand Up @@ -196,6 +200,13 @@ private function load($model)
$order->payment_details = $this->_orderHelper->getPaymentDetails();
$order->line_items = $this->_orderHelper->getLineItems();
$order->shipping_lines = $this->_orderHelper->getShippingLines();
$refundDetails = $this->_orderHelper->getRefundDetails();

if(empty($refundDetails) != 1){
$order->refunds = $refundDetails;
}else{
$order->refunds = 'no refunds';
}

if (!$this->_backendAuthSession->isLoggedIn()) {
$order->client_details = $this->_orderHelper->getClientDetails();
Expand Down
70 changes: 70 additions & 0 deletions app/code/Riskified/Decider/Api/Order/Helper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Riskified\Decider\Api\Order;

use Riskified\OrderWebhook\Model;

class Helper
{
private $_order;
Expand All @@ -12,7 +14,10 @@ class Helper
private $_orderFactory;
private $_categoryFactory;
private $_storeManager;
private $_registry;

public function __construct(
\Magento\Framework\Registry $registry,
\Magento\Framework\Logger\Monolog $logger,
\Riskified\Decider\Api\Config $apiConfig,
Log $apiLogger,
Expand All @@ -23,6 +28,7 @@ public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager
)
{
$this->_registry = $registry;
$this->_logger = $logger;
$this->_messageManager = $messageManager;
$this->_apiConfig = $apiConfig;
Expand All @@ -32,21 +38,25 @@ public function __construct(
$this->_categoryFactory = $categoryFactory;
$this->_storeManager = $storeManager;
}

public function setOrder($model)
{
$this->_order = $model;
}

public function getOrder()
{
return $this->_order;
}

public function getOrderOrigId()
{
if (!$this->getOrder()) {
return null;
}
return $this->getOrder()->getId() . '_' . $this->getOrder()->getIncrementId();
}

public function getDiscountCodes()
{
$code = $this->getOrder()->getDiscountDescription();
Expand All @@ -58,16 +68,35 @@ public function getDiscountCodes()
)));
return null;
}

public function getCreditMemoFromRegistry()
{
return $this->_registry->registry('creditMemo');
}
public function buildRefundDetailsObject($payload)
{
$refundObject = new Model\RefundDetails(array_filter(array(
'refund_id' => $payload->getIncrementId(),
'amount' => $payload->getSubtotal(),
'currency' => $payload->getBaseCurrencyCode(),
'refunded_at' => $payload->getCreatedAt(),
'reason' => $payload->getCustomerNote()
), 'strlen'));
return $refundObject;
}

public function getShippingAddress()
{
$mageAddr = $this->getOrder()->getShippingAddress();
return $this->getAddress($mageAddr);
}

public function getBillingAddress()
{
$mageAddr = $this->getOrder()->getBillingAddress();
return $this->getAddress($mageAddr);
}

public function getClientDetails()
{
$om = \Magento\Framework\App\ObjectManager::getInstance();
Expand All @@ -78,6 +107,26 @@ public function getClientDetails()
'user_agent' => $httpHeader->getHttpUserAgent()
), 'strlen'));
}

public function getRefundDetails()
{
$order = $this->getOrder();
$creditMemos = $order->getCreditmemosCollection();
$refundObjectCollection = array();

if($creditMemos->getSize() > 0){
foreach($creditMemos as $memo){
array_push($refundObjectCollection, $this->buildRefundDetailsObject($memo));
}
}

$currentMemo = $this->getCreditMemoFromRegistry();
if(!is_null($currentMemo)){
array_push($refundObjectCollection, $this->buildRefundDetailsObject($currentMemo));
}
return $refundObjectCollection;
}

public function getCustomer()
{
$customer_id = $this->getOrder()->getCustomerId();
Expand Down Expand Up @@ -111,11 +160,13 @@ public function getCustomer()
}
return new Model\Customer(array_filter($customer_props, 'strlen'));
}

public function getCustomerSession()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
return $objectManager->get('Magento\Customer\Model\Session');
}

public function getLineItems()
{
$line_items = array();
Expand All @@ -124,6 +175,7 @@ public function getLineItems()
}
return $line_items;
}

public function getAllLineItems()
{
$line_items = array();
Expand All @@ -132,6 +184,7 @@ public function getAllLineItems()
}
return $line_items;
}

protected function getPreparedLineItem($item) {
$prod_type = null;
$prod_type = null;
Expand Down Expand Up @@ -176,6 +229,7 @@ protected function getPreparedLineItem($item) {
}
return $line_items;
}

public function getAddress($address)
{
if (!$address) {
Expand Down Expand Up @@ -206,6 +260,7 @@ public function getAddress($address)
}
return new Model\Address($addrArray);
}

public function getPaymentDetails()
{
$payment = $this->getOrder()->getPayment();
Expand Down Expand Up @@ -363,31 +418,39 @@ public function getPaymentDetails()
}
} catch (\Exception $e) {
}

if (!isset($cvv_result_code)) {
$cvv_result_code = $payment->getCcCidStatus();
}

if (!isset($credit_card_number)) {
$credit_card_number = $payment->getCcLast4();
}

if (!isset($credit_card_company)) {
$credit_card_company = $payment->getCcType();
}

if (!isset($avs_result_code)) {
$avs_result_code = $payment->getCcAvsStatus();
}

$om = \Magento\Framework\App\ObjectManager::getInstance();
if (!isset($credit_card_bin) || !$credit_card_bin) {
$session = $om->get('Magento\Customer\Model\Session');
$credit_card_bin = $session->getRiskifiedBin();
$session->unsRiskifiedBin();
}

if (!isset($credit_card_bin) || !$credit_card_bin) {
$coreRegistry = $om->get('Magento\Framework\Registry');
$credit_card_bin = $coreRegistry->registry('riskified_cc_bin');
}

if (isset($credit_card_number)) {
$credit_card_number = "XXXX-XXXX-XXXX-" . $credit_card_number;
}

return new Model\PaymentDetails(array_filter(array(
'authorization_id' => $transactionId,
'avs_result_code' => $avs_result_code,
Expand All @@ -397,6 +460,7 @@ public function getPaymentDetails()
'credit_card_bin' => $credit_card_bin
), 'strlen'));
}

public function getShippingLines()
{
return new Model\ShippingLine(array_filter(array(
Expand All @@ -405,6 +469,7 @@ public function getShippingLines()
'code' => $this->getOrder()->getShippingMethod()
), 'strlen'));
}

public function getCancelledAt()
{
$commentCollection = $this->getOrder()->getStatusHistoryCollection();
Expand All @@ -415,6 +480,7 @@ public function getCancelledAt()
}
return null;
}

public function getOrderCancellation()
{
$orderCancellation = new Model\OrderCancellation(array_filter(array(
Expand All @@ -424,6 +490,7 @@ public function getOrderCancellation()
)));
return $orderCancellation;
}

public function getOrderFulfillments()
{
$fulfillments = array();
Expand All @@ -447,6 +514,7 @@ public function getOrderFulfillments()
)));
return $orderFulfillments;
}

public function getRemoteIp()
{
$this->_apiLogger->log("remote ip: " . $this->getOrder()->getRemoteIp() .
Expand All @@ -470,10 +538,12 @@ public function getRemoteIp()
}
return $remoteIp;
}

public function formatDateAsIso8601($dateStr)
{
return ($dateStr == NULL) ? NULL : date('c', strtotime($dateStr));
}

public function isAdmin()
{
$om = \Magento\Framework\App\ObjectManager::getInstance();
Expand Down
14 changes: 12 additions & 2 deletions app/code/Riskified/Decider/Observer/OrderPaymentRefund.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ class OrderPaymentRefund implements ObserverInterface
private $logger;
private $apiOrderLayer;
private $messageManager;
private $registry;

public function __construct(
\Magento\Framework\Registry $registry,
\Riskified\Decider\Api\Log $logger,
\Riskified\Decider\Api\Order $orderApi,
\Magento\Framework\Message\ManagerInterface $messageManager
)
{
$this->registry = $registry;
$this->logger = $logger;
$this->apiOrderLayer = $orderApi;
$this->messageManager = $messageManager;
Expand All @@ -25,12 +28,19 @@ public function execute(\Magento\Framework\Event\Observer $observer)
{
try {
$order = $observer->getPayment()->getOrder();
$this->apiOrderLayer->post($order, Api::ACTION_CANCEL);
$creditMemo = $observer->getEvent()->getCreditmemo();
$this->saveMemoInRegistry($creditMemo);
$this->apiOrderLayer->post($order, Api::ACTION_REFUND);
} catch(\Exception $e) {
$this->messageManager->addErrorMessage(
__("Riskified API Respond : %1", $e->getMessage())
);
$this->logger->logException($e);
}
}
}

public function saveMemoInRegistry($creditMemo)
{
$this->registry->register('creditMemo', $creditMemo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Order extends AbstractModel {
'tags' => 'string optional',
'vendor_id' => 'string optional',
'vendor_name' => 'string optional',
'refunds' => 'object \RefundDetails',

'shipping_address' => 'object \Address optional',
'billing_address' => 'object \Address optional',
Expand Down