Skip to content

Commit

Permalink
Update docblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Jan 3, 2025
1 parent 467c059 commit 17f39e8
Show file tree
Hide file tree
Showing 18 changed files with 540 additions and 128 deletions.
3 changes: 0 additions & 3 deletions src/Payment/MollieObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Mollie\WooCommerce\Payment;

use Inpsyde\PaymentGateway\PaymentGateway;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Resources\Order;
use Mollie\Api\Resources\Payment;
use Mollie\WooCommerce\Gateway\MolliePaymentGatewayHandler;
use Mollie\WooCommerce\Payment\Request\RequestFactory;
use Mollie\WooCommerce\PaymentMethods\Voucher;
use Mollie\WooCommerce\SDK\Api;
Expand All @@ -17,7 +15,6 @@
use WC_Order;
use WC_Payment_Gateway;
use Psr\Log\LoggerInterface as Logger;
use stdClass;

class MollieObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,40 @@
use Psr\Container\ContainerInterface;
use WC_Order;

/**
* Class AddCustomRequestFieldsMiddleware
*
* This middleware adds custom request fields to the payment request data.
*
* @package Mollie\WooCommerce\Payment\Request\Middleware
*/
class AddCustomRequestFieldsMiddleware implements RequestMiddlewareInterface
{
private array $paymentMethods;
private ContainerInterface $container;

/**
* AddCustomRequestFieldsMiddleware constructor.
*
* @param array $paymentMethods An array of available payment methods.
* @param ContainerInterface $container A container for dependency injection.
*/
public function __construct($paymentMethods, $container)
{
$this->paymentMethods = $paymentMethods;
$this->container = $container;
}

public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array
/**
* Invoke the middleware.
*
* @param array $requestData The request data to be modified.
* @param WC_Order $order The WooCommerce order object.
* @param mixed $context Additional context for the middleware.
* @param callable $next The next middleware to be called.
* @return array The modified request data.
*/
public function __invoke(array $requestData, WC_Order $order, $context, $next): array
{
$gateway = wc_get_payment_gateway_by_order($order);
$methodId = substr($gateway->id, strrpos($gateway->id, '_') + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,43 @@
use Mollie\WooCommerce\Shared\Data;
use WC_Order;

/**
* Middleware to add sequence type for subscription payments.
*/
class AddSequenceTypeForSubscriptionsMiddleware implements RequestMiddlewareInterface
{
/**
* @var Data Helper class for data operations.
*/
private Data $dataHelper;

/**
* @var string Plugin ID.
*/
private string $pluginId;

/**
* Constructor.
*
* @param Data $dataHelper Helper class for data operations.
* @param string $pluginId Plugin ID.
*/
public function __construct($dataHelper, $pluginId)
{
$this->dataHelper = $dataHelper;
$this->pluginId = $pluginId;
}

public function __invoke(array $requestData, WC_Order $order, $context = null, callable $next): array
/**
* Invoke the middleware.
*
* @param array $requestData The request data.
* @param WC_Order $order The WooCommerce order object.
* @param string $context The context of the request.
* @param callable $next The next middleware to call.
* @return array The modified request data.
*/
public function __invoke(array $requestData, WC_Order $order, $context, callable $next): array
{
$gateway = wc_get_payment_gateway_by_order($order);
if ($gateway) {
Expand All @@ -27,6 +52,15 @@ public function __invoke(array $requestData, WC_Order $order, $context = null, c
return $next($requestData, $order, $context);
}

/**
* Add sequence type for the first payments of subscriptions.
*
* @param int $orderId The order ID.
* @param WC_Payment_Gateway $gateway The payment gateway.
* @param array $requestData The request data.
* @param string $context The context of the request.
* @return array The modified request data.
*/
private function addSequenceTypeForSubscriptionsFirstPayments($orderId, $gateway, $requestData, $context): array
{
if ($this->dataHelper->isSubscription($orderId) || $this->dataHelper->isWcSubscription($orderId)) {
Expand All @@ -40,6 +74,13 @@ private function addSequenceTypeForSubscriptionsFirstPayments($orderId, $gateway
return $requestData;
}

/**
* Add the sequence type 'first' to the request data.
*
* @param array $requestData The request data.
* @param string $context The context of the request.
* @return array The modified request data.
*/
private function addSequenceTypeFirst($requestData, $context)
{
if ($context === 'order') {
Expand Down
80 changes: 63 additions & 17 deletions src/Payment/Request/Middleware/AddressMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,30 @@
use stdClass;
use WC_Order;

/**
* Class AddressMiddleware
*
* This middleware adds address information to the payment request data.
*
* @package Mollie\WooCommerce\Payment\Request\Middleware
*/
class AddressMiddleware implements RequestMiddlewareInterface
{
public const MAXIMAL_LENGTH_ADDRESS = 100;
public const MAXIMAL_LENGTH_POSTALCODE = 20;
public const MAXIMAL_LENGTH_CITY = 200;
public const MAXIMAL_LENGTH_REGION = 200;
public function __invoke(array $requestData, WC_Order $order, $context = null, callable $next): array

/**
* Invoke the middleware.
*
* @param array<string, mixed> $requestData The request data to be modified.
* @param WC_Order $order The WooCommerce order object.
* @param mixed $context Additional context for the middleware.
* @param callable $next The next middleware to be called.
* @return array<string, mixed> The modified request data.
*/
public function __invoke(array $requestData, WC_Order $order, $context, callable $next): array
{
$isPayPalExpressOrder = $order->get_meta('_mollie_payment_method_button') === 'PayPalButton';
$billingAddress = null;
Expand All @@ -34,7 +51,13 @@ public function __invoke(array $requestData, WC_Order $order, $context = null, c
return $next($requestData, $order, $context);
}

private function createBillingAddress(WC_Order $order)
/**
* Create the billing address object.
*
* @param WC_Order $order The WooCommerce order object.
* @return stdClass The billing address object.
*/
private function createBillingAddress(WC_Order $order): stdClass
{
// Setup billing and shipping objects
$billingAddress = new stdClass();
Expand Down Expand Up @@ -99,7 +122,13 @@ private function createBillingAddress(WC_Order $order)
return $billingAddress;
}

private function createShippingAddress(WC_Order $order)
/**
* Create the shipping address object.
*
* @param WC_Order $order The WooCommerce order object.
* @return stdClass The shipping address object.
*/
private function createShippingAddress(WC_Order $order): stdClass
{
$shippingAddress = new stdClass();
// Get user details
Expand All @@ -113,7 +142,6 @@ private function createShippingAddress(WC_Order $order)
? null
: $order->get_billing_email(); // WooCommerce doesn't have a shipping email


// Create shippingAddress object
$shippingAddress->streetAndNumber = (ctype_space(
$order->get_shipping_address_1()
Expand Down Expand Up @@ -162,18 +190,29 @@ private function createShippingAddress(WC_Order $order)
return $shippingAddress;
}

protected function getPhoneNumber($order)
/**
* Get the phone number from the order.
*
* @param WC_Order $order The WooCommerce order object.
* @return string|null The phone number.
*/
protected function getPhoneNumber($order): ?string
{

$phone = !empty($order->get_billing_phone()) ? $order->get_billing_phone() : $order->get_shipping_phone();
if (empty($phone)) {
//phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$phone = wc_clean(wp_unslash($_POST['billing_phone'] ?? ''));
$phone = wc_clean(wp_unslash($_POST['billing_phone'] ?? ''));
}
return $phone;
}

protected function getFormatedPhoneNumber(string $phone)
/**
* Format the phone number.
*
* @param string $phone The phone number.
* @return string|null The formatted phone number.
*/
protected function getFormatedPhoneNumber(string $phone): ?string
{
//remove whitespaces and all non numerical characters except +
$phone = preg_replace('/[^0-9+]+/', '', $phone);
Expand All @@ -191,8 +230,10 @@ protected function getFormatedPhoneNumber(string $phone)
}

/**
* @param $order
* @return string|null
* Get the billing company field.
*
* @param WC_Order $order The WooCommerce order object.
* @return string|null The billing company field.
*/
public function billingCompanyField($order): ?string
{
Expand All @@ -205,7 +246,13 @@ public function billingCompanyField($order): ?string
);
}

private function checkBillieCompanyField($order)
/**
* Check the Billie company field.
*
* @param WC_Order $order The WooCommerce order object.
* @return string|null The Billie company field.
*/
private function checkBillieCompanyField($order): ?string
{
$gateway = wc_get_payment_gateway_by_order($order);
if (!$gateway || !$gateway->id) {
Expand All @@ -227,14 +274,13 @@ private function checkBillieCompanyField($order)
}

/**
* Method that shortens the field to a certain length
*
* @param string $field
* @param int $maximalLength
* Method that shortens the field to a certain length.
*
* @return null|string
* @param string $field The field to be shortened.
* @param int $maximalLength The maximal length of the field.
* @return string|null The shortened field.
*/
protected function maximalFieldLengths($field, $maximalLength)
protected function maximalFieldLengths($field, $maximalLength): ?string
{
if (!is_string($field)) {
return null;
Expand Down
14 changes: 13 additions & 1 deletion src/Payment/Request/Middleware/ApplePayTokenMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@

use WC_Order;

/**
* Middleware to handle Apple Pay token in the request.
*/
class ApplePayTokenMiddleware implements RequestMiddlewareInterface
{
public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array
/**
* Invoke the middleware.
*
* @param array $requestData The request data.
* @param WC_Order $order The WooCommerce order object.
* @param string $context The context of the request.
* @param callable $next The next middleware to call.
* @return array The modified request data.
*/
public function __invoke(array $requestData, WC_Order $order, $context, $next): array
{
// phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$applePayToken = wc_clean(wp_unslash($_POST["token"] ?? ''));
Expand Down
14 changes: 13 additions & 1 deletion src/Payment/Request/Middleware/CardTokenMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@

use WC_Order;

/**
* Middleware to handle Card Token in the request.
*/
class CardTokenMiddleware implements RequestMiddlewareInterface
{
public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array
/**
* Invoke the middleware.
*
* @param array $requestData The request data.
* @param WC_Order $order The WooCommerce order object.
* @param string $context The context of the request.
* @param callable $next The next middleware to call.
* @return array The modified request data.
*/
public function __invoke(array $requestData, WC_Order $order, $context, $next): array
{
$cardToken = mollieWooCommerceCardToken();
if ($cardToken && isset($requestData['payment']) && $context === 'order') {
Expand Down
24 changes: 22 additions & 2 deletions src/Payment/Request/Middleware/CustomerBirthdateMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@

use WC_Order;

/**
* Middleware to handle customer birthdate in the request.
*/
class CustomerBirthdateMiddleware implements RequestMiddlewareInterface
{
/**
* @var array The payment methods.
*/
private array $paymentMethods;

/**
* Constructor.
*
* @param array $paymentMethods The payment methods.
*/
public function __construct(array $paymentMethods)
{
$this->paymentMethods = $paymentMethods;
}

public function __invoke(array $requestData, WC_Order $order, $context = null, $next): array
/**
* Invoke the middleware.
*
* @param array $requestData The request data.
* @param WC_Order $order The WooCommerce order object.
* @param string $context The context of the request.
* @param callable $next The next middleware to call.
* @return array The modified request data.
*/
public function __invoke(array $requestData, WC_Order $order, $context, $next): array
{
$gateway = wc_get_payment_gateway_by_order($order);
if (!$gateway || !isset($gateway->id)) {
Expand All @@ -28,7 +48,7 @@ public function __invoke(array $requestData, WC_Order $order, $context = null, $
$methodId = $additionalFields && in_array('birthdate', $additionalFields, true);
if ($methodId) {
$optionName = 'billing_birthdate_' . $paymentMethod->getProperty('id');
//phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
// phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$fieldPosted = wc_clean(wp_unslash($_POST[$optionName] ?? ''));
if ($fieldPosted === '' || !is_string($fieldPosted)) {
return $requestData;
Expand Down
Loading

0 comments on commit 17f39e8

Please sign in to comment.