Skip to content

Commit

Permalink
Merge pull request #9 from donmo-roundup/integration-improvements
Browse files Browse the repository at this point in the history
Integration improvements
  • Loading branch information
DavidVin357 authored Jul 13, 2023
2 parents b5947a1 + 928535b commit def2c74
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 72 deletions.
59 changes: 32 additions & 27 deletions Controller/Roundup/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,62 @@

namespace Donmo\Roundup\Controller\Roundup;

use Magento\Checkout\Model\Cart as CustomerCart;
use Donmo\Roundup\Logger\Logger;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Webapi\Rest\Request;
use Magento\Quote\Model\Quote;
use Magento\Checkout\Model\Session;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Quote\Api\CartRepositoryInterface;


class Create implements HttpPostActionInterface
{

/**
* @var Request
*/
private $request;
private Logger $logger;
private ResultFactory $resultFactory;
private Request $request;
private CheckoutSession $checkoutSession;
private CartRepositoryInterface $cartRepository;

public function __construct(
ResultFactory $resultFactory,
Request $request,
Quote $quote,
Session $checkoutSession,
CartRepositoryInterface $quoteRepository,
CustomerCart $cart,
CheckoutSession $checkoutSession,
CartRepositoryInterface $cartRepository,
Logger $logger
) {
$this->logger = $logger;
$this->resultFactory = $resultFactory;
$this->request = $request;
$this->quote = $quote;
$this->checkoutSession = $checkoutSession;
$this->quoteRepository = $quoteRepository;
$this->cart = $cart;
$this->cartRepository = $cartRepository;
}


public function execute()
{
// Add Donation to quote
$jsonResponse = $this->resultFactory->create(ResultFactory::TYPE_JSON);

$donationAmount = floatval($this->request->getBodyParams()['amount']);

$cartQuote = $this->cart->getQuote();
$cartQuote->setDonmodonation($donationAmount)->collectTotals();
$this->quoteRepository->save($cartQuote);
$this->checkoutSession->setCartGrandTotal($cartQuote->getGrandTotal());

$jsonResponse->setData(
[
'status' => 'success',
'donationAmount' => $donationAmount
]
);
if ($this->checkoutSession->hasQuote()) {
$quote = $this->checkoutSession->getQuote();

$quote->setDonmodonation($donationAmount)->collectTotals();
$this->cartRepository->save($quote);

$jsonResponse->setData(
[
'message' => 'success',
]
);
} else {
$jsonResponse->setHttpResponseCode(404);

$jsonResponse->setData(
['message' => 'no quote provided']
);
}

return $jsonResponse;
}
}
50 changes: 28 additions & 22 deletions Controller/Roundup/Remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,55 @@

namespace Donmo\Roundup\Controller\Roundup;

use Magento\Checkout\Model\Cart as CustomerCart;
use Donmo\Roundup\Logger\Logger;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\HttpDeleteActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Webapi\Rest\Request;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;

class Remove implements HttpDeleteActionInterface
{

private Logger $logger;
private ResultFactory $resultFactory;
private Session $checkoutSession;
private CartRepositoryInterface $cartRepository;

public function __construct(
ResultFactory $resultFactory,
Request $request,
Quote $quote,
Session $checkoutSession,
CartRepositoryInterface $quoteRepository,
CustomerCart $cart
CartRepositoryInterface $cartRepository,
Logger $logger
)
{
$this->resultFactory = $resultFactory;
$this->request = $request;
$this->quote = $quote;
$this->checkoutSession = $checkoutSession;
$this->quoteRepository = $quoteRepository;
$this->cart = $cart;
$this->cartRepository = $cartRepository;
$this->logger = $logger;
}
public function execute()
{
// Remove Donation from quote
$jsonResponse = $this->resultFactory->create(ResultFactory::TYPE_JSON);

$cartQuote = $this->cart->getQuote();
$cartQuote->setDonmodonation(0)->collectTotals();
$this->quoteRepository->save($cartQuote);
$this->checkoutSession->setCartGrandTotal($cartQuote->getGrandTotal());

$total = $cartQuote->getGrandTotal();
$jsonResponse->setData(
[
'total' => $total
]
);
if ($this->checkoutSession->hasQuote()) {
$quote = $this->checkoutSession->getQuote();

$quote->setDonmodonation(0)->collectTotals();
$this->cartRepository->save($quote);

$jsonResponse->setData(
[
'message' => 'success'
]
);
} else {
$jsonResponse->setHttpResponseCode(404);

$jsonResponse->setData(
['message' => 'no quote provided']
);
}
return $jsonResponse;
}
}
44 changes: 44 additions & 0 deletions Model/Resolver/AddDonationToQuote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Donmo\Roundup\Model\Resolver;

