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

[PLUGIN1CC-3423] Magic integration #476

Draft
wants to merge 35 commits into
base: master-2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
01c9e90
Magic integration
ChetanGN Mar 5, 2024
1d3b68a
Commented devstack mode
ChetanGN Mar 6, 2024
b81cc2f
Button fix
ChetanGN Mar 6, 2024
440255b
Added magic spinner
ChetanGN Mar 6, 2024
96d2b0a
Handled loader in payment success
ChetanGN Mar 6, 2024
afc1364
Added minicart template
ChetanGN Mar 10, 2024
357e03b
Removed dummy button
ChetanGN Mar 12, 2024
b225dbb
Added mini cart checkout
ChetanGN Mar 12, 2024
3128b8b
Added cart empty check condition for minicart
ChetanGN Mar 13, 2024
0a14050
Bug fix for auto discount
ChetanGN Mar 14, 2024
c95eaa1
Refactoring the code and handled auto coupon
ChetanGN Mar 18, 2024
650a4ce
Buy now fix
ChetanGN Mar 18, 2024
48a2fcf
Added magic notes in order summary
ChetanGN Mar 26, 2024
97660a5
Added button changes and image fix
ChetanGN Apr 4, 2024
e9bffdf
Added support for auto discount
ChetanGN Apr 4, 2024
b0324bd
Added support for coupon apply on cart
ChetanGN Apr 4, 2024
79cf8ca
Bug fix for order status update for COD payments
ChetanGN Apr 5, 2024
0f52715
Merchant name fix
ChetanGN Apr 5, 2024
f78cf33
Sending one cc falg in checkout options and name as empty
ChetanGN Apr 5, 2024
e5eab62
Removed rzp offer discount update on magento order for navya
ChetanGN Apr 6, 2024
2a5c184
Added merchant order id in order notes
ChetanGN Apr 7, 2024
02552ee
Added support for GTM events
ChetanGN Apr 12, 2024
1869ba0
Updated the php sdk version in composer
ChetanGN Apr 12, 2024
81170bb
Removed debugger
ChetanGN Apr 12, 2024
7d5122e
Reverted branded button from mini cart
ChetanGN Apr 12, 2024
2613b8c
Added coupon widget config on magento admin panel
ChetanGN Apr 18, 2024
73354f8
Passing affiliation param in GTM
ChetanGN Apr 18, 2024
ab9759b
Fixed the state name issue for Odisha
ChetanGN Apr 23, 2024
9ee89ab
Added auto refund for out of stock inventory case and added logged in…
ChetanGN May 2, 2024
9850a30
Modified the purchase event push data logic
ChetanGN May 2, 2024
28ac684
Syntax issue fix
ChetanGN May 2, 2024
3326e68
Added website logged in customer email address to order notes
ChetanGN May 15, 2024
a6c253d
Added Abandoned quote update feature
ChetanGN May 22, 2024
638e12a
Added GSTIN field to magento order
ChetanGN May 24, 2024
f39bd80
Exit the webhook process if it's a magic order
ChetanGN Jun 21, 2024
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
230 changes: 230 additions & 0 deletions Controller/OneClick/AbandonedQuote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php

namespace Razorpay\Magento\Controller\OneClick;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Pricing\Helper\Data;
use Razorpay\Magento\Controller\OneClick\StateMap;
use Razorpay\Magento\Model\PaymentMethod;
use Razorpay\Magento\Model\Config;
use Razorpay\Api\Api;
use Magento\Framework\App\Request\Http;
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory;
use Magento\Directory\Model\ResourceModel\Region\Collection;
use Razorpay\Magento\Model\CartConverter;

