Skip to content

Commit

Permalink
Add missing decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Dec 17, 2024
1 parent 150597e commit 3c71099
Show file tree
Hide file tree
Showing 15 changed files with 693 additions and 545 deletions.
444 changes: 0 additions & 444 deletions src/Payment/MollieObject.php

Large diffs are not rendered by default.

17 changes: 0 additions & 17 deletions src/Payment/MolliePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,21 +469,4 @@ protected function maybeUpdateStatus(
}
$this->updateOrderStatus($order, $newOrderStatus);
}

protected function addCustomRequestFields($order, array $paymentRequestData)
{
if ($this->paymentMethod->hasProperty('paymentAPIfields')) {
$paymentAPIfields = $this->paymentMethod->getProperty('paymentAPIfields');
foreach ($paymentAPIfields as $field) {
if (!method_exists($this, 'create' . ucfirst($field))) {
continue;
}
$value = $this->{'create' . ucfirst($field)}($order);
if ($value) {
$paymentRequestData[$field] = $value;
}
}
}
return $paymentRequestData;
}
}
39 changes: 39 additions & 0 deletions src/Payment/Request/Decorators/AddCustomRequestFieldsDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Decorator;

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 ContainerInterface $container;

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

public function decorate(array $requestData, WC_Order $order): array
{
if (property_exists($this->paymentMethod, 'paymentAPIfields')) {
$paymentAPIfields = $this->paymentMethod->paymentAPIfields;
foreach ($paymentAPIfields as $field) {
$decoratorClass = 'Mollie\\WooCommerce\\Payment\\Decorator\\' . $field;
if (class_exists($decoratorClass)) {
$decorator = $this->container->get($decoratorClass);
if ($decorator instanceof RequestDecoratorInterface) {
$requestData = $decorator->decorate($requestData, $order);
}
}
}
}
return $requestData;
}
}
246 changes: 246 additions & 0 deletions src/Payment/Request/Decorators/AddressDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Decorator;

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

class AddressDecorator implements RequestDecoratorInterface
{
public function decorate(array $requestData, WC_Order $order): array
{
$isPayPalExpressOrder = $order->get_meta('_mollie_payment_method_button') === 'PayPalButton';
$billingAddress = null;
if (!$isPayPalExpressOrder) {
$billingAddress = $this->createBillingAddress($order);
$shippingAddress = $this->createShippingAddress($order);
}
$requestData['billingAddress'] = $billingAddress;
// Only add shippingAddress if all required fields are set
if (
!empty($shippingAddress->streetAndNumber)
&& !empty($shippingAddress->postalCode)
&& !empty($shippingAddress->city)
&& !empty($shippingAddress->country)
) {
$requestData['shippingAddress'] = $shippingAddress;
}

return $requestData;
}

private function createBillingAddress(WC_Order $order)
{
// Setup billing and shipping objects
$billingAddress = new stdClass();

// Get user details
$billingAddress->givenName = (ctype_space(
$order->get_billing_first_name()
)) ? null : $order->get_billing_first_name();
$billingAddress->familyName = (ctype_space(
$order->get_billing_last_name()
)) ? null : $order->get_billing_last_name();
$billingAddress->email = (ctype_space($order->get_billing_email()))
? null : $order->get_billing_email();
// Create billingAddress object
$billingAddress->streetAndNumber = (ctype_space(
$order->get_billing_address_1()
))
? null
: $this->maximalFieldLengths(
$order->get_billing_address_1(),
self::MAXIMAL_LENGHT_ADDRESS
);
$billingAddress->streetAdditional = (ctype_space(
$order->get_billing_address_2()
))
? null
: $this->maximalFieldLengths(
$order->get_billing_address_2(),
self::MAXIMAL_LENGHT_ADDRESS
);
$billingAddress->postalCode = (ctype_space(
$order->get_billing_postcode()
))
? null
: $this->maximalFieldLengths(
$order->get_billing_postcode(),
self::MAXIMAL_LENGHT_POSTALCODE
);
$billingAddress->city = (ctype_space($order->get_billing_city()))
? null
: $this->maximalFieldLengths(
$order->get_billing_city(),
self::MAXIMAL_LENGHT_CITY
);
$billingAddress->region = (ctype_space($order->get_billing_state()))
? null
: $this->maximalFieldLengths(
$order->get_billing_state(),
self::MAXIMAL_LENGHT_REGION
);
$billingAddress->country = (ctype_space($order->get_billing_country()))
? null
: $this->maximalFieldLengths(
$order->get_billing_country(),
self::MAXIMAL_LENGHT_REGION
);
$billingAddress->organizationName = $this->billingCompanyField($order);
$phone = $this->getPhoneNumber($order);
$billingAddress->phone = (ctype_space($phone))
? null
: $this->getFormatedPhoneNumber($phone);
return $billingAddress;
}

private function createShippingAddress(WC_Order $order)
{
$shippingAddress = new stdClass();
// Get user details
$shippingAddress->givenName = (ctype_space(
$order->get_shipping_first_name()
)) ? null : $order->get_shipping_first_name();
$shippingAddress->familyName = (ctype_space(
$order->get_shipping_last_name()
)) ? null : $order->get_shipping_last_name();
$shippingAddress->email = (ctype_space($order->get_billing_email()))
? null
: $order->get_billing_email(); // WooCommerce doesn't have a shipping email


// Create shippingAddress object
$shippingAddress->streetAndNumber = (ctype_space(
$order->get_shipping_address_1()
))
? null
: $this->maximalFieldLengths(
$order->get_shipping_address_1(),
self::MAXIMAL_LENGHT_ADDRESS
);
$shippingAddress->streetAdditional = (ctype_space(
$order->get_shipping_address_2()
))
? null
: $this->maximalFieldLengths(
$order->get_shipping_address_2(),
self::MAXIMAL_LENGHT_ADDRESS
);
$shippingAddress->postalCode = (ctype_space(
$order->get_shipping_postcode()
))
? null
: $this->maximalFieldLengths(
$order->get_shipping_postcode(),
self::MAXIMAL_LENGHT_POSTALCODE
);
$shippingAddress->city = (ctype_space($order->get_shipping_city()))
? null
: $this->maximalFieldLengths(
$order->get_shipping_city(),
self::MAXIMAL_LENGHT_CITY
);
$shippingAddress->region = (ctype_space($order->get_shipping_state()))
? null
: $this->maximalFieldLengths(
$order->get_shipping_state(),
self::MAXIMAL_LENGHT_REGION
);
$shippingAddress->country = (ctype_space(
$order->get_shipping_country()
))
? null
: $this->maximalFieldLengths(
$order->get_shipping_country(),
self::MAXIMAL_LENGHT_REGION
);
return $shippingAddress;
}

protected function getPhoneNumber($order)
{

$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'] ?? ''));
}
return $phone;
}

protected function getFormatedPhoneNumber(string $phone)
{
//remove whitespaces and all non numerical characters except +
$phone = preg_replace('/[^0-9+]+/', '', $phone);
if (!is_string($phone)) {
return null;
}
//check if phone starts with 06 and replace with +316
$phone = transformPhoneToNLFormat($phone);

//check that $phone is in E164 format or can be changed by api
if (is_string($phone) && preg_match('/^\+[1-9]\d{10,13}$|^[1-9]\d{9,13}$/', $phone)) {
return $phone;
}
return null;
}

/**
* @param $order
* @return string|null
*/
public function billingCompanyField($order): ?string
{
if (!trim($order->get_billing_company())) {
return $this->checkBillieCompanyField($order);
}
return $this->maximalFieldLengths(
$order->get_billing_company(),
self::MAXIMAL_LENGHT_ADDRESS
);
}

private function checkBillieCompanyField($order)
{
$gateway = wc_get_payment_gateway_by_order($order);
if (!$gateway || !$gateway->id) {
return null;
}
$isBillieMethodId = $gateway->id === 'mollie_wc_gateway_billie';
if ($isBillieMethodId) {
//phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$fieldPosted = wc_clean(wp_unslash($_POST["billing_company"] ?? ''));
if ($fieldPosted === '' || !is_string($fieldPosted)) {
return null;
}
return $this->maximalFieldLengths(
$fieldPosted,
self::MAXIMAL_LENGHT_ADDRESS
);
}
return null;
}

/**
* Method that shortens the field to a certain length
*
* @param string $field
* @param int $maximalLength
*
* @return null|string
*/
protected function maximalFieldLengths($field, $maximalLength)
{
if (!is_string($field)) {
return null;
}
if (is_int($maximalLength) && strlen($field) > $maximalLength) {
$field = substr($field, 0, $maximalLength);
$field = !$field ? null : $field;
}

return $field;
}
}
13 changes: 9 additions & 4 deletions src/Payment/Request/Decorators/ApplePaytokenDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@

class ApplePayTokenDecorator implements RequestDecoratorInterface
{
public function decorate(array $requestData, WC_Order $order): array
public function decorate(array $requestData, WC_Order $order, $context): array
{
// phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$applePayToken = wc_clean(wp_unslash($_POST["token"] ?? ''));
if ($applePayToken && isset($requestData['payment'])) {
$encodedApplePayToken = wp_json_encode($applePayToken);
$requestData['payment']['applePayPaymentToken'] = $encodedApplePayToken;
if (!$applePayToken) {
return $requestData;
}
$encodedApplePayToken = wp_json_encode($applePayToken);
if($context === 'order') {
$requestData['payment']['applePayToken'] = $encodedApplePayToken;
} elseif ($context === 'payment') {
$requestData['applePayToken'] = $encodedApplePayToken;
}
return $requestData;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Payment/Request/Decorators/CardTokenDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

class CardTokenDecorator implements RequestDecoratorInterface
{
public function decorate(array $requestData, WC_Order $order): array
public function decorate(array $requestData, WC_Order $order, $context): array
{
$cardToken = mollieWooCommerceCardToken();
if ($cardToken && isset($requestData['payment'])) {
if ($cardToken && isset($requestData['payment']) && $context === 'order') {
$requestData['payment']['cardToken'] = $cardToken;
}elseif ($cardToken && isset($requestData['payment']) && $context === 'payment') {
$requestData['cardToken'] = $cardToken;
}
return $requestData;
}
Expand Down
28 changes: 28 additions & 0 deletions src/Payment/Request/Decorators/OrderLinesDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\Payment\Decorator;

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

class OrderLinesDecorator implements RequestDecoratorInterface
{
private OrderLines $orderLines;
private string $voucherDefaultCategory;

public function __construct($orderLines, $voucherDefaultCategory)
{
$this->orderLines = $orderLines;
$this->voucherDefaultCategory = $voucherDefaultCategory;
}

public function decorate(array $requestData, WC_Order $order): array
{
$orderLines = $this->orderLines->order_lines($order, $this->voucherDefaultCategory);
$requestData['lines'] = $orderLines['lines'];
return $requestData;
}
}
Loading

0 comments on commit 3c71099

Please sign in to comment.