actions-abstract
2 years ago
assets
3 months ago
db-models
2 years ago
export
2 years ago
legacy
2 years ago
meta-boxes
2 years ago
pages
2 years ago
paypal
4 months ago
query-views
2 years ago
rest-api
9 months ago
scenarios-abstract
2 years ago
tab-handlers
2 years ago
table-views
11 months ago
base-gateway-action.php
2 years ago
base-gateway.php
4 months ago
base-scenario-gateway.php
2 years ago
gateways-editor-data.php
4 months ago
legacy-base-gateway.php
9 months ago
migrate-legacy-data.php
2 years ago
module.php
1 year ago
scenario-item.php
2 years ago
gateways-editor-data.php
187 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 | 'message_success' => _x( 'Payment success message', 'Gateways editor data', 'jet-form-builder' ), |
| 20 | 'message_failed' => _x( 'Payment failed message', 'Gateways editor data', 'jet-form-builder' ), |
| 21 | 'use_success_redirect' => _x( 'Redirect to a page', 'Gateways editor data', 'jet-form-builder' ), |
| 22 | 'action_order' => _x( 'Create payment order notification:', 'Gateways editor data', 'jet-form-builder' ), |
| 23 | 'use_success_redirect_help' => _x( |
| 24 | 'Enable this toggle to redirect a user after successful payment.', |
| 25 | 'Gateways editor data', |
| 26 | 'jet-form-builder' |
| 27 | ), |
| 28 | ), |
| 29 | $this->options_labels(), |
| 30 | $this->custom_labels() |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | public function default_messages() { |
| 35 | return array( |
| 36 | 'success' => 'Payment success message', |
| 37 | 'failed' => 'Payment failed message', |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | private function options_labels() { |
| 42 | $result = array(); |
| 43 | |
| 44 | foreach ( $this->rep_get_items() as $gateway ) { |
| 45 | /** @var Base_Gateway $gateway */ |
| 46 | $result[ $gateway->get_id() ] = $gateway->options( 'label' ); |
| 47 | } |
| 48 | |
| 49 | return $result; |
| 50 | } |
| 51 | |
| 52 | private function custom_labels() { |
| 53 | $result = array(); |
| 54 | |
| 55 | foreach ( $this->rep_get_items() as $gateway ) { |
| 56 | /** @var Base_Gateway $gateway */ |
| 57 | $result = array_merge( |
| 58 | $result, |
| 59 | $this->join_keys( |
| 60 | $gateway->custom_labels(), |
| 61 | $gateway->get_id() |
| 62 | ) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | return $result; |
| 67 | } |
| 68 | |
| 69 | private function join_keys( $source, $prefix ): array { |
| 70 | $custom_labels = array(); |
| 71 | |
| 72 | foreach ( $source as $key => $label ) { |
| 73 | $computed_key = "{$prefix}.{$key}"; |
| 74 | |
| 75 | if ( is_string( $label ) ) { |
| 76 | $custom_labels[ $computed_key ] = $label; |
| 77 | continue; |
| 78 | } |
| 79 | if ( is_array( $label ) ) { |
| 80 | $custom_labels = array_merge( $custom_labels, $this->join_keys( $label, $computed_key ) ); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return $custom_labels; |
| 85 | } |
| 86 | |
| 87 | private function gateways_additional() { |
| 88 | $result = array(); |
| 89 | |
| 90 | foreach ( $this->rep_get_items() as $gateway ) { |
| 91 | /** @var Base_Gateway $gateway */ |
| 92 | $result[ $gateway->get_id() ] = $gateway->additional_editor_data(); |
| 93 | } |
| 94 | |
| 95 | return $result; |
| 96 | } |
| 97 | |
| 98 | private function gateways_for_js() { |
| 99 | $result = array(); |
| 100 | |
| 101 | foreach ( $this->rep_get_items() as $gateway ) { |
| 102 | $result[] = array( |
| 103 | 'value' => $gateway->get_id(), |
| 104 | 'label' => $gateway->get_name(), |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | return $result; |
| 109 | } |
| 110 | |
| 111 | private function required_fields_map(): array { |
| 112 | // Fallback hardcode (until gateway plugins can self-declare) |
| 113 | $map = array( |
| 114 | 'paypal' => array( 'client_id', 'secret' ), |
| 115 | 'stripe' => array( 'public', 'secret' ), |
| 116 | ); |
| 117 | |
| 118 | foreach ( $this->rep_get_items() as $gateway ) { |
| 119 | /** @var Base_Gateway $gateway */ |
| 120 | $id = $gateway->get_id(); |
| 121 | $fields = (array) $gateway->required_credentials_fields(); |
| 122 | |
| 123 | if ( ! empty( $fields ) ) { |
| 124 | $map[ $id ] = array_values( $fields ); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return $map; |
| 129 | } |
| 130 | |
| 131 | private function gateways_global_valid() { |
| 132 | |
| 133 | $required_map = $this->required_fields_map(); |
| 134 | $result = array(); |
| 135 | |
| 136 | foreach ( $this->rep_get_items() as $gateway ) { |
| 137 | |
| 138 | $id = $gateway->get_id(); |
| 139 | |
| 140 | if ( ! isset( $required_map[ $id ] ) ) { |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | $class_name = get_class( $gateway ); |
| 145 | |
| 146 | if ( ! method_exists( $class_name, 'get_credentials' ) ) { |
| 147 | $result[ $id ] = false; |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | $creds = $class_name::get_credentials(); |
| 152 | |
| 153 | $is_valid = true; |
| 154 | |
| 155 | foreach ( $required_map[ $id ] as $field ) { |
| 156 | if ( empty( $creds[ $field ] ) ) { |
| 157 | $is_valid = false; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | $result[ $id ] = $is_valid; |
| 163 | } |
| 164 | |
| 165 | return $result; |
| 166 | } |
| 167 | |
| 168 | public function editor_data() { |
| 169 | $required_map = $this->required_fields_map(); |
| 170 | |
| 171 | $result = array( |
| 172 | 'allowed' => true, |
| 173 | 'labels' => $this->labels(), |
| 174 | 'list' => $this->gateways_for_js(), |
| 175 | 'messages' => $this->default_messages(), |
| 176 | 'additional' => $this->gateways_additional(), |
| 177 | 'validation' => array( |
| 178 | 'required_map' => $required_map, |
| 179 | 'global_valid' => $this->gateways_global_valid(), |
| 180 | ), |
| 181 | ); |
| 182 | |
| 183 | return apply_filters( 'jet-form-builder/gateways/editor-data', $result ); |
| 184 | } |
| 185 | |
| 186 | } |
| 187 |