PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.3
JetFormBuilder — Dynamic Blocks Form Builder v1.2.3
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 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 5 years ago base-gateway.php 5 years ago gateway-manager.php 5 years ago gateways-editor-data.php 5 years ago
base-gateway.php
446 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 if ( ! empty( $keep_these ) ) {
167 $notifications = new Action_Handler( $this->data['form_id'] );
168 $notifications->add_request( $this->data['form_data'] );
169
170 $notifications->unregister_action( 'redirect_to_page' );
171
172 $all = $notifications->get_all();
173
174 if ( empty( $all ) ) {
175 return;
176 }
177
178 foreach ( $all as $index => $notification ) {
179 $this->maybe_unregister_action( $notifications, $index, $keep_these );
180 }
181
182 if ( empty( $notifications->form_actions ) ) {
183 return;
184 }
185
186 $notifications->run_actions();
187 }
188
189 }
190
191 final public function set_payment_token() {
192 if ( empty( $this->token_query_name ) || empty( $_GET[ $this->token_query_name ] ) ) {
193 throw new Gateway_Exception( 'Empty payment token.' );
194 }
195
196 $this->payment_token = $this->get_payment_token();
197
198 if ( empty( $this->payment_token ) ) {
199 throw new Gateway_Exception( 'Invalid payment token.' );
200 }
201
202 return $this;
203 }
204
205
206 final public function set_gateways_meta() {
207 $row_data = $this->get_form_data();
208
209 $this->payment_id = $row_data['post_id'];
210 $this->data = Tools::decode_unserializable( $row_data['meta_value'] );
211
212 return $this;
213 }
214
215 final public function set_form_gateways_meta() {
216 $data = $this->retrieve_gateway_meta();
217
218 if ( isset( $data[ $this->get_id() ]['use_global'] ) && $data[ $this->get_id() ]['use_global'] ) {
219 $data[ $this->get_id() ] = array_merge( $data[ $this->get_id() ], Tab_Handler_Manager::instance()->tab( $this->get_id() )->on_load() );
220 }
221
222 $this->gateways_meta = $data;
223
224 return $this;
225 }
226
227 final protected function set_order_token() {
228 $this->order_token = $this->query_order_token( $this->order_id, $this->action_handler->form_id );
229
230 if ( ! $this->order_token ) {
231 throw new Gateway_Exception( 'Invalid token', $this->order_token );
232 }
233 }
234
235 public function get_payment_token() {
236 return esc_attr( $_GET[ $this->token_query_name ] );
237 }
238
239 public function get_payment_id() {
240 return absint( explode( '-', $this->payment_token )[0] );
241 }
242
243 /**
244 * Prevent unnecessary notifications processing before form is send.
245 *
246 * @param $action_handler
247 *
248 * @param $keep_these
249 *
250 * @return void [description]
251 */
252 public function before_actions( Action_Handler $action_handler, $keep_these ) {
253
254 if ( empty( $action_handler->get_all() ) ) {
255 return;
256 }
257 $meta = Gateway_Manager::instance()->gateways();
258
259 foreach ( $action_handler->get_all() as $index => $action ) {
260 $action_order = isset( $meta['action_order'] ) ? (int) $meta['action_order'] : 0;
261
262 if ( 'insert_post' === $action->get_id() && $action_order === $action->_id ) {
263 continue;
264 }
265
266 if ( 'redirect_to_page' === $action->get_id() ) {
267 $action_handler->unregister_action( $action->get_id() );
268 $this->redirect = $action;
269 }
270
271 if ( ! array_key_exists( $index, $keep_these ) ) {
272 $action_handler->unregister_action( $index );
273 }
274 $this->maybe_unregister_action( $action_handler, $index, $keep_these );
275 }
276 }
277
278 /**
279 * Returns form data by payment ID
280 *
281 * @param [type] $payment [description]
282 *
283 * @return [type] [description]
284 */
285 public function get_form_by_payment_token( $payment = null ) {
286
287 if ( ! $payment ) {
288 return;
289 }
290
291 global $wpdb;
292 $sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key = '" . self::GATEWAY_META_KEY . "' AND meta_value LIKE '%$payment%';";
293
294 return $wpdb->get_row( $sql, ARRAY_A );
295 }
296
297 protected function set_order_id() {
298 $inserted_id = empty( $this->gateways_meta['action_order'] ) ? 0 : $this->gateways_meta['action_order'];
299 $inserted_id = $this->action_handler->get_inserted_post_id( $inserted_id );
300
301 if ( ! $inserted_id ) {
302 throw new Gateway_Exception( 'There is not inserted_post_id' );
303 }
304
305 $this->order_id = $inserted_id;
306 }
307
308
309 protected function set_price_field() {
310 if ( isset( $this->gateways_meta['price_field'] ) && ! empty( $this->gateways_meta['price_field'] ) ) {
311 $this->price_field = esc_attr( $this->gateways_meta['price_field'] );
312 }
313
314 $this->price_field = apply_filters( 'jet-form-builder/gateways/price-field', $this->price_field, $this->action_handler );
315
316 if ( ! $this->price_field ) {
317 throw new Gateway_Exception( 'Invalid price field' );
318 }
319 }
320
321 protected function set_price_from_filed() {
322 if ( isset( $this->action_handler->request_data[ $this->price_field ] ) ) {
323 $this->price = $this->get_price( $this->action_handler->request_data[ $this->price_field ] );
324 }
325
326 if ( ! $this->price ) {
327 throw new Gateway_Exception( 'Empty price field' );
328 }
329 }
330
331 protected function set_current_gateway_options() {
332 $gateway = $this->gateways_meta[ $this->get_id() ];
333
334 foreach ( $this->options_list() as $name => $option ) {
335 if ( ! isset( $gateway[ $name ] ) || empty( $gateway[ $name ] ) ) {
336 throw new Gateway_Exception( 'Invalid gateway options' );
337 }
338 $this->options[ $name ] = esc_attr( $gateway[ $name ] );
339 }
340 }
341
342 protected function set_gateway_data( Action_Handler $action_handler ) {
343 $this->gateways_meta = GM::instance()->gateways();
344 $this->action_handler = $action_handler;
345
346 try {
347 $this->set_order_id();
348 $this->set_price_field();
349 $this->set_price_from_filed();
350 $this->set_current_gateway_options();
351 $this->set_order_token();
352
353 } catch ( Gateway_Exception $exception ) {
354 return false;
355 }
356
357 return true;
358 }
359
360 protected function get_price( $price ) {
361 return (float) $price;
362 }
363
364 protected function get_refer_url( $type, array $additional_args = array() ) {
365
366 $success_redirect = ! empty( $this->gateways_meta['use_success_redirect'] ) ? $this->gateways_meta['use_success_redirect'] : false;
367 $success_redirect = filter_var( $success_redirect, FILTER_VALIDATE_BOOLEAN );
368 $refer = $this->action_handler->request_data['__refer'];
369
370 if ( $success_redirect && $this->redirect && 'success' === $type ) {
371 $settings = $this->redirect->settings;
372
373 $type = ! empty( $settings['redirect_type'] ) ? $settings['redirect_type'] : 'static_page';
374
375 if ( 'static_page' === $type ) {
376 $to_page = ! empty( $settings['redirect_page'] ) ? $settings['redirect_page'] : false;
377 $refer = ! empty( $to_page ) ? get_permalink( $to_page ) : false;
378 } else {
379 $refer = ! empty( $settings['redirect_url'] ) ? $settings['redirect_url'] : false;
380 }
381 }
382
383 return add_query_arg(
384 array_merge(
385 array( GM::PAYMENT_TYPE_PARAM => $this->get_id() ),
386 $additional_args
387 ),
388 $refer
389 );
390 }
391
392 abstract protected function query_order_token( $order_id, $form_id );
393
394 protected function get_form_data() {
395 return $this->get_form_by_payment_token( $this->payment_token );
396 }
397
398 public function options( $param = false ) {
399 $labels = array();
400
401 if ( ! $param ) {
402 return $this->options_list();
403 }
404 foreach ( $this->options_list() as $name => $option ) {
405 $labels[ $name ] = $option[ $param ];
406 }
407
408 return $labels;
409 }
410
411 /**
412 * Apply macros in string
413 *
414 * @return [type] [description]
415 */
416 public function apply_macros( $string = null ) {
417
418 return preg_replace_callback( '/%(.*?)%/', function ( $matches ) {
419 switch ( $matches[1] ) {
420 case 'gateway_amount':
421 $amount = ! empty( $this->data['amount'] ) ? $this->data['amount'] : false;
422
423 return ! empty( $amount ) ? $amount['value'] . ' ' . $amount['currency_code'] : 0;
424
425 case 'gateway_status':
426 return ! empty( $this->data['status'] ) ? $this->data['status'] : '';
427
428 default:
429 $form_data = ! empty( $this->data['form_data'] ) ? $this->data['form_data'] : array();
430
431 return ! empty( $form_data[ $matches[1] ] ) ? $form_data[ $matches[1] ] : '';
432 }
433
434 }, $string );
435 }
436
437 private function maybe_unregister_action( Action_Handler $handler, $index, $keep_these ) {
438 if ( ! array_key_exists( $index, $keep_these )
439 || ! isset( $keep_these[ $index ]['active'] )
440 || ! $keep_these[ $index ]['active']
441 ) {
442 $handler->unregister_action( $index );
443 }
444 }
445
446 }