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

PIPRES-349: Subscription carrier data provider #858

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
6 changes: 5 additions & 1 deletion subscription/Api/SubscriptionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ public function getSubscription(GetSubscriptionData $subscriptionData): MollieSu
public function updateSubscription(UpdateSubscriptionData $updateSubscriptionData): MollieSubscription
{
try {
return $this->apiClient->subscriptions->update($updateSubscriptionData->getCustomerId(), $updateSubscriptionData->getSubscriptionId(), $updateSubscriptionData->jsonSerialize());
return $this->apiClient->subscriptions->update(
$updateSubscriptionData->getCustomerId(),
$updateSubscriptionData->getSubscriptionId(),
$updateSubscriptionData->toArray()
);
} catch (ApiException $e) {
throw new SubscriptionApiException('Failed to update subscription', SubscriptionApiException::UPDATE_FAILED, $e);
}
Expand Down
9 changes: 9 additions & 0 deletions subscription/DTO/Object/Amount.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public function __construct(float $value, string $currency)
$this->currency = $currency;
}

public function toArray(): array
{
return [
'value' => (string) number_format($this->value, 2, '.', ''),
'currency' => $this->currency,
];
}

// TODO jsonSerialize should be only used for json_encode operation. If values needs to be casted to array, use method above.
public function jsonSerialize(): array
{
return [
Expand Down
87 changes: 87 additions & 0 deletions subscription/DTO/UpdateSubscriptionCarrierProviderData.php
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
);
}
}
33 changes: 24 additions & 9 deletions subscription/DTO/UpdateSubscriptionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,37 @@

namespace Mollie\Subscription\DTO;

use JsonSerializable;
use Mollie\Subscription\DTO\Object\Amount;

if (!defined('_PS_VERSION_')) {
exit;
}

class UpdateSubscriptionData implements JsonSerializable
class UpdateSubscriptionData
{
/** @var string */
private $customerId;

/** @var string */
private $subscriptionId;

/** @var string */
/** @var ?string */
private $mandateId;
/** @var ?array */
private $metadata;
/** @var ?Amount */
private $amount;

public function __construct(string $customerId, string $subscriptionId, string $mandateId)
{
public function __construct(
string $customerId,
string $subscriptionId,
string $mandateId = null,
array $metadata = null,
Amount $amount = null
) {
$this->customerId = $customerId;
$this->subscriptionId = $subscriptionId;
$this->mandateId = $mandateId;
$this->metadata = $metadata;
$this->amount = $amount;
}

public function getCustomerId(): string
Expand All @@ -48,10 +57,16 @@ public function getSubscriptionId(): string
return $this->subscriptionId;
}

public function jsonSerialize(): array
public function toArray(): array
{
return [
$data = [
'mandateId' => $this->mandateId,
'metadata' => $this->metadata,
'amount' => $this->amount ? $this->amount->toArray() : null,
];

return array_filter($data, static function ($val) {
return !empty($val);
});
}
}
48 changes: 48 additions & 0 deletions subscription/Exception/CouldNotProvideSubscriptionCarrierData.php
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 subscription/Provider/UpdateSubscriptionCarrierDataProvider.php
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,
Copy link
Contributor

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

$metadata,
$orderAmount
);
}
}
Loading
Loading