Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/1.13.0 #71

Merged
merged 7 commits into from
Oct 24, 2023
Merged
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
10 changes: 9 additions & 1 deletion Controller/Api/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Mollie\Subscriptions\Service\Magento\CreateOrderFromSubscription;
use Mollie\Subscriptions\Service\Mollie\RetryUsingOtherStoreViews;
use Mollie\Subscriptions\Service\Mollie\SendAdminNotification;
use Mollie\Subscriptions\Service\Mollie\UpdateNextPaymentDate;

class Webhook extends Action implements CsrfAwareActionInterface
{
Expand Down Expand Up @@ -89,6 +90,10 @@ class Webhook extends Action implements CsrfAwareActionInterface
* @var OrderCommentHistory
*/
private $orderCommentHistory;
/**
* @var UpdateNextPaymentDate
*/
private $updateNextPaymentDate;

public function __construct(
Context $context,
Expand All @@ -102,7 +107,8 @@ public function __construct(
LinkTransactionToOrder $linkTransactionToOrder,
OrderCommentHistory $orderCommentHistory,
SendAdminNotification $sendAdminNotification,
CreateOrderFromSubscription $createOrderFromSubscription
CreateOrderFromSubscription $createOrderFromSubscription,
UpdateNextPaymentDate $updateNextPaymentDate
) {
parent::__construct($context);

Expand All @@ -117,6 +123,7 @@ public function __construct(
$this->orderCommentHistory = $orderCommentHistory;
$this->sendAdminNotification = $sendAdminNotification;
$this->createOrderFromSubscription = $createOrderFromSubscription;
$this->updateNextPaymentDate = $updateNextPaymentDate;
}

public function execute()
Expand Down Expand Up @@ -145,6 +152,7 @@ public function execute()
try {
$molliePayment = $this->getPayment($id);
$subscription = $this->api->subscriptions->getForId($molliePayment->customerId, $molliePayment->subscriptionId);
$this->updateNextPaymentDate->execute($subscription);

$order = $this->createOrderFromSubscription->execute($this->api, $molliePayment, $subscription);

Expand Down
17 changes: 14 additions & 3 deletions Service/Magento/CreateOrderFromSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Mollie\Subscriptions\Service\Magento;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
Expand Down Expand Up @@ -84,6 +85,11 @@ class CreateOrderFromSubscription
*/
private $orderAddressRepository;

/**
* @var ProductInterface
*/
private $product;

public function __construct(
Config $config,
MollieCustomerRepositoryInterface $mollieCustomerRepository,
Expand Down Expand Up @@ -128,7 +134,10 @@ public function execute(MollieApiClient $api, Payment $molliePayment, Subscripti
$this->addProduct($cart);

$cart->setBillingAddress($this->formatAddress($this->getAddress('billing')));
$this->setShippingAddress($cart);

if (!$this->product->getIsVirtual()) {
$this->setShippingAddress($cart);
}

$cart->getPayment()->addData(['method' => 'mollie_methods_' . $molliePayment->method]);

Expand Down Expand Up @@ -159,9 +168,11 @@ private function addProduct(CartInterface $cart)
$sku = $this->subscription->metadata->sku;
$quantity = isset($this->subscription->metadata) && isset($this->subscription->metadata->quantity) ?
(float)$this->subscription->metadata->quantity : 1;
$product = $this->productRepository->get($sku);
$this->product = $this->productRepository->get($sku);

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

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

/**
Expand Down
11 changes: 11 additions & 0 deletions Service/Mollie/GetShippingCostForOrderItem.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Subscriptions\Service\Mollie;

Expand Down Expand Up @@ -53,7 +59,12 @@ public function __construct(

public function execute(OrderInterface $order, OrderItemInterface $orderItem): float
{
if ($order->getIsVirtual()) {
return 0.0;
}

$this->order = $order;

$result = $this->getCarrierResult($orderItem);

if ($price = $this->getRateByCarrier($result)) {
Expand Down
9 changes: 8 additions & 1 deletion Service/Mollie/SubscriptionOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ private function addAmount(): void

private function addShippingCost(): void
{
if ($this->orderItem->getIsVirtual()) {
return;
}

$shippingCost = $this->getShippingCostForOrderItem->execute($this->order, $this->orderItem);
$this->options['amount'] += $shippingCost;
}
Expand Down Expand Up @@ -176,8 +180,11 @@ private function addMetadata(): void
'sku' => $product->getSku(),
'quantity' => $this->orderItem->getQtyOrdered(),
'billingAddressId' => $this->order->getBillingAddressId(),
'shippingAddressId' => $this->order->getshippingAddressId(),
];

if (!$this->orderItem->getIsVirtual()) {
$this->options['metadata']['shippingAddressId'] = $this->order->getshippingAddressId();
}
}

private function addWebhookUrl(): void
Expand Down
33 changes: 33 additions & 0 deletions Service/Mollie/UpdateNextPaymentDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Subscriptions\Service\Mollie;

use Mollie\Api\Resources\Subscription;
use Mollie\Subscriptions\Api\SubscriptionToProductRepositoryInterface;

class UpdateNextPaymentDate
{
/**
* @var SubscriptionToProductRepositoryInterface
*/
private $subscriptionToProductRepository;

public function __construct(
SubscriptionToProductRepositoryInterface $subscriptionToProductRepository
) {
$this->subscriptionToProductRepository = $subscriptionToProductRepository;
}

public function execute(Subscription $subscription): void
{
$row = $this->subscriptionToProductRepository->getBySubscriptionId($subscription->id);
$row->setNextPaymentDate($subscription->nextPaymentDate);
$this->subscriptionToProductRepository->save($row);
}
}
57 changes: 57 additions & 0 deletions Test/Integration/Controller/Api/WebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@
use Mollie\Payment\Model\Mollie;
use Mollie\Payment\Test\Fakes\FakeEncryptor;
use Mollie\Payment\Test\Integration\MolliePaymentBuilder;
use Mollie\Subscriptions\Api\Data\SubscriptionToProductInterface;
use Mollie\Subscriptions\Api\SubscriptionToProductRepositoryInterface;
use Mollie\Subscriptions\Service\Mollie\MollieSubscriptionApi;

class WebhookTest extends ControllerTestCase
{
/**
* @var Subscription
*/
private $subscription;

public function testAcceptsPost()
{
$instance = $this->_objectManager->get(FakeEncryptor::class);
Expand Down Expand Up @@ -113,6 +120,38 @@ public function testDoesNotCreateMultipleOrders(): void
$this->assertEquals(0, $spy->getInvocationCount());
}

/**
* @magentoDataFixture Magento/Customer/_files/customer_with_addresses.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoConfigFixture default_store mollie_subscriptions/general/shipping_method flatrate_flatrate
*/
public function testUpdatesNextPaymentDate(): void
{
$transactionId = 'tr_testtransaction';

$this->createMollieCustomer();
$api = $this->getApi($transactionId);

$mollieSubscriptionApi = $this->createMock(MollieSubscriptionApi::class);
$mollieSubscriptionApi->method('loadByStore')->willReturn($api);
$this->_objectManager->addSharedInstance($mollieSubscriptionApi, MollieSubscriptionApi::class);

// Check how many orders there are before the webhook is called
$ordersCount = count($this->getOrderIdsByTransactionId($transactionId));

$mollieMock = $this->createMock(Mollie::class);
$mollieMock->method('processTransactionForOrder');
$this->_objectManager->addSharedInstance($mollieMock, Mollie::class);

$this->dispatch('mollie-subscriptions/api/webhook?id=' . $transactionId);
$this->assertEquals(200, $this->getResponse()->getStatusCode());

$repository = $this->_objectManager->create(SubscriptionToProductRepositoryInterface::class);
$subscription = $repository->getBySubscriptionId('sub_testsubscription');

$this->assertEquals('2019-11-19', $subscription->getNextPaymentDate());
}

private function createMollieCustomer(): void
{
/** @var CustomerRepositoryInterface $customerRepository */
Expand Down Expand Up @@ -155,9 +194,11 @@ private function getSubscription(): Subscription
{
/** @var Subscription $subscription */
$subscription = $this->_objectManager->get(Subscription::class);
$subscription->id = 'sub_testsubscription';
$subscription->customerId = 'cst_testcustomer';
$subscription->metadata = new \stdClass();
$subscription->metadata->sku = 'simple';
$subscription->nextPaymentDate = '2019-11-19';
return $subscription;
}

Expand All @@ -169,6 +210,7 @@ private function getPayment(string $transactionId): Payment

$payment->id = $transactionId;
$payment->customerId = 'cst_testcustomer';
$payment->subscriptionId = 'sub_testsubscription';
$payment->_links = new \stdClass();
$payment->_links->subscription = new \stdClass();
$payment->_links->subscription->href = 'https://example.com/mollie/subscriptions/sub_testsubscription';
Expand All @@ -179,6 +221,9 @@ private function getPayment(string $transactionId): Payment
private function getApi(string $transactionId): MollieApiClient
{
$subscription = $this->getSubscription();
$this->subscription = $subscription;

$this->createSubscriptionDatabaseRecord();

$subscriptionsEndpointMock = $this->createMock(SubscriptionEndpoint::class);
$subscriptionsEndpointMock->method('getForId')->willReturn($subscription);
Expand Down Expand Up @@ -206,4 +251,16 @@ private function loadOrderById($orderId): OrderInterface

return array_shift($orderList);
}

private function createSubscriptionDatabaseRecord(): void
{
/** @var SubscriptionToProductInterface $subscription */
$subscription = $this->_objectManager->create(SubscriptionToProductInterface::class);
$subscription->setSubscriptionId('sub_testsubscription');
$subscription->setNextPaymentDate('2019-11-12');
$subscription->setCustomerId('cst_testcustomer');
$subscription->setProductId(1);

$this->_objectManager->get(SubscriptionToProductRepositoryInterface::class)->save($subscription);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Mollie\Subscriptions\Test\Integration\Service\Magento;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Mollie\Api\MollieApiClient;
Expand Down Expand Up @@ -50,6 +52,43 @@ public function testUsesOrderBillingAndShippingAddresses(): void
$this->expectNotToPerformAssertions();
}

/**
* @magentoDataFixture Magento/Customer/_files/customer_with_addresses.php
* @magentoDataFixture Magento/Sales/_files/order.php
*
* @return void
*/
public function testHandlesVirtualProductsCorrect(): void
{
$this->createMollieCustomer();
$order = $this->loadOrderById('100000001');

/** @var ProductInterface $product */
$product = $this->objectManager->get(ProductRepositoryInterface::class)->get('simple');
$product->setTypeId('virtual');

$molliePaymentBuilder = $this->objectManager->get(MolliePaymentBuilder::class);
$molliePaymentBuilder->setMethod('ideal');

$payment = $molliePaymentBuilder->build();
$payment->customerId = 'cst_testcustomer';

$subscription = $this->objectManager->get(Subscription::class);
$subscription->customerId = 'cst_testcustomer';
$subscription->metadata = new \stdClass();
$subscription->metadata->quantity = '1';
$subscription->metadata->sku = 'simple';

// If these aren't processed, the test will fail due to the customer not having a billing address
$subscription->metadata->billingAddressId = $order->getBillingAddressId();

$instance = $this->objectManager->create(CreateOrderFromSubscription::class);

$order = $instance->execute(new MollieApiClient(), $payment, $subscription);

$this->assertSame(1, $order->getIsVirtual());
}

private function createMollieCustomer(): void
{
/** @var CustomerRepositoryInterface $customerRepository */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Subscriptions\Test\Integration\Service\Mollie;

use Mollie\Payment\Test\Integration\IntegrationTestCase;
use Mollie\Subscriptions\Service\Mollie\GetShippingCostForOrderItem;

class GetShippingCostForOrderItemTest extends IntegrationTestCase
{
/**
* @magentoDataFixture Magento/Sales/_files/order.php
* @return void
*/
public function testDoesNotAddShippingCostsForVirtualOrders(): void
{
$order = $this->loadOrder('100000001');
$order->setIsVirtual(1);

$items = $order->getItems();
/** @var GetShippingCostForOrderItem $instance */
$instance = $this->objectManager->create(GetShippingCostForOrderItem::class);

$result = $instance->execute($order, array_shift($items));

$this->assertSame(0.0, $result);
}
}
Loading