Skip to content

Commit

Permalink
Merge pull request #4 from Returnless-com/staging_giftcard_ee
Browse files Browse the repository at this point in the history
Gift cards instead of coupons for Adobe Commerce users
  • Loading branch information
sedonik authored Apr 22, 2022
2 parents 87809a7 + 8af7eaa commit 9ffee65
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 8 deletions.
18 changes: 18 additions & 0 deletions Api/GiftCardAccountInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Returnless\Connector\Api;

/**
* Interface GiftCardAccountInterface
*/
interface GiftCardAccountInterface
{
/**
* Creates Coupon By the Request Params
*
* @api
* @param $requestParams
* @return mixed
*/
public function createGiftCardAccount($requestParams);
}
24 changes: 18 additions & 6 deletions Controller/Order/Coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Returnless\Connector\Controller\Order;

use Magento\Framework\App\Action\Context;
use Returnless\Connector\Model\Api\OrderCoupon;
use Returnless\Connector\Api\OrderCouponInterface;
use Returnless\Connector\Api\GiftCardAccountInterface;
use Returnless\Connector\Model\Config;
use Psr\Log\LoggerInterface;
use Magento\Framework\Controller\Result\JsonFactory;
Expand All @@ -15,26 +16,33 @@
class Coupon extends AbstractController implements HttpPostActionInterface
{
/**
* @var OrderCoupon
* @var OrderCouponInterface
*/
protected $orderCoupon;

/**
* Coupon constructor.
* @param OrderCoupon $orderCoupon
* @var GiftCardAccountInterface
*/
private $giftCardAccount;

/**
* @param OrderCouponInterface $orderCoupon
* @param GiftCardAccountInterface $giftCardAccount
* @param Config $config
* @param LoggerInterface $logger
* @param JsonFactory $resultJsonFactory
* @param Context $context
*/
public function __construct(
OrderCoupon $orderCoupon,
OrderCouponInterface $orderCoupon,
GiftCardAccountInterface $giftCardAccount,
Config $config,
LoggerInterface $logger,
JsonFactory $resultJsonFactory,
Context $context
) {
$this->orderCoupon = $orderCoupon;
$this->giftCardAccount = $giftCardAccount;
return parent::__construct(
$config,
$logger,
Expand All @@ -53,7 +61,11 @@ public function execute()
// validate if Service is enabled
if ($this->checkEnabled()) {
if ($this->checkSignature($requestData['return_id'])) {
$response = $this->orderCoupon->createCouponReturnless($requestData);
if ($this->config->getGenerationType() == 'coupon') {
$response = $this->orderCoupon->createCouponReturnless($requestData);
} else {
$response = $this->giftCardAccount->createGiftCardAccount($requestData);
}
// set Response
$this->setResponse("Success!", 200, false, $response);
}
Expand Down
113 changes: 113 additions & 0 deletions Model/Api/GiftCardAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Returnless\Connector\Model\Api;

use Returnless\Connector\Api\GiftCardAccountInterface;
use Magento\Framework\Module\ResourceInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\GiftCardAccount\Model\Giftcardaccount as GiftCardModel;
use Magento\GiftCardAccount\Model\Pool;
use Psr\Log\LoggerInterface;
use Magento\Sales\Model\Order;
use Magento\Store\Model\StoreManagerInterface;

/**
* Interface OrderCoupon
*/
class GiftCardAccount implements GiftCardAccountInterface
{
/**
* const NAMESPACE_MODULE
*/
const NAMESPACE_MODULE = 'Returnless_Connector';

/**
* @var ResourceInterface
*/
private $moduleResource;

/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var Order
*/
private $orderModel;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param ResourceInterface $moduleResource
* @param ObjectManagerInterface $objectManager
* @param LoggerInterface $logger
* @param Order $orderModel
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ResourceInterface $moduleResource,
ObjectManagerInterface $objectManager,
LoggerInterface $logger,
Order $orderModel,
StoreManagerInterface $storeManager
) {
$this->moduleResource = $moduleResource;
$this->objectManager = $objectManager;
$this->logger = $logger;
$this->orderModel = $orderModel;
$this->storeManager = $storeManager;
}

/**
* @param $requestParams
* @return array
*/
public function createGiftCardAccount($requestParams)
{
$response['installed_module_version'] = $this->moduleResource->getDbVersion(self::NAMESPACE_MODULE);
$codePool = $this->objectManager->create(Pool::class);
$codes = $codePool->getCollection()->addFieldToFilter('status', Pool::STATUS_FREE)->getSize();
if (!$codes) {
$codePool->generatePool();
}
$model = $this->objectManager->create(GiftCardModel::class);
$order = $this->orderModel->loadByIncrementId($requestParams['order_id']);
if ($order->getId()) {
try {
$websiteId = $this->storeManager->getStore($order->getStoreId())->getWebsiteId();
} catch (\Exception $e) {
$websiteId = $this->storeManager->getDefaultStoreView()->getWebsiteId();
$this->logger->error($e->getMessage());
}
} else {
$websiteId = $this->storeManager->getDefaultStoreView()->getWebsiteId();
}
$data = [
'status' => 1,
'is_redeemable' => 1,
'website_id' => $websiteId,
'balance' => $requestParams['coupon_amount'],
];
$model->addData($data);

try {
$model->save();
if ($model->getId()) {
$response['result']['coupon_id'] = $model->getId();
$response['result']['coupon_code'] = $model->getCode();
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
return $response;
}
}
20 changes: 20 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class Config
*/
const CONFIG_MARKETPLACE_SEARCH_VENDOR_ID = 'returnless_connector/marketplace_orders/integration_partner';

/**
* const coupon or gift card account (for Magento Enterprise only)
*/
const CONFIG_GENERATION_TYPE = 'returnless_connector/general/generation_type';

/**
* Config constructor.
*
Expand Down Expand Up @@ -167,4 +172,19 @@ public function getBrandAttributeCode($store = null)

return $brandttributeCode;
}

/**
* @param $store
* @return string
*/
public function getGenerationType($store = null)
{
$generationType = (string)$this->scopeConfig->getValue(
self::CONFIG_GENERATION_TYPE,
ScopeInterface::SCOPE_STORE,
$store
);

return $generationType;
}
}
46 changes: 46 additions & 0 deletions Model/Config/Frontend/GenerationType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Returnless\Connector\Model\Config\Frontend;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Backend\Block\Template\Context;

/**
* Class GenerationType
*/
class GenerationType extends Field
{
protected $productMetadata;

public function __construct(
ProductMetadataInterface $productMetadata,
Context $context,
array $data = []
) {
$this->productMetadata = $productMetadata;
parent::__construct($context, $data);
}

/**
* Render connection button considering request parameter
*
* @param AbstractElement $element
* @return string
*/
public function render(AbstractElement $element)
{
if (strtolower($this->productMetadata->getEdition()) == 'enterprise' ||
strtolower($this->productMetadata->getEdition()) == 'b2b') {
return parent::render($element);
}
//$isConnected = $this->_scopeConfig->isSetFlag(Config::XML_PATH_QBONLINE_IS_CONNECTED);
//if ($isConnected) {
//$element->setDisabled(true);
//}


return $this->productMetadata->getVersion() . ' ' . $this->productMetadata->getEdition();//parent::render($element);
}
}
30 changes: 30 additions & 0 deletions Model/Config/Source/GenerationType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Returnless\Connector\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

/**
* Class GenerationType
* @package Returnless\Connector\Model\Config\Source
*/
class GenerationType implements OptionSourceInterface
{
/**
* @return array
*/
public function toOptionArray()
{
return
[
[
'value' => 'coupon',
'label' => __('Coupon code')
],
[
'value' => 'gift',
'label' => __('Gift card')
]
];
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"OSL-3.0",
"AFL-3.0"
],
"version": "1.2.0"
"version": "1.2.1"
}
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[Separation of bundles will be enabled.]]></comment>
</field>
<field id="generation_type" translate="label" type="select" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Generation type</label>
<frontend_model>Returnless\Connector\Model\Config\Frontend\GenerationType</frontend_model>
<source_model>Returnless\Connector\Model\Config\Source\GenerationType</source_model>
</field>
</group>
<group id="marketplace_orders" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Marketplace Orders</label>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<general>
<enabled>1</enabled>
<integration_api_password backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
<generation_type>coupon</generation_type>
</general>
</returnless_connector>
</default>
Expand Down
2 changes: 2 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Returnless\Connector\Api\OrderInfoInterface" type="Returnless\Connector\Model\Api\OrderInfo" />
<preference for="Returnless\Connector\Api\OrderCouponInterface" type="Returnless\Connector\Model\Api\OrderCoupon" />
<preference for="Returnless\Connector\Api\GiftCardAccountInterface" type="Returnless\Connector\Model\Api\GiftCardAccount" />
<type name="Returnless\Connector\Model\PartnersSourceAdapter">
<arguments>
<argument name="partnersResource" xsi:type="array">
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Returnless_Connector" setup_version="1.2.0">
<module name="Returnless_Connector" setup_version="1.2.1">
<sequence>
<module name="Magento_Sales"/>
</sequence>
Expand Down

0 comments on commit 9ffee65

Please sign in to comment.