Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog
All notable changes to this project will be documented in this file.

## [2.1.0]
### Fixed
- Fix: Payment not captured when order status is changed to done for digital products.
- Fix: Payment handler skipped when AltaPay Terminal ID field is empty but Sales Channel dropdown is set.
- Fix: PHP warnings from missing custom field values.

## [2.0.9]
### Added
- Support for sales channel–specific terminals for payment methods.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Integrates your Shopware web shop to the AltaPay payments gateway.

## Compatibility
- Shopware 6.6.x (Tested version 6.6.9.0)
- Shopware 6.7.x (Tested version 6.7.5.1)
- Shopware 6.7.x (Tested version 6.7.8.1)

## Changelog
See [Changelog](CHANGELOG.md) for all the release notes.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "AltaPay plugin for Shopware 6",
"type": "shopware-platform-plugin",
"license": "MIT",
"version": "2.0.9",
"version": "2.1.0",
"authors": [
{
"name": "AltaPay A/S",
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<service id="Wexo\AltaPay\Subscriber\PaymentMethodSubscriber">
<argument type="service" id="payment_method.repository" />
<argument type="service" id="Shopware\Core\Framework\Plugin\Util\PluginIdProvider" />
<argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService" />
<tag name="kernel.event_subscriber" />
</service>

Expand Down
72 changes: 42 additions & 30 deletions src/Service/PaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ public function pay(
);

$paymentMethod = $salesChannelContext->getPaymentMethod();
$terminal = $paymentMethod->getTranslated()['customFields'][self::ALTAPAY_TERMINAL_ID_CUSTOM_FIELD];
$salesChannelTerminal = $paymentMethod->getTranslated()['customFields'][self::ALTAPAY_SALES_CHANNEL_TERMINAL_ID];
$terminal = $paymentMethod->getTranslated()['customFields'][self::ALTAPAY_TERMINAL_ID_CUSTOM_FIELD] ?? null;
$salesChannelTerminal = $paymentMethod->getTranslated()['customFields'][self::ALTAPAY_SALES_CHANNEL_TERMINAL_ID] ?? null;

if (!empty($salesChannelTerminal)) {
$field = 'WexoAltaPay.config.' . $salesChannelTerminal;
Expand Down Expand Up @@ -278,16 +278,40 @@ public function transactionCallback(
) {
$status = "Success";
}
$customFields = $order->getCustomFields() ?? [];
$orderStatus = $customFields[self::ALTAPAY_ORDER_STATUS] ?? null;

if ($orderStatus === 'processed') {
return;
}

if ($status === "Open" || $status === "Success") {
$altaPayTransactionId = (string)$result->Body->Transactions->Transaction->TransactionId;
$altaPayPaymentSchemeName = (string)$result->Body->Transactions->Transaction->PaymentSchemeName;
$altaPayPaymentNature = (string)$result->Body->Transactions->Transaction->PaymentNature;
$altaPayPaymentId = (string)$result->Body->Transactions->Transaction->PaymentId;

$customFields = array_merge(
$customFields,
[
self::ALTAPAY_TRANSACTION_ID_CUSTOM_FIELD => $altaPayTransactionId,
self::ALTAPAY_TRANSACTION_PAYMENT_SCHEME_NAME_CUSTOM_FIELD => $altaPayPaymentSchemeName,
self::ALTAPAY_TRANSACTION_PAYMENT_NATURE_CUSTOM_FIELD => $altaPayPaymentNature,
self::ALTAPAY_PAYMENT_ID_CUSTOM_FIELD => $altaPayPaymentId,
]
);
Comment on lines +294 to +302
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really necessary?

The way I see it, you just need to insert the custom fields you want to add using the repository:

https://developer.shopware.com/docs/guides/plugins/plugins/framework/custom-field/add-custom-field.html#filling-data-into-custom-fields

Rather than loading all the custom fields, merging them, and then inserting them again.

You risk that if the custom fields is changed while this operation is processing, then you will insert custom fields with an older value (race condition)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emicha ,
Order custom_fields are stored as JSON, but there is no clear guarantee that writing only part of that JSON will always keep the other existing fields.
There is also a Shopware core issue showing that existing custom_fields can be overwritten when a new array is written instead of merging first:
shopware/shopware#9613

We also need this to behave predictably across different Shopware versions used by merchants.
The safest long-term solution would be to use a separate table/entity instead of relying on the order custom_fields JSON. I have created a separate ticket for that:

https://developer.shopware.com/docs/guides/plugins/plugins/framework/data-handling/add-custom-compl…

So for this change, I think the explicit merge is the safer short-term fix, and the separate entity is the better long-term solution.
Ticket: PL-1598

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BushraAsif The issue you shared seems to be pointing at updating the full user, and not just a field using:

$this->swagExampleRepository->upsert([[
    'id' => '<your ID here>',
    'customFields' => ['swag_example_size' => 15]
]], $context);

I am not sure what you shared is applicable here. Please discuss with @mateusz and @ovidiu in my absence.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emicha @ovidiuba the information I shared is not related to the user field, it belongs to the order field. I'm attaching the order details screen for reference. As shown in the screenshot, the custom_fields data is saved as JSON, which is why I merge the existing data before saving it again.

That's why we read custom fields before writing to it, so we don't mess with other plugins saving data to the custom field.

image

To properly handle the race condition you mentioned, we need to create a custom table according to the Shopware documentation:
https://developer.shopware.com/docs/guides/plugins/plugins/framework/data-handling/add-custom-complex-data.html

Ticket: PL-1598


$this->orderRepository->update([
[
'id' => $order->getId(),
'customFields' => $customFields,
]
], $salesChannelContext->getContext());
}
switch ($status) {
case "Open":
break;
case "Success":
$customFields = $order->getCustomFields() ?? [];
$orderStatus = $customFields[self::ALTAPAY_ORDER_STATUS] ?? null;

if ($orderStatus === 'processed') {
break;
}
// Delete cart when either customer or AltaPay reaches this page.
$cartToken = $order->getCustomFieldsValue(field: WexoAltaPay::ALTAPAY_CART_TOKEN);
if (!empty($cartToken)) {
Expand Down Expand Up @@ -334,10 +358,16 @@ public function transactionCallback(
// Update order state to "in progress"
$this->updateOrderStateToInProgress($order, $salesChannelContext->getContext());
}
}
$order->changeCustomFields([
self::ALTAPAY_ORDER_STATUS => 'processed'
]);
}
$this->orderRepository->update([
[
'id' => $order->getId(),
'customFields' => array_merge(
$customFields,
[self::ALTAPAY_ORDER_STATUS => 'processed']
),
]
], $salesChannelContext->getContext());
} catch (\Exception $e) {
$this->logger->error("order transaction state error:". $e->getMessage());
}
Expand Down Expand Up @@ -397,17 +427,6 @@ public function transactionCallback(
?? (string)$result->APIResponse?->Header?->ErrorMessage
);
}
$altaPayTransactionId = (string)$result->Body->Transactions->Transaction->TransactionId;
$altaPayPaymentSchemeName= (string)$result->Body->Transactions->Transaction->PaymentSchemeName;
$altaPayPaymentNature= (string)$result->Body->Transactions->Transaction->PaymentNature;
$altaPayPaymentId= (string)$result->Body->Transactions->Transaction->PaymentId;

$order->changeCustomFields([
self::ALTAPAY_TRANSACTION_ID_CUSTOM_FIELD => $altaPayTransactionId,
self::ALTAPAY_TRANSACTION_PAYMENT_SCHEME_NAME_CUSTOM_FIELD => $altaPayPaymentSchemeName,
self::ALTAPAY_TRANSACTION_PAYMENT_NATURE_CUSTOM_FIELD => $altaPayPaymentNature,
self::ALTAPAY_PAYMENT_ID_CUSTOM_FIELD => $altaPayPaymentId
]);

$surchargeAmount = (float)$result->Body->Transactions->Transaction->SurchargeAmount;
$paymentMethod = $salesChannelContext->getPaymentMethod();
Expand Down Expand Up @@ -451,13 +470,6 @@ public function transactionCallback(

$this->orderRepository->merge($versionId, $systemContext);
}

$this->orderRepository->update([
[
'id' => $order->getId(),
'customFields' => $order->getCustomFields()
]
], $salesChannelContext->getContext());
}

