| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Payments\Components; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
use FluentForm\App\Helpers\Helper; |
| 10 |
use FluentForm\App\Services\FormBuilder\BaseFieldManager; |
| 11 |
use FluentForm\App\Services\FormBuilder\Components\Text; |
| 12 |
use FluentForm\App\Modules\Payments\PaymentHelper; |
| 13 |
|
| 14 |
class CustomPaymentComponent extends BaseFieldManager |
| 15 |
{ |
| 16 |
public function __construct( |
| 17 |
$key = 'custom_payment_component', |
| 18 |
$title = 'Custom Payment', |
| 19 |
$tags = ['custom', 'payment', 'donation'], |
| 20 |
$position = 'payments' |
| 21 |
) |
| 22 |
{ |
| 23 |
parent::__construct( |
| 24 |
$key, |
| 25 |
$title, |
| 26 |
$tags, |
| 27 |
$position |
| 28 |
); |
| 29 |
|
| 30 |
add_filter('fluentform/editor_init_element_custom_payment_component', function ($item) { |
| 31 |
if (!isset($item['settings']['numeric_formatter'])) { |
| 32 |
$item['settings']['numeric_formatter'] = ''; |
| 33 |
} |
| 34 |
return $item; |
| 35 |
}, 10, 2); |
| 36 |
|
| 37 |
add_filter('fluentform/response_render_' . $this->key, function($amount, $field, $form_id, $isHtml = false) { |
| 38 |
if (!$isHtml) { |
| 39 |
return $amount; |
| 40 |
} |
| 41 |
if ($currency = PaymentHelper::getFormCurrency($form_id)) { |
| 42 |
$amount = PaymentHelper::formatMoney(PaymentHelper::convertToCents($amount), $currency); |
| 43 |
} |
| 44 |
return $amount; |
| 45 |
}, 10, 4); |
| 46 |
} |
| 47 |
|
| 48 |
function getComponent() |
| 49 |
{ |
| 50 |
return array( |
| 51 |
'index' => 6, |
| 52 |
'element' => $this->key, |
| 53 |
'attributes' => array( |
| 54 |
'type' => 'number', |
| 55 |
'name' => 'custom-payment-amount', |
| 56 |
'value' => '', |
| 57 |
'id' => '', |
| 58 |
'class' => '', |
| 59 |
'placeholder' => '', |
| 60 |
'data-payment_item' => 'yes' |
| 61 |
), |
| 62 |
'settings' => array( |
| 63 |
'container_class' => '', |
| 64 |
'is_payment_field' => 'yes', |
| 65 |
'label' => __('Custom Payment Amount', 'fluentform'), |
| 66 |
'admin_field_label' => '', |
| 67 |
'label_placement' => '', |
| 68 |
'help_message' => '', |
| 69 |
'number_step' => '', |
| 70 |
'prefix_label' => '', |
| 71 |
'suffix_label' => '', |
| 72 |
'validation_rules' => array( |
| 73 |
'required' => array( |
| 74 |
'value' => false, |
| 75 |
'global' => true, |
| 76 |
'message' => Helper::getGlobalDefaultMessage('required'), |
| 77 |
'global_message' => Helper::getGlobalDefaultMessage('required'), |
| 78 |
), |
| 79 |
'numeric' => array( |
| 80 |
'value' => true, |
| 81 |
'global' => true, |
| 82 |
'message' => Helper::getGlobalDefaultMessage('numeric'), |
| 83 |
'global_message' => Helper::getGlobalDefaultMessage('numeric'), |
| 84 |
), |
| 85 |
'min' => array( |
| 86 |
'value' => '', |
| 87 |
'global' => true, |
| 88 |
'message' => Helper::getGlobalDefaultMessage('min'), |
| 89 |
'global_message' => Helper::getGlobalDefaultMessage('min'), |
| 90 |
), |
| 91 |
'max' => array( |
| 92 |
'value' => '', |
| 93 |
'global' => true, |
| 94 |
'message' => Helper::getGlobalDefaultMessage('max'), |
| 95 |
'global_message' => Helper::getGlobalDefaultMessage('max'), |
| 96 |
), |
| 97 |
), |
| 98 |
'conditional_logics' => array(), |
| 99 |
'calculation_settings' => array( |
| 100 |
'status' => false, |
| 101 |
'formula' => '' |
| 102 |
) |
| 103 |
), |
| 104 |
'editor_options' => array( |
| 105 |
'title' => __('Custom Payment Amount', 'fluentform'), |
| 106 |
'icon_class' => 'ff-edit-keyboard-o', |
| 107 |
'template' => 'inputText' |
| 108 |
), |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
public function getGeneralEditorElements() |
| 113 |
{ |
| 114 |
return [ |
| 115 |
'label', |
| 116 |
'label_placement', |
| 117 |
'admin_field_label', |
| 118 |
'placeholder', |
| 119 |
'validation_rules', |
| 120 |
'numeric_formatter' |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
public function getAdvancedEditorElements() |
| 125 |
{ |
| 126 |
return [ |
| 127 |
'value', |
| 128 |
'container_class', |
| 129 |
'class', |
| 130 |
'help_message', |
| 131 |
'prefix_label', |
| 132 |
'suffix_label', |
| 133 |
'name', |
| 134 |
'conditional_logics', |
| 135 |
'calculation_settings' |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
function render($data, $form) |
| 140 |
{ |
| 141 |
$data['attributes']['class'] .= ' ff_payment_item'; |
| 142 |
$data['attributes']['inputmode'] = 'decimal'; |
| 143 |
|
| 144 |
return (new Text())->compile($data, $form); |
| 145 |
} |
| 146 |
} |
| 147 |
|