Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Shopware 6.5 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
lucionescu committed Sep 4, 2023
1 parent 0d92168 commit 5d927cc
Show file tree
Hide file tree
Showing 69 changed files with 4,973 additions and 165 deletions.
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lunar/plugin-shopware-6",
"description": "Secure payment with credit card via Lunar",
"version": "1.0.0",
"version": "1.1.0",
"type": "shopware-platform-plugin",
"license": "MIT",
"authors": [
Expand All @@ -11,15 +11,16 @@
}
],
"require": {
"shopware/core": "6.4.*",
"shopware/administration": "6.4.*",
"shopware/storefront": "6.4.*"
"shopware/core": "^6.4 | ^6.5",
"shopware/administration": "^6.4 | ^6.5",
"shopware/storefront": "^6.4 | ^6.5",
"paylike/php-api": "^2.0"
},
"extra": {
"shopware-plugin-class": "Lunar\\Payment\\LunarPayment",
"label": {
"de-DE": "Cards - Lunar",
"en-GB": "Cards - Lunar"
"de-DE": "Lunar Payment",
"en-GB": "Lunar Payment"
},
"description": {
"de-DE": "Secure payment with credit card via Lunar",
Expand Down
9 changes: 4 additions & 5 deletions src/Controller/OrderTransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\System\StateMachine\StateMachineRegistry;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
Expand All @@ -18,7 +17,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStateHandler;

use Lunar\Payment\lib\ApiClient;
use Paylike\Paylike as ApiClient;
use Lunar\Payment\Helpers\OrderHelper;
use Lunar\Payment\Helpers\PluginHelper;
use Lunar\Payment\Helpers\CurrencyHelper;
Expand All @@ -27,7 +26,7 @@
/**
* Responsible for handling order payment transactions
*
* @RouteScope(scopes={"api"})
* @Route(defaults={"_routeScope"={"administration"}})
*/
class OrderTransactionController extends AbstractController
{
Expand Down Expand Up @@ -143,7 +142,7 @@ private function processPaymentAction(
$actionType = ucfirst($actionType);
$actionTypeAllCaps = strtoupper($actionType);

$params = $request->request->get('params');
$params = $request->request->all()['params'];
$orderId = $params['orderId'];
$lunarTransactionId = $params['lunarTransactionId'];

Expand Down Expand Up @@ -305,7 +304,7 @@ private function getApiKey($order)
public function fetchTransactions(Request $request, Context $context): JsonResponse
{
$errors = [];
$orderId = $request->request->get('params')['orderId'];
$orderId = $request->request->all()['params']['orderId'];

try {
/**
Expand Down
131 changes: 71 additions & 60 deletions src/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,40 @@
namespace Lunar\Payment\Controller;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\System\SystemConfig\SystemConfigService;
// use Shopware\Core\System\SystemConfig\SystemConfigService;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

use Lunar\Payment\lib\ApiClient;
use Lunar\Payment\Helpers\PluginHelper;
use Paylike\Paylike as ApiClient;
use Paylike\Exception\ApiException;
// use Lunar\Payment\Helpers\PluginHelper;
use Lunar\Payment\Helpers\ValidationHelper;
use Lunar\Payment\lib\Exception\ApiException;

/**
*
* @Route(defaults={"_routeScope"={"administration"}})
*/
class SettingsController extends AbstractController
{
private const CONFIG_PATH = PluginHelper::PLUGIN_CONFIG_PATH;
// private const CONFIG_PATH = PluginHelper::PLUGIN_CONFIG_PATH;

// /** @var SystemConfigService $systemConfigService */
// private $systemConfigService;

private array $errors = [];
private array $livePublicKeys = [];
private array $testPublicKeys = [];

public function __construct(
SystemConfigService $systemConfigService
)
{
$this->systemConfigService = $systemConfigService;
}
// public function __construct(
// SystemConfigService $systemConfigService
// )
// {
// $this->systemConfigService = $systemConfigService;
// }

/**
* @RouteScope(scopes={"api"})
* @Route("/api/lunar/validate-api-keys", name="api.lunar.validate.api.keys", methods={"POST"})
*/
public function validateApiKeys(Request $request, Context $context): JsonResponse
Expand All @@ -45,8 +46,9 @@ public function validateApiKeys(Request $request, Context $context): JsonRespons
$testAppKeyName = 'testModeAppKey';
$testPublicKeyName = 'testModePublicKey';

$settingsKeys = $request->request->get('keys');
$settingsKeys = $request->request->all()['keys'] ?? [];

// validate all fields regardless of transactionMode
$this->validateLiveAppKey($settingsKeys[$liveAppKeyName] ?? '');
$this->validateLivePublicKey($settingsKeys[$livePublicKeyName] ?? '');
$this->validateTestAppKey($settingsKeys[$testAppKeyName] ?? '');
Expand Down Expand Up @@ -76,36 +78,37 @@ public function validateApiKeys(Request $request, Context $context): JsonRespons
*/
private function validateLiveAppKey($liveAppKeyValue)
{
if ($liveAppKeyValue) {
if (!$liveAppKeyValue) {
return;
}

$apiClient = new ApiClient($liveAppKeyValue);
$apiClient = new ApiClient($liveAppKeyValue);

try {
$identity = $apiClient->apps()->fetch();
try {
$identity = $apiClient->apps()->fetch();

} catch (ApiException $exception) {
$message = "The app key doesn't seem to be valid. <br>";
$message = ValidationHelper::handleExceptions($exception, $message);
} catch (ApiException $exception) {
$message = "The app key doesn't seem to be valid. <br>";
$message = ValidationHelper::handleExceptions($exception, $message);

$this->errors['liveModeAppKey'] = $message;
}
$this->errors['liveModeAppKey'] = $message;
}

try {
$merchants = $apiClient->merchants()->find($identity['id'] ?? '');
if ($merchants) {
foreach ($merchants as $merchant) {
if ( ! $merchant['test']) {
$this->livePublicKeys[] = $merchant['key'];
}
try {
$merchants = $apiClient->merchants()->find($identity['id'] ?? '');
if ($merchants) {
foreach ($merchants as $merchant) {
if ( ! $merchant['test']) {
$this->livePublicKeys[] = $merchant['key'];
}
}
} catch (ApiException $exception) {
// handle this bellow
}
} catch (ApiException $exception) {
// handle this bellow
}

if (empty($this->livePublicKeys)) {
$this->errors['liveModeAppKey'] = 'The private key is not valid or set to test mode.';
}
if (empty($this->livePublicKeys)) {
$this->errors['liveModeAppKey'] = 'The private key is not valid or set to test mode.';
}
}

Expand All @@ -114,7 +117,11 @@ private function validateLiveAppKey($liveAppKeyValue)
*/
private function validateLivePublicKey($livePublicKeyValue)
{
if ($livePublicKeyValue && !empty($this->livePublicKeys)) {
if (!$livePublicKeyValue) {
return;
}

if (!empty($this->livePublicKeys)) {
/** Check if the public key exists among the saved ones. */
if (!in_array($livePublicKeyValue, $this->livePublicKeys)) {
$this->errors['liveModePublicKey'] = 'The public key doesn\'t seem to be valid.';
Expand All @@ -127,37 +134,38 @@ private function validateLivePublicKey($livePublicKeyValue)
*/
private function validateTestAppKey($testAppKeyValue)
{
if ($testAppKeyValue) {
if (!$testAppKeyValue) {
return;
}

$apiClient = new ApiClient($testAppKeyValue);
$apiClient = new ApiClient($testAppKeyValue);

try {
$identity = $apiClient->apps()->fetch();
try {
$identity = $apiClient->apps()->fetch();

} catch (ApiException $exception) {
$message = "The test app key doesn't seem to be valid. <br>";
$message = ValidationHelper::handleExceptions($exception, $message);
} catch (ApiException $exception) {
$message = "The test app key doesn't seem to be valid. <br>";
$message = ValidationHelper::handleExceptions($exception, $message);

$this->errors['testModeAppKey'] = $message;
}
$this->errors['testModeAppKey'] = $message;
}


try {
$merchants = $apiClient->merchants()->find($identity['id'] ?? '');
if ($merchants) {
foreach ($merchants as $merchant) {
if ($merchant['test']) {
$this->testPublicKeys[] = $merchant['key'];
}
try {
$merchants = $apiClient->merchants()->find($identity['id'] ?? '');
if ($merchants) {
foreach ($merchants as $merchant) {
if ($merchant['test']) {
$this->testPublicKeys[] = $merchant['key'];
}
}
} catch (ApiException $exception) {
// handle this bellow
}
} catch (ApiException $exception) {
// handle this bellow
}

if (empty($this->testPublicKeys)) {
$this->errors['testModeAppKey'] = 'The test private key is not valid or set to live mode.';
}
if (empty($this->testPublicKeys)) {
$this->errors['testModeAppKey'] = 'The test private key is not valid or set to live mode.';
}
}

Expand All @@ -166,7 +174,11 @@ private function validateTestAppKey($testAppKeyValue)
*/
private function validateTestPublicKey($testPublicKeyValue)
{
if ($testPublicKeyValue && !empty($this->testPublicKeys)) {
if (!$testPublicKeyValue) {
return;
}

if (!empty($this->testPublicKeys)) {
/** Check if the public key exists among the saved ones. */
if (!in_array($testPublicKeyValue, $this->testPublicKeys)) {
$this->errors['testModePublicKey'] = 'The test public key doesn\'t seem to be valid.';
Expand All @@ -175,7 +187,6 @@ private function validateTestPublicKey($testPublicKeyValue)
}

/**
* @//RouteScope(scopes={"api"})
* @//Route("/api/lunar/get-setting", name="api.lunar.get.setting", methods={"GET"})
*/
// public function getSetting(Request $request, Context $context): JsonResponse
Expand Down
12 changes: 6 additions & 6 deletions src/Helpers/PluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

class PluginHelper
{
public const PLUGIN_VERSION = '1.0.0';
public const PLUGIN_VERSION = '1.1.0';

// generated with \Shopware\Core\Framework\Uuid\Uuid::randomHex()
public const PAYMENT_METHOD_UUID = '1a9bc76a3c244278a51a2e90c1e6f040';

public const VENDOR_NAME = 'lunar';

public const PLUGIN_NAME = 'LunarPayment';

public const PAYMENT_METHOD_NAME = 'Cards - Lunar';
public const PLUGIN_CODE = 'LunarPayment';
public const PAYMENT_METHOD_NAME = 'Card';

public const TRANSACTION_MODE = 'live';
public const CAPTURE_MODE = 'delayed';
public const PAYMENT_METHOD_DESCRIPTION = 'Secure payment with credit card via © Lunar';

public const ACCEPTED_CARDS = ['visa', 'visaelectron', 'mastercard', 'maestro'];

public const PLUGIN_CONFIG_PATH = self::PLUGIN_NAME . '.settings.';
public const PLUGIN_CONFIG_PATH = self::PLUGIN_CODE . '.settings.';
}
2 changes: 1 addition & 1 deletion src/Helpers/ValidationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Lunar\Payment\Helpers;

use Lunar\Payment\lib\Exception\ApiException;
use Paylike\Exception\ApiException;

/**
* Returns descriptive error messages
Expand Down
22 changes: 20 additions & 2 deletions src/LunarPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

namespace Lunar\Payment;

/**
* Load sdk from vendor folder if exists
* It can be installed also via composer without problems
*/
if (file_exists(dirname(__DIR__) . '/vendor/autoload.php')) {
$loader = require_once dirname(__DIR__) . '/vendor/autoload.php';
if ($loader !== true) {
spl_autoload_unregister([$loader, 'loadClass']);
$loader->register(false);
}
}

use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Context;
use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
Expand Down Expand Up @@ -50,9 +62,13 @@ public function install(InstallContext $context): void

$this->addPaymentMethod($context->getContext());

/** Defaults for multi-select field "supportedCards". */
/** Defaults for multi-select field "acceptedCards". */
$config = $this->container->get(SystemConfigService::class);
$config->set(PluginHelper::PLUGIN_CONFIG_PATH . 'supportedCards', PluginHelper::ACCEPTED_CARDS);
$config->set(PluginHelper::PLUGIN_CONFIG_PATH . 'acceptedCards', PluginHelper::ACCEPTED_CARDS);
$config->set(PluginHelper::PLUGIN_CONFIG_PATH . 'transactionMode', PluginHelper::TRANSACTION_MODE);
$config->set(PluginHelper::PLUGIN_CONFIG_PATH . 'captureMode', PluginHelper::CAPTURE_MODE);
$config->set(PluginHelper::PLUGIN_CONFIG_PATH . 'shopTitle', $config->get('core.basicInformation.shopName'));
$config->set(PluginHelper::PLUGIN_CONFIG_PATH . 'description', PluginHelper::PAYMENT_METHOD_DESCRIPTION);
}

/**
Expand All @@ -63,6 +79,7 @@ public function uninstall(UninstallContext $context): void
$this->setPaymentMethodIsActive(false, $context->getContext());

parent:: uninstall($context);

if ($context->keepUserData()) {
return;
}
Expand Down Expand Up @@ -122,6 +139,7 @@ private function addPaymentMethod(Context $context): void

/** @var EntityRepository $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
/** @var EntityRepository $translationRepository */
$translationRepository = $this->container->get('payment_method_translation.repository');

$paymentRepository->upsert([$paymentMethodData], $context);
Expand Down
13 changes: 13 additions & 0 deletions src/Resources/app/administration/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5d927cc

Please sign in to comment.