Skip to content

Commit

Permalink
#32830 use paymentMethod ID instead of Name
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan-Medv committed Feb 17, 2023
1 parent f8f9364 commit 269fc7d
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 160 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.2.0
- Klarna and Oney payment methods

# 1.1.0
- Tokenization

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mediaopt/worldline",
"description": "Worldline Online Payments",
"version": "1.1.0",
"version": "1.2.0",
"type": "shopware-platform-plugin",
"license": "proprietary",
"config": {
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/WorldlineSDKAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private function setCustomProperties(
$redirectPaymentMethodSpecificInput = new RedirectPaymentMethodSpecificInput();
$redirectPaymentMethodSpecificInput->setPaymentProductId($worldlinePaymentProductId);
$redirectPaymentMethodSpecificInput->setRequiresApproval(true);
$redirectPaymentMethodSpecificInput->setPaymentOption('W3999'); //todo
$redirectPaymentMethodSpecificInput->setPaymentOption($this->getPluginConfig(Form::ONEY_PAYMENT_OPTION_FIELD));
$hostedCheckoutRequest->setRedirectPaymentMethodSpecificInput($redirectPaymentMethodSpecificInput);
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/Bootstrap/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Form
const WEBHOOK_SECRET_FIELD = 'MoptWorldline.config.webhookSecret';
const LOG_LEVEL = 'MoptWorldline.config.logLevel';
const IFRAME_TEMPLATE_NAME = 'MoptWorldline.config.iframeTemplateName';
const ONEY_PAYMENT_OPTION_FIELD = 'MoptWorldline.config.oneyPaymentOption';
const FULL_REDIRECT_TEMPLATE_NAME = 'MoptWorldline.config.fullRedirectTemplateName';
const AUTO_CAPTURE = 'MoptWorldline.config.autoCapture';
const AUTO_CAPTURE_DISABLED = 'disabled';
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Api/ApiTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function testConnection(Request $request, Context $context): JsonResponse
$message = '';
try {
$paymentMethodController = $this->getPaymentMethodController();
$paymentMethods = $paymentMethodController->getPaymentMentodsList(
$paymentMethods = $paymentMethodController->getPaymentMethodsList(
$credentials,
$salesChannelId,
$countryIso3,
Expand Down
154 changes: 42 additions & 112 deletions src/Controller/PaymentMethod/PaymentMethodController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use MoptWorldline\Bootstrap\Form;
use MoptWorldline\MoptWorldline;
use MoptWorldline\Service\Payment;
use MoptWorldline\Service\PaymentMethodHelper;
use OnlinePayments\Sdk\Domain\GetPaymentProductsResponse;
use OnlinePayments\Sdk\Domain\PaymentProduct;
use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
Expand Down Expand Up @@ -73,7 +74,12 @@ public function saveMethod(Request $request, Context $context, ?string $countryI
foreach ($data as $paymentMethod) {
if (!empty($paymentMethod['internalId'])) {
//Activate/deactivate method, that already exist
$this->processPaymentMethod($paymentMethod);
PaymentMethodHelper::setDBPaymentMethodStatus(
$this->paymentMethodRepository,
$paymentMethod['status'],
$context,
$paymentMethod['internalId']
);
continue;
}

Expand All @@ -83,18 +89,33 @@ public function saveMethod(Request $request, Context $context, ?string $countryI
}

if (empty($toCreate)) {
return $this->response(true, '', []);
return $this->response();
}
$adapter = new WorldlineSDKAdapter($this->systemConfigService, $this->logger, $salesChannelId);
$adapter->getMerchantClient();
$paymentProducts = $adapter->getPaymentProducts($countryIso3, $currencyIsoCode);
foreach ($paymentProducts->getPaymentProducts() as $product) {
$name = 'Worldline ' . $product->getDisplayHints()->getLabel();
if (in_array($product->getId(), $toCreate)) {
$this->createPaymentMethod($product, $context, $salesChannelId);
$method = [
'id' => $product->getId(),
'name' => $name,
'description' => '',
'active' => true,
];
PaymentMethodHelper::addPaymentMethod(
$this->paymentMethodRepository,
$this->salesChannelPaymentRepository,
$this->pluginIdProvider,
$context,
$method,
$salesChannelId,
null
);
}
}

return $this->response(true, '', []);
return $this->response();
}

/**
Expand All @@ -105,29 +126,24 @@ public function saveMethod(Request $request, Context $context, ?string $countryI
* @return array
* @throws \Exception
*/
public function getPaymentMentodsList(
public function getPaymentMethodsList(
array $credentials,
?string $salesChannelId,
?string $countryIso3,
?string $currencyIsoCode
)
): array
{
$fullRedirectMethod = $this->getPaymentMethod(MoptWorldline::FULL_REDIRECT_PAYMENT_METHOD_NAME);
$toFrontend[] = [
'id' => 0,
'logo' => '',
'label' => 'Worldline full redirect',
'isActive' => $fullRedirectMethod['isActive'],
'internalId' => $fullRedirectMethod['internalId']
];
$iframeMethod = $this->getPaymentMethod(MoptWorldline::IFRAME_PAYMENT_METHOD_NAME);
$toFrontend[] = [
'id' => '0_iframe',
'logo' => '',
'label' => MoptWorldline::IFRAME_PAYMENT_METHOD_NAME,
'isActive' => $iframeMethod['isActive'],
'internalId' => $iframeMethod['internalId']
];
$toFrontend = [];
foreach (Payment::METHODS_LIST as $method) {
$dbMethod = PaymentMethodHelper::getPaymentMethod($this->paymentMethodRepository, $method['id']);
$toFrontend[] = [
'id' => $method['id'],
'logo' => '',
'label' => $dbMethod['label'],
'isActive' => $dbMethod['isActive'],
'internalId' => $dbMethod['internalId']
];
}

$adapter = new WorldlineSDKAdapter($this->systemConfigService, $this->logger, $salesChannelId);
$adapter->getMerchantClient($credentials);
Expand All @@ -138,113 +154,27 @@ public function getPaymentMentodsList(
}

$paymentProducts = $adapter->getPaymentProducts($countryIso3, $currencyIsoCode);

foreach ($paymentProducts->getPaymentProducts() as $product) {
$name = $this->getPaymentMethodName($product->getDisplayHints()->getLabel());
$createdPaymentMethod = $this->getPaymentMethod($name);
$createdPaymentMethod = PaymentMethodHelper::getPaymentMethod($this->paymentMethodRepository, $product->getId());

$toFrontend[] = [
'id' => $product->getId(),
'logo' => $product->getDisplayHints()->getLogo(),
'label' => $product->getDisplayHints()->getLabel(),
'label' => $createdPaymentMethod['label'],
'isActive' => $createdPaymentMethod['isActive'],
'internalId' => $createdPaymentMethod['internalId']
];
};
return $toFrontend;
}

/**
* @param PaymentProduct $product
* @param Context $context
* @param string $salesChannelId
* @return void
*/
private function createPaymentMethod(PaymentProduct $product, Context $context, string $salesChannelId)
{
$pluginId = $this->pluginIdProvider->getPluginIdByBaseClass(MoptWorldline::class, $context);

$name = $this->getPaymentMethodName($product->getDisplayHints()->getLabel());
$paymentMethodId = Uuid::randomHex();
$paymentData = [
'id' => $paymentMethodId,
'handlerIdentifier' => Payment::class,
'name' => $name,
'pluginId' => $pluginId,
'active' => true,
'afterOrderEnabled' => true,
'customFields' => [
Form::CUSTOM_FIELD_WORLDLINE_PAYMENT_METHOD_ID => $product->getId()
]
];

$salesChannelPaymentData = [
'salesChannelId' => $salesChannelId,
'paymentMethodId' => $paymentMethodId
];

$dbPaymentMethod =$this->getPaymentMethod($name);

if (empty($dbPaymentMethod['internalId'])) {
$this->paymentMethodRepository->create([$paymentData], $context);
$this->salesChannelPaymentRepository->create([$salesChannelPaymentData], $context);
}
}

/**
* @param $paymentMethod
* @return void
*/
private function processPaymentMethod($paymentMethod)
{
$data = [
'id' => $paymentMethod['internalId'],
'active' => $paymentMethod['status']
];
$this->paymentMethodRepository->update([$data], Context::createDefaultContext());
}

/**
* @param $name
* @return array
*/
private function getPaymentMethod($name): array
{
$paymentCriteria = (
new Criteria())
->addFilter(new EqualsFilter('handlerIdentifier', Payment::class))
->addFilter(new EqualsFilter('name', $name));
$payments = $this->paymentMethodRepository->search($paymentCriteria, Context::createDefaultContext());

/** @var PaymentMethodEntity $payment */
foreach ($payments as $payment) {
return [
'internalId' => $payment->getId(),
'isActive' => $payment->getActive()
];
}

return [
'internalId' => '',
'isActive' => false
];
}

/**
* @param $label
* @return string
*/
private function getPaymentMethodName($label)
{
return "Worldline $label";
}

/**
* @param bool $success
* @param string $message
* @param $paymentMethods
* @return JsonResponse
*/
private function response(bool $success, string $message, $paymentMethods = []): JsonResponse
private function response(bool $success = true, string $message = '', $paymentMethods = []): JsonResponse
{
return new JsonResponse([
'success' => $success,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use _HumbugBoxa991b62ce91e\Nette\Schema\Context;
use MoptWorldline\Bootstrap\Form;
use MoptWorldline\MoptWorldline;
use MoptWorldline\Service\Payment;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
Expand Down Expand Up @@ -82,7 +83,11 @@ public function load(Request $request, SalesChannelContext $context, Criteria $c
$isDefaultSet = false;
/** @var PaymentMethodEntity $method */
foreach ($paymentMethods as $key => $method) {
if ($method->getName() === MoptWorldline::SAVED_CARD_PAYMENT_METHOD_NAME) {
$customFields = $method->getCustomFields();
if (!empty($customFields)
&& array_key_exists(Form::CUSTOM_FIELD_WORLDLINE_PAYMENT_METHOD_ID, $customFields)
&& $customFields[Form::CUSTOM_FIELD_WORLDLINE_PAYMENT_METHOD_ID] == Payment::SAVED_CARD_PAYMENT_METHOD_ID
) {
$savedCardMethod = clone $method;
$paymentMethods->remove($key);
continue;
Expand Down
58 changes: 26 additions & 32 deletions src/MoptWorldline.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,22 @@

namespace MoptWorldline;

use MoptWorldline\Service\Payment;
use MoptWorldline\Service\PaymentMethodHelper;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use MoptWorldline\Service\CustomField;
use MoptWorldline\Service\PaymentMethod;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;

class MoptWorldline extends Plugin
{
const PLUGIN_NAME = 'MoptWorldline';

const PLUGIN_VERSION = '1.1.0';
const FULL_REDIRECT_PAYMENT_METHOD_NAME = "Worldline";
const IFRAME_PAYMENT_METHOD_NAME = "Worldline Iframe";
const SAVED_CARD_PAYMENT_METHOD_NAME = "Worldline saved card";
const METHODS_LIST = [
[
'name' => self::FULL_REDIRECT_PAYMENT_METHOD_NAME,
'description' => 'Worldline full redirect payment method',
'activeOnInstall' => true
],
[
'name' => self::IFRAME_PAYMENT_METHOD_NAME,
'description' => 'Worldline Iframe payment method',
'activeOnInstall' => false
],
[
'name' => self::SAVED_CARD_PAYMENT_METHOD_NAME,
'description' => 'Worldline saved card payment method',
'activeOnInstall' => false
]
];
const PLUGIN_VERSION = '1.2.0';

/**
* @param InstallContext $installContext
Expand All @@ -53,13 +35,24 @@ public function install(InstallContext $installContext): void
$customField = new CustomField($this->container);
$customField->addCustomFields($installContext);

$paymentMethod = new PaymentMethod($this->container);
foreach (self::METHODS_LIST as $method) {
$paymentMethod->addPaymentMethod(
/** @var EntityRepositoryInterface $paymentMethodRep */
$paymentMethodRep =$this->container->get('payment_method.repository');
/** @var EntityRepositoryInterface $salesChannelPaymentMethodRep */
$salesChannelPaymentMethodRep = $this->container->get('sales_channel_payment_method.repository');
/** @var PluginIdProvider $pluginIdProvider */
$pluginIdProvider = $this->container->get(PluginIdProvider::class);
/** @var EntityRepositoryInterface $salesChannelRep */
$salesChannelRep = $this->container->get('sales_channel.repository');

foreach (Payment::METHODS_LIST as $method) {
PaymentMethodHelper::addPaymentMethod(
$paymentMethodRep,
$salesChannelPaymentMethodRep,
$pluginIdProvider,
$installContext->getContext(),
$method['name'],
$method['description'],
$method['activeOnInstall']
$method,
null,
$salesChannelRep
);
}
}
Expand Down Expand Up @@ -93,9 +86,10 @@ public function deactivate(DeactivateContext $deactivateContext): void

private function setPaymentMethodsStatus(bool $status, Context $context)
{
$paymentMethod = new PaymentMethod($this->container);
foreach (self::METHODS_LIST as $method) {
$paymentMethod->setPaymentMethodStatus($status, $context, $method['name']);
/** @var EntityRepositoryInterface $paymentMethodRepository */
$paymentMethodRepository = $this->container->get('payment_method.repository');
foreach (Payment::METHODS_LIST as $method) {
PaymentMethodHelper::setPaymentMethodStatus($paymentMethodRepository, $status, $context, $method['id']);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
</options>
<defaultValue>INFO</defaultValue>
</input-field>
<input-field>
<name>oneyPaymentOption</name>
<label>Oney Payment Option</label>
</input-field>
<input-field type="single-select">
<name>logLevel</name>
<helpText>Choose the loglevel of non-error messages</helpText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<input type="hidden" id="moptWorldlineCustomerId" value="{{ context.customer.id }}">
<input type="hidden" id="moptWorldlineShowIframe" value="{{
moptPageId == 'cartConfirm'
and (context.paymentMethod.name == 'Worldline Iframe'
or context.paymentMethod.name == 'Worldline saved card'
and (context.paymentMethod.customFields.worldline_payment_method_id == 'moptWorldlineIframe'
or context.paymentMethod.customFields.worldline_payment_method_id == 'moptWorldlineSavedCard'
)
}}">
{% if app.request.get('cardDeleted') == '1' %}
Expand Down
Loading

0 comments on commit 269fc7d

Please sign in to comment.