Skip to content

Commit

Permalink
Add Phone and birthdate fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Jul 19, 2024
1 parent f6d4fea commit da8bbae
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 9 deletions.
10 changes: 10 additions & 0 deletions inc/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,13 @@ function transformPhoneToNLFormat($phone)
}
return $phone;
}

function isMollieBirthValid($billing_birthdate)
{
$today = new DateTime();
$birthdate = DateTime::createFromFormat('Y-m-d', $billing_birthdate);
if ($birthdate >= $today) {
return false;
}
return true;
}
4 changes: 4 additions & 0 deletions public/images/riverty.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 2 additions & 7 deletions src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,14 +794,9 @@ private function isPhoneValid($billing_phone)
return preg_match('/^\+[1-9]\d{10,13}$|^[1-9]\d{9,13}$|^06\d{9,13}$/', $billing_phone);
}

private function isBirthValid($billing_birthdate)
private function isBirthValid($billing_birthdate): bool
{
$today = new DateTime();
$birthdate = DateTime::createFromFormat('Y-m-d', $billing_birthdate);
if ($birthdate >= $today) {
return false;
}
return true;
return isMollieBirthValid($billing_birthdate);
}

public function addPhoneWhenRest($arrayContext)
Expand Down
2 changes: 1 addition & 1 deletion src/Gateway/MolliePaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function __construct(
'yes';
$this->enabled = $isEnabledAtWoo;

if ($this->paymentMethod->getProperty('filtersOnBuild')) {
if ($this->enabled && $this->paymentMethod->getProperty('filtersOnBuild')) {
$this->paymentMethod->filtersOnBuild();
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Payment/MollieOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,11 @@ protected function getCustomerBirthdate($order)
if (!$gateway || !isset($gateway->id)) {
return null;
}
$methodId = $gateway->id === 'mollie_wc_gateway_in3';
if(strpos($gateway->id, 'mollie_wc_gateway_') === false){
return null;
}
$additionalFields = $gateway->paymentMethod()->getProperty('additionalFields');
$methodId = in_array('birthdate', $additionalFields, true);
if ($methodId) {
//phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$fieldPosted = wc_clean(wp_unslash($_POST["billing_birthdate"] ?? ''));
Expand Down
1 change: 1 addition & 0 deletions src/Payment/PaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ protected function processAsMollieOrder(
'mollie_wc_gateway_klarna',
'mollie_wc_gateway_billie',
'mollie_wc_gateway_in3',
'mollie_wc_gateway_riverty'
];

if (in_array($order_payment_method, $orderMandatoryPaymentMethods, true)) {
Expand Down
1 change: 1 addition & 0 deletions src/PaymentMethods/In3.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function getConfig(): array
'settingsDescription' => '',
'defaultDescription' => __('Pay in 3 instalments, 0% interest', 'mollie-payments-for-woocommerce'),
'paymentFields' => true,
'additionalFields' => ['birthdate', 'phone'],
'instructions' => false,
'supports' => [
'products',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies;

class RivertyFieldsStrategy implements PaymentFieldsStrategyI
{
const FIELD_BIRTHDATE = "billing_birthdate";
const FIELD_PHONE = "billing_phone_riverty";

public function execute($gateway, $dataHelper)
{
$showBirthdateField = false;
$showPhoneField = false;
$isPhoneRequired = get_option('mollie_wc_is_phone_required_flag');
$phoneValue = false;

if (is_checkout_pay_page()) {
$showBirthdateField = true;
$showPhoneField = true;
$order = $this->getOrderIdOnPayForOrderPage();
$phoneValue = $order->get_billing_phone();
}

if (is_checkout() && !is_checkout_pay_page() && !$isPhoneRequired) {
$showPhoneField = true;
}
if (is_checkout() && !is_checkout_pay_page()) {
$showBirthdateField = true;
}

if ($showPhoneField) {
$this->phoneNumber($phoneValue);
}

if ($showBirthdateField) {
$this->dateOfBirth();
}
}

protected function getOrderIdOnPayForOrderPage()
{
global $wp;
$orderId = absint($wp->query_vars['order-pay']);
return wc_get_order($orderId);
}

protected function dateOfBirth()
{
?>
<p class="form-row form-row-wide" id="billing_birthdate_field">
<label for="<?= esc_attr(self::FIELD_BIRTHDATE); ?>" class=""><?= esc_html__('Birthdate', 'mollie-payments-for-woocommerce'); ?>
</label>
<span class="woocommerce-input-wrapper">
<input type="date" class="input-text " name="<?= esc_attr(self::FIELD_BIRTHDATE); ?>"
id="<?= esc_attr(self::FIELD_BIRTHDATE); ?>" value=""
autocomplete="birthdate"></span>
</p>
<?php
}

protected function phoneNumber($phoneValue)
{
$phoneValue = $phoneValue ?: '';
?>
<p class="form-row form-row-wide" id="billing_phone_field">
<label for="<?= esc_attr(self::FIELD_PHONE); ?>" class=""><?= esc_html__('Phone', 'mollie-payments-for-woocommerce'); ?>
</label>
<span class="woocommerce-input-wrapper">
<input type="tel" class="input-text " name="<?= esc_attr(self::FIELD_PHONE); ?>" id="<?= esc_attr(self::FIELD_PHONE); ?>"
placeholder="+316xxxxxxxx"
value="<?= esc_attr($phoneValue); ?>" autocomplete="phone">
</span>
</p>
<?php
}

public function getFieldMarkup($gateway, $dataHelper)
{
return "";
}
}
109 changes: 109 additions & 0 deletions src/PaymentMethods/Riverty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\PaymentMethods;

use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;

class Riverty extends AbstractPaymentMethod implements PaymentMethodI
{
protected function getConfig(): array
{
return [
'id' => 'riverty',
'defaultTitle' => __('Riverty', 'mollie-payments-for-woocommerce'),
'settingsDescription' => __(
'To accept payments via Riverty, all default WooCommerce checkout fields should be enabled and required.',
'mollie-payments-for-woocommerce'
),
'defaultDescription' => '',
'paymentFields' => true,
'additionalFields' => ['birthdate', 'phone'],
'instructions' => false,
'supports' => [
'products',
'refunds',
],
'filtersOnBuild' => true,
'confirmationDelayed' => false,
'SEPA' => false,
'orderMandatory' => true,
];
}

public function getFormFields($generalFormFields): array
{
return $generalFormFields;
}

public function filtersOnBuild()
{
add_action(
'woocommerce_checkout_posted_data',
[$this, 'switchFields'],
11
);
add_action('woocommerce_rest_checkout_process_payment_with_context', [$this, 'addPhoneWhenRest'], 11);
add_action('woocommerce_rest_checkout_process_payment_with_context', [$this, 'addBirthdateWhenRest'], 11);
}

public function switchFields($data)
{
if (isset($data['payment_method']) && $data['payment_method'] === 'mollie_wc_gateway_riverty') {
$fieldName = 'billing_phone_' . $this->getConfig()['id'];
$fieldPosted = filter_input(INPUT_POST, $fieldName, FILTER_SANITIZE_SPECIAL_CHARS) ?? false;
if (!empty($fieldPosted)) {
$data['billing_phone'] = $fieldPosted;
}
}
return $data;
}

public function addPhoneWhenRest($arrayContext)
{
$context = $arrayContext;
$phoneMandatoryGateways = ['mollie_wc_gateway_riverty'];
$paymentMethod = $context->payment_data['payment_method'] ?? null;
if ($paymentMethod && in_array($paymentMethod, $phoneMandatoryGateways)) {
$billingPhone = $context->order->get_billing_phone();
if (!empty($billingPhone)) {
return;
}

$billingPhone = $context->payment_data['billing_phone'] ?? null;
if ($billingPhone) {
$context->order->set_billing_phone($billingPhone);
$context->order->save();
}
}
}

/**
* @throws RouteException
*/
public function addBirthdateWhenRest($context)
{
$birthMandatoryGateways = ['mollie_wc_gateway_riverty'];
$paymentMethod = $context->payment_data['payment_method'] ?? null;
if ($paymentMethod && in_array($paymentMethod, $birthMandatoryGateways)) {
$billingBirthdate = $context->payment_data['billing_birthdate'] ?? null;
if ($billingBirthdate && $this->isBirthValid($billingBirthdate)) {
$context->order->update_meta_data('billing_birthdate', $billingBirthdate);
$context->order->save();
} else {
$message = __('Please introduce a valid birthdate number.', 'mollie-payments-for-woocommerce');
throw new RouteException(
'woocommerce_rest_checkout_process_payment_error',
$message,
402
);
}
}
}

private function isBirthValid($billing_birthdate): bool
{
return isMollieBirthValid($billing_birthdate);
}
}
1 change: 1 addition & 0 deletions src/Shared/SharedDataDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SharedDataDictionary
'Mollie_WC_Gateway_Twint',
'Mollie_WC_Gateway_Bancomatpay',
'Mollie_WC_Gateway_Alma',
'Mollie_WC_Gateway_Riverty',
];

public const MOLLIE_OPTIONS_NAMES = [
Expand Down

0 comments on commit da8bbae

Please sign in to comment.