Skip to content

Commit

Permalink
Merge branch 'dev/1.5.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinder committed Feb 13, 2020
2 parents 030a81a + 18a4ad4 commit bbf24d9
Show file tree
Hide file tree
Showing 20 changed files with 1,030 additions and 377 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
logs
.idea
releases
releases
/*.sh
/scoper.inc.php
/UpdateVersionNumber.php
/composer.json
125 changes: 125 additions & 0 deletions Command/KlarnaShippingCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace MollieShopware\Command;

use Mollie\Api\MollieApiClient;
use MollieShopware\Components\Config;
use MollieShopware\Models\Transaction;
use MollieShopware\Models\TransactionRepository;
use Shopware\Commands\ShopwareCommand;
use Shopware\Components\Model\ModelManager;
use Shopware\Models\Order\Order;
use Shopware\Models\Order\Repository;
use Shopware\Models\Order\Status;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class KlarnaShippingCommand extends ShopwareCommand
{
/** @var Config */
private $config;

/** @var ModelManager */
private $modelManager;

/** @var MollieApiClient */
private $apiClient;

public function __construct(
Config $config,
ModelManager $modelManager,
MollieApiClient $apiClient,
$name = null
)
{
parent::__construct($name);

$this->config = $config;
$this->modelManager = $modelManager;
$this->apiClient = $apiClient;
}

public function configure()
{
$this
->setName('mollie:ship:klarna')
->setDescription('Ship completed Klarna orders');
}

public function execute(InputInterface $input, OutputInterface $output)
{
/** @var Transaction[] $transactions */
$transactions = null;

/** @var TransactionRepository $transactionRepository */
$transactionRepository = $this->modelManager
->getRepository(Transaction::class);

if ($transactionRepository !== null) {
$transactions = $transactionRepository->findBy([
'isShipped' => false,
'paymentMethod' => 'mollie_klarna'
]);
}

if (
$transactions !== null
&& is_array($transactions)
) {
$output->writeln(count($transactions) . ' orders to update.');

foreach ($transactions as $transaction) {
$output->writeln('Updating order ' . $transaction->getOrderNumber());

/** @var Order $order */
$order = null;

if ($transaction->getOrderId() === null) {
continue;
}

/** @var Repository $orderRepository */
$orderRepository = $this->modelManager->getRepository(Order::class);

if ($orderRepository === null) {
continue;
}

/** @var Order $order */
$order = $orderRepository->find($transaction->getOrderId());

if ($order === null) {
continue;
}

// Ship order
try {
$mollieOrder = $this->apiClient->orders->get($transaction->getMollieId());

if (
$mollieOrder !== null
&& $order->getOrderStatus()->getId() === $this->config->getKlarnaShipOnStatus()
&& !in_array($order->getPaymentStatus()->getId(), [
Status::PAYMENT_STATE_OPEN,
Status::PAYMENT_STATE_THE_PROCESS_HAS_BEEN_CANCELLED
], true)
) {
$mollieOrder->shipAll();
$transaction->setIsShipped(true);
}
} catch (\Exception $e) {
//
}

// Save order
try {
$transactionRepository->save($transaction);
} catch (\Exception $e) {
//
}
}

$output->writeln('Done.');
}
}
}
19 changes: 11 additions & 8 deletions Components/Base/AbstractPaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,24 +176,27 @@ protected function getUserId()
*/
protected function getPaymentId()
{
$paymentId = null;
$user = $this->getUser();
$userId = $this->getUserId();

if (!empty($user['additional']['payment']['id'])) {
return $user['additional']['payment']['id'];
$paymentId = $user['additional']['payment']['id'];
}

if (!empty($user['additional']['user']['paymentID'])) {
return $user['additional']['user']['paymentID'];
$paymentId = $user['additional']['user']['paymentID'];
}

$userId = $this->getUserId();

if (empty($userId)) {
return null;
if ($paymentId === null && $userId !== null) {
$connection = $this->container->get('models')->getConnection();
$paymentId = $connection->fetchColumn('SELECT paymentID FROM s_user WHERE id = :userId', ['userId' => $userId]);
}

$connection = $this->container->get('models')->getConnection();
return $connection->fetchColumn('SELECT paymentID FROM s_user WHERE id = :userId', [ 'userId' => $userId ]);
$user['additional']['payment']['id'] = $paymentId;
$user['additional']['user']['paymentID'] = $paymentId;

return $paymentId;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions Components/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

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

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

Expand Down Expand Up @@ -104,6 +107,14 @@ public function useOrdersApiOnlyWhereMandatory()
return ($this->get('orders_api_only_where_mandatory', 'yes') == 'yes');
}

/**
* @return string
*/
public function getTransactionNumberType()
{
return (string) $this->get('transaction_number_type', self::TRANSACTION_NUMBER_TYPE_MOLLIE);
}

/**
* @return int
*/
Expand Down Expand Up @@ -144,13 +155,22 @@ public function cancelFailedOrders()
return ($this->get('auto_cancel_failed_orders', 'yes') === 'yes');
}