use Donmo\Roundup\Logger\Logger;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;


use Magento\Quote\Api\CartRepositoryInterface;

class AddDonationToQuote implements ResolverInterface
{
private Logger $logger;
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId;
private CartRepositoryInterface $cartRepository;

public function __construct(
Logger $logger,
CartRepositoryInterface $cartRepository,
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
) {
$this->logger = $logger;
$this->cartRepository = $cartRepository;
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
}

public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$donationAmount = $args['donationAmount'];
$maskedId = $args['cartId'];

$quoteId = $this->maskedQuoteIdToQuoteId->execute($maskedId);

$quote = $this->cartRepository->get($quoteId);

$quote->setDonmodonation($donationAmount)->collectTotals();
$this->cartRepository->save($quote);

return ['message' => 'success'];
}
}
31 changes: 31 additions & 0 deletions Model/Resolver/DonationResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Donmo\Roundup\Model\Resolver;

use Donmo\Roundup\Logger\Logger;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\Quote;

class DonationResolver implements ResolverInterface
{
private Logger $logger;

public function __construct(Logger $logger)
{
$this->logger = $logger;
}

public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
/** @var Quote $quote */
$quote = $value['model'];

$donmoDonation = $quote->getDonmodonation();

$currency = $quote->getQuoteCurrencyCode();

return ['value' => $donmoDonation, 'currency' => $currency];
}
}
44 changes: 44 additions & 0 deletions Model/Resolver/RemoveDonationFromQuote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Donmo\Roundup\Model\Resolver;

use Donmo\Roundup\Logger\Logger;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;

class RemoveDonationFromQuote implements ResolverInterface
{
private Logger $logger;
private CartRepositoryInterface $cartRepository;
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId;


public function __construct(
Logger $logger,
CartRepositoryInterface $cartRepository,
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
) {
$this->logger = $logger;
$this->cartRepository = $cartRepository;
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
}


public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$maskedId = $args['cartId'];

$quoteId = $this->maskedQuoteIdToQuoteId->execute($maskedId);

$quote = $this->cartRepository->get($quoteId);

$quote->setDonmodonation(0)->collectTotals();
$this->cartRepository->save($quote);

return ['message' => 'success'];
}
}
12 changes: 4 additions & 8 deletions Model/Total/Donation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@

namespace Donmo\Roundup\Model\Total;


use Magento\Quote\Api\Data\ShippingAssignmentInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address\Total;
use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
use Donmo\Roundup\Model\Config as DonmoConfig;

class Donation extends AbstractTotal {

private DonmoConfig $donmoConfig;

public function __construct(DonmoConfig $donmoConfig) {
$this->donmoConfig = $donmoConfig;
$this->setCode('donmodonation');
}



/**
* @param Quote $quote
* @param ShippingAssignmentInterface $shippingAssignment
* @param Total $total
* @return $this
*/

public function collect(
Quote $quote,
ShippingAssignmentInterface $shippingAssignment,
Expand All @@ -38,9 +34,9 @@ public function collect(

$donationAmount = $quote->getDonmodonation();

$total->addTotalAmount($this->getCode(), $donationAmount);
$total->setTotalAmount('donmodonation', $donationAmount);

$total->addBaseTotalAmount($this->getCode(), $donationAmount);
$total->setBaseTotalAmount('donmodonation', $donationAmount);

return $this;
}
Expand All @@ -51,7 +47,7 @@ public function fetch(Quote $quote, Total $total)
$donationAmount = $quote->getDonmodonation();
if ($donationAmount) {
return [
'code' => $this->getCode(),
'code' => 'donmodonation',
'title' => $this->donmoConfig->getDonationLabel(),
'value' => $donationAmount
];
Expand Down
16 changes: 16 additions & 0 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type CartPrices @doc(description: "Contains details about the final price of items in the cart, including discount and tax information.") {
donmo_donation: Money
@doc(description: "Donmo Roundup donation")
@resolver(class: "\\Donmo\\Roundup\\Model\\Resolver\\DonationResolver")
}

type Mutation {
addDonationToQuote(cartId: String!, donationAmount: Float!): DonmoDonationOutput @resolver(class: "\\Donmo\\Roundup\\Model\\Resolver\\AddDonationToQuote") @doc(description:"Add Donmo donation to quote")

removeDonationFromQuote(cartId: String!): DonmoDonationOutput @resolver(class: "\\Donmo\\Roundup\\Model\\Resolver\\RemoveDonationFromQuote") @doc(description:"Remove Donmo donation from quote")
}

type DonmoDonationOutput {
message: String @doc(description: "Result message")
}

Loading

0 comments on commit def2c74

Please sign in to comment.