Skip to content

Commit

Permalink
Merge pull request #3 from Riskified/master
Browse files Browse the repository at this point in the history
t
  • Loading branch information
wkaminsk authored Jan 19, 2021
2 parents 6632c52 + fdf3f14 commit 6cc334d
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 99 deletions.
43 changes: 18 additions & 25 deletions Model/Api/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Riskified\Decider\Model\Api;

use Magento\Checkout\Model\Session;
use Riskified\OrderWebhook\Model;

class Order
Expand Down Expand Up @@ -138,10 +137,10 @@ public function post($order, $action)
throw new \Exception("Order doesn't not exists");
}
$this->_orderHelper->setOrder($order);
$eventData = array(
$eventData = [
'order' => $order,
'action' => $action
);
];
try {
switch ($action) {
case Api::ACTION_CREATE:
Expand All @@ -166,7 +165,7 @@ public function post($order, $action)
case Api::ACTION_FULFILL:
$this->_orderHelper->setOrder($order->getOrder());
$orderForTransport = $this->_orderHelper->getOrderFulfillments($order);

$order = $order->getOrder();
$eventData['order'] = $order->getOrder();

Expand All @@ -185,7 +184,6 @@ public function post($order, $action)
'riskified_decider_post_order_success',
$eventData
);

} catch (\Riskified\OrderWebhook\Exception\CurlException $curlException) {
$this->_raiseOrderUpdateEvent($order, 'error', null, 'Error transferring order data to Riskified');
$this->scheduleSubmissionRetry($order, $action);
Expand Down Expand Up @@ -225,12 +223,12 @@ public function post($order, $action)
*/
private function _raiseOrderUpdateEvent($order, $status, $oldStatus, $description)
{
$eventData = array(
$eventData = [
'order' => $order,
'status' => $status,
'old_status' => $oldStatus,
'description' => $description
);
];
$this->_eventManager->dispatch(
'riskified_decider_order_update',
$eventData
Expand Down Expand Up @@ -272,15 +270,15 @@ private function load($model)
if ($model->getPayment()) {
$gateway = $model->getPayment()->getMethod();
}
if(is_null($model->getRiskifiedCartToken())){
if (is_null($model->getRiskifiedCartToken())) {
$cartToken = $this->session->getSessionId();
//save card_token into db
$model->setRiskifiedCartToken($cartToken);
$model->save();
}else{
} else {
$cartToken = $model->getRiskifiedCartToken();
}
$order_array = array(
$order_array = [
'id' => $this->_orderHelper->getOrderOrigId(),
'name' => $model->getIncrementId(),
'email' => $model->getCustomerEmail(),
Expand All @@ -290,28 +288,23 @@ private function load($model)
'gateway' => $gateway,
'browser_ip' => $this->_orderHelper->getRemoteIp(),
'note' => $model->getCustomerNote(),
'total_price' => $model->getGrandTotal(),
'total_price' => floatval($model->getGrandTotal()),
'total_discounts' => $model->getDiscountAmount(),
'subtotal_price' => $model->getBaseSubtotalInclTax(),
'discount_codes' => $this->_orderHelper->getDiscountCodes($model),
'taxes_included' => true,
'total_tax' => $model->getBaseTaxAmount(),
'total_weight' => $model->getWeight(),
'cancelled_at' => $this->_orderHelper->formatDateAsIso8601($this->_orderHelper->getCancelledAt()),
'financial_status' => $model->getState(),
'fulfillment_status' => $model->getStatus(),
'discount_codes' => $this->_orderHelper->getDiscountCodes(),
'cancelled_at' => $this->_orderHelper->formatDateAsIso8601($this->_orderHelper->getCancelledAt()),
'vendor_id' => strval($model->getStoreId()),
'vendor_name' => $model->getStoreName(),
'cart_token' => $cartToken
);

];

if ($this->_orderHelper->isAdmin()) {
unset($order_array['browser_ip']);
unset($order_array['cart_token']);
$order_array['source'] = 'admin';
} else {
$order_array['source'] = 'web';
$order_array['source'] = 'desktop_web';
}

$order = new Model\Order(array_filter($order_array, 'strlen'));
Expand Down Expand Up @@ -349,12 +342,12 @@ public function update($order, $status, $oldStatus, $description)

$this->logger->log('Dispatching event for order ' . $order->getId() . ' with status "' . $status .
'" old status "' . $oldStatus . '" and description "' . $description . '"');
$eventData = array(
$eventData = [
'order' => $order,
'status' => $status,
'old_status' => $oldStatus,
'description' => $description
);
];

$this->_eventManager->dispatch(
'riskified_decider_order_update',
Expand Down Expand Up @@ -442,7 +435,7 @@ public function postHistoricalOrders($models)
if (!$this->_apiConfig->isEnabled()) {
return;
}
$orders = array();
$orders = [];

foreach ($models as $model) {
$orders[] = $this->getOrder($model);
Expand All @@ -467,11 +460,11 @@ public function scheduleSubmissionRetry(\Magento\Sales\Model\Order $order, $acti

if ($existingRetries->getSize() == 0) {
$queue = $this->queueFactory->create();
$queue->addData(array(
$queue->addData([
'order_id' => $order->getId(),
'action' => $action,
'updated_at' => $this->date->gmtDate()
))->save();
])->save();

$this->logger->log("New retry scheduled successfully");
}
Expand Down
60 changes: 25 additions & 35 deletions Model/Api/Order/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ public function getCustomer()
'first_name' => $this->getOrder()->getCustomerFirstname(),
'last_name' => $this->getOrder()->getCustomerLastname(),
'note' => $this->getOrder()->getCustomerNote(),
'group_name' => $this->getOrder()->getCustomerGroupId()
);
if ($customer_id) {
$customer_details = $this->customer->load($customer_id);
Expand Down Expand Up @@ -346,9 +345,7 @@ public function getAllShipmentItems($object)
*/
protected function getPreparedLineItem($item)
{
$prod_type = null;

$prod_type = null;
$prod_type = "physical";
$category = null;
$sub_categories = null;
$brand = null;
Expand Down Expand Up @@ -379,6 +376,10 @@ protected function getPreparedLineItem($item)
}
}

if ($item->getIsVirtual()) {
$prod_type = "digital";
}

$line_item = new Model\LineItem(array_filter(array(
'price' => floatval($item->getPrice()),
'quantity' => intval($item->getQtyOrdered()),
Expand All @@ -389,7 +390,8 @@ protected function getPreparedLineItem($item)
'product_type' => $prod_type,
'brand' => $brand,
'category' => (isset($categories) && !empty($categories)) ? implode('|', $categories) : '',
'sub_category' => (isset($sub_categories) && !empty($sub_categories)) ? implode('|', $sub_categories) : ''
'sub_category' => (isset($sub_categories) && !empty($sub_categories)) ? implode('|', $sub_categories) : '',
'requires_shipping' => (bool)!$item->getIsVirtual()
), 'strlen'));

return $line_item;
Expand Down Expand Up @@ -435,24 +437,6 @@ public function getAddress($address)
return new Model\Address($addrArray);
}

/**
* @param $payload
* @return Model\RefundDetails
* @throws \Exception
*/
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;
}

/**
* @return array
* @throws \Exception
Expand All @@ -462,9 +446,16 @@ 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));
if ($creditMemos->getSize() > 0) {
foreach ($creditMemos->getData() as $memo) {
$refundObjectCollection[] = new Model\RefundDetails(array_filter([
'refund_id' => $memo['increment_id'],
'amount' => $memo['subtotal'],
'currency' => $memo['base_currency_code'],
'refunded_at' => $memo['created_at'],
'reason' => $memo['customer_note']
], 'strlen'));

}
}

Expand Down Expand Up @@ -563,22 +554,21 @@ public function preparePaymentData($payment, &$paymentData)
if (!isset($paymentData['credit_card_bin']) || !$paymentData['credit_card_bin']) {
$paymentData['credit_card_bin'] = $this->registry->registry('riskified_cc_bin');
}
if (isset($paymentData['credit_card_bin'])) {
$paymentData['credit_card_bin'] = "XXXX-XXXX-XXXX-" . $paymentData['credit_card_bin'];
}
}

/**
* @return Model\ShippingLine
* @return array
* @throws \Exception
*/
public function getShippingLines()
{
return [new Model\ShippingLine(array_filter(array(
'price' => $this->getOrder()->getShippingAmount(),
'title' => strip_tags($this->getOrder()->getShippingDescription()),
'code' => $this->getOrder()->getShippingMethod()
), 'strlen'))];
return [
[
'price' => floatval($this->getOrder()->getShippingAmount()),
'title' => strip_tags($this->getOrder()->getShippingDescription()),
'code' => $this->getOrder()->getShippingMethod()
]
];
}

/**
Expand Down
20 changes: 20 additions & 0 deletions Model/Api/Order/PaymentProcessor/DefaultPaymentProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Riskified\Decider\Model\Api\Order\PaymentProcessor;

class DefaultPaymentProcessor extends AbstractPayment
{
/**
* @return array
*/
public function getDetails()
{
$details = [];
$details['cvv_result_code'] = $this->payment->getCcCidStatus();
$details['avs_result_code'] = $this->payment->getCcAvsStatus();
$details['credit_card_company'] = $this->payment->getCcType();
$details['credit_card_number'] = $this->payment->getCcLast4();

return $details;
}
}
14 changes: 8 additions & 6 deletions Model/Api/Order/PaymentProcessorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@

use Magento\Framework\ObjectManagerInterface;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\AbstractPayment;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\DirectPost;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Adyen;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Authcim;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Braintree;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Cryozonic;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\DefaultPaymentProcessor;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\DirectPost;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\OptimalHosted;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Payflowpro;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Paypal;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\PaypalDirect;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\SagePay;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Transarmor;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Braintree;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Payflowpro;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Adyen;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Cryozonic;
use Riskified\Decider\Model\Api\Order\PaymentProcessor\Vantiv;

class PaymentProcessorFactory
{
const GATEWAY_INSTANCE = [
'default' => Paypal::class,
'default' => DefaultPaymentProcessor::class,
'authorizenet_directpost' => DirectPost::class,
'authnetcim' => Authcim::class,
'optimal_hosted' => OptimalHosted::class,
Expand All @@ -38,6 +39,7 @@ class PaymentProcessorFactory
'payflowpro' => Payflowpro::class,
'adyen_oneclick' => Adyen::class,
'adyen_cc' => Adyen::class,
'adyen_hpp' => Paypal::class,
'cryozonic_stripe' => Cryozonic::class,
'vantiv_cc' => Vantiv::class
];
Expand Down
Loading

0 comments on commit 6cc334d

Please sign in to comment.