From e339a2a7be51cb0a7fb371d5a92b8ff245f83390 Mon Sep 17 00:00:00 2001 From: Logeecom Belgrade Date: Wed, 4 Sep 2024 10:00:31 +0200 Subject: [PATCH] Add 1.13 compatibility (#9) * add compatibility with 1.13 Sylius * add compatibility with 1.13 Sylius * update friendsofsymfony/jsrouting-bundle version * add composer.lock file * update sylius/admin-order-creation-plugin reference in composer.json file * update installation docs * update VersionChecker class. Update installation manual * fix issue with payment link * update installation manual to support migrations are running on Sylius 1.13.x * update installation manual to support migrations are running on Sylius 1.13.x * refactor VersionChecker class * update MollieGatewayConfig orm file. Update ProductTrait * update installation docs to support Sylius 1.13 * fix issue with completing Alma payments * refactor version.xml file --------- Co-authored-by: SergejSavic --- composer.json | 4 +- doc/installation.md | 295 ++++++++++++++++++ src/Action/Api/CreatePaymentAction.php | 5 + src/Action/ConvertMolliePaymentAction.php | 23 ++ .../MolliePluginLatestVersionChecker.php | 40 --- src/Entity/ProductTrait.php | 2 +- src/Resolver/PaymentlinkResolver.php | 11 +- .../doctrine/MollieGatewayConfig.orm.xml | 2 +- .../config/services/checker/version.xml | 3 - 9 files changed, 333 insertions(+), 52 deletions(-) diff --git a/composer.json b/composer.json index a920a696..7c3d1025 100644 --- a/composer.json +++ b/composer.json @@ -5,11 +5,11 @@ "license": "MIT", "require": { "php": "^7.4 || ^8.0", - "sylius/sylius": "~1.9.0 || ~1.10.0 || ~1.11.0 || ~1.12.0", + "sylius/sylius": "~1.9.0 || ~1.10.0 || ~1.11.0 || ~1.12.0 || ~1.13.0", "symfony/messenger": "^4.4 || ^5.2 || ^6.0", "mollie/mollie-api-php": "^v2.71.0", "sylius/refund-plugin": "^1.0", - "sylius/admin-order-creation-plugin": "^0.12 || ^0.13 || v0.14", + "sylius/admin-order-creation-plugin": "^0.12 || ^0.13 || v0.14 || v0.15.0", "ext-json": "*", "willdurand/js-translation-bundle": "^4.0 || ^5.0" }, diff --git a/doc/installation.md b/doc/installation.md index f2d0cda9..2440142c 100644 --- a/doc/installation.md +++ b/doc/installation.md @@ -55,6 +55,57 @@ class GatewayConfig extends BaseGatewayConfig implements GatewayConfigInterface $this->mollieGatewayConfig = new ArrayCollection(); } } +``` +In case if you have installed Sylius version 1.13.x, use the following code instead +```php +mollieGatewayConfig = new ArrayCollection(); + } +} + ``` You can find more annotation examples under the [tests/Application/src/Entity/*](/tests/Application/src/Entity/) path. @@ -109,6 +160,7 @@ use SyliusMolliePlugin\Entity\RecurringOrderTrait; use Doctrine\Common\Collections\Collection; use Sylius\Component\Core\Model\Order as BaseOrder; use Sylius\Component\Core\Model\OrderItemInterface; +use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity @@ -189,6 +241,114 @@ class Order extends BaseOrder implements OrderInterface } } +``` +In case if you have installed Sylius version 1.13.x, use the following code instead +```php +items + ->filter(function (OrderItemInterface $orderItem) { + $variant = $orderItem->getVariant(); + + return $variant !== null + && true === $variant->isRecurring(); + }) + ; + } + + public function getNonRecurringItems(): Collection + { + return $this + ->items + ->filter(function (OrderItemInterface $orderItem) { + $variant = $orderItem->getVariant(); + + return $variant !== null + && false === $variant->isRecurring(); + }) + ; + } + + public function hasRecurringContents(): bool + { + return 0 < $this->getRecurringItems()->count(); + } + + public function hasNonRecurringContents(): bool + { + return 0 < $this->getNonRecurringItems()->count(); + } +} + + ``` If you don't use annotations, you can also define new Entity mapping inside your `src/Resources/config/doctrine` directory. @@ -247,6 +407,40 @@ class Product extends BaseProduct implements ProductInterface */ protected ?ProductType $productType = null; } +``` +In case if you have installed Sylius version 1.13.x, use the following code instead +```php +getProduct()->getName(); + } +} + +``` +Add RecurringProductVariantTrait implementation: +```php + 0])] + private bool $recurring = false; + + /** + * @var ?int + * @ORM\Column(type="integer", name="recurring_times", nullable="true") + */ + #[ORM\Column(name: "recurring_times", type: "integer", nullable: true)] + private ?int $times = null; + + /** + * @var ?string + * @ORM\Column(type="string", name="recurring_interval", nullable="true") + */ + #[ORM\Column(name: "recurring_interval", type: "string", nullable: true)] + private ?string $interval = null; + + public function isRecurring(): bool + { + return $this->recurring; + } + + public function setRecurring(bool $recurring): void + { + $this->recurring = $recurring; + } + + public function getTimes(): ?int + { + return $this->times; + } + + public function setTimes(?int $times): void + { + $this->times = $times; + } + + public function getInterval(): ?string + { + return $this->interval; + } + + public function setInterval(?string $interval): void + { + $this->interval = $interval; + } +} +``` If you don't use annotations, you can also define new Entity mapping inside your `src/Resources/config/doctrine` directory. @@ -467,6 +759,9 @@ sylius_mollie_plugin: #### 12. Update your database +In case if you have installed Sylius 1.13.x version, you will need to first remove { resource: "@SyliusRefundPlugin/Resources/config/app/config.yml" } +from acme/config/packages/sylius_refund.yaml + Apply migration to your database: (this is for the plugin fresh installation only) ``` bin/console doctrine:migrations:migrate diff --git a/src/Action/Api/CreatePaymentAction.php b/src/Action/Api/CreatePaymentAction.php index e53182fe..79f7b684 100644 --- a/src/Action/Api/CreatePaymentAction.php +++ b/src/Action/Api/CreatePaymentAction.php @@ -5,6 +5,7 @@ namespace SyliusMolliePlugin\Action\Api; +use Mollie\Api\Types\PaymentMethod; use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; use SyliusMolliePlugin\Logger\MollieLoggerActionInterface; use SyliusMolliePlugin\Parser\Response\GuzzleNegativeResponseParserInterface; @@ -67,6 +68,10 @@ public function execute($request): void 'metadata' => $details['metadata'], ]; + if ($details['metadata']['molliePaymentMethods'] === PaymentMethod::ALMA) { + $paymentDetails['billingAddress'] = $details['billingAddress']; + } + if (true === isset($details['locale'])) { $paymentDetails['locale'] = $details['locale']; } diff --git a/src/Action/ConvertMolliePaymentAction.php b/src/Action/ConvertMolliePaymentAction.php index bab14c41..41f73beb 100644 --- a/src/Action/ConvertMolliePaymentAction.php +++ b/src/Action/ConvertMolliePaymentAction.php @@ -5,6 +5,7 @@ namespace SyliusMolliePlugin\Action; +use Mollie\Api\Types\PaymentMethod; use SyliusMolliePlugin\Action\Api\BaseApiAwareAction; use SyliusMolliePlugin\Entity\MollieGatewayConfigInterface; use SyliusMolliePlugin\Factory\ApiCustomerFactoryInterface; @@ -140,6 +141,28 @@ public function execute($request): void $details['metadata']['customer_mollie_id'] = $model['customer_mollie_id']; } + if ($method->getMethodId() === PaymentMethod::ALMA) { + $billingAddress = $order->getBillingAddress(); + $customer = $order->getCustomer(); + $address = []; + + if ($billingAddress) { + $address = [ + 'streetAndNumber' => $billingAddress->getStreet(), + 'postalCode' => $billingAddress->getPostcode(), + 'city' => $billingAddress->getCity(), + 'country' => $billingAddress->getCountryCode(), + 'givenName' => $billingAddress->getFirstName(), + 'familyName' => $billingAddress->getLastName(), + 'organizationName' => $billingAddress->getCompany(), + 'email' => ($customer && $customer->getEmail()) ? $customer->getEmail() : + (($order->getUser() && $order->getUser()->getEmail()) ? $order->getUser()->getEmail() : null) + ]; + } + + $details['billingAddress'] = $address; + } + if (false === $this->mollieApiClient->isRecurringSubscription()) { $details['customerId'] = $model['customer_mollie_id'] ?? null; $details['metadata']['methodType'] = Options::PAYMENT_API; diff --git a/src/Checker/Version/MolliePluginLatestVersionChecker.php b/src/Checker/Version/MolliePluginLatestVersionChecker.php index f0617ced..76fe3889 100644 --- a/src/Checker/Version/MolliePluginLatestVersionChecker.php +++ b/src/Checker/Version/MolliePluginLatestVersionChecker.php @@ -1,55 +1,15 @@ client = $client; - $this->hubUri = new Uri($hubUri); - } - public function checkLatestVersion(): ?string { - return SyliusMolliePlugin::VERSION; -// try { -// $hubResponse = $this->client->request('GET', $this->hubUri); -// } catch (GuzzleException $exception) { -// return null; -// } -//checkLatestVersion -// $hubResponse = json_decode($hubResponse->getBody()->getContents(), true); -// -// return $this->getMolliePluginLatestVersion($hubResponse); - } - - private function getMolliePluginLatestVersion(array $data): ?string - { - $latestVersion = end($data['packages']['mollie/sylius-plugin']); - - if (isset($latestVersion['version'])) { - return $latestVersion['version']; - } - - return null; } } diff --git a/src/Entity/ProductTrait.php b/src/Entity/ProductTrait.php index e68a2d96..0edb2a51 100644 --- a/src/Entity/ProductTrait.php +++ b/src/Entity/ProductTrait.php @@ -8,7 +8,7 @@ trait ProductTrait { /** @var ProductTypeInterface */ - protected $productType; + protected ?ProductType $productType = null; public function getProductType(): ?ProductTypeInterface { diff --git a/src/Resolver/PaymentlinkResolver.php b/src/Resolver/PaymentlinkResolver.php index 6bf50378..2397b303 100644 --- a/src/Resolver/PaymentlinkResolver.php +++ b/src/Resolver/PaymentlinkResolver.php @@ -61,16 +61,17 @@ public function resolve( /** @var PaymentInterface $syliusPayment */ $syliusPayment = $order->getPayments()->last(); + $firstPayment = $order->getPayments()->first(); /** @var PaymentMethodInterface $paymentMethod */ $paymentMethod = $syliusPayment->getMethod(); Assert::notNull($paymentMethod->getGatewayConfig()); if (false === in_array( - $paymentMethod->getGatewayConfig()->getFactoryName(), - [MollieGatewayFactory::FACTORY_NAME, MollieSubscriptionGatewayFactory::FACTORY_NAME], - true - )) { + $paymentMethod->getGatewayConfig()->getFactoryName(), + [MollieGatewayFactory::FACTORY_NAME, MollieSubscriptionGatewayFactory::FACTORY_NAME], + true + )) { throw new NotFoundException('No method mollie found in order'); } @@ -86,7 +87,7 @@ public function resolve( } $this->mollieApiClient->setApiKey($modusKey); - $details = $syliusPayment->getDetails(); + $details = $firstPayment->getDetails(); if (!isset($details['webhookUrl'])) { return ''; diff --git a/src/Resources/config/doctrine/MollieGatewayConfig.orm.xml b/src/Resources/config/doctrine/MollieGatewayConfig.orm.xml index d9a27bf3..7acff4db 100644 --- a/src/Resources/config/doctrine/MollieGatewayConfig.orm.xml +++ b/src/Resources/config/doctrine/MollieGatewayConfig.orm.xml @@ -26,7 +26,7 @@ - + diff --git a/src/Resources/config/services/checker/version.xml b/src/Resources/config/services/checker/version.xml index 46057e75..0bd3470a 100644 --- a/src/Resources/config/services/checker/version.xml +++ b/src/Resources/config/services/checker/version.xml @@ -9,9 +9,6 @@ - - %sylius_mollie_plugin.admin.version.uri% -