public function getAltaPayClient(string $salesChannelId): Client
Expand Down
14 changes: 12 additions & 2 deletions src/Subscriber/PaymentMethodSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Wexo\AltaPay\Service\PaymentService;
use Wexo\AltaPay\WexoAltaPay;
use Shopware\Core\System\SystemConfig\SystemConfigService;

class PaymentMethodSubscriber implements EventSubscriberInterface
{
Expand All @@ -22,7 +23,8 @@ class PaymentMethodSubscriber implements EventSubscriberInterface
*/
public function __construct(
protected EntityRepository $paymentMethodRepository,
protected PluginIdProvider $pluginIdProvider
protected PluginIdProvider $pluginIdProvider,
protected SystemConfigService $systemConfigService
) {
}

Expand Down Expand Up @@ -52,7 +54,15 @@ public function associateAltaPayPaymentMethod(EntityWrittenEvent $event): void
$payload = [];

$payload['id'] = $paymentMethod->getId();
if ($paymentMethod->getCustomFieldsValue('wexoAltaPayTerminalId')) {
$salesChannelTerminal = $paymentMethod->getCustomFieldsValue('altapaySalesChannelTerminalId');
$salesChannelTerminalValue = null;

if (!empty($salesChannelTerminal)) {
$field = 'WexoAltaPay.config.' . $salesChannelTerminal;
$salesChannelTerminalValue = $this->systemConfigService->get($field);
}

if ($paymentMethod->getCustomFieldsValue('wexoAltaPayTerminalId') || $salesChannelTerminalValue) {
$payload['handlerIdentifier'] = PaymentService::class;
$payload['pluginId'] = $this->pluginIdProvider->getPluginIdByBaseClass(
WexoAltaPay::class,
Expand Down
2 changes: 1 addition & 1 deletion src/WexoAltaPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class WexoAltaPay extends Plugin
public const ALTAPAY_FIELD_SET_NAME = "wexoAltaPay";
public const ALTAPAY_PAYMENT_METHOD_FIELD_SET_NAME = "wexoAltaPayPaymentMethod";
public const ALTAPAY_CART_TOKEN = "wexoAltaPayCartToken";
public const ALTAPAY_PLUGIN_VERSION = '2.0.9';
public const ALTAPAY_PLUGIN_VERSION = '2.1.0';
public const ALTAPAY_PLUGIN_NAME = 'WexoAltaPay';

public function update(UpdateContext $updateContext): void
Expand Down
2 changes: 1 addition & 1 deletion wiki.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ The new credentials can now be used as the API Username and API Password in the
- Shopware 6.6.x
- Shopware 6.7.x

The latest tested versions are `6.6.9.0` and `6.7.5.1`
The latest tested versions are `6.6.9.0` and `6.7.8.1`

## Troubleshooting

Expand Down
Loading