-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from donmo-roundup/split-guest-and-customer-ca…
…rt-functionality Split guest and customer cart functionality
- Loading branch information
Showing
13 changed files
with
275 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Donmo\Roundup\Api; | ||
|
||
interface CartDonationManagementInterface | ||
{ | ||
/** | ||
* Add Donmo donation to customer cart | ||
* | ||
* @param int $cartId | ||
* @param float $donationAmount | ||
* @return string | ||
*/ | ||
public function addDonationToCart(int $cartId, float $donationAmount): string; | ||
|
||
/** | ||
* Remove Donmo donation from customer cart | ||
* | ||
* @api | ||
* @param int $cartId | ||
* @return string | ||
*/ | ||
public function removeDonationFromCart(int $cartId) : string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Donmo\Roundup\Api; | ||
|
||
interface GuestCartDonationManagementInterface | ||
{ | ||
/** | ||
* Add Donmo donation to guest cart | ||
* | ||
* @param string $cartId (masked quote ID) | ||
* @param float $donationAmount | ||
* @return string | ||
*/ | ||
public function addDonationToCart(string $cartId, float $donationAmount): string; | ||
|
||
/** | ||
* Remove Donmo donation from guest cart | ||
* | ||
* @api | ||
* @param string $cartId (masked quote ID) | ||
* @return string | ||
*/ | ||
public function removeDonationFromCart(string $cartId) : string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Donmo\Roundup\Model; | ||
|
||
use Donmo\Roundup\Api\CartDonationManagementInterface; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\Serialize\SerializerInterface; | ||
use Magento\Quote\Api\CartRepositoryInterface; | ||
|
||
class CartDonationManagement implements CartDonationManagementInterface | ||
{ | ||
public function __construct( | ||
CartRepositoryInterface $cartRepository, | ||
SerializerInterface $serializer, | ||
) | ||
{ | ||
$this->cartRepository = $cartRepository; | ||
$this->serializer = $serializer; | ||
} | ||
|
||
/** | ||
@inheritdoc | ||
*/ | ||
public function addDonationToCart(int $cartId, float $donationAmount): string | ||
{ | ||
try { | ||
$quote = $this->cartRepository->get($cartId); | ||
|
||
if ($donationAmount > 0) { | ||
$quote->setDonmodonation($donationAmount)->collectTotals(); | ||
$this->cartRepository->save($quote); | ||
|
||
return $this->serializer->serialize(['message' => 'Success']); | ||
} else { | ||
return $this->serializer->serialize(['message' => 'Invalid donation']); | ||
} | ||
} catch (NoSuchEntityException) { | ||
throw new NoSuchEntityException(__('The quote could not be loaded')); | ||
} catch (\Exception $e) { | ||
throw new \Exception('Some error has occurred'); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function removeDonationFromCart(int $cartId): string | ||
{ | ||
try { | ||
$quote = $this->cartRepository->get($cartId); | ||
|
||
$quote->setDonmodonation(0)->collectTotals(); | ||
$this->cartRepository->save($quote); | ||
|
||
return $this->serializer->serialize(['message' => 'Success']); | ||
} catch (NoSuchEntityException) { | ||
throw new NoSuchEntityException(__('The quote could not be loaded')); | ||
} catch (\Exception $e) { | ||
throw new \Exception('Some error has occurred'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Donmo\Roundup\Model; | ||
|
||
use Donmo\Roundup\Api\GuestCartDonationManagementInterface; | ||
use Donmo\Roundup\Logger\Logger; | ||
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface; | ||
use Donmo\Roundup\Api\CartDonationManagementInterface; | ||
class GuestCartDonationManagement implements GuestCartDonationManagementInterface | ||
{ | ||
private Logger $logger; | ||
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId; | ||
private CartDonationManagementInterface $cartDonationManagement; | ||
|
||
public function __construct( | ||
Logger $logger, | ||
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId, | ||
CartDonationManagementInterface $cartDonationManagement | ||
) | ||
{ | ||
$this->logger = $logger; | ||
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId; | ||
$this->cartDonationManagement = $cartDonationManagement; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function addDonationToCart(string $cartId, float $donationAmount): string | ||
{ | ||
$this->logger->info('addDonationToCart guest'); | ||
return $this->cartDonationManagement->addDonationToCart($this->maskedQuoteIdToQuoteId->execute($cartId), $donationAmount); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function removeDonationFromCart(string $cartId): string | ||
{ | ||
return $this->cartDonationManagement->removeDonationFromCart($this->maskedQuoteIdToQuoteId->execute($cartId)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,37 @@ | ||
<?xml version="1.0" ?> | ||
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Webapi/etc/webapi.xsd"> | ||
|
||
<route url="/V1/donmo/add_donation" method="POST"> | ||
<service class="Donmo\Roundup\Api\DonationManagementInterface" method="addDonationToQuote"/> | ||
<route url="V1/guest-carts/:cartId/add_donmo_donation" method="POST"> | ||
<service class="Donmo\Roundup\Api\GuestCartDonationManagementInterface" method="addDonationToCart"/> | ||
<resources> | ||
<resource ref="anonymous"/> | ||
</resources> | ||
</route> | ||
|
||
<route url="/V1/donmo/remove_donation/:cartId" method="DELETE"> | ||
<service class="Donmo\Roundup\Api\DonationManagementInterface" method="removeDonationFromQuote"/> | ||
<route url="/V1/guest-carts/:cartId/remove_donmo_donation" method="DELETE"> | ||
<service class="Donmo\Roundup\Api\GuestCartDonationManagementInterface" method="removeDonationFromCart"/> | ||
<resources> | ||
<resource ref="anonymous"/> | ||
</resources> | ||
</route> | ||
|
||
<route url="/V1/carts/mine/add_donmo_donation" method="POST"> | ||
<service class="Donmo\Roundup\Api\CartDonationManagementInterface" method="addDonationToCart"/> | ||
<resources> | ||
<resource ref="self" /> | ||
</resources> | ||
<data> | ||
<parameter name="cartId" force="true">%cart_id%</parameter> | ||
</data> | ||
</route> | ||
|
||
<route url="/V1/carts/mine/remove_donmo_donation" method="DELETE"> | ||
<service class="Donmo\Roundup\Api\CartDonationManagementInterface" method="removeDonationFromCart"/> | ||
<resources> | ||
<resource ref="self" /> | ||
</resources> | ||
<data> | ||
<parameter name="cartId" force="true">%cart_id%</parameter> | ||
</data> | ||
</route> | ||
</routes> |
Oops, something went wrong.