/**
* @return int
*/
public function getKlarnaShipOnStatus()
{
return (int) $this->get('klarna_ship_on_status', \Shopware\Models\Order\Status::ORDER_STATE_COMPLETELY_DELIVERED);
}

/**
* @return int
*/
public function getShippedStatus()
{
return (int) $this->get('klarna_shipped_status', -1);
}

/**
* @return string
*/
Expand Down
76 changes: 76 additions & 0 deletions Components/Helpers/LogHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace MollieShopware\Components\Helpers;

use Psr\Container\ContainerInterface;
use Shopware\Components\Logger;

class LogHelper
{
const LOG_CRITICAL = 'critical';
const LOG_DEBUG = 'debug';
const LOG_ERROR = 'error';
const LOG_INFO = 'info';
const LOG_WARNING = 'warning';

/** @var ContainerInterface */
private static $container;

/** @var Logger */
private static $pluginLogger;

/**
* Creates a new instance of the log helper.
*
* @param ContainerInterface $container
*/
public function __construct(
ContainerInterface $container
)
{
static::$container = $container;
}

/**
* Logs a message to the plugin logger.
*
* @param string $message
* @param string $logMethod
* @param null $exception
* @return bool
*/
public static function logMessage($message, $logMethod = self::LOG_ERROR, $exception = null)
{
// Check if the log method exists
if (!method_exists(static::pluginLogger(), $logMethod)) {
$logMethod = self::LOG_DEBUG;

// Throw a warning that the log level is unknown
trigger_error(
'Using an unknown log level, fallback to debug',
E_USER_WARNING
);
}

return static::pluginLogger()->$logMethod($message, [$exception]);
}

/**
* Returns the plugin logger.
*
* @param ContainerInterface|null $container
* @return Logger
*/
private static function pluginLogger(ContainerInterface $container = null)
{
// Set the container
static::$container = ($container === null ? static::$container : $container);

// Get the plugin logger from the container
if (static::$pluginLogger === null && static::$container !== null) {
static::$pluginLogger = static::$container->get('pluginlogger');
}

return static::$pluginLogger;
}
}
34 changes: 33 additions & 1 deletion Components/MollieApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MollieShopware\Components;

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

use Mollie\Api\MollieApiClient;

class MollieApiFactory
Expand Down Expand Up @@ -32,6 +34,8 @@ public function __construct(Config $config)
*/
public function create()
{
$this->requireDependencies();

if (empty($this->apiClient)) {
$this->apiClient = new MollieApiClient();
$this->apiClient->setApiKey($this->config->apikey());
Expand All @@ -45,7 +49,7 @@ public function create()

// add plugin name and version
$this->apiClient->addVersionString(
'MollieShopware/1.5.5'
'MollieShopware/1.5.6'
);
}
catch (\Exception $ex) {
Expand All @@ -55,4 +59,32 @@ public function create()

return $this->apiClient;
}

public function requireDependencies()
{
// Load composer libraries
if (file_exists(__DIR__ . '/../Client/vendor/scoper-autoload.php')) {
require_once __DIR__ . '/../Client/vendor/scoper-autoload.php';
}

// Load guzzle functions
if (file_exists(__DIR__ . '/../Client/vendor/guzzlehttp/guzzle/src/functions_include.php')) {
require_once __DIR__ . '/../Client/vendor/guzzlehttp/guzzle/src/functions_include.php';
}

// Load promises functions
if (file_exists(__DIR__ . '/../Client/vendor/guzzlehttp/promises/src/functions_include.php')) {
require_once __DIR__ . '/../Client/vendor/guzzlehttp/promises/src/functions_include.php';
}

// Load psr7 functions
if (file_exists(__DIR__ . '/../Client/vendor/guzzlehttp/psr7/src/functions_include.php')) {
require_once __DIR__ . '/../Client/vendor/guzzlehttp/psr7/src/functions_include.php';
}

// Load client
if (file_exists(__DIR__ . '/../Client/vendor/mollie/mollie-api-php/src/MollieApiClient.php')) {
require_once __DIR__ . '/../Client/vendor/mollie/mollie-api-php/src/MollieApiClient.php';
}
}
}
5 changes: 3 additions & 2 deletions Components/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ class Notifier
* @param $error
* @throws \Exception
*/
public static function notifyException($error) {
public static function notifyException($error, $exception = null) {
// log the error
Logger::log(
'error',
$error
$error,
$exception
);

// return the error json
Expand Down
2 changes: 1 addition & 1 deletion Components/Services/BasketService.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function restoreBasket($orderId)

// save order
$this->modelManager->persist($order);
$this->modelManager->flush($order);
$this->modelManager->flush();
}
}

Expand Down
2 changes: 1 addition & 1 deletion Components/Services/IdealService.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function setSelectedIssuer($issuer)
$attributes->setMollieShopwareIdealIssuer($issuer);

$this->modelManager->persist($attributes);
$this->modelManager->flush();
$this->modelManager->flush($attributes);

return $issuer;
}
Expand Down
Loading

0 comments on commit bbf24d9

Please sign in to comment.