Skip to content

Commit

Permalink
Migrate to v3 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kadukia123 authored Dec 10, 2024
1 parent 30d1961 commit 33baddc
Show file tree
Hide file tree
Showing 19 changed files with 395 additions and 198 deletions.
5 changes: 5 additions & 0 deletions src/app/code/Fera/Ai/Block/Footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function getPublicKey()
return $this->helper->getPublicKey();
}

public function getAppUrl()
{
return $this->helper->getAppUrl();
}

public function getApiUrl()
{
return $this->helper->getApiUrl();
Expand Down
125 changes: 0 additions & 125 deletions src/app/code/Fera/Ai/Block/Footer/Checkout/Success.php

This file was deleted.

15 changes: 14 additions & 1 deletion src/app/code/Fera/Ai/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,22 @@ public function isConfigured()
{
$publicKey = $this->getPublicKey();
$secretKey = $this->getSecretKey();
$appUrl = $this->getAppUrl();
$apiUrl = $this->getApiUrl();
$jsUrl = $this->getJsUrl();
return !empty($publicKey) && !empty($secretKey) && !empty($apiUrl) && !empty($jsUrl);
return !empty($publicKey) && !empty($secretKey) && !empty($appUrl) && !empty($apiUrl) && !empty($jsUrl);
}

/**
* The URL path to the APP (https). For example: https://app.fera.ai
* @return string
*/
public function getAppUrl()
{
return $this->scopeConfig->getValue(
'fera_ai/fera_ai_group/app_url',
ScopeInterface::SCOPE_STORE
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace Fera\Ai\Observer\Backend;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\HTTP\Client\Curl as Curl;

use Fera\Ai\Helper\Data as FeraHelper;

class OrderFulfilledStatusUpdate implements ObserverInterface
{

protected $_curl;
protected $helper;

/**
* Sales order constructor.
* @param FeraHelper $helper
*/
public function __construct(
FeraHelper $helper,
Curl $curl,
)
{
$this->helper = $helper;
$this->_curl = $curl;
}

/**
* Send put request to update order status
*/
public function updateOrderStatus($data) {
$url = $this->helper->getApiUrl()."v3/private/orders/".$data['external_id']."/fulfill";
$this->_curl->addHeader("Content-Type", "application/json");
$this->_curl->addHeader("SECRET-KEY", $this->helper->getSecretKey());
$this->_curl->setOption(CURLOPT_CUSTOMREQUEST, "PUT");
$this->_curl->post($url, $this->helper->jsonEncode($data));
$response = $this->_curl->getBody();
// $this->helper->log("Order status is updated for order". $response);
//echo $response;
}

/**
* get orders data and and send request
*
* @param Observer $observer
*/
public function execute(\Magento\Framework\Event\Observer $observer) {
if (!$this->helper->isEnabled()) { return; }

$shipment = $observer->getEvent()->getShipment();
$orderId = $shipment->getOrder()->getId();
$shipmentData = [
'fulfilled_at' => $this->helper->formatDate($shipment->getCreatedAt()),
'external_id' => $orderId,
];
$this->updateOrderStatus($shipmentData);

// $this->helper->log("Order id is: ". $orderId);
// $this->helper->log("Order created at is: ". $shipment->getCreatedAt());
return;
}
}
159 changes: 159 additions & 0 deletions src/app/code/Fera/Ai/Observer/Frontend/OrderPushEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
namespace Fera\Ai\Observer\Frontend;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order\Config;
use Magento\Sales\Model\Order as Order;
use Magento\Store\Model\StoreManagerInterface as StoreManager;
use Magento\Framework\HTTP\Client\Curl as Curl;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Directory\Model\Currency as Currency;
use Magento\Directory\Helper\Data as DirectoryHelperData;
use Magento\Customer\Model\Session as CustomerModelSession;

use Fera\Ai\Helper\Data as FeraHelper;

class OrderPushEvent implements ObserverInterface
{
protected $_storeManager;
protected $_currency;
protected $_curl;
protected $helper;
protected $order;
protected $customerSession;
protected $_checkoutSession;
protected $directoryHelper;

/**
* Sales order constructor.
* @param FeraHelper $helper
* @param Order $order
* @param StoreManager $storeManager
* @param Currency $currency
* @param Config $orderConfig
* @param DirectoryHelperData $directoryhelper
* @param CustomerModelSession $customerSession
* @param CheckoutSession $checkoutSession
*/
public function __construct(
FeraHelper $helper,
Order $order,
Curl $curl,
StoreManager $storeManager,
Currency $currency,
Config $orderConfig,
DirectoryHelperData $directoryHelper,
CustomerModelSession $customerSession,
CheckoutSession $checkoutSession,
)
{
$this->helper = $helper;
$this->_curl = $curl;
$this->order = $order;
$this->directoryHelper = $directoryHelper;
$this->customerSession = $customerSession;
$this->_checkoutSession = $checkoutSession;
$this->_storeManager = $storeManager;
$this->_currency = $currency;
}

/**
* Send post request to push orders
*/
public function pushOrders($data) {
$url = $this->helper->getApiUrl()."v3/private/orders.json";
$this->_curl->addHeader("Content-Type", "application/json");
$this->_curl->addHeader("SECRET-KEY", $this->helper->getSecretKey());
$this->_curl->post($url, $this->helper->jsonEncode($data));

$response = $this->_curl->getBody();
}

/**
* get orders data and and send request
*
* @param Observer $observer
*/
public function execute(Observer $observer) {
if (!$this->helper->isEnabled()) { return; }

$orderId = $this->_checkoutSession->getLastOrderId();

$order = $this->order->load($orderId);
$customer = $this->getCustomerData($order);

$currencyCode = $this->_storeManager->getStore()->getCurrentCurrencyCode();
$total = $order->getGrandTotal();
$totalUsd = $this->directoryHelper->currencyConvert($total, $currencyCode, 'USD');

$orderData = [
'external_id' => $orderId,
'number' => $order->getIncrementId(),
'total' => $total,
'total_usd' => $totalUsd,
'external_created_at' => $this->helper->formatDate($order->getCreatedAt()),
'external_updated_at' => $this->helper->formatDate($order->getUpdatedAt()),
'line_items' => $this->helper->serializeQuoteItems($order->getAllItems()),
'customer' => $customer,
'external_customer_id' => $order->getCustomerId(),
'tags' => [],
'source_name' => 'web'
];

if (!empty($order->getShippingAddress())) { $orderData['shipping_address'] = $this->getShippingData($order); }
if (!empty($order->getBillingAddress())) { $orderData['shipping_address'] = $this->getBillingData($order); }

$this->pushOrders($orderData);

return;
}

/**
* get customer data
*/
public function getCustomerData($order) {
$customerId = $order->getCustomerId();
$customerName = $order->getCustomerFirstname() . ' ' . $order->getCustomerLastname();

$customer = [
'external_id' => $customerId,
'name' => $customerName,
'email' => $order->getCustomerEmail(),
];

return $customer;
}

/**
* get shhipping details
*/
public function getShippingData($order) {
$shippingAddress = $order->getShippingAddress();
$shippingData = [
'name' => $shippingAddress->getData('firstname') . ' ' . $shippingAddress->getData('lastname'),
'address1' => $shippingAddress->getData('street'),
'city_name' => $shippingAddress->getData('city'),
'region_name' => $shippingAddress->getData('region'),
'zip_code' => $shippingAddress->getData('postcode'),
];

return $shippingData;
}

/**
* get billing data
*/

public function getBillingData($order) {
$billingAddress = $order->getBillingAddress();
$billingData = [
'name' => $billingAddress->getData('firstname') . ' ' . $billingAddress->getData('lastname'),
'address1' => $billingAddress->getData('street'),
'city_name' => $billingAddress->getData('city'),
'region_name' => $billingAddress->getData('region'),
'zip_code' => $billingAddress->getData('postcode'),
];
return $billingData;
}
}
Loading

0 comments on commit 33baddc

Please sign in to comment.