class AbandonedQuote extends Action
{
/**
* @var Http
*/
protected $request;

/**
* @var JsonFactory
*/
protected $resultJsonFactory;

protected $collectionFactory;

/**
* @var Data
*/
protected $priceHelper;

protected $config;

/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

protected $rzp;

protected $cartRepositoryInterface;

protected $checkoutSession;
protected $stateNameMap;
protected $cartConverter;

const COD = 'cashondelivery';
const RAZORPAY = 'razorpay';

/**
* CompleteOrder constructor.
* @param Http $request
* @param Context $context
* @param JsonFactory $jsonFactory
* @param Data $priceHelper
* @param PaymentMethod $paymentMethod
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
Context $context,
Http $request,
JsonFactory $jsonFactory,
PaymentMethod $paymentMethod,
\Razorpay\Magento\Model\Config $config,
\Psr\Log\LoggerInterface $logger,
\Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
\Magento\Checkout\Model\Session $checkoutSession,
CollectionFactory $collectionFactory,
StateMap $stateNameMap,
CartConverter $cartConverter
)
{
parent::__construct($context);
$this->request = $request;
$this->resultJsonFactory = $jsonFactory;
$this->config = $config;
$this->rzp = $paymentMethod->setAndGetRzpApiInstance();
$this->logger = $logger;
$this->cartRepositoryInterface = $cartRepositoryInterface;
$this->checkoutSession = $checkoutSession;
$this->collectionFactory = $collectionFactory;
$this->stateNameMap = $stateNameMap;
$this->cartConverter = $cartConverter;
}

public function execute()
{
$params = $this->request->getParams();

$resultJson = $this->resultJsonFactory->create();

$rzpOrderId = $params['rzp_order_id'];

try {
$rzpOrderData = $this->rzp->order->fetch($rzpOrderId);

$cartMaskId = isset($rzpOrderData->notes) ? $rzpOrderData->notes->cart_mask_id : null;

$cartId = isset($rzpOrderData->notes) ? $rzpOrderData->notes->cart_id : null;
$email = $rzpOrderData->customer_details->email ?? null;

$quote = $this->cartRepositoryInterface->get($cartId);

$this->updateQuote($quote, $rzpOrderData);

$quoteId = $rzpOrderData->notes->cart_mask_id;

// Set customer to quote
$customerCartId = $this->cartConverter->convertGuestCartToCustomer($cartId);
$this->logger->info('graphQL: customerCartId ' . $customerCartId);

return $resultJson->setData([
'status' => 'success',
'message' => 'Successfully updated the quote',
])->setHttpResponseCode(200);

} catch (\Razorpay\Api\Errors\Error $e) {
$this->logger->critical("Validate: Razorpay Error message:" . $e->getMessage());

$code = $e->getCode();
$this->messageManager->addError(__('Payment Failed.'));

return $resultJson->setData([
'status' => 'error',
'code' => $code,
'message' => __('An error occurred on the server. Please try again after sometime.'),
])->setHttpResponseCode(500);
} catch (\Exception $e) {
$this->logger->critical("Validate: Exception Error message:" . $e->getMessage());
$this->messageManager->addError(__('Payment Failed.'));

$code = $e->getCode();

return $resultJson->setData([
'status' => 'error',
'code' => $code,
'message' => __('An error occurred on the server. Please try again.'),
])->setHttpResponseCode(500);
}
}

protected function updateQuote($quote, $rzpOrderData)
{
$carrierCode = $rzpOrderData->notes->carrier_code ?? 'freeshipping';
$methodCode = $rzpOrderData->notes->method_code ?? 'freeshipping';

$email = $rzpOrderData->customer_details->email ?? '';

$quote->setCustomerEmail($email);

if(empty($rzpOrderData->customer_details->shipping_address) === false) {

$shippingCountry = $rzpOrderData->customer_details->shipping_address->country;
$shippingState = $rzpOrderData->customer_details->shipping_address->state;

$billingCountry = $rzpOrderData->customer_details->billing_address->country;
$billingState = $rzpOrderData->customer_details->billing_address->state;

$shippingRegionCode = $this->getRegionCode($shippingCountry, $shippingState);
$billingRegionCode = $this->getRegionCode($billingCountry, $billingState);

$shipping = $this->getAddress($rzpOrderData->customer_details->shipping_address, $shippingRegionCode, $email);
$billing = $this->getAddress($rzpOrderData->customer_details->billing_address, $billingRegionCode, $email);

$quote->getBillingAddress()->addData($billing['address']);
$quote->getShippingAddress()->addData($shipping['address']);

$shippingMethod = 'NA';
if (empty($carrierCode) === false && empty($methodCode) === false) {
$shippingMethod = $carrierCode . "_" . $methodCode;
}

$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod($shippingMethod);

}
$paymentMethod = static::RAZORPAY;

$quote->setPaymentMethod($paymentMethod);
$quote->setInventoryProcessed(false);
// Set Sales Order Payment
$quote->getPayment()->importData(['method' => $paymentMethod]);

$quote->save();

}

protected function getRegionCode($country, $state)
{
$magentoStateName = $this->stateNameMap->getMagentoStateName($country, $state);

$this->logger->info('graphQL: Magento state name:' . $magentoStateName);

$regionCode = $this->collectionFactory->create()
->addRegionNameFilter($magentoStateName)
->getFirstItem()
->toArray();

return $regionCode['code'] ?? 'NA';

}

protected function getAddress($rzpAddress, $regionCode, $email)
{
$name = explode(' ', $rzpAddress->name);

return [
'email' => $email, //buyer email id
'address' => [
'firstname' => $name[0], //address Details
'lastname' => empty($name[1]) === false ? $name[1] : '.',
'street' => $rzpAddress->line1,
'city' => $rzpAddress->city,
'country_id' => strtoupper($rzpAddress->country),
'region' => $regionCode,
'postcode' => $rzpAddress->zipcode,
'telephone' => $rzpAddress->contact,
'save_in_address_book' => 1
]
];
}
}
Loading