Skip to content

Commit

Permalink
Update PaymentFactory
Browse files Browse the repository at this point in the history
now uses callbacks to container objects
namespaces were fixed
dependencies fixed
  • Loading branch information
mmaymo committed Dec 18, 2024
1 parent 3c71099 commit 67df6cd
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 178 deletions.
25 changes: 17 additions & 8 deletions src/Payment/MollieOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
use Mollie\Api\Resources\Payment;
use Mollie\Api\Resources\Order;
use Mollie\Api\Resources\Refund;
use Mollie\WooCommerce\Gateway\MolliePaymentGatewayHandler;
use Mollie\WooCommerce\Payment\Request\RequestFactory;
use Mollie\WooCommerce\PaymentMethods\Voucher;
use Mollie\WooCommerce\SDK\Api;
use Mollie\WooCommerce\Settings\Settings;
use Mollie\WooCommerce\Shared\Data;
use Mollie\WooCommerce\Shared\SharedDataDictionary;
use Psr\Log\LoggerInterface as Logger;

use Psr\Log\LogLevel;
use WC_Order;
use WP_Error;
Expand Down Expand Up @@ -47,24 +51,24 @@ class MollieOrder extends MollieObject
public function __construct(
OrderItemsRefunder $orderItemsRefunder,
$data,
$pluginId,
string $pluginId,
Api $apiHelper,
$settingsHelper,
$dataHelper,
$logger,
Settings $settingsHelper,
Data $dataHelper,
Logger $logger,
OrderLines $orderLines,
$paymentMethod
RequestFactory $requestFactory
)
{
$this->data = $data;
$this->orderItemsRefunder = $orderItemsRefunder;
$this->pluginId = $pluginId;
$this->apiHelper = $apiHelper;
$this->settingsHelper = $settingsHelper;
$this->dataHelper = $dataHelper;
$this->logger = $logger;
$this->requestFactory = $requestFactory;
$this->dataHelper = $dataHelper;
$this->orderLines = $orderLines;
$this->paymentMethod = $paymentMethod;
}

public function getPaymentObject($paymentId, $testMode = false, $useCache = true)
Expand Down Expand Up @@ -914,4 +918,9 @@ protected function processOrderItemsRefund(
// drop item from array
unset($items[$item->get_id()]);
}

public function setOrder( $data)
{
$this->data = $data;
}
}
33 changes: 22 additions & 11 deletions src/Payment/MolliePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,44 @@

use Inpsyde\PaymentGateway\PaymentGateway;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Resources\Order;
use Mollie\Api\Resources\Payment;
use Mollie\Api\Resources\Refund;
use Mollie\WooCommerce\Gateway\MolliePaymentGatewayHandler;
use Mollie\WooCommerce\Gateway\MolliePaymentGatewayI;
use Mollie\WooCommerce\Payment\Request\RequestFactory;
use Mollie\WooCommerce\PaymentMethods\Voucher;
use Mollie\WooCommerce\SDK\Api;
use Mollie\WooCommerce\Settings\Settings;
use Mollie\WooCommerce\Shared\Data;
use Mollie\WooCommerce\Shared\SharedDataDictionary;
use Psr\Log\LoggerInterface as Logger;

use Psr\Log\LogLevel;
use WC_Order;
use WC_Payment_Gateway;
use WC_Subscriptions_Manager;
use WP_Error;

class MolliePayment extends MollieObject
{
public const ACTION_AFTER_REFUND_PAYMENT_CREATED = 'mollie-payments-for-woocommerce' . '_refund_payment_created';
protected $pluginId;
/**
* @var mixed
*/
private $paymentMethod;

public function __construct($data, $pluginId, Api $apiHelper, $settingsHelper, $dataHelper, $logger, $paymentMethod)

public function __construct(
$data,
string $pluginId,
Api $apiHelper,
Settings $settingsHelper,
Data $dataHelper,
Logger $logger,
RequestFactory $requestFactory
)
{
$this->data = $data;
$this->pluginId = $pluginId;
$this->apiHelper = $apiHelper;
$this->settingsHelper = $settingsHelper;
$this->dataHelper = $dataHelper;
$this->logger = $logger;
$this->paymentMethod = $paymentMethod;
$this->requestFactory = $requestFactory;
$this->dataHelper = $dataHelper;
}

public function getPaymentObject($paymentId, $testMode = false, $useCache = true)
Expand Down Expand Up @@ -469,4 +475,9 @@ protected function maybeUpdateStatus(
}
$this->updateOrderStatus($order, $newOrderStatus);
}

