Skip to content
This repository has been archived by the owner on Nov 5, 2020. It is now read-only.

Commit

Permalink
Merge pull request #12 from magento-api/master
Browse files Browse the repository at this point in the history
[API] API Out: RPC Integration. Part 1
  • Loading branch information
Korshenko, Oleksii(okorshenko) committed Mar 29, 2016
2 parents 276d93a + 0c27494 commit 63e2ec0
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 38 deletions.
9 changes: 5 additions & 4 deletions sample-module-sample-message-queue/Model/AddToCartPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ public function aroundSave(
throw new \Exception('Invalid gift card code');
}
$payload = [
'amount' => $giftCard->getGiftCardsAmount(),
'balance' => $giftCard->getBalance(),
'customer_email' => $customer->getEmail(),
'customer_name' => $customer->getFirstname() . ' ' . $customer->getLastname(),
'cart_id' => $subject->getQuote()->getId(),
'giftcard_code' => $giftCard->getCode(),
'giftcard_is_redeemable' => $giftCard->getIsRedeemable()
];

$this->publisher->publish('add.to.cart.giftcard.added', $payload);

$this->publisher->publish('add.to.cart.giftcard.added.success', $payload);
$this->publisher
->publish('add.to.cart.giftcard.added', json_encode($payload));

} catch (\Exception $e) {
$this->logger->debug('Plugin Error: ' . $e->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Magento\SampleMessageQueue\Model\Handler\Async;

use \Psr\Log\LoggerInterface;
use Psr\Log\LoggerInterface;

class GiftCardAddedSuccess
{
Expand All @@ -28,11 +28,10 @@ public function __construct(LoggerInterface $logger)
/**
* Log information about added gift card
*
* @param array $data
* @return void
* @param string $data
*/
public function log(array $data)
public function log($data)
{
$this->logger->debug('ASYNC Handler: Gift Card Added Successfully: ' . serialize($data));
$this->logger->debug('ASYNC Handler: Gift Card Added Successfully: ' . $data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\SampleMessageQueue\Model\Handler\Async;

use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use \Psr\Log\LoggerInterface;
use Psr\Log\LoggerInterface;
use Magento\GiftCard\Helper\Data as GiftCardData;
use Magento\Framework\Locale\CurrencyInterface as LocaleCurrency;

class SendCustomerNotification
{
Expand All @@ -33,35 +34,52 @@ class SendCustomerNotification
*/
protected $storeManager;

/**
* @var GiftCardData
*/
protected $giftCardData;

/**
* @var LocaleCurrency
*/
protected $localeCurrency;

/**
* Initialize dependencies.
*
* @param LoggerInterface $logger
* @param TransportBuilder $transportBuilder
* @param CartRepositoryInterface $cartRepository
* @param StoreManagerInterface $storeManager
* @param GiftCardData $giftCardData
* @param LocaleCurrency $localeCurrency
*/
public function __construct(
LoggerInterface $logger,
TransportBuilder $transportBuilder,
CartRepositoryInterface $cartRepository,
StoreManagerInterface $storeManager
StoreManagerInterface $storeManager,
GiftCardData $giftCardData,
LocaleCurrency $localeCurrency
) {
$this->logger = $logger;
$this->transportBuilder = $transportBuilder;
$this->cartRepository = $cartRepository;
$this->storeManager = $storeManager;
$this->giftCardData = $giftCardData;
$this->localeCurrency = $localeCurrency;
}

/**
* Send customer notification
*
* @param array $payload
* @param string $payload
* @throws \InvalidArgumentException
* @return void
*/
public function send(array $payload)
public function send($payload)
{
$payload = json_decode($payload, true);
if (!isset($payload['cart_id'])) {
throw new \InvalidArgumentException('Cart ID is required');
}
Expand All @@ -76,16 +94,21 @@ public function send(array $payload)

$transport = $this->transportBuilder->setTemplateIdentifier('giftcard_email_template')
->setTemplateOptions(
[
'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
'store' => $quote->getStoreId()
]
['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $quote->getStoreId()]
)
->setTemplateVars(
[
'name' => isset($payload['customer_name']) ? $payload['customer_name'] : '',
'sender_name' => 'Your Friends at ' . $storeName,
'balance' => '$' . isset($payload['amount']) ? $payload['amount'] : 0
'name' => isset($payload['customer_name']) ? $payload['customer_name'] : '',
'sender_name' => 'Your Friends at ' . $storeName,
'balance' => $this->getFormattedBalance(
isset($payload['amount']) ? $payload['amount'] : 0,
$quote->getStoreId()
),
'giftcards' => $this->getCodeHtml($payload, $quote->getStoreId()),
'is_redeemable' => $payload['giftcard_is_redeemable'],
'store' => $store,
'store_name' => $storeName,
'is_multiple_codes' => 0
]
)
->setFrom(['name' => 'Your Friends at ' . $storeName, 'email' => ''])
Expand All @@ -95,4 +118,41 @@ public function send(array $payload)

$this->logger->debug('ASYNC Handler: Sent customer notification email to: ' . $payload['customer_email']);
}

/**
* Return gift card code html
*
* @param array $payload
* @param int $storeId
* @return string
*/
private function getCodeHtml(array $payload, $storeId)
{
return $this->giftCardData->getEmailGeneratedItemsBlock()->setCodes(
[$payload['giftcard_code']]
)->setArea(
\Magento\Framework\App\Area::AREA_FRONTEND
)->setIsRedeemable(
$payload['giftcard_is_redeemable']
)->setStore(
$this->storeManager->getStore($storeId)
)->toHtml();
}

/**
* Return formatted Balance
*
* @param int|float $balance
* @param int $storeId
* @return string
* @throws \Zend_Currency_Exception
*/
private function getFormattedBalance($balance, $storeId)
{
return $this->localeCurrency->getCurrency(
$this->storeManager->getStore($storeId)->getBaseCurrencyCode()
)->toCurrency(
$balance
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Magento\SampleMessageQueue\Model\Handler\Sync;

use \Psr\Log\LoggerInterface;
use Psr\Log\LoggerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\GiftCardAccount\Model\Giftcardaccount;

Expand Down Expand Up @@ -56,7 +56,7 @@ public function __construct(

/**
* Add Gift Card Account to Shopping Card
*
*
* @param int $quoteId
* @return string
* @throws \Exception
Expand Down
9 changes: 1 addition & 8 deletions sample-module-sample-message-queue/etc/communication.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:communication/etc/communication.xsd">
<topic name="add.to.cart.giftcard.added" request="string">
<handler name="add.to.cart.giftcard.handler" type="Magento\SampleMessageQueue\Model\Handler\Async\SendCustomerNotification" method="send" />
</topic>

<topic name="add.to.cart.giftcard.added.success" request="string[]">
<handler name="add.to.cart.giftcard.added.success.handler" type="Magento\SampleMessageQueue\Model\Handler\Async\GiftCardAddedSuccess" method="log" />
</topic>

<topic name="add.to.cart.giftcard.added" request="string" />
<topic name="add.to.cart.product.added" request="string" response="string">
<handler name="add.to.cart.product.handler" type="Magento\SampleMessageQueue\Model\Handler\Sync\AddGiftCardAccount" method="add" />
</topic>
Expand Down
8 changes: 8 additions & 0 deletions sample-module-sample-message-queue/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@
<type name="Magento\Checkout\Model\Cart">
<plugin name="add.to.cart.queue" type="Magento\SampleMessageQueue\Model\AddToCartPlugin" />
</type>
<type name="Magento\Framework\Logger\Monolog">
<arguments>
<argument name="name" xsi:type="string">main</argument>
<argument name="handlers" xsi:type="array">
<item name="debug" xsi:type="object">Magento\Framework\Logger\Handler\Debug</item>
</argument>
</arguments>
</type>
</config>
10 changes: 3 additions & 7 deletions sample-module-sample-message-queue/etc/queue.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:MessageQueue/etc/queue.xsd">
<broker topic="add.to.cart.giftcard.added" type="amqp" exchange="magento">
<consumer name="async.consumer" queue="async.giftcard.added" />
<consumer name="async.consumer.for.same.queue.and.same.topic" queue="async.giftcard.added" />
</broker>
<broker topic="add.to.cart.giftcard.added.success" type="amqp" exchange="magento">
<consumer name="async.consumer.for.same.queue" queue="async.giftcard.added" />
<queue name="async.giftcard.added" handler="Magento\SampleMessageQueue\Model\Handler\Async\SendCustomerNotification::send" consumer="async.consumer" />
<queue name="async.giftcard.added.success" handler="Magento\SampleMessageQueue\Model\Handler\Async\GiftCardAddedSuccess::log" consumer="async.consumer.success" />
</broker>
<broker topic="add.to.cart.product.added" type="amqp" exchange="magento">
<consumer name="sync.consumer" queue="sync.product.added" />
<queue name="sync.product.added" consumer="sync.consumer" />
</broker>
</config>

0 comments on commit 63e2ec0

Please sign in to comment.