PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.4
JetFormBuilder — Dynamic Blocks Form Builder v3.6.4
3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 All 130 releases
jetformbuilder / modules / gateways / module.php

module.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.4, at modules/gateways/module.php

458 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace JFB_Modules\Gateways;
4
5 use Jet_Form_Builder\Actions\Events\Default_Process\Default_With_Gateway_Executor;
6 use Jet_Form_Builder\Admin\Single_Pages\Base_Single_Page;
7 use Jet_Form_Builder\Admin\Single_Pages\Meta_Containers\Base_Meta_Container;
8 use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager;
9 use JFB_Components\Admin\Print_Page\Header;
10 use JFB_Components\Module\Base_Module_After_Install_It;
11 use JFB_Components\Module\Base_Module_Dir_It;
12 use JFB_Components\Module\Base_Module_Dir_Trait;
13 use JFB_Components\Module\Base_Module_Handle_It;
14 use JFB_Components\Module\Base_Module_Handle_Trait;
15 use JFB_Components\Module\Base_Module_It;
16 use JFB_Components\Module\Base_Module_Static_Instance_It;
17 use JFB_Components\Module\Base_Module_Static_Instance_Trait;
18 use JFB_Components\Module\Base_Module_Url_It;
19 use JFB_Components\Module\Base_Module_Url_Trait;
20 use JFB_Components\Repository\Repository_Pattern_Trait;
21 use Jet_Form_Builder\Exceptions\Repository_Exception;
22 use JFB_Modules\Gateways\Meta_Boxes\Payment_Info_For_Record;
23 use JFB_Modules\Gateways\Paypal;
24 use JFB_Modules\Gateways\Rest_Api\Rest_Api_Controller;
25 use Jet_Form_Builder\Plugin;
26 use JFB_Modules\Gateways\Pages;
27
28 // If this file is called directly, abort.
29 if ( ! defined( 'WPINC' ) ) {
30 die;
31 }
32
33 /**
34 * @method static Module instance()
35 *
36 * @package JFB_Modules\Gateways
37 */
38 class Module implements
39 Base_Module_Dir_It,
40 Base_Module_It,
41 Base_Module_Handle_It,
42 Base_Module_Url_It,
43 Base_Module_After_Install_It,
44 Base_Module_Static_Instance_It {
45
46 use Gateways_Editor_Data;
47 use Repository_Pattern_Trait;
48
49 use Base_Module_Handle_Trait;
50 use Base_Module_Dir_Trait;
51 use Base_Module_Url_Trait;
52 use Base_Module_Static_Instance_Trait;
53
54 const PAYMENT_TYPE_PARAM = 'jet_form_gateway';
55 const OPTION_NAME = 'payments-gateways';
56
57 private $gateways_form_data = array();
58 private $form_id;
59
60 public $message = null;
61 public $data = null;
62 public $is_sandbox;
63 private $current_gateway_type;
64
65 private $rest;
66
67 public static function get_instance_id(): string {
68 return 'gateways';
69 }
70
71 public function condition(): bool {
72 $allow = apply_filters_deprecated(
73 'jet-form-builder/allow-gateways',
74 array( false ),
75 '3.1.0',
76 __( 'Settings -> Payments Gateways -> Enable Gateways', 'jet-form-builder' )
77 );
78
79 // install tab handlers for Settings page
80 Tab_Handler_Manager::instance()->install( new Tab_Handlers\Payments_Gateways_Handler() );
81
82 $gateways = Tab_Handler_Manager::get_options( self::OPTION_NAME );
83
84 $use_gateways = isset( $gateways['use_gateways'] ) ? boolval( $gateways['use_gateways'] ) : $allow;
85 return apply_filters( 'jet-form-builder/use-gateways', $use_gateways );
86 }
87
88 public function init_hooks() {
89 add_action( 'rest_api_init', array( $this->get_rest(), 'register_routes' ) );
90 add_action( 'init', array( $this, 'register_gateways' ) );
91 add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ) );
92 add_action(
93 'jet-form-builder/before-print-page/header',
94 array( $this, 'before_print_page' ),
95 10,
96 2
97 );
98
99 add_filter( 'jet-form-builder/admin/pages', array( $this, 'add_stable_pages' ), 0 );
100 add_filter( 'jet-form-builder/admin/single-pages', array( $this, 'add_single_pages' ), 0 );
101 add_filter( 'jet-form-builder/admin/action-pages', array( $this, 'add_action_admin_pages' ) );
102 add_filter(
103 'jet-form-builder/page-containers/jfb-records-single',
104 array( $this, 'add_box_to_single_record' ),
105 0
106 );
107 add_filter(
108 'jet-form-builder/default-process-event/executors',
109 array( $this, 'add_executor_to_default_process' )
110 );
111 add_filter( 'jet-form-builder/editor/config', array( $this, 'on_localize_editor_config' ) );
112 }
113
114 public function remove_hooks() {
115 remove_action( 'rest_api_init', array( $this->get_rest(), 'register_routes' ) );
116 remove_action( 'init', array( $this, 'register_gateways' ) );
117 remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ) );
118 remove_action(
119 'jet-form-builder/before-print-page/header',
120 array( $this, 'before_print_page' )
121 );
122
123 remove_filter( 'jet-form-builder/admin/pages', array( $this, 'add_stable_pages' ), 0 );
124 remove_filter( 'jet-form-builder/admin/single-pages', array( $this, 'add_single_pages' ), 0 );
125 remove_filter( 'jet-form-builder/admin/action-pages', array( $this, 'add_action_admin_pages' ) );
126 remove_filter(
127 'jet-form-builder/page-containers/jfb-records-single',
128 array( $this, 'add_box_to_single_record' ),
129 0
130 );
131 remove_filter(
132 'jet-form-builder/default-process-event/executors',
133 array( $this, 'add_executor_to_default_process' )
134 );
135 remove_filter( 'jet-form-builder/editor/config', array( $this, 'on_localize_editor_config' ) );
136 }
137
138 public function on_install() {
139 // backward compatibility
140 require_once $this->get_dir( 'legacy/payment-details-box.php' );
141 require_once $this->get_dir( 'legacy/base-gateway-action.php' );
142
143 $options = Tab_Handler_Manager::get_options( self::OPTION_NAME );
144 $this->is_sandbox = apply_filters_deprecated(
145 'jet-form-builder/gateways/paypal/sandbox-mode',
146 array( false ),
147 '3.1.0',
148 __( 'Settings -> Payments Gateways -> Enable Test Mode', 'jet-form-builder' )
149 );
150
151 if ( isset( $options['enable_test_mode'] ) ) {
152 $this->is_sandbox = $options['enable_test_mode'];
153 }
154
155 // rest api
156 $this->rest = new Rest_Api_Controller();
157
158 // catch webhook by get param
159 $this->catch_payment_result();
160 }
161
162 public function on_uninstall() {
163 // install tab handlers for Settings page
164 Tab_Handler_Manager::instance()->uninstall( new Tab_Handlers\Payments_Gateways_Handler() );
165
166 unset( $this->is_sandbox, $this->rest );
167 }
168
169 public function rep_instances(): array {
170 return array(
171 new Paypal\Controller(),
172 );
173 }
174
175 public function enqueue_editor_assets() {
176 $script_asset = require_once $this->get_dir( 'assets/build/editor.asset.php' );
177
178 wp_enqueue_script(
179 $this->get_handle( 'editor' ),
180 $this->get_url( 'assets/build/editor.js' ),
181 $script_asset['dependencies'],
182 $script_asset['version'],
183 true
184 );
185 }
186
187 public function add_executor_to_default_process( array $executors ): array {
188 return array_merge(
189 array(
190 new Default_With_Gateway_Executor(),
191 ),
192 $executors
193 );
194 }
195
196 public function add_stable_pages( $pages ): array {
197 $pages[] = new Pages\Payments_Page();
198
199 return $pages;
200 }
201
202 public function add_single_pages( $pages ): array {
203 $pages[] = new Pages\Single_Payment_Page();
204
205 return $pages;
206 }
207
208 public function add_action_admin_pages( array $pages ): array {
209 array_push(
210 $pages,
211 new Pages\Export_Page(),
212 new Pages\Print_Page()
213 );
214
215 return $pages;
216 }
217
218 /**
219 * @param Header $header
220 * @param Base_Single_Page $page
221 */
222 public function before_print_page( Header $header, Base_Single_Page $page ) {
223 if ( ! ( $page instanceof Pages\Single_Payment_Print_Page ) ) {
224 return;
225 }
226 $header->set_title( __( 'JetFormBuilder Payment', 'jet-form-builder' ) );
227 }
228
229 /**
230 * @param Base_Meta_Container[] $containers
231 *
232 * @return array
233 */
234 public function add_box_to_single_record( array $containers ): array {
235 $containers[1]->add_meta_box( new Payment_Info_For_Record() );
236
237 return $containers;
238 }
239
240 public function on_localize_editor_config( array $config ): array {
241 $config['gateways'] = $this->editor_data();
242
243 return $config;
244 }
245
246 /**
247 * Returns all registered gateways
248 *
249 * @return void [description]
250 */
251 public function register_gateways() {
252 // install gateways
253 $this->rep_install();
254
255 do_action( 'jet-form-builder/gateways/register', $this );
256 }
257
258 /**
259 * @param Base_Gateway $gateway
260 *
261 * @throws Repository_Exception
262 */
263 public function register_gateway( Base_Gateway $gateway ) {
264 $this->rep_install_item( $gateway );
265 }
266
267 public function save_gateways_form_data( $data ) {
268 $id = $data['gateway'];
269
270 $this->gateways_form_data = $this->with_global_settings( $data, $id );
271 }
272
273 /**
274 * Catch processed payment results
275 *
276 * @return void [description]
277 */
278 public function catch_payment_result() {
279 // phpcs:disable WordPress.Security.NonceVerification.Recommended
280 if ( ! isset( $_GET[ self::PAYMENT_TYPE_PARAM ] ) ) {
281 return;
282 }
283
284 $this->current_gateway_type = wp_unslash( sanitize_key( $_GET[ self::PAYMENT_TYPE_PARAM ] ) );
285 // phpcs:enable WordPress.Security.NonceVerification.Recommended
286
287 add_action( 'parse_request', array( $this, 'on_has_gateway_request' ) );
288 }
289
290 public function on_has_gateway_request() {
291 try {
292 $this->get_gateway_controller( $this->current_gateway() )->try_run_on_catch();
293 } catch ( Repository_Exception $exception ) {
294 return;
295 }
296 }
297
298 /**
299 * @param false $type
300 *
301 * @return Base_Gateway|Base_Scenario_Gateway
302 * @throws Repository_Exception
303 */
304 public function get_gateway_controller( $type = false ) {
305 return $this->rep_get_item( $type );
306 }
307
308 /**
309 * @return Base_Gateway|Base_Scenario_Gateway
310 * @throws Repository_Exception
311 */
312 public function get_current_gateway_controller() {
313 return $this->get_gateway_controller( $this->get_gateway_id() );
314 }
315
316 /**
317 * @return false|Base_Gateway|Base_Scenario_Gateway
318 */
319 public function get_current_gateway_controller_or_die() {
320 try {
321 return $this->get_current_gateway_controller();
322 } catch ( Repository_Exception $exception ) {
323 return false;
324 }
325 }
326
327 /**
328 * @param $gateway_id
329 *
330 * @return Base_Gateway
331 */
332 public function controller( $gateway_id ) {
333 try {
334 return $this->get_gateway_controller( $gateway_id );
335 } catch ( Repository_Exception $exception ) {
336 _doing_it_wrong( __METHOD__, esc_html( "Undefined gateway: {$gateway_id}" ), '2.0.0' );
337 }
338 }
339
340 /**
341 * Returns gateways config for current form
342 *
343 * @param [type] $post_id [description]
344 *
345 * @return [type] [description]
346 */
347 public function get_form_gateways_by_id( $form_id = null ) {
348
349 if ( ! $form_id ) {
350 $form_id = get_the_ID();
351 }
352 $default = array( 'gateway' => 'none' );
353
354 $meta = Plugin::instance()->post_type->get_gateways( $form_id );
355
356 return is_array( $meta ) ? $meta : $default;
357 }
358
359 public function gateways( $prop = '', $if_empty = false ) {
360 return $prop ? ( $this->gateways_form_data[ $prop ] ?? $if_empty ) : $this->gateways_form_data;
361 }
362
363 public function has_gateway( $form_id ) {
364 $this->set_gateways_options_by_form_id( $form_id );
365
366 return $this->get_gateway_id( false );
367 }
368
369 public function get_global_settings( $gateway_id ) {
370 return Tab_Handler_Manager::instance()->tab( $gateway_id )->on_load();
371 }
372
373 public function with_global_settings( $gateways_data, $gateway_id ) {
374 if ( ! isset( $gateways_data[ $gateway_id ] ) ) {
375 return $gateways_data;
376 }
377 $gateway = &$gateways_data[ $gateway_id ];
378
379 if ( ! empty( $gateway['use_global'] ) ) {
380 $gateway = array_merge( $gateway, $this->get_global_settings( $gateway_id ) );
381 }
382
383 return $gateways_data;
384 }
385
386 public function get_gateway_id( $if_empty = 'none' ) {
387 return $this->gateways_form_data['gateway'] ?? $this->current_gateway_type ?? $if_empty;
388 }
389
390 public function set_form_id( $form_id ) {
391 $this->form_id = (int) $form_id;
392
393 return $this;
394 }
395
396 public function get_form_id() {
397 return $this->form_id;
398 }
399
400 public function set_gateways_options_by_form_id( $form_id ) {
401 if ( (int) $form_id === $this->get_form_id() ) {
402 return;
403 }
404 $this->set_form_id( $form_id );
405 $gateways = $this->get_form_gateways_by_id( $this->get_form_id() );
406
407 if ( 'none' === ( $gateways['gateway'] ?? 'none' ) ) {
408 return;
409 }
410 $this->save_gateways_form_data( $gateways );
411 }
412
413 public function current_gateway() {
414 return $this->current_gateway_type;
415 }
416
417 public function currency_symbol( $currency ) {
418 $currency = strtoupper( $currency );
419
420 switch ( $currency ) {
421 case 'USD':
422 case 'AUD':
423 case 'NZD':
424 case 'CAD':
425 case 'HKD':
426 case 'MXN':
427 case 'SGD':
428 $symbol = '&#36;';
429 break;
430 case 'EUR':
431 $symbol = '&euro;';
432 break;
433 case 'GBP':
434 $symbol = '&pound;';
435 break;
436 case 'BRL':
437 $symbol = 'R&#36;';
438 break;
439 case 'JPY':
440 $symbol = '&yen;';
441 break;
442 case 'AOA':
443 $symbol = 'Kz';
444 break;
445 default:
446 $symbol = $currency;
447 break;
448 }
449
450 return apply_filters( 'jet-form-builder/gateways/currency-symbol', $symbol, $currency );
451 }
452
453 public function get_rest(): Rest_Api_Controller {
454 return $this->rest;
455 }
456
457 }
458