Skip to content

Commit

Permalink
Fixed an issue where the API of the subshop wasn't selected.
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinder committed Jun 16, 2020
1 parent 8397c28 commit aad002b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 39 deletions.
32 changes: 23 additions & 9 deletions Components/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MollieShopware\Components;

use Exception;
use MollieShopware\Components\Services\ShopService;
use Shopware\Components\Plugin\ConfigReader;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
Expand All @@ -12,16 +13,16 @@ class Config
const TRANSACTION_NUMBER_TYPE_PAYMENT_METHOD = 'payment_method';

/** @var ConfigReader */
protected $configReader;
private $configReader;

/** @var int */
protected $shopId = 1;
private $shopId = null;

/** @var array */
protected $data = null;
private $data = null;

/** @var ShopService */
protected $shopService;
private $shopService;

public function __construct(
ConfigReader $configReader,
Expand All @@ -43,11 +44,24 @@ public function __construct(
public function get($key = null, $default = null)
{
if (empty($this->data)) {
try {
$shop = $this->shopService->shopById($this->shopId);
$shop = null;

// if a shop id is given, get the shop object for the given id
if ($this->shopId !== null) {
try {
$shop = $this->shopService->shopById($this->shopId);
} catch (ServiceNotFoundException $ex) {
$shop = null;
}
}
catch(ServiceNotFoundException $ex) {
$shop = null;

// if no shop was given, or the given shop was not found, fallback to the current shop
if ($shop === null) {
try {
$shop = Shopware()->Shop();
} catch (Exception $e) {
//
}
}

// get config for shop or for main if shopid is null
Expand All @@ -71,7 +85,7 @@ public function get($key = null, $default = null)
public function setShop($shopId)
{
// Set the shop ID
$this->shopId = (int) $shopId;
$this->shopId = $shopId;

// Reset the data
$this->data = null;
Expand Down
30 changes: 11 additions & 19 deletions Components/MollieApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require_once __DIR__ . '/../Client/vendor/autoload.php';

use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\MollieApiClient;

class MollieApiFactory
Expand All @@ -27,18 +28,19 @@ public function __construct(Config $config)
/**
* Create the API client
*
* @param null $shopId
*
* @return MollieApiClient
*
* @throws \Mollie\Api\Exceptions\ApiException
* @throws ApiException
* @throws \Mollie\Api\Exceptions\IncompatiblePlatform
*/
public function create()
public function create($shopId = null)
{
$this->requireDependencies();

if (empty($this->apiClient)) {
$this->apiClient = new MollieApiClient();
$this->apiClient->setApiKey($this->config->apikey());

try {
// add platform name and version
Expand All @@ -49,31 +51,21 @@ public function create()

// add plugin name and version
$this->apiClient->addVersionString(
'MollieShopware/1.5.13'
'MollieShopware/1.5.14'
);
}
catch (\Exception $ex) {
//
}
}

return $this->apiClient;
}

/**
* Sets the api key for the sub shop.
*
* @param int $shopId
*/
public function setApiKeyForSubShop($shopId = 1)
{
// set the configuration for the shop
$this->config->setShop($shopId);

try {
$this->apiClient = $this->create();
} catch (\Exception $e) {
//
}
// set the api key based on the configuration
$this->apiClient->setApiKey($this->config->apiKey());

return $this->apiClient;
}

public function requireDependencies()
Expand Down
21 changes: 14 additions & 7 deletions Controllers/Frontend/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -1413,22 +1413,29 @@ private function getConfig() {
* @param int $shopId
*
* @return \Mollie\Api\MollieApiClient
* @throws Exception
*/
private function getMollieApi($shopId = 1)
private function getMollieApi($shopId = null)
{
/** @var MollieApiClient $apiClient */
$apiClient = null;

/** @var \MollieShopware\Components\MollieApiFactory $apiFactory */
$apiFactory = Shopware()->Container()->get('mollie_shopware.api_factory');

if ($apiFactory !== null) {
$apiFactory->setApiKeyForSubShop($shopId);

try {
return $apiFactory->create();
} catch (Exception $e) {
//
$apiClient = $apiFactory->create($shopId);
} catch (ApiException $e) {
Logger::log(
'error',
'Could not create an API client.',
$e,
true
);
}
}

return null;
return $apiClient;
}
}
8 changes: 4 additions & 4 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<label lang="de">Mollie</label>
<label lang="nl">Mollie</label>

<version>1.5.13</version>
<version>1.5.14</version>
<copyright>(c) Mollie B.V.</copyright>
<author>Kiener E-commerce</author>
<link>https://www.kiener.nl</link>

<description lang="en">Plugin to allow iDeal payments using the Mollie API (1.5.13)</description>
<description lang="nl">Plugin die iDealbetalingen via de Mollie API doorgeeft (1.5.13)</description>
<description lang="de">Plugin um Zahlungen mit der Mollie API zu ermöglichen (1.5.13)</description>
<description lang="en">Plugin to allow iDeal payments using the Mollie API (1.5.14)</description>
<description lang="nl">Plugin die iDealbetalingen via de Mollie API doorgeeft (1.5.14)</description>
<description lang="de">Plugin um Zahlungen mit der Mollie API zu ermöglichen (1.5.14)</description>

<compatibility minVersion="5.3.0"/>
</plugin>

0 comments on commit aad002b

Please sign in to comment.