Skip to content

Commit

Permalink
Merge pull request #56 from mollie/release/1.11.0
Browse files Browse the repository at this point in the history
Release/1.11.0
  • Loading branch information
Marvin-Magmodules authored Jun 13, 2023
2 parents 7f6f9b8 + 390bd41 commit 065ec02
Show file tree
Hide file tree
Showing 31 changed files with 1,348 additions and 236 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Install Mollie and Mollie Subscription extensions and run setup:di:compile
run: |
docker exec magento-project-community-edition php bin/magento module:enable Mollie_Payment Mollie_Subscriptions
docker exec magento-project-community-edition php bin/magento setup:di:compile
docker exec magento-project-community-edition ./retry "php bin/magento setup:di:compile"
- name: Run PHPStan
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"
14 changes: 7 additions & 7 deletions Block/Frontend/Customer/Account/ActiveSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Magento\Customer\Helper\Session\CurrentCustomer;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\View\Element\Template;
use Mollie\Payment\Model\Mollie;
use Mollie\Subscriptions\DTO\SubscriptionResponse;
use Mollie\Subscriptions\Service\Mollie\MollieSubscriptionApi;

class ActiveSubscriptions extends Template
{
Expand All @@ -20,9 +20,9 @@ class ActiveSubscriptions extends Template
private $currentCustomer;

/**
* @var Mollie
* @var MollieSubscriptionApi
*/
private $mollie;
private $mollieSubscriptionApi;

/**
* @var PriceCurrencyInterface
Expand All @@ -37,21 +37,21 @@ class ActiveSubscriptions extends Template
public function __construct(
Template\Context $context,
CurrentCustomer $currentCustomer,
Mollie $mollie,
MollieSubscriptionApi $mollieSubscriptionApi,
PriceCurrencyInterface $priceCurrency,
array $data = []
) {
parent::__construct($context, $data);

$this->currentCustomer = $currentCustomer;
$this->mollie = $mollie;
$this->mollieSubscriptionApi = $mollieSubscriptionApi;
$this->priceCurrency = $priceCurrency;
}

/**
* @return SubscriptionResponse[]
*/
public function getSubscriptions()
public function getSubscriptions(): array
{
if ($this->subscriptions) {
return $this->subscriptions;
Expand All @@ -63,7 +63,7 @@ public function getSubscriptions()
return [];
}

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

$this->subscriptions = array_map(function ($subscription) use ($customer) {
Expand Down
2 changes: 1 addition & 1 deletion Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,6 @@ public function getCustomerCancelNotificationTemplate($storeId = null, $scope =
*/
public function disableNewOrderConfirmation($storeId = null, $scope = ScopeInterface::SCOPE_STORE): bool
{
return $this->getFlag(static::XML_PATH_EMAILS_ENABLE_ADMIN_CANCEL_NOTIFICATION, $storeId, $scope);
return $this->getFlag(static::XML_PATH_DISABLE_NEW_ORDER_CONFIRMATION, $storeId, $scope);
}
}
12 changes: 6 additions & 6 deletions Controller/Adminhtml/Subscriptions/Cancel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Magento\Backend\App\Action;
use Magento\Framework\Event\ManagerInterface;
use Mollie\Payment\Config;
use Mollie\Payment\Model\Mollie;
use Mollie\Subscriptions\Api\SubscriptionToProductRepositoryInterface;
use Mollie\Subscriptions\Service\Mollie\MollieSubscriptionApi;

class Cancel extends Action
{
Expand All @@ -20,9 +20,9 @@ class Cancel extends Action
private $config;

/**
* @var Mollie
* @var MollieSubscriptionApi
*/
private $mollie;
private $mollieSubscriptionApi;

/**
* @var SubscriptionToProductRepositoryInterface
Expand All @@ -37,20 +37,20 @@ class Cancel extends Action
public function __construct(
Action\Context $context,
Config $config,
Mollie $mollie,
MollieSubscriptionApi $mollieSubscriptionApi,
SubscriptionToProductRepositoryInterface $subscriptionToProductRepository,
ManagerInterface $eventManager
) {
parent::__construct($context);
$this->config = $config;
$this->mollie = $mollie;
$this->mollieSubscriptionApi = $mollieSubscriptionApi;
$this->subscriptionToProductRepository = $subscriptionToProductRepository;
$this->eventManager = $eventManager;
}

public function execute()
{
$api = $this->mollie->getMollieApi($this->getRequest()->getParam('store_id'));
$api = $this->mollieSubscriptionApi->loadByStore($this->getRequest()->getParam('store_id'));
$customerId = $this->getRequest()->getParam('customer_id');
$subscriptionId = $this->getRequest()->getParam('subscription_id');

Expand Down
70 changes: 61 additions & 9 deletions Controller/Api/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@
use Mollie\Api\Resources\Subscription;
use Mollie\Payment\Api\MollieCustomerRepositoryInterface;
use Mollie\Payment\Logger\MollieLogger;
use Mollie\Payment\Model\Client\Payments;
use Mollie\Payment\Model\Mollie;
use Mollie\Payment\Service\Mollie\Order\LinkTransactionToOrder;
use Mollie\Payment\Service\Mollie\ValidateMetadata;
use Mollie\Payment\Service\Order\OrderCommentHistory;
use Mollie\Payment\Service\Order\SendOrderEmails;
use Mollie\Subscriptions\Config;
use Mollie\Subscriptions\Service\Mollie\MollieSubscriptionApi;
use Mollie\Subscriptions\Service\Mollie\RetryUsingOtherStoreViews;

class Webhook extends Action implements CsrfAwareActionInterface
Expand All @@ -47,6 +51,11 @@ class Webhook extends Action implements CsrfAwareActionInterface
*/
private $mollie;

/**
* @var MollieSubscriptionApi
*/
private $mollieSubscriptionApi;

/**
* @var MollieCustomerRepositoryInterface
*/
Expand Down Expand Up @@ -106,15 +115,27 @@ class Webhook extends Action implements CsrfAwareActionInterface
* @var MollieApiClient
*/
private $api;

/**
* @var ValidateMetadata
*/
private $validateMetadata;

/**
* @var LinkTransactionToOrder
*/
private $linkTransactionToOrder;

/**
* @var OrderCommentHistory
*/
private $orderCommentHistory;

public function __construct(
Context $context,
Config $config,
Mollie $mollie,
MollieSubscriptionApi $mollieSubscriptionApi,
MollieCustomerRepositoryInterface $mollieCustomerRepository,
CartManagementInterface $cartManagement,
CartRepositoryInterface $cartRepository,
Expand All @@ -126,12 +147,15 @@ public function __construct(
MollieLogger $mollieLogger,
SendOrderEmails $sendOrderEmails,
RetryUsingOtherStoreViews $retryUsingOtherStoreViews,
ValidateMetadata $validateMetadata
ValidateMetadata $validateMetadata,
LinkTransactionToOrder $linkTransactionToOrder,
OrderCommentHistory $orderCommentHistory
) {
parent::__construct($context);

$this->config = $config;
$this->mollie = $mollie;
$this->mollieSubscriptionApi = $mollieSubscriptionApi;
$this->mollieCustomerRepository = $mollieCustomerRepository;
$this->cartManagement = $cartManagement;
$this->cartRepository = $cartRepository;
Expand All @@ -144,6 +168,8 @@ public function __construct(
$this->sendOrderEmails = $sendOrderEmails;
$this->retryUsingOtherStoreViews = $retryUsingOtherStoreViews;
$this->validateMetadata = $validateMetadata;
$this->linkTransactionToOrder = $linkTransactionToOrder;
$this->orderCommentHistory = $orderCommentHistory;
}

public function execute()
Expand All @@ -163,7 +189,7 @@ public function execute()

if ($orders = $this->mollie->getOrderIdsByTransactionId($id)) {
foreach ($orders as $orderId) {
$this->mollie->processTransaction($orderId, 'webhook');
$this->mollie->processTransaction($orderId, Payments::TRANSACTION_TYPE_SUBSCRIPTION);
}

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

$customerId = $this->mollieCustomerRepository->getByMollieCustomerId($molliePayment->customerId)->getCustomerId();
$mollieCustomer = $this->mollieCustomerRepository->getByMollieCustomerId($molliePayment->customerId);
if (!$mollieCustomer) {
throw new \Exception(
'Mollie customer with ID ' . $molliePayment->customerId . ' not found in database'
);
}

$customerId = $mollieCustomer->getCustomerId();
$customer = $this->customerRepository->getById($customerId);

$cart = $this->getCart($customer);
$this->addProduct($molliePayment, $cart);
$this->addProduct($molliePayment, $cart, $subscription->metadata->quantity ?? 1);

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

$this->mollie->processTransactionForOrder($order, 'webhook');
$this->orderCommentHistory->add($order, __('Order created by Mollie subscription %1', $molliePayment->id));

$this->linkTransactionToOrder->execute($molliePayment->id, $order);

$this->mollie->processTransactionForOrder($order, Payments::TRANSACTION_TYPE_SUBSCRIPTION);
return $this->returnOkResponse();
} catch (ApiException $exception) {
$this->mollieLogger->addInfoLog('ApiException occured while checking transaction', [
$this->mollieLogger->addErrorLog('ApiException occured while checking transaction', [
'id' => $id,
'exception' => $exception->__toString()
]);
Expand All @@ -220,18 +257,19 @@ private function formatAddress(\Magento\Customer\Api\Data\AddressInterface $cust
$address->setVatId($customerAddress->getVatId());
$address->setSuffix($customerAddress->getSuffix());
$address->setPrefix($customerAddress->getPrefix());
$address->setRegionId($customerAddress->getRegionId());

return $address;
}

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

$cart->addProduct($product);
$cart->addProduct($product, $quantity);
}

private function setShippingAddress(CustomerInterface $customer, CartInterface $cart)
Expand All @@ -243,6 +281,20 @@ private function setShippingAddress(CustomerInterface $customer, CartInterface $
$shippingAddress->setCollectShippingRates(true);
$shippingAddress->collectShippingRates();
$shippingAddress->setShippingMethod($this->config->getShippingMethod());

// There are no rates available. Switch to the first available shipping method.
if ($shippingAddress->getShippingRateByCode($this->config->getShippingMethod()) === false &&
count($shippingAddress->getShippingRatesCollection()->getItems()) > 0
) {
$newMethod = $shippingAddress->getShippingRatesCollection()->getFirstItem()->getCode();
$shippingAddress->setShippingMethod($newMethod);

$this->mollieLogger->addInfoLog(
'subscriptions',
'No rates available for ' . $this->config->getShippingMethod() .
', switched to ' . $newMethod
);
}
}

private function getCart(CustomerInterface $customer): CartInterface
Expand All @@ -267,7 +319,7 @@ private function returnOkResponse()
public function getPayment(string $id): Payment
{
try {
$this->api = $this->mollie->getMollieApi();
$this->api = $this->mollieSubscriptionApi->loadByStore();

return $this->api->payments->get($id);
} catch (ApiException $exception) {
Expand Down
12 changes: 6 additions & 6 deletions Controller/Index/Cancel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\ManagerInterface;
use Mollie\Payment\Config;
use Mollie\Payment\Model\Mollie;
use Mollie\Subscriptions\Api\SubscriptionToProductRepositoryInterface;
use Mollie\Subscriptions\Service\Email\SendNotificationEmail;
use Mollie\Subscriptions\Service\Mollie\MollieSubscriptionApi;

class Cancel extends Action implements HttpPostActionInterface
{
Expand All @@ -26,9 +26,9 @@ class Cancel extends Action implements HttpPostActionInterface
private $config;

/**
* @var Mollie
* @var MollieSubscriptionApi
*/
private $mollie;
private $mollieSubscriptionApi;

/**
* @var SubscriptionToProductRepositoryInterface
Expand Down Expand Up @@ -63,7 +63,7 @@ class Cancel extends Action implements HttpPostActionInterface
public function __construct(
Context $context,
Config $config,
Mollie $mollie,
MollieSubscriptionApi $mollieSubscriptionApi,
SubscriptionToProductRepositoryInterface $subscriptionToProductRepository,
CurrentCustomer $currentCustomer,
Session $customerSession,
Expand All @@ -73,7 +73,7 @@ public function __construct(
) {
parent::__construct($context);
$this->config = $config;
$this->mollie = $mollie;
$this->mollieSubscriptionApi = $mollieSubscriptionApi;
$this->subscriptionToProductRepository = $subscriptionToProductRepository;
$this->currentCustomer = $currentCustomer;
$this->customerSession = $customerSession;
Expand All @@ -96,7 +96,7 @@ public function execute()
$customer = $this->currentCustomer->getCustomer();
$extensionAttributes = $customer->getExtensionAttributes();

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

try {
Expand Down
Loading

0 comments on commit 065ec02

Please sign in to comment.