public function setPayment( $data)
{
$this->data = $data;
}
}
66 changes: 12 additions & 54 deletions src/Payment/PaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,86 +12,44 @@

class PaymentFactory
{
/**
* @var Data
*/
protected $dataHelper;
/**
* @var Api
*/
protected $apiHelper;
protected $settingsHelper;
/**
* @var string
*/
protected $pluginId;
protected $logger;
/**
* @var OrderLines
*/
protected $orderLines;
private $mollieOrderFactory;
private $molliePaymentFactory;

/**
* PaymentFactory constructor.
*/
public function __construct(Data $dataHelper, Api $apiHelper, Settings $settingsHelper, string $pluginId, $logger, OrderLines $orderLines)
public function __construct(callable $mollieOrderFactory, callable $molliePaymentFactory)
{
$this->dataHelper = $dataHelper;
$this->apiHelper = $apiHelper;
$this->settingsHelper = $settingsHelper;
$this->pluginId = $pluginId;
$this->logger = $logger;
$this->orderLines = $orderLines;
$this->mollieOrderFactory = $mollieOrderFactory;
$this->molliePaymentFactory = $molliePaymentFactory;
}

/**
* @param $data
* @return bool|MollieOrder|MolliePayment
* @throws ApiException
*/
public function getPaymentObject($data, $paymentMethod = null)
public function getPaymentObject($data)
{

if (
(!is_object($data) && $data === 'order')
|| (is_string($data) && strpos($data, 'ord_') !== false)
|| (is_object($data) && $data->resource === 'order')
) {
$refundLineItemsBuilder = new RefundLineItemsBuilder($this->dataHelper);
$apiKey = $this->settingsHelper->getApiKey();
$orderItemsRefunded = new OrderItemsRefunder(
$refundLineItemsBuilder,
$this->dataHelper,
$this->apiHelper->getApiClient($apiKey)->orders
);

return new MollieOrder(
$orderItemsRefunded,
$data,
$this->pluginId,
$this->apiHelper,
$this->settingsHelper,
$this->dataHelper,
$this->logger,
$this->orderLines,
$paymentMethod
);
$mollieOrder = ($this->mollieOrderFactory)();
$mollieOrder->setOrder($data);
return $mollieOrder;
}

if (
(!is_object($data) && $data === 'payment')
|| (!is_object($data) && strpos($data, 'tr_') !== false)
|| (is_object($data) && $data->resource === 'payment')
) {
return new MolliePayment(
$data,
$this->pluginId,
$this->apiHelper,
$this->settingsHelper,
$this->dataHelper,
$this->logger,
$paymentMethod
);
$molliePayment = ($this->molliePaymentFactory)();
$molliePayment->setPayment($data);
return $molliePayment;
}

return false;
Expand Down
3 changes: 1 addition & 2 deletions src/Payment/PaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,7 @@ protected function processAsMolliePayment(
) {

$paymentObject = $this->paymentFactory->getPaymentObject(
self::PAYMENT_METHOD_TYPE_PAYMENT,
$paymentMethod
self::PAYMENT_METHOD_TYPE_PAYMENT
);
$paymentRequestData = $paymentObject->getPaymentRequestData(
$order,
Expand Down
19 changes: 10 additions & 9 deletions src/Payment/Request/Decorators/AddCustomRequestFieldsDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Decorator;
namespace Mollie\WooCommerce\Payment\Request\Decorators;

use Mollie\WooCommerce\Payment\Request\Decorators\RequestDecoratorInterface;
use Mollie\WooCommerce\PaymentMethods\PaymentMethodI;
use Psr\Container\ContainerInterface;
use WC_Order;

class AddCustomRequestFieldsDecorator implements RequestDecoratorInterface
{
private PaymentMethodI $paymentMethod;
private array $paymentMethods;
private ContainerInterface $container;

public function __construct($paymentMethod, $container)
public function __construct($paymentMethods, $container)
{
$this->paymentMethod = $paymentMethod;
$this->paymentMethods = $paymentMethods;
$this->container = $container;
}

public function decorate(array $requestData, WC_Order $order): array
public function decorate(array $requestData, WC_Order $order, $context = null): array
{
if (property_exists($this->paymentMethod, 'paymentAPIfields')) {
$paymentAPIfields = $this->paymentMethod->paymentAPIfields;
$gateway = wc_get_payment_gateway_by_order($order);
$methodId = substr($gateway->id, strrpos($gateway->id, '_') + 1);
$paymentMethod = $this->paymentMethods[$methodId];
if (property_exists($paymentMethod, 'paymentAPIfields')) {
$paymentAPIfields = $paymentMethod->paymentAPIfields;
foreach ($paymentAPIfields as $field) {
$decoratorClass = 'Mollie\\WooCommerce\\Payment\\Decorator\\' . $field;
if (class_exists($decoratorClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Decorator;
namespace Mollie\WooCommerce\Payment\Request\Decorators;

use Mollie\WooCommerce\Payment\Request\Decorators\RequestDecoratorInterface;
use Mollie\WooCommerce\Shared\Data;
use WC_Order;

Expand All @@ -19,7 +18,7 @@ public function __construct($dataHelper, $pluginId)
$this->pluginId = $pluginId;
}

public function decorate(array $requestData, WC_Order $order, $context): array
public function decorate(array $requestData, WC_Order $order, $context = null): array
{
$gateway = wc_get_payment_gateway_by_order($order);
if ($gateway) {
Expand Down
5 changes: 2 additions & 3 deletions src/Payment/Request/Decorators/AddressDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Decorator;
namespace Mollie\WooCommerce\Payment\Request\Decorators;

use Mollie\WooCommerce\Payment\Request\Decorators\RequestDecoratorInterface;
use WC_Order;

class AddressDecorator implements RequestDecoratorInterface
{
public function decorate(array $requestData, WC_Order $order): array
public function decorate(array $requestData, WC_Order $order, $context = null): array
{
$isPayPalExpressOrder = $order->get_meta('_mollie_payment_method_button') === 'PayPalButton';
$billingAddress = null;
Expand Down
5 changes: 2 additions & 3 deletions src/Payment/Request/Decorators/ApplePaytokenDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Decorator;
namespace Mollie\WooCommerce\Payment\Request\Decorators;

use Mollie\WooCommerce\Payment\Request\Decorators\RequestDecoratorInterface;
use WC_Order;

class ApplePayTokenDecorator implements RequestDecoratorInterface
{
public function decorate(array $requestData, WC_Order $order, $context): array
public function decorate(array $requestData, WC_Order $order, $context = null): array
{
// phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$applePayToken = wc_clean(wp_unslash($_POST["token"] ?? ''));
Expand Down
5 changes: 2 additions & 3 deletions src/Payment/Request/Decorators/CardTokenDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Decorator;
namespace Mollie\WooCommerce\Payment\Request\Decorators;

use Mollie\WooCommerce\Payment\Request\Decorators\RequestDecoratorInterface;
use WC_Order;

class CardTokenDecorator implements RequestDecoratorInterface
{
public function decorate(array $requestData, WC_Order $order, $context): array
public function decorate(array $requestData, WC_Order $order, $context = null): array
{
$cardToken = mollieWooCommerceCardToken();
if ($cardToken && isset($requestData['payment']) && $context === 'order') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(array $paymentMethods)
$this->paymentMethods = $paymentMethods;
}

public function decorate(array $requestData, WC_Order $order): array
public function decorate(array $requestData, WC_Order $order, $context = null): array
{
$gateway = wc_get_payment_gateway_by_order($order);
if (!$gateway || !isset($gateway->id)) {
Expand Down
5 changes: 2 additions & 3 deletions src/Payment/Request/Decorators/OrderLinesDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Decorator;
namespace Mollie\WooCommerce\Payment\Request\Decorators;

use Mollie\WooCommerce\Payment\OrderLines;
use Mollie\WooCommerce\Payment\Request\Decorators\RequestDecoratorInterface;
use WC_Order;

class OrderLinesDecorator implements RequestDecoratorInterface
Expand All @@ -19,7 +18,7 @@ public function __construct($orderLines, $voucherDefaultCategory)
$this->voucherDefaultCategory = $voucherDefaultCategory;
}

public function decorate(array $requestData, WC_Order $order): array
public function decorate(array $requestData, WC_Order $order, $context = null): array
{
$orderLines = $this->orderLines->order_lines($order, $this->voucherDefaultCategory);
$requestData['lines'] = $orderLines['lines'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Decorator;
namespace Mollie\WooCommerce\Payment\Request\Decorators;

use Mollie\WooCommerce\Payment\Request\Decorators\RequestDecoratorInterface;
use WC_Order;

class PaymentDescriptionDecorator implements RequestDecoratorInterface
Expand Down
Loading

0 comments on commit 67df6cd

Please sign in to comment.