Skip to content

Commit 065ec02

Browse files
Merge pull request #56 from mollie/release/1.11.0
Release/1.11.0
2 parents 7f6f9b8 + 390bd41 commit 065ec02

31 files changed

+1348
-236
lines changed

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Install Mollie and Mollie Subscription extensions and run setup:di:compile
2929
run: |
3030
docker exec magento-project-community-edition php bin/magento module:enable Mollie_Payment Mollie_Subscriptions
31-
docker exec magento-project-community-edition php bin/magento setup:di:compile
31+
docker exec magento-project-community-edition ./retry "php bin/magento setup:di:compile"
3232
3333
- name: Run PHPStan
3434
run: docker exec magento-project-community-edition /bin/bash -c "./vendor/bin/phpstan analyse --debug -c /data/extensions/mollie-magento2-subscriptions/phpstan.neon /data/extensions/mollie-magento2-subscriptions"

Block/Frontend/Customer/Account/ActiveSubscriptions.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use Magento\Customer\Helper\Session\CurrentCustomer;
1010
use Magento\Framework\Pricing\PriceCurrencyInterface;
1111
use Magento\Framework\View\Element\Template;
12-
use Mollie\Payment\Model\Mollie;
1312
use Mollie\Subscriptions\DTO\SubscriptionResponse;
13+
use Mollie\Subscriptions\Service\Mollie\MollieSubscriptionApi;
1414

1515
class ActiveSubscriptions extends Template
1616
{
@@ -20,9 +20,9 @@ class ActiveSubscriptions extends Template
2020
private $currentCustomer;
2121

2222
/**
23-
* @var Mollie
23+
* @var MollieSubscriptionApi
2424
*/
25-
private $mollie;
25+
private $mollieSubscriptionApi;
2626

2727
/**
2828
* @var PriceCurrencyInterface
@@ -37,21 +37,21 @@ class ActiveSubscriptions extends Template
3737
public function __construct(
3838
Template\Context $context,
3939
CurrentCustomer $currentCustomer,
40-
Mollie $mollie,
40+
MollieSubscriptionApi $mollieSubscriptionApi,
4141
PriceCurrencyInterface $priceCurrency,
4242
array $data = []
4343
) {
4444
parent::__construct($context, $data);
4545

4646
$this->currentCustomer = $currentCustomer;
47-
$this->mollie = $mollie;
47+
$this->mollieSubscriptionApi = $mollieSubscriptionApi;
4848
$this->priceCurrency = $priceCurrency;
4949
}
5050

5151
/**
5252
* @return SubscriptionResponse[]
5353
*/
54-
public function getSubscriptions()
54+
public function getSubscriptions(): array
5555
{
5656
if ($this->subscriptions) {
5757
return $this->subscriptions;
@@ -63,7 +63,7 @@ public function getSubscriptions()
6363
return [];
6464
}
6565

66-
$api = $this->mollie->getMollieApi();
66+
$api = $this->mollieSubscriptionApi->loadByStore($customer->getStoreId());
6767
$subscriptions = $api->subscriptions->listForId($extensionAttributes->getMollieCustomerId());
6868

6969
$this->subscriptions = array_map(function ($subscription) use ($customer) {

Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,6 @@ public function getCustomerCancelNotificationTemplate($storeId = null, $scope =
356356
*/
357357
public function disableNewOrderConfirmation($storeId = null, $scope = ScopeInterface::SCOPE_STORE): bool
358358
{
359-
return $this->getFlag(static::XML_PATH_EMAILS_ENABLE_ADMIN_CANCEL_NOTIFICATION, $storeId, $scope);
359+
return $this->getFlag(static::XML_PATH_DISABLE_NEW_ORDER_CONFIRMATION, $storeId, $scope);
360360
}
361361
}

Controller/Adminhtml/Subscriptions/Cancel.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use Magento\Backend\App\Action;
1010
use Magento\Framework\Event\ManagerInterface;
1111
use Mollie\Payment\Config;
12-
use Mollie\Payment\Model\Mollie;
1312
use Mollie\Subscriptions\Api\SubscriptionToProductRepositoryInterface;
13+
use Mollie\Subscriptions\Service\Mollie\MollieSubscriptionApi;
1414

1515
class Cancel extends Action
1616
{
@@ -20,9 +20,9 @@ class Cancel extends Action
2020
private $config;
2121

2222
/**
23-
* @var Mollie
23+
* @var MollieSubscriptionApi
2424
*/
25-
private $mollie;
25+
private $mollieSubscriptionApi;
2626

2727
/**
2828
* @var SubscriptionToProductRepositoryInterface
@@ -37,20 +37,20 @@ class Cancel extends Action
3737
public function __construct(
3838
Action\Context $context,
3939
Config $config,
40-
Mollie $mollie,
40+
MollieSubscriptionApi $mollieSubscriptionApi,
4141
SubscriptionToProductRepositoryInterface $subscriptionToProductRepository,
4242
ManagerInterface $eventManager
4343
) {
4444
parent::__construct($context);
4545
$this->config = $config;
46-
$this->mollie = $mollie;
46+
$this->mollieSubscriptionApi = $mollieSubscriptionApi;
4747
$this->subscriptionToProductRepository = $subscriptionToProductRepository;
4848
$this->eventManager = $eventManager;
4949
}
5050

5151
public function execute()
5252
{
53-
$api = $this->mollie->getMollieApi($this->getRequest()->getParam('store_id'));
53+
$api = $this->mollieSubscriptionApi->loadByStore($this->getRequest()->getParam('store_id'));
5454
$customerId = $this->getRequest()->getParam('customer_id');
5555
$subscriptionId = $this->getRequest()->getParam('subscription_id');
5656

Controller/Api/Webhook.php

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
use Mollie\Api\Resources\Subscription;
3030
use Mollie\Payment\Api\MollieCustomerRepositoryInterface;
3131
use Mollie\Payment\Logger\MollieLogger;
32+
use Mollie\Payment\Model\Client\Payments;
3233
use Mollie\Payment\Model\Mollie;
34+
use Mollie\Payment\Service\Mollie\Order\LinkTransactionToOrder;
3335
use Mollie\Payment\Service\Mollie\ValidateMetadata;
36+
use Mollie\Payment\Service\Order\OrderCommentHistory;
3437
use Mollie\Payment\Service\Order\SendOrderEmails;
3538
use Mollie\Subscriptions\Config;
39+
use Mollie\Subscriptions\Service\Mollie\MollieSubscriptionApi;
3640
use Mollie\Subscriptions\Service\Mollie\RetryUsingOtherStoreViews;
3741

3842
class Webhook extends Action implements CsrfAwareActionInterface
@@ -47,6 +51,11 @@ class Webhook extends Action implements CsrfAwareActionInterface
4751
*/
4852
private $mollie;
4953

54+
/**
55+
* @var MollieSubscriptionApi
56+
*/
57+
private $mollieSubscriptionApi;
58+
5059
/**
5160
* @var MollieCustomerRepositoryInterface
5261
*/
@@ -106,15 +115,27 @@ class Webhook extends Action implements CsrfAwareActionInterface
106115
* @var MollieApiClient
107116
*/
108117
private $api;
118+
109119
/**
110120
* @var ValidateMetadata
111121
*/
112122
private $validateMetadata;
113123

124+
/**
125+
* @var LinkTransactionToOrder
126+
*/
127+
private $linkTransactionToOrder;
128+
129+
/**
130+
* @var OrderCommentHistory
131+
*/
132+
private $orderCommentHistory;
133+
114134
public function __construct(
115135
Context $context,
116136
Config $config,
117137
Mollie $mollie,
138+
MollieSubscriptionApi $mollieSubscriptionApi,
118139
MollieCustomerRepositoryInterface $mollieCustomerRepository,
119140
CartManagementInterface $cartManagement,
120141
CartRepositoryInterface $cartRepository,
@@ -126,12 +147,15 @@ public function __construct(
126147
MollieLogger $mollieLogger,
127148
SendOrderEmails $sendOrderEmails,
128149
RetryUsingOtherStoreViews $retryUsingOtherStoreViews,
129-
ValidateMetadata $validateMetadata
150+
ValidateMetadata $validateMetadata,
151+
LinkTransactionToOrder $linkTransactionToOrder,
152+
OrderCommentHistory $orderCommentHistory
130153
) {
131154
parent::__construct($context);
132155

133156
$this->config = $config;
134157
$this->mollie = $mollie;
158+
$this->mollieSubscriptionApi = $mollieSubscriptionApi;
135159
$this->mollieCustomerRepository = $mollieCustomerRepository;
136160
$this->cartManagement = $cartManagement;
137161
$this->cartRepository = $cartRepository;
@@ -144,6 +168,8 @@ public function __construct(
144168
$this->sendOrderEmails = $sendOrderEmails;
145169
$this->retryUsingOtherStoreViews = $retryUsingOtherStoreViews;
146170
$this->validateMetadata = $validateMetadata;
171+
$this->linkTransactionToOrder = $linkTransactionToOrder;
172+
$this->orderCommentHistory = $orderCommentHistory;
147173
}
148174

149175
public function execute()
@@ -163,7 +189,7 @@ public function execute()
163189

164190
if ($orders = $this->mollie->getOrderIdsByTransactionId($id)) {
165191
foreach ($orders as $orderId) {
166-
$this->mollie->processTransaction($orderId, 'webhook');
192+
$this->mollie->processTransaction($orderId, Payments::TRANSACTION_TYPE_SUBSCRIPTION);
167193
}
168194

169195
return $this->returnOkResponse();
@@ -173,11 +199,18 @@ public function execute()
173199
$molliePayment = $this->getPayment($id);
174200
$subscription = $this->api->subscriptions->getForId($molliePayment->customerId, $molliePayment->subscriptionId);
175201

176-
$customerId = $this->mollieCustomerRepository->getByMollieCustomerId($molliePayment->customerId)->getCustomerId();
202+
$mollieCustomer = $this->mollieCustomerRepository->getByMollieCustomerId($molliePayment->customerId);
203+
if (!$mollieCustomer) {
204+
throw new \Exception(
205+
'Mollie customer with ID ' . $molliePayment->customerId . ' not found in database'
206+
);
207+
}
208+
209+
$customerId = $mollieCustomer->getCustomerId();
177210
$customer = $this->customerRepository->getById($customerId);
178211

179212
$cart = $this->getCart($customer);
180-
$this->addProduct($molliePayment, $cart);
213+
$this->addProduct($molliePayment, $cart, $subscription->metadata->quantity ?? 1);
181214

182215
$cart->setBillingAddress($this->formatAddress($this->addressRepository->getById($customer->getDefaultBilling())));
183216
$this->setShippingAddress($customer, $cart);
@@ -192,10 +225,14 @@ public function execute()
192225
$order->getPayment()->setAdditionalInformation('subscription_created', $subscription->createdAt);
193226
$this->orderRepository->save($order);
194227

195-
$this->mollie->processTransactionForOrder($order, 'webhook');
228+
$this->orderCommentHistory->add($order, __('Order created by Mollie subscription %1', $molliePayment->id));
229+
230+
$this->linkTransactionToOrder->execute($molliePayment->id, $order);
231+
232+
$this->mollie->processTransactionForOrder($order, Payments::TRANSACTION_TYPE_SUBSCRIPTION);
196233
return $this->returnOkResponse();
197234
} catch (ApiException $exception) {
198-
$this->mollieLogger->addInfoLog('ApiException occured while checking transaction', [
235+
$this->mollieLogger->addErrorLog('ApiException occured while checking transaction', [
199236
'id' => $id,
200237
'exception' => $exception->__toString()
201238
]);
@@ -220,18 +257,19 @@ private function formatAddress(\Magento\Customer\Api\Data\AddressInterface $cust
220257
$address->setVatId($customerAddress->getVatId());
221258
$address->setSuffix($customerAddress->getSuffix());
222259
$address->setPrefix($customerAddress->getPrefix());
260+
$address->setRegionId($customerAddress->getRegionId());
223261

224262
return $address;
225263
}
226264

227-
private function addProduct(Payment $mollieOrder, CartInterface $cart)
265+
private function addProduct(Payment $mollieOrder, CartInterface $cart, float $quantity)
228266
{
229267
/** @var Subscription $subscription */
230268
$subscription = $this->api->performHttpCallToFullUrl(MollieApiClient::HTTP_GET, $mollieOrder->_links->subscription->href);
231269
$sku = $subscription->metadata->sku;
232270
$product = $this->productRepository->get($sku);
233271

234-
$cart->addProduct($product);
272+
$cart->addProduct($product, $quantity);
235273
}
236274

237275
private function setShippingAddress(CustomerInterface $customer, CartInterface $cart)
@@ -243,6 +281,20 @@ private function setShippingAddress(CustomerInterface $customer, CartInterface $
243281
$shippingAddress->setCollectShippingRates(true);
244282
$shippingAddress->collectShippingRates();
245283
$shippingAddress->setShippingMethod($this->config->getShippingMethod());
284+
285+
// There are no rates available. Switch to the first available shipping method.
286+
if ($shippingAddress->getShippingRateByCode($this->config->getShippingMethod()) === false &&
287+
count($shippingAddress->getShippingRatesCollection()->getItems()) > 0
288+
) {
289+
$newMethod = $shippingAddress->getShippingRatesCollection()->getFirstItem()->getCode();
290+
$shippingAddress->setShippingMethod($newMethod);
291+
292+
$this->mollieLogger->addInfoLog(
293+
'subscriptions',
294+
'No rates available for ' . $this->config->getShippingMethod() .
295+
', switched to ' . $newMethod
296+
);
297+
}
246298
}
247299

248300
private function getCart(CustomerInterface $customer): CartInterface
@@ -267,7 +319,7 @@ private function returnOkResponse()
267319
public function getPayment(string $id): Payment
268320
{
269321
try {
270-
$this->api = $this->mollie->getMollieApi();
322+
$this->api = $this->mollieSubscriptionApi->loadByStore();
271323

272324
return $this->api->payments->get($id);
273325
} catch (ApiException $exception) {

Controller/Index/Cancel.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
use Magento\Framework\App\RequestInterface;
1515
use Magento\Framework\Event\ManagerInterface;
1616
use Mollie\Payment\Config;
17-
use Mollie\Payment\Model\Mollie;
1817
use Mollie\Subscriptions\Api\SubscriptionToProductRepositoryInterface;
1918
use Mollie\Subscriptions\Service\Email\SendNotificationEmail;
19+
use Mollie\Subscriptions\Service\Mollie\MollieSubscriptionApi;
2020

2121
class Cancel extends Action implements HttpPostActionInterface
2222
{
@@ -26,9 +26,9 @@ class Cancel extends Action implements HttpPostActionInterface
2626
private $config;
2727

2828
/**
29-
* @var Mollie
29+
* @var MollieSubscriptionApi
3030
*/
31-
private $mollie;
31+
private $mollieSubscriptionApi;
3232

3333
/**
3434
* @var SubscriptionToProductRepositoryInterface
@@ -63,7 +63,7 @@ class Cancel extends Action implements HttpPostActionInterface
6363
public function __construct(
6464
Context $context,
6565
Config $config,
66-
Mollie $mollie,
66+
MollieSubscriptionApi $mollieSubscriptionApi,
6767
SubscriptionToProductRepositoryInterface $subscriptionToProductRepository,
6868
CurrentCustomer $currentCustomer,
6969
Session $customerSession,
@@ -73,7 +73,7 @@ public function __construct(
7373
) {
7474
parent::__construct($context);
7575
$this->config = $config;
76-
$this->mollie = $mollie;
76+
$this->mollieSubscriptionApi = $mollieSubscriptionApi;
7777
$this->subscriptionToProductRepository = $subscriptionToProductRepository;
7878
$this->currentCustomer = $currentCustomer;
7979
$this->customerSession = $customerSession;
@@ -96,7 +96,7 @@ public function execute()
9696
$customer = $this->currentCustomer->getCustomer();
9797
$extensionAttributes = $customer->getExtensionAttributes();
9898

99-
$api = $this->mollie->getMollieApi();
99+
$api = $this->mollieSubscriptionApi->loadByStore($customer->getStoreId());
100100
$subscriptionId = $this->getRequest()->getParam('subscription_id');
101101

102102
try {

0 commit comments

Comments
 (0)