diff --git a/Api/GiftCardAccountInterface.php b/Api/GiftCardAccountInterface.php new file mode 100644 index 0000000..0144cd1 --- /dev/null +++ b/Api/GiftCardAccountInterface.php @@ -0,0 +1,18 @@ +orderCoupon = $orderCoupon; + $this->giftCardAccount = $giftCardAccount; return parent::__construct( $config, $logger, @@ -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); } diff --git a/Model/Api/GiftCardAccount.php b/Model/Api/GiftCardAccount.php new file mode 100644 index 0000000..c713043 --- /dev/null +++ b/Model/Api/GiftCardAccount.php @@ -0,0 +1,113 @@ +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; + } +} diff --git a/Model/Config.php b/Model/Config.php index 337a2d8..529aa88 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -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. * @@ -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; + } } diff --git a/Model/Config/Frontend/GenerationType.php b/Model/Config/Frontend/GenerationType.php new file mode 100644 index 0000000..2682bed --- /dev/null +++ b/Model/Config/Frontend/GenerationType.php @@ -0,0 +1,46 @@ +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); + } +} diff --git a/Model/Config/Source/GenerationType.php b/Model/Config/Source/GenerationType.php new file mode 100644 index 0000000..37e13d4 --- /dev/null +++ b/Model/Config/Source/GenerationType.php @@ -0,0 +1,30 @@ + 'coupon', + 'label' => __('Coupon code') + ], + [ + 'value' => 'gift', + 'label' => __('Gift card') + ] + ]; + } +} diff --git a/composer.json b/composer.json index fa4be4b..48adc7e 100644 --- a/composer.json +++ b/composer.json @@ -17,5 +17,5 @@ "OSL-3.0", "AFL-3.0" ], - "version": "1.2.0" + "version": "1.2.1" } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 2949247..9597003 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -37,6 +37,11 @@ Magento\Config\Model\Config\Source\Yesno + + + Returnless\Connector\Model\Config\Frontend\GenerationType + Returnless\Connector\Model\Config\Source\GenerationType + diff --git a/etc/config.xml b/etc/config.xml index 0ee5ab5..d238a9c 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -6,6 +6,7 @@ 1 + coupon diff --git a/etc/di.xml b/etc/di.xml index 8f4b2c9..17885cf 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -1,6 +1,8 @@ + + diff --git a/etc/module.xml b/etc/module.xml index c5bb8a7..b0e84e1 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - +