Skip to content

Commit

Permalink
MOL-1280: Fix subscription metadata after retry (#682)
Browse files Browse the repository at this point in the history
Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik authored Jan 12, 2024
1 parent 48281db commit cbcc33c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/Facade/MolliePaymentDoPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Resources\OrderLine;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Checkout\Payment\Cart\AsyncPaymentTransactionStruct;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
Expand Down Expand Up @@ -230,10 +231,14 @@ public function startMolliePayment(string $paymentMethod, AsyncPaymentTransactio
$orderCustomFields->setSubscriptionData($subscriptionId, '');
}

/**
* @var OrderLineItemCollection $orderLineItems
*/
$orderLineItems = $order->getLineItems();
# we save that data in both, the order and
# the order line items
$this->updaterOrderCustomFields->updateOrder($order->getId(), $orderCustomFields, $salesChannelContext->getContext());
$this->updaterLineItemCustomFields->updateOrderLineItems($molliePaymentData->getMollieLineItems(), $salesChannelContext);
$this->updaterLineItemCustomFields->updateOrderLineItems($molliePaymentData->getMollieLineItems(), $orderLineItems, $salesChannelContext);


# this condition somehow looks weird to me (TODO)
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<argument type="service" id="Kiener\MolliePayments\Service\MollieApi\RequestAnonymizer\MollieRequestAnonymizer"/>
<argument type="service" id="mollie_payments.logger"/>
<argument type="service" id="Kiener\MolliePayments\Service\SettingsService"/>
<argument type="service" id="Kiener\MolliePayments\Service\CustomerService"/>
</service>

<service id="Kiener\MolliePayments\Service\MollieApi\Shipment">
Expand Down
27 changes: 26 additions & 1 deletion src/Service/MollieApi/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
use Kiener\MolliePayments\Factory\MollieApiFactory;
use Kiener\MolliePayments\Handler\Method\CreditCardPayment;
use Kiener\MolliePayments\Handler\PaymentHandler;
use Kiener\MolliePayments\Service\CustomerService;
use Kiener\MolliePayments\Service\MollieApi\Payment as MolliePayment;
use Kiener\MolliePayments\Service\MollieApi\Payment as PaymentApiService;
use Kiener\MolliePayments\Service\MollieApi\RequestAnonymizer\MollieRequestAnonymizer;
use Kiener\MolliePayments\Service\Router\RoutingBuilder;
use Kiener\MolliePayments\Service\SettingsService;
use Kiener\MolliePayments\Struct\MollieApi\ShipmentTrackingInfoStruct;
use Kiener\MolliePayments\Struct\OrderLineItemEntity\OrderLineItemEntityAttributes;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Resources\Order as MollieOrder;
use Mollie\Api\Resources\OrderLine;
Expand All @@ -27,6 +29,7 @@
use RuntimeException;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
Expand Down Expand Up @@ -63,6 +66,11 @@ class Order
*/
private $settingsService;

/**
* @var CustomerService
*/
private $customerService;

/**
* @param MollieApiFactory $clientFactory
* @param MolliePayment $paymentApiService
Expand All @@ -71,14 +79,15 @@ class Order
* @param LoggerInterface $logger
* @param SettingsService $settingsService
*/
public function __construct(MollieApiFactory $clientFactory, PaymentApiService $paymentApiService, RoutingBuilder $routingBuilder, MollieRequestAnonymizer $requestAnonymizer, LoggerInterface $logger, SettingsService $settingsService)
public function __construct(MollieApiFactory $clientFactory, PaymentApiService $paymentApiService, RoutingBuilder $routingBuilder, MollieRequestAnonymizer $requestAnonymizer, LoggerInterface $logger, SettingsService $settingsService, CustomerService $customerService)
{
$this->clientFactory = $clientFactory;
$this->logger = $logger;
$this->paymentApiService = $paymentApiService;
$this->routingBuilder = $routingBuilder;
$this->requestAnonymizer = $requestAnonymizer;
$this->settingsService = $settingsService;
$this->customerService = $customerService;
}

/**
Expand Down Expand Up @@ -360,6 +369,22 @@ private function createNewOrderPayment(MollieOrder $mollieOrder, string $payment
$paymentHandler->setEnableSingleClickPayment(true);
}

$lineItems = $order->getLineItems();

if ($settings->isSubscriptionsEnabled() && $lineItems instanceof OrderLineItemCollection) {
# mollie customer ID is required for recurring payments, see https://docs.mollie.com/reference/v2/orders-api/create-order-payment
$mollieCustomerId = $this->customerService->getMollieCustomerId($customer->getId(), $salesChannelContext->getSalesChannelId(), $salesChannelContext->getContext());

foreach ($lineItems as $lineItem) {
$attributes = new OrderLineItemEntityAttributes($lineItem);
if ($attributes->isSubscriptionProduct()) {
$newPaymentData['payment']['sequenceType'] = 'first';
$newPaymentData['payment']['customerId'] = $mollieCustomerId;
break;
}
}
}

# now we have to add payment specific data
# like we would do with initial orders too
$tmpOrder = $paymentHandler->processPaymentMethodSpecificParameters($newPaymentData, $order, $salesChannelContext, $customer);
Expand Down
19 changes: 14 additions & 5 deletions src/Service/Order/UpdateOrderLineItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Mollie\Api\Resources\Order;
use Mollie\Api\Resources\OrderLine;
use Mollie\Api\Types\OrderLineType;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

class UpdateOrderLineItems
Expand All @@ -29,7 +31,7 @@ public function __construct(OrderLineItemRepositoryInterface $orderLineRepositor
* @param SalesChannelContext $salesChannelContext
* @return void
*/
public function updateOrderLineItems(array $orderLines, SalesChannelContext $salesChannelContext): void
public function updateOrderLineItems(array $orderLines, OrderLineItemCollection $shopwareOrderLines, SalesChannelContext $salesChannelContext): void
{
foreach ($orderLines as $orderLine) {
if ($orderLine->type === OrderLineType::TYPE_SHIPPING_FEE) {
Expand All @@ -41,13 +43,20 @@ public function updateOrderLineItems(array $orderLines, SalesChannelContext $sal
if (empty($shopwareLineItemId)) {
continue;
}
/** @var OrderLineItemEntity $shopwareLine */
$shopwareLine = $shopwareOrderLines->get($shopwareLineItemId);
if (!$shopwareLine instanceof OrderLineItemEntity) {
continue;
}

## we need some customfields for later when we edit an order, for example subscription information
$originalCustomFields = $shopwareLine->getPayload()['customFields'] ?? [];
$originalCustomFields['order_line_id'] = $orderLine->id;

$data = [
'id' => $shopwareLineItemId,
'id' => $shopwareLine->getId(),
'customFields' => [
'mollie_payments' => [
'order_line_id' => $orderLine->id
]
'mollie_payments' => $originalCustomFields
]
];

Expand Down
4 changes: 3 additions & 1 deletion tests/PHPUnit/Service/MollieApi/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Kiener\MolliePayments\Exception\CouldNotFetchMollieOrderException;
use Kiener\MolliePayments\Factory\MollieApiFactory;
use Kiener\MolliePayments\Service\CustomerService;
use Kiener\MolliePayments\Service\MollieApi\Order as MollieOrderApi;
use Kiener\MolliePayments\Service\MollieApi\Payment as MolliePaymentApi;
use Kiener\MolliePayments\Service\MollieApi\RequestAnonymizer\MollieRequestAnonymizer;
Expand Down Expand Up @@ -75,7 +76,8 @@ protected function setUp(): void
$this->buildRoutingBuilder($this, ''),
new MollieRequestAnonymizer('*'),
new NullLogger(),
$this->createMock(SettingsService::class)
$this->createMock(SettingsService::class),
$this->createMock(CustomerService::class),
);
}

Expand Down

0 comments on commit cbcc33c

Please sign in to comment.