PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.6
JetFormBuilder — Dynamic Blocks Form Builder v1.2.6
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 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / gateways / base-gateway.php
jetformbuilder / includes / gateways Last commit date
paypal 4 years ago base-gateway.php 4 years ago gateway-manager.php 4 years ago gateways-editor-data.php 4 years ago
base-gateway.php
458 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Gateways;
5
6
7 use Jet_Form_Builder\Actions\Action_Handler;
8 use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager;
9 use Jet_Form_Builder\Classes\Tools;
10 use Jet_Form_Builder\Exceptions\Action_Exception;
11 use Jet_Form_Builder\Exceptions\Gateway_Exception;
12 use Jet_Form_Builder\Form_Messages\Manager;
13 use Jet_Form_Builder\Form_Response\Response;
14 use Jet_Form_Builder\Form_Response\Types\Reload_Response;
15 use Jet_Form_Builder\Gateways\Gateway_Manager as GM;
16
17 /**
18 * @property Action_Handler action_handler
19 *
20 * Class Base_Gateway
21 * @package Jet_Form_Builder\Gateways
22 */
23 abstract class Base_Gateway {
24
25 const GATEWAY_META_KEY = '_jet_gateway_data';
26
27 const SUCCESS_TYPE = 'success';
28 const FAILED_TYPE = 'cancel';
29
30 protected $payment_id;
31 protected $payment_token;
32 protected $data;
33 protected $action_handler;
34 protected $order_id;
35 protected $gateways_meta;
36 protected $price_field;
37 protected $price;
38 protected $options;
39 protected $redirect;
40 protected $order_token;
41
42 protected $payment_instance = array();
43 protected $token_query_name;
44
45 protected $removed_query_args_on_payment = array(
46 GM::PAYMENT_TYPE_PARAM,
47 'order_token',
48 'status',
49 'PayerID'
50 );
51
52 /**
53 * Returns current gateway ID
54 *
55 * @return [type] [description]
56 */
57 abstract public function get_id();
58
59 /**
60 * Returns current gateway name
61 *
62 * @return [type] [description]
63 */
64 abstract public function get_name();
65
66 abstract protected function options_list();
67
68 abstract public function after_actions( Action_Handler $action_handler );
69
70 abstract protected function set_gateway_data_on_result();
71
72 abstract protected function failed_statuses();
73
74 abstract protected function retrieve_payment_instance();
75
76 abstract protected function retrieve_gateway_meta();
77
78 public function set_payment_instance() {
79 $this->payment_instance = $this->retrieve_payment_instance();
80 }
81
82 /**
83 * Store payment status into order and show success/failed message
84 * @return [type] [description]
85 */
86 public function on_success_payment() {
87 $this->set_gateway_data_on_result();
88 $this->data['date'] = date_i18n( 'F j, Y, H:i' );
89
90 $this->data['gateway'] = $this->get_name();
91
92 update_post_meta( $this->payment_id, self::GATEWAY_META_KEY, json_encode( $this->data ) );
93
94 $this->try_do_actions();
95
96 $this->send_response( array(
97 'status' => $this->get_status_on_payment( $this->data['status'] ),
98 ) );
99 }
100
101 public function get_status_on_payment( $status ) {
102 if ( ! $status || in_array( $status, $this->failed_statuses() ) ) {
103 $message = Manager::dynamic_error( $this->get_meta_message( 'failed' ) );
104 } else {
105 $message = Manager::dynamic_success( $this->get_meta_message( 'success' ) );
106 }
107 $message = stripcslashes( $message );
108
109 return $message;
110 }
111
112 public function get_meta_message( $type ) {
113 if ( isset( $this->gateways_meta['messages'] ) && isset( $this->gateways_meta['messages'][ $type ] ) ) {
114 return $this->apply_macros( $this->gateways_meta['messages'][ $type ] );
115 }
116
117 return Gateway_Manager::instance()->default_messages()[ $type ];
118 }
119
120 /**
121 * Execute actions or something else when payment is success
122 *
123 * @return [type] [description]
124 */
125 protected function try_do_actions() {
126 try {
127 if ( in_array( $this->data['status'], $this->failed_statuses() ) ) {
128 $this->process_status( 'failed' );
129 } else {
130 $this->process_status( 'success' );
131 }
132 } catch ( Action_Exception $exception ) {
133 $this->send_response( array(
134 'status' => $exception->get_form_status(),
135 ) );
136 }
137 }
138
139 private function send_response( $args = array() ) {
140 ( new Response( $this->get_response_manager() ) )->init( $args )->send();
141 }
142
143 private function get_response_manager() {
144 global $wp;
145
146 return new Reload_Response( array(
147 'refer' => home_url( $wp->request ),
148 'remove_args' => $this->removed_query_args_on_payment,
149 ) );
150 }
151
152 /**
153 * Process status notification and enqueue message
154 *
155 * @param string $type [description]
156 *
157 * @return void [type] [description]
158 * @throws Action_Exception
159 */
160 public function process_status( $type = 'success' ) {
161
162 $settings = $this->gateways_meta;
163
164 $keep_these = isset( $settings[ 'notifications_' . $type ] ) ? $settings[ 'notifications_' . $type ] : array();
165
166 do_action( 'jet-form-builder/gateways/on-payment-' . $type, $this );
167
168 if ( ! empty( $keep_these ) ) {
169 $notifications = new Action_Handler( $this->data['form_id'] );
170 $notifications->add_request( $this->data['form_data'] );
171
172 $notifications->unregister_action( 'redirect_to_page' );
173
174 $all = $notifications->get_all();
175
176 if ( empty( $all ) ) {
177 return;
178 }
179
180 foreach ( $all as $index => $notification ) {
181 $this->maybe_unregister_action( $notifications, $index, $keep_these );
182 }
183
184 if ( empty( $notifications->form_actions ) ) {
185 return;
186 }
187
188 $notifications->run_actions();
189 }
190
191 }
192
193 final public function set_payment_token() {
194 if ( empty( $this->token_query_name ) || empty( $_GET[ $this->token_query_name ] ) ) {
195 throw new Gateway_Exception( 'Empty payment token.' );
196 }
197
198 $this->payment_token = $this->get_payment_token();
199
200 if ( empty( $this->payment_token ) ) {
201 throw new Gateway_Exception( 'Invalid payment token.' );
202 }
203
204 return $this;
205 }
206
207
208 final public function set_gateways_meta() {
209 $row_data = $this->get_form_data();
210
211 $this->payment_id = $row_data['post_id'];
212 $this->data = Tools::decode_unserializable( $row_data['meta_value'] );
213
214 return $this;
215 }
216
217 final public function set_form_gateways_meta() {
218 $data = $this->retrieve_gateway_meta();
219
220 if ( isset( $data[ $this->get_id() ]['use_global'] ) && $data[ $this->get_id() ]['use_global'] ) {
221 $data[ $this->get_id() ] = array_merge( $data[ $this->get_id() ], Tab_Handler_Manager::instance()->tab( $this->get_id() )->on_load() );
222 }
223
224 $this->gateways_meta = $data;
225
226 return $this;
227 }
228
229 final protected function set_order_token() {
230 $this->order_token = $this->query_order_token( $this->order_id, $this->action_handler->form_id );
231
232 if ( ! $this->order_token ) {
233 throw new Gateway_Exception( 'Invalid token', $this->order_token );
234 }
235 }
236
237 public function get_payment_token() {
238 return esc_attr( $_GET[ $this->token_query_name ] );
239 }
240
241 public function get_payment_id() {
242 return absint( explode( '-', $this->payment_token )[0] );
243 }
244
245 /**
246 * Prevent unnecessary notifications processing before form is send.
247 *
248 * @param $action_handler
249 *
250 * @param $keep_these
251 *
252 * @return void [description]
253 */
254 public function before_actions( Action_Handler $action_handler, $keep_these ) {
255 if ( empty( $action_handler->get_all() ) ) {
256 return;
257 }
258 $meta = Gateway_Manager::instance()->gateways();
259
260 foreach ( $action_handler->get_all() as $index => $action ) {
261 $action_order = isset( $meta['action_order'] ) ? (int) $meta['action_order'] : 0;
262
263 if ( 'insert_post' === $action->get_id() && $action_order === $action->_id ) {
264 continue;
265 }
266
267 if ( 'redirect_to_page' === $action->get_id() ) {
268 $action_handler->unregister_action( $action->get_id() );
269 $this->redirect = $action;
270 }
271
272 if ( ! array_key_exists( $index, $keep_these ) ) {
273 $action_handler->unregister_action( $index );
274 }
275 $this->maybe_unregister_action( $action_handler, $index, $keep_these );
276 }
277 }
278
279 /**
280 * Returns form data by payment ID
281 *
282 * @param [type] $payment [description]
283 *
284 * @return [type] [description]
285 */
286 public function get_form_by_payment_token( $payment = null ) {
287
288 if ( ! $payment ) {
289 return;
290 }
291
292 global $wpdb;
293 $sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key = '" . self::GATEWAY_META_KEY . "' AND meta_value LIKE '%$payment%';";
294
295 return $wpdb->get_row( $sql, ARRAY_A );
296 }
297
298 protected function set_order_id() {
299 $inserted_id = empty( $this->gateways_meta['action_order'] ) ? 0 : $this->gateways_meta['action_order'];
300 $inserted_id = $this->action_handler->get_inserted_post_id( $inserted_id );
301
302 if ( ! $inserted_id ) {
303 throw new Gateway_Exception( 'There is not inserted_post_id' );
304 }
305
306 $this->order_id = $inserted_id;
307 }
308
309
310 protected function set_price_field() {
311 if ( isset( $this->gateways_meta['price_field'] ) && ! empty( $this->gateways_meta['price_field'] ) ) {
312 $this->price_field = esc_attr( $this->gateways_meta['price_field'] );
313 }
314
315 $this->price_field = apply_filters( 'jet-form-builder/gateways/price-field', $this->price_field, $this->action_handler );
316
317 if ( ! $this->price_field ) {
318 throw new Gateway_Exception( 'Invalid price field' );
319 }
320 }
321
322 protected function set_price_from_filed() {
323 if ( isset( $this->action_handler->request_data[ $this->price_field ] ) ) {
324 $this->price = $this->get_price( $this->action_handler->request_data[ $this->price_field ] );
325 }
326
327 if ( ! $this->price ) {
328 throw new Gateway_Exception( 'Empty price field' );
329 }
330 }
331
332 /**
333 * @throws Gateway_Exception
334 */
335 protected function set_current_gateway_options() {
336 $gateway = $this->gateways_meta[ $this->get_id() ];
337
338 foreach ( $this->options_list() as $name => $option ) {
339 $is_required = isset( $option['required'] )
340 ? filter_var( $option['required'], FILTER_VALIDATE_BOOLEAN )
341 : true;
342
343 if ( $is_required && ( ! isset( $gateway[ $name ] ) || empty( $gateway[ $name ] ) ) ) {
344 throw new Gateway_Exception( 'Invalid gateway options', $name );
345 }
346 $this->options[ $name ] = esc_attr( $gateway[ $name ] );
347 }
348 }
349
350 protected function set_gateway_data( Action_Handler $action_handler ) {
351 $this->gateways_meta = GM::instance()->gateways();
352 $this->action_handler = $action_handler;
353
354 try {
355 $this->set_order_id();
356 $this->set_price_field();
357 $this->set_price_from_filed();
358 $this->set_current_gateway_options();
359 $this->set_order_token();
360
361 } catch ( Gateway_Exception $exception ) {
362 return false;
363 }
364
365 return true;
366 }
367
368 protected function get_price( $price ) {
369 return (float) $price;
370 }
371
372 protected function get_refer_url( $type, array $additional_args = array() ) {
373
374 $success_redirect = ! empty( $this->gateways_meta['use_success_redirect'] ) ? $this->gateways_meta['use_success_redirect'] : false;
375 $success_redirect = filter_var( $success_redirect, FILTER_VALIDATE_BOOLEAN );
376 $refer = $this->action_handler->request_data['__refer'];
377
378 if ( $success_redirect && $this->redirect && 'success' === $type ) {
379 $settings = $this->redirect->settings;
380
381 $type = ! empty( $settings['redirect_type'] ) ? $settings['redirect_type'] : 'static_page';
382
383 if ( 'static_page' === $type ) {
384 $to_page = ! empty( $settings['redirect_page'] ) ? $settings['redirect_page'] : false;
385 $refer = ! empty( $to_page ) ? get_permalink( $to_page ) : false;
386 } else {
387 $refer = ! empty( $settings['redirect_url'] ) ? $settings['redirect_url'] : false;
388 }
389 }
390
391 return add_query_arg(
392 array_merge(
393 array( GM::PAYMENT_TYPE_PARAM => $this->get_id() ),
394 $additional_args
395 ),
396 $refer
397 );
398 }
399
400 abstract protected function query_order_token( $order_id, $form_id );
401
402 protected function get_form_data() {
403 return $this->get_form_by_payment_token( $this->payment_token );
404 }
405
406 public function options( $param = false ) {
407 $labels = array();
408
409 if ( ! $param ) {
410 return $this->options_list();
411 }
412 foreach ( $this->options_list() as $name => $option ) {
413 $labels[ $name ] = $option[ $param ];
414 }
415
416 return $labels;
417 }
418
419 /**
420 * Apply macros in string
421 *
422 * @return [type] [description]
423 */
424 public function apply_macros( $string = null ) {
425
426 return preg_replace_callback( '/%(.*?)%/', function ( $matches ) {
427 switch ( $matches[1] ) {
428 case 'gateway_amount':
429 $amount = ! empty( $this->data['amount'] ) ? $this->data['amount'] : false;
430
431 return ! empty( $amount ) ? $amount['value'] . ' ' . $amount['currency_code'] : 0;
432
433 case 'gateway_status':
434 return ! empty( $this->data['status'] ) ? $this->data['status'] : '';
435
436 default:
437 $form_data = ! empty( $this->data['form_data'] ) ? $this->data['form_data'] : array();
438
439 return ! empty( $form_data[ $matches[1] ] ) ? $form_data[ $matches[1] ] : '';
440 }
441
442 }, $string );
443 }
444
445 private function maybe_unregister_action( Action_Handler $handler, $index, $keep_these ) {
446 if ( ! array_key_exists( $index, $keep_these )
447 || ! isset( $keep_these[ $index ]['active'] )
448 || ! $keep_these[ $index ]['active']
449 ) {
450 $handler->unregister_action( $index );
451 }
452 }
453
454 public function property( $prop ) {
455 return $this->$prop;
456 }
457
458 }