-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
217 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/PaymentMethods/PaymentFieldsStrategies/RivertyFieldsStrategy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters