Skip to content

Commit

Permalink
Add first release that was presented at Magento Live 2019
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Ludgero Miura committed Oct 25, 2019
0 parents commit 7616665
Show file tree
Hide file tree
Showing 38 changed files with 2,651 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/payment-request.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

204 changes: 204 additions & 0 deletions Controller/Cart/Totals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

/**
* W3C Payment Request (https://www.w3.org/TR/payment-request/)
*
* Add the W3C payment request api to Magento 2
*
* @package ImaginationMedia\PaymentRequest
* @author Igor Ludgero Miura <[email protected]>
* @copyright Copyright (c) 2019 Imagination Media (https://www.imaginationmedia.com/)
* @license https://opensource.org/licenses/OSL-3.0.php Open Software License 3.0
*/

declare(strict_types=1);

namespace ImaginationMedia\PaymentRequest\Controller\Cart;

use ImaginationMedia\PaymentRequest\Model\Address;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address\Rate as ShippingRate;
use Magento\Store\Model\StoreManagerInterface;

class Totals extends Action
{
/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @var CartRepositoryInterface
*/
protected $cartRepository;

/**
* @var ShippingRate
*/
protected $shippingRate;

/**
* @var Session
*/
protected $session;

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

/**
* @var Address
*/
protected $address;

const REQUIRED_FIELDS = [
"quoteId",
"shippingAddress",
"shippingMethod"
];

/**
* Totals constructor.
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param CartRepositoryInterface $cartRepository
* @param ShippingRate $shippingRate
* @param Session $session
* @param JsonFactory $jsonFactory
* @param Address $address
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
CartRepositoryInterface $cartRepository,
ShippingRate $shippingRate,
Session $session,
JsonFactory $jsonFactory,
Address $address
) {
parent::__construct($context);
$this->storeManager = $storeManager;
$this->cartRepository = $cartRepository;
$this->shippingRate = $shippingRate;
$this->session = $session;
$this->jsonFactory = $jsonFactory;
$this->address = $address;
}

/**
* Get cart totals
* @return bool|ResponseInterface|Json|ResultInterface
* @throws NoSuchEntityException
*/
public function execute()
{
$params = $this->getRequest()->getParams();
$error = "";
$totals = [];
$jsonResult = $this->jsonFactory->create();

/**
* Validate info
*/
foreach (self::REQUIRED_FIELDS as $REQUIRED_FIELD) {
if (!isset($params[$REQUIRED_FIELD]) ||
in_array($params[$REQUIRED_FIELD], ["undefined", "null"])) {
$error = __("Not provided " . $REQUIRED_FIELD . " field.");
}
}

if ($error === "") {
$quoteId = (int)$params["quoteId"];
$store = $this->storeManager->getStore();
$cart = $this->cartRepository->get($quoteId);
$customerId = $this->session->isLoggedIn() ? (int)$this->session->getCustomerId() : 0;

/**
* Validate if there is an active quote
*/
if (!$cart instanceof Quote || is_null($cart->getData("entity_id"))) {
$error = __("Invalid cart");
}

/**
* Check if cart is empty
*/
if ((int)$cart->getItemsCount() === 0) {
$error = __("Empty cart");
}

$cart->setStore($store);
$cart->setCurrency();

/**
* Set shipping addreess
*/
$magentoAddress = $this->address->convertAddressToMagentoAdress(
$params["shippingAddress"],
[],
$customerId
);
$cart->getShippingAddress()->addData($magentoAddress);

/**
* Set shipping method
*/
$shippingMethod = $params["shippingMethod"]["carrier_code"] . "_" .
$params["shippingMethod"]["method_code"];
$this->shippingRate
->setCode($shippingMethod)
->getPrice();
$shippingAddress = $cart->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod($shippingMethod);
$cart->getShippingAddress()->addShippingRate($this->shippingRate);

/**
* Apply all the changes
*/
$cart->collectTotals();
$cart->save();

$magentoTotals = $cart->getTotals();

foreach ($magentoTotals as $total) {
if ($total["value"] > 0) {
$totals[$total["code"]] = $total["value"];
}
}

/**
* Add discounts by coupon code
*/
$couponCode = (string)$cart->getCouponCode();
if ($couponCode !== "") {
$totals["discount"] = [
"label" => sprintf(__("Discount (%s)"), $cart->getCouponCode()),
"amount" => -($cart->getSubtotal() - $cart->getSubtotalWithDiscount())
];
}
}

if ($error !== "") {
$totals = [];
}

$jsonResult->setData([
"error" => $error,
"totals" => $totals
]);

return $jsonResult;
}
}
Loading

0 comments on commit 7616665

Please sign in to comment.