-
Notifications
You must be signed in to change notification settings - Fork 44
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
PIPRES-349: Subscription carrier data provider #858
Merged
mandan2
merged 4 commits into
PIPRES-349-enable-subscription-carrier-change
from
PIPRES-349-subscription-carrier-data-provider
Dec 11, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
subscription/DTO/UpdateSubscriptionCarrierProviderData.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
/** | ||
* Mollie https://www.mollie.nl | ||
* | ||
* @author Mollie B.V. <[email protected]> | ||
* @copyright Mollie B.V. | ||
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md | ||
* | ||
* @see https://github.com/mollie/PrestaShop | ||
* @codingStandardsIgnoreStart | ||
*/ | ||
|
||
namespace Mollie\Subscription\DTO; | ||
|
||
if (!defined('_PS_VERSION_')) { | ||
exit; | ||
} | ||
|
||
class UpdateSubscriptionCarrierProviderData | ||
{ | ||
/** @var string */ | ||
private $mollieCustomerId; | ||
/** @var string */ | ||
private $mollieSubscriptionId; | ||
/** @var int */ | ||
private $subscriptionCarrierId; | ||
/** @var int */ | ||
private $orderId; | ||
|
||
private function __construct( | ||
string $mollieCustomerId, | ||
string $mollieSubscriptionId, | ||
int $subscriptionCarrierId, | ||
int $orderId | ||
) { | ||
$this->mollieCustomerId = $mollieCustomerId; | ||
$this->mollieSubscriptionId = $mollieSubscriptionId; | ||
$this->subscriptionCarrierId = $subscriptionCarrierId; | ||
$this->orderId = $orderId; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getMollieCustomerId(): string | ||
{ | ||
return $this->mollieCustomerId; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getMollieSubscriptionId(): string | ||
{ | ||
return $this->mollieSubscriptionId; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getSubscriptionCarrierId(): int | ||
{ | ||
return $this->subscriptionCarrierId; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getOrderId(): int | ||
{ | ||
return $this->orderId; | ||
} | ||
|
||
public static function create( | ||
string $mollieCustomerId, | ||
string $mollieSubscriptionId, | ||
int $subscriptionCarrierId, | ||
int $orderId | ||
): self { | ||
return new self( | ||
$mollieCustomerId, | ||
$mollieSubscriptionId, | ||
$subscriptionCarrierId, | ||
$orderId | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
subscription/Exception/CouldNotProvideSubscriptionCarrierData.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Mollie https://www.mollie.nl | ||
* | ||
* @author Mollie B.V. <[email protected]> | ||
* @copyright Mollie B.V. | ||
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md | ||
* | ||
* @see https://github.com/mollie/PrestaShop | ||
* @codingStandardsIgnoreStart | ||
*/ | ||
|
||
namespace Mollie\Subscription\Exception; | ||
|
||
if (!defined('_PS_VERSION_')) { | ||
exit; | ||
} | ||
|
||
class CouldNotProvideSubscriptionCarrierData extends MollieSubscriptionException | ||
{ | ||
public static function failedToFindOrder(int $orderId): self | ||
{ | ||
return new self( | ||
sprintf( | ||
'Failed to find order. Order ID: (%s).', | ||
$orderId | ||
), | ||
ExceptionCode::ORDER_FAILED_TO_FIND_ORDER | ||
); | ||
} | ||
|
||
public static function failedToFindSubscriptionProduct(): self | ||
{ | ||
return new self( | ||
'Failed to find subscription product.', | ||
ExceptionCode::ORDER_FAILED_TO_FIND_SUBSCRIPTION_PRODUCT | ||
); | ||
} | ||
|
||
public static function failedToProvideSubscriptionOrderAmount(\Throwable $exception): self | ||
{ | ||
return new self( | ||
'Failed to provide subscription order amount.', | ||
ExceptionCode::ORDER_FAILED_TO_PROVIDE_SUBSCRIPTION_ORDER_AMOUNT, | ||
$exception | ||
); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
subscription/Provider/UpdateSubscriptionCarrierDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* Mollie https://www.mollie.nl | ||
* | ||
* @author Mollie B.V. <[email protected]> | ||
* @copyright Mollie B.V. | ||
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md | ||
* | ||
* @see https://github.com/mollie/PrestaShop | ||
* @codingStandardsIgnoreStart | ||
*/ | ||
|
||
namespace Mollie\Subscription\Provider; | ||
|
||
use Mollie\Factory\ModuleFactory; | ||
use Mollie\Repository\OrderRepositoryInterface; | ||
use Mollie\Subscription\DTO\SubscriptionOrderAmountProviderData; | ||
use Mollie\Subscription\DTO\UpdateSubscriptionCarrierProviderData; | ||
use Mollie\Subscription\DTO\UpdateSubscriptionData; | ||
use Mollie\Subscription\Exception\CouldNotProvideSubscriptionCarrierData; | ||
use Mollie\Subscription\Exception\MollieSubscriptionException; | ||
use Mollie\Utility\SecureKeyUtility; | ||
|
||
if (!defined('_PS_VERSION_')) { | ||
exit; | ||
} | ||
|
||
class UpdateSubscriptionCarrierDataProvider | ||
{ | ||
/** @var OrderRepositoryInterface */ | ||
private $orderRepository; | ||
/** @var \Mollie */ | ||
private $module; | ||
/** @var SubscriptionProductProvider */ | ||
private $subscriptionProductProvider; | ||
/** @var SubscriptionOrderAmountProvider */ | ||
private $subscriptionOrderAmountProvider; | ||
|
||
public function __construct( | ||
OrderRepositoryInterface $orderRepository, | ||
ModuleFactory $moduleFactory, | ||
SubscriptionProductProvider $subscriptionProductProvider, | ||
SubscriptionOrderAmountProvider $subscriptionOrderAmountProvider | ||
) { | ||
$this->orderRepository = $orderRepository; | ||
$this->module = $moduleFactory->getModule(); | ||
$this->subscriptionProductProvider = $subscriptionProductProvider; | ||
$this->subscriptionOrderAmountProvider = $subscriptionOrderAmountProvider; | ||
} | ||
|
||
/** | ||
* @throws MollieSubscriptionException | ||
*/ | ||
public function get(UpdateSubscriptionCarrierProviderData $data): UpdateSubscriptionData | ||
{ | ||
/** @var ?\Order $order */ | ||
$order = $this->orderRepository->findOneBy([ | ||
'id_order' => $data->getOrderId(), | ||
]); | ||
|
||
if (!$order) { | ||
throw CouldNotProvideSubscriptionCarrierData::failedToFindOrder($data->getOrderId()); | ||
} | ||
|
||
$subscriptionProduct = $this->subscriptionProductProvider->getProduct($order->getCartProducts()); | ||
|
||
if (empty($subscriptionProduct)) { | ||
throw CouldNotProvideSubscriptionCarrierData::failedToFindSubscriptionProduct(); | ||
} | ||
|
||
$key = SecureKeyUtility::generateReturnKey( | ||
(int) $order->id_customer, | ||
(int) $order->id_cart, | ||
$this->module->name | ||
); | ||
|
||
$metadata = [ | ||
'secure_key' => $key, | ||
'subscription_carrier_id' => $data->getSubscriptionCarrierId(), | ||
]; | ||
|
||
try { | ||
$orderAmount = $this->subscriptionOrderAmountProvider->get( | ||
SubscriptionOrderAmountProviderData::create( | ||
(int) $order->id_address_delivery, | ||
(int) $order->id_cart, | ||
(int) $order->id_customer, | ||
$subscriptionProduct, | ||
$data->getSubscriptionCarrierId(), | ||
(int) $order->id_currency | ||
) | ||
); | ||
} catch (\Throwable $exception) { | ||
throw CouldNotProvideSubscriptionCarrierData::failedToProvideSubscriptionOrderAmount($exception); | ||
} | ||
|
||
return new UpdateSubscriptionData( | ||
$data->getMollieCustomerId(), | ||
$data->getMollieSubscriptionId(), | ||
null, | ||
$metadata, | ||
$orderAmount | ||
); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this null if it's really needed in any way