Skip to content

Commit

Permalink
Merge pull request #899 from mollie/feature/PIWOO-418-alma-method
Browse files Browse the repository at this point in the history
Add alma payment method
  • Loading branch information
mmaymo authored May 13, 2024
2 parents 9cd676c + c09c595 commit 46737da
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 221 deletions.
11 changes: 11 additions & 0 deletions public/images/alma.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
222 changes: 221 additions & 1 deletion src/Payment/MollieObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
use Mollie\WooCommerce\PaymentMethods\Voucher;
use Mollie\WooCommerce\SDK\Api;
use Mollie\WooCommerce\Settings\Settings;
use Psr\Log\LogLevel;
use WC_Order;
use WC_Payment_Gateway;
use Psr\Log\LoggerInterface as Logger;
use stdClass;

class MollieObject
{
public const MAXIMAL_LENGHT_ADDRESS = 100;
public const MAXIMAL_LENGHT_POSTALCODE = 20;
public const MAXIMAL_LENGHT_CITY = 200;
public const MAXIMAL_LENGHT_REGION = 200;
protected $data;
/**
* @var string[]
Expand Down Expand Up @@ -983,4 +987,220 @@ protected function replaceTagsDescription($order, $description)
}
return $description;
}

/**
* @param $order
* @return stdClass
*/
protected function createBillingAddress($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;
}

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);

//check that $phone is in E164 format
if ($phone !== null && preg_match('/^\+[1-9]\d{1,14}$/', $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;
}

/**
* @param $order
* @return stdClass
*/
protected function createShippingAddress($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;
}

/**
* 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;
}
}
Loading

0 comments on commit 46737da

Please sign in to comment.