actions-abstract
2 years ago
assets
17 hours ago
db-models
2 years ago
export
2 years ago
legacy
2 years ago
meta-boxes
2 years ago
pages
2 years ago
paypal
17 hours ago
query-views
2 years ago
rest-api
8 months ago
scenarios-abstract
2 years ago
tab-handlers
2 years ago
table-views
17 hours ago
base-gateway-action.php
1 year ago
base-gateway.php
3 months ago
base-scenario-gateway.php
2 years ago
gateways-editor-data.php
17 hours ago
legacy-base-gateway.php
17 hours ago
migrate-legacy-data.php
2 years ago
module.php
17 hours ago
scenario-item.php
2 years ago
secure-price-notice.php
17 hours ago
trusted-price-resolver-expression-parser.php
17 hours ago
trusted-price-resolver.php
17 hours ago
gateways-editor-data.php
210 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Modules\Gateways; |
| 4 | |
| 5 | // If this file is called directly, abort. |
| 6 | if ( ! defined( 'WPINC' ) ) { |
| 7 | die; |
| 8 | } |
| 9 | |
| 10 | trait Gateways_Editor_Data { |
| 11 | |
| 12 | private function labels() { |
| 13 | return array_merge( |
| 14 | array( |
| 15 | 'notifications_before' => _x( 'Before payment processed:', 'Gateways editor data', 'jet-form-builder' ), |
| 16 | 'notifications_success' => _x( 'On successful payment:', 'Gateways editor data', 'jet-form-builder' ), |
| 17 | 'notifications_failed' => _x( 'On failed payment:', 'Gateways editor data', 'jet-form-builder' ), |
| 18 | 'price_field' => _x( 'Price/amount field', 'Gateways editor data', 'jet-form-builder' ), |
| 19 | 'protect_price_field' => _x( 'Secure payment amount', 'Gateways editor data', 'jet-form-builder' ), |
| 20 | 'message_success' => _x( 'Payment success message', 'Gateways editor data', 'jet-form-builder' ), |
| 21 | 'message_failed' => _x( 'Payment failed message', 'Gateways editor data', 'jet-form-builder' ), |
| 22 | 'use_success_redirect' => _x( 'Redirect to a page', 'Gateways editor data', 'jet-form-builder' ), |
| 23 | 'action_order' => _x( 'Create payment order notification:', 'Gateways editor data', 'jet-form-builder' ), |
| 24 | 'use_success_redirect_help' => _x( |
| 25 | 'Enable this toggle to redirect a user after successful payment.', |
| 26 | 'Gateways editor data', |
| 27 | 'jet-form-builder' |
| 28 | ), |
| 29 | 'protect_price_field_help' => _x( |
| 30 | 'Applies to all gateways linked to the price field.', |
| 31 | 'Gateways editor data', |
| 32 | 'jet-form-builder' |
| 33 | ), |
| 34 | 'protect_price_field_risk' => _x( |
| 35 | 'Disabled: the gateway strictly trusts the price submitted by the browser(Unsecure).', |
| 36 | 'Gateways editor data', |
| 37 | 'jet-form-builder' |
| 38 | ), |
| 39 | 'protect_price_field_safe' => array( |
| 40 | 'valid' => _x( |
| 41 | 'Calculated Field, Hidden Field (Render in HTML disabled), Select/Radio/Checkbox fields with calculate values, Booking price macros (e.g. %ADVANCED_PRICE::...%).', |
| 42 | 'Gateways editor data', |
| 43 | 'jet-form-builder' |
| 44 | ), |
| 45 | 'invalid' => _x( |
| 46 | 'Text/Number/Date fields, Hidden Field (Render in HTML enabled), post meta macros (e.g. %META::...%).', |
| 47 | 'Gateways editor data', |
| 48 | 'jet-form-builder' |
| 49 | ), |
| 50 | ), |
| 51 | ), |
| 52 | $this->options_labels(), |
| 53 | $this->custom_labels() |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | public function default_messages() { |
| 58 | return array( |
| 59 | 'success' => 'Payment success message', |
| 60 | 'failed' => 'Payment failed message', |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | private function options_labels() { |
| 65 | $result = array(); |
| 66 | |
| 67 | foreach ( $this->rep_get_items() as $gateway ) { |
| 68 | /** @var Base_Gateway $gateway */ |
| 69 | $result[ $gateway->get_id() ] = $gateway->options( 'label' ); |
| 70 | } |
| 71 | |
| 72 | return $result; |
| 73 | } |
| 74 | |
| 75 | private function custom_labels() { |
| 76 | $result = array(); |
| 77 | |
| 78 | foreach ( $this->rep_get_items() as $gateway ) { |
| 79 | /** @var Base_Gateway $gateway */ |
| 80 | $result = array_merge( |
| 81 | $result, |
| 82 | $this->join_keys( |
| 83 | $gateway->custom_labels(), |
| 84 | $gateway->get_id() |
| 85 | ) |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | return $result; |
| 90 | } |
| 91 | |
| 92 | private function join_keys( $source, $prefix ): array { |
| 93 | $custom_labels = array(); |
| 94 | |
| 95 | foreach ( $source as $key => $label ) { |
| 96 | $computed_key = "{$prefix}.{$key}"; |
| 97 | |
| 98 | if ( is_string( $label ) ) { |
| 99 | $custom_labels[ $computed_key ] = $label; |
| 100 | continue; |
| 101 | } |
| 102 | if ( is_array( $label ) ) { |
| 103 | $custom_labels = array_merge( $custom_labels, $this->join_keys( $label, $computed_key ) ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return $custom_labels; |
| 108 | } |
| 109 | |
| 110 | private function gateways_additional() { |
| 111 | $result = array(); |
| 112 | |
| 113 | foreach ( $this->rep_get_items() as $gateway ) { |
| 114 | /** @var Base_Gateway $gateway */ |
| 115 | $result[ $gateway->get_id() ] = $gateway->additional_editor_data(); |
| 116 | } |
| 117 | |
| 118 | return $result; |
| 119 | } |
| 120 | |
| 121 | private function gateways_for_js() { |
| 122 | $result = array(); |
| 123 | |
| 124 | foreach ( $this->rep_get_items() as $gateway ) { |
| 125 | $result[] = array( |
| 126 | 'value' => $gateway->get_id(), |
| 127 | 'label' => $gateway->get_name(), |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | return $result; |
| 132 | } |
| 133 | |
| 134 | private function required_fields_map(): array { |
| 135 | // Fallback hardcode (until gateway plugins can self-declare) |
| 136 | $map = array( |
| 137 | 'paypal' => array( 'client_id', 'secret' ), |
| 138 | 'stripe' => array( 'public', 'secret' ), |
| 139 | ); |
| 140 | |
| 141 | foreach ( $this->rep_get_items() as $gateway ) { |
| 142 | /** @var Base_Gateway $gateway */ |
| 143 | $id = $gateway->get_id(); |
| 144 | $fields = (array) $gateway->required_credentials_fields(); |
| 145 | |
| 146 | if ( ! empty( $fields ) ) { |
| 147 | $map[ $id ] = array_values( $fields ); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return $map; |
| 152 | } |
| 153 | |
| 154 | private function gateways_global_valid() { |
| 155 | |
| 156 | $required_map = $this->required_fields_map(); |
| 157 | $result = array(); |
| 158 | |
| 159 | foreach ( $this->rep_get_items() as $gateway ) { |
| 160 | |
| 161 | $id = $gateway->get_id(); |
| 162 | |
| 163 | if ( ! isset( $required_map[ $id ] ) ) { |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | $class_name = get_class( $gateway ); |
| 168 | |
| 169 | if ( ! method_exists( $class_name, 'get_credentials' ) ) { |
| 170 | $result[ $id ] = false; |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | $creds = $class_name::get_credentials(); |
| 175 | |
| 176 | $is_valid = true; |
| 177 | |
| 178 | foreach ( $required_map[ $id ] as $field ) { |
| 179 | if ( empty( $creds[ $field ] ) ) { |
| 180 | $is_valid = false; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | $result[ $id ] = $is_valid; |
| 186 | } |
| 187 | |
| 188 | return $result; |
| 189 | } |
| 190 | |
| 191 | public function editor_data() { |
| 192 | $required_map = $this->required_fields_map(); |
| 193 | |
| 194 | $result = array( |
| 195 | 'allowed' => true, |
| 196 | 'labels' => $this->labels(), |
| 197 | 'list' => $this->gateways_for_js(), |
| 198 | 'messages' => $this->default_messages(), |
| 199 | 'additional' => $this->gateways_additional(), |
| 200 | 'validation' => array( |
| 201 | 'required_map' => $required_map, |
| 202 | 'global_valid' => $this->gateways_global_valid(), |
| 203 | ), |
| 204 | ); |
| 205 | |
| 206 | return apply_filters( 'jet-form-builder/gateways/editor-data', $result ); |
| 207 | } |
| 208 | |
| 209 | } |
| 210 |