Skip to content

Commit

Permalink
Created version 1.5.13 of the Mollie plugin for Shopware 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinder committed Jun 15, 2020
1 parent 7335fb6 commit 8397c28
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 23 deletions.
32 changes: 29 additions & 3 deletions Components/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,34 @@

namespace MollieShopware\Components;

use MollieShopware\Components\Services\ShopService;
use Shopware\Components\Plugin\ConfigReader;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

class Config
{
const TRANSACTION_NUMBER_TYPE_MOLLIE = 'mollie';
const TRANSACTION_NUMBER_TYPE_PAYMENT_METHOD = 'payment_method';

/** @var \Shopware\Components\Plugin\ConfigReader */
/** @var ConfigReader */
protected $configReader;

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

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

public function __construct(\Shopware\Components\Plugin\ConfigReader $configReader)
/** @var ShopService */
protected $shopService;

public function __construct(
ConfigReader $configReader,
ShopService $shopService
)
{
$this->configReader = $configReader;
$this->shopService = $shopService;
}

/**
Expand All @@ -32,7 +44,7 @@ public function get($key = null, $default = null)
{
if (empty($this->data)) {
try {
$shop = Shopware()->Shop();
$shop = $this->shopService->shopById($this->shopId);
}
catch(ServiceNotFoundException $ex) {
$shop = null;
Expand All @@ -51,6 +63,20 @@ public function get($key = null, $default = null)
return $this->data;
}

/**
* Sets the current shop.
*
* @param $shopId
*/
public function setShop($shopId)
{
// Set the shop ID
$this->shopId = (int) $shopId;

// Reset the data
$this->data = null;
}

/**
* Get the API key
*
Expand Down
18 changes: 17 additions & 1 deletion Components/MollieApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function create()

// add plugin name and version
$this->apiClient->addVersionString(
'MollieShopware/1.5.12'
'MollieShopware/1.5.13'
);
}
catch (\Exception $ex) {
Expand All @@ -60,6 +60,22 @@ public function create()
return $this->apiClient;
}

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

try {
$this->apiClient = $this->create();
} catch (\Exception $e) {
//
}
}

public function requireDependencies()
{
// Load composer libraries
Expand Down
24 changes: 24 additions & 0 deletions Components/Services/PaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,24 @@ public function startOrderTransaction(
return $checkoutUrl;
}

/**
* Set the correct API key for the subshop.
*
* @param \Shopware\Models\Order\Order $order
*/
public function setApiKeyForSubShop(\Shopware\Models\Order\Order $order)
{
// Use the order's shop in the in the config service
$this->config->setShop($order->getShop()->getId());

// Set the corresponding API key in the client
try {
$this->apiClient->setApiKey($this->config->apiKey());
} catch (\Exception $e) {
//
}
}

/**
* Get the Mollie order object
*
Expand All @@ -312,6 +330,9 @@ public function getMollieOrder(\Shopware\Models\Order\Order $order)
/** @var \MollieShopware\Models\Transaction $transaction */
$transaction = $transactionRepo->getMostRecentTransactionForOrder($order);

// Set the correct API key for the order's shop
$this->setApiKeyForSubShop($order);

/** @var \Mollie\Api\Resources\Order $mollieOrder */
$mollieOrder = $this->apiClient->orders->get(
$transaction->getMollieId(),
Expand Down Expand Up @@ -345,6 +366,9 @@ public function getMolliePayment(\Shopware\Models\Order\Order $order, $paymentId
if (empty($paymentId))
$paymentId = $transaction->getMolliePaymentId();

// Set the correct API key for the order's shop
$this->setApiKeyForSubShop($order);

/** @var \Mollie\Api\Resources\Payment $molliePayment */
$molliePayment = $this->apiClient->payments->get(
$paymentId
Expand Down
42 changes: 42 additions & 0 deletions Components/Services/ShopService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace MollieShopware\Components\Services;

use Shopware\Components\Model\ModelManager;
use Shopware\Models\Shop\Repository as ShopRepository;
use Shopware\Models\Shop\Shop;

class ShopService
{
/** @var ModelManager */
private $modelManager;

/**
* Creates a new instance of the shop helper.
*
* @param ModelManager $modelManager
*/
public function __construct(
ModelManager $modelManager
)
{
$this->modelManager = $modelManager;
}

/**
* Returns a shop by it's id.
*
* @param int $id
* @return Shop|object|null
*/
public function shopById($id)
{
/** @var ShopRepository $shopRepo */
$shopRepo = $this->modelManager->getRepository(Shop::class);

/** @var Shop $shop */
return $shopRepo->findOneBy([
'id' => $id
]);
}
}
40 changes: 40 additions & 0 deletions Controllers/Backend/MollieOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,46 @@ public function refundAction()
}
}

public function shippableAction()
{
$shippable = false;

try {
/** @var \Enlight_Controller_Request_Request $request */
$request = $this->Request();

/** @var \Shopware\Components\Model\ModelManager $modelManager */
$this->modelManager = $this->container->get('models');

/** @var \MollieShopware\Components\Config $config */
$this->config = $this->container->get('mollie_shopware.config');

/** @var \Mollie\Api\MollieApiClient $apiClient */
$this->apiClient = $this->container->get('mollie_shopware.api');

/** @var \MollieShopware\Components\Services\OrderService $orderService */
$this->orderService = $this->container->get('mollie_shopware.order_service');

/** @var \Shopware\Models\Order\Order $order */
$order = $this->orderService->getOrderById(
$request->getParam('orderId')
);

if (
$order !== null
&& (string) $this->orderService->getMollieOrderId($order) !== ''
) {
$shippable = true;
}
} catch (Exception $e) {
//
}

$this->returnJson([
'shippable' => $shippable,
]);
}

/**
* Refund a Mollie order
*
Expand Down
26 changes: 20 additions & 6 deletions Controllers/Frontend/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ private function updateTransactionId(Order $order, $transaction = null)

if ($transaction !== null) {
$config = $this->getConfig();
$mollieApi = $this->getMollieApi();
$mollieApi = $this->getMollieApi($order->getShop()->getId());
$molliePayment = null;
$paymentTransactionNumber = null;
$transactionNumber = null;
Expand Down Expand Up @@ -696,7 +696,7 @@ private function getOrderFromTransaction($transactionNumber, $mayCreateOrder = t
$createOrder = false;

/** @var \Mollie\Api\MollieApiClient $mollieApi */
$mollieApi = $this->container->get('mollie_shopware.api');
$mollieApi = $this->getMollieApi();

if ($mollieApi !== null) {
if ((string) $transaction->getMollieId() !== '') {
Expand Down Expand Up @@ -922,7 +922,7 @@ private function updateMollieOrderNumber(
{
if ((string) $transaction->getMollieId() !== null) {
/** @var \Mollie\Api\MollieApiClient $mollieApi */
$mollieApi = $this->container->get('mollie_shopware.api');
$mollieApi = $this->getMollieApi();

try {
/** @var \Mollie\Api\Resources\Order $mollieOrder */
Expand Down Expand Up @@ -1311,7 +1311,6 @@ private function getOrderCanceledOrFailed($transaction)
{
/** @var \Mollie\Api\MollieApiClient $mollieApi */
$mollieApi = $this->getMollieApi();

$mollieOrder = null;
$molliePayment = null;

Expand Down Expand Up @@ -1411,10 +1410,25 @@ private function getConfig() {
}

/**
* @param int $shopId
*
* @return \Mollie\Api\MollieApiClient
*/
private function getMollieApi()
private function getMollieApi($shopId = 1)
{
return Shopware()->Container()->get('mollie_shopware.api');
/** @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) {
//
}
}

return null;
}
}
6 changes: 5 additions & 1 deletion MollieShopware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MollieShopware\Components\Config;
use MollieShopware\Components\MollieApiFactory;
use MollieShopware\Components\Services\PaymentMethodService;
use MollieShopware\Components\Services\ShopService;
use MollieShopware\Models\TransactionItem;
use MollieShopware\Models\Transaction;
use MollieShopware\Models\OrderLines;
Expand Down Expand Up @@ -380,6 +381,9 @@ protected function getMollieApiClient()
/** @var Plugin\ConfigReader $configReader */
$configReader = $this->container->get('shopware.plugin.cached_config_reader');

/** @var ShopService $shopService */
$shopService = new ShopService($this->container->get('models'));

/** @var MollieApiFactory $factory */
$factory = null;

Expand All @@ -388,7 +392,7 @@ protected function getMollieApiClient()

// Get the config
if ($configReader !== null) {
$config = new Config($configReader);
$config = new Config($configReader, $shopService);
}

// Get the Mollie API factory service
Expand Down
5 changes: 5 additions & 0 deletions Resources/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@
<argument type="service" id="models" />
</service>

<service id="mollie_shopware.shop_service" class="MollieShopware\Components\Services\ShopService">
<argument type="service" id="models" />
</service>

<service id="mollie_shopware.attributes" class="MollieShopware\Components\Attributes">
<argument type="service" id="models" />
<argument type="service" id="shopware_attribute.crud_service" />
</service>

<service id="mollie_shopware.config" class="MollieShopware\Components\Config">
<argument type="service" id="shopware.plugin.cached_config_reader" />
<argument type="service" id="mollie_shopware.shop_service" />
</service>

<service id="mollie_shopware.api_factory" class="MollieShopware\Components\MollieApiFactory">
Expand Down
19 changes: 19 additions & 0 deletions Resources/views/backend/mollie_extend_order/controller/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,31 @@ Ext.define('Shopware.apps.Mollie.controller.List', {
'order-list-main-window order-list': {
refundOrder: me.onRefundOrder,
shipOrder: me.onShipOrder,
shippable: me.isShippable,
}
});

me.callParent(arguments);
},

isShippable: function(record) {
Ext.Ajax.request({
url: '{url action="shippable" controller=MollieOrders}',
params: {
orderId: record.get('id'),
orderNumber: record.get('number')
},
success: function (res) {
try {
var result = JSON.parse(res.responseText);
return result.shippable;
} catch (e) {
//
}
}
});
},

onShipOrder: function(record) {
var me = this;
var store = me.subApplication.getStore('Order');
Expand Down
Loading

0 comments on commit 8397c28

Please sign in to comment.