PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.8
JetFormBuilder — Dynamic Blocks Form Builder v3.0.8
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
db-models 3 years ago meta-boxes 3 years ago pages 3 years ago paypal 3 years ago query-views 3 years ago rest-api 3 years ago scenarios-abstract 3 years ago table-views 3 years ago base-gateway-action.php 3 years ago base-gateway.php 3 years ago base-scenario-gateway.php 3 years ago gateway-manager.php 3 years ago gateways-editor-data.php 3 years ago legacy-base-gateway.php 3 years ago migrate-legacy-data.php 3 years ago scenario-item.php 3 years ago
base-gateway.php
370 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Gateways;
5
6 use Jet_Form_Builder\Actions\Types\Redirect_To_Page;
7 use Jet_Form_Builder\Classes\Http\Http_Tools;
8 use Jet_Form_Builder\Db_Queries\Exceptions\Skip_Exception;
9 use Jet_Form_Builder\Exceptions\Action_Exception;
10 use Jet_Form_Builder\Exceptions\Gateway_Exception;
11 use Jet_Form_Builder\Form_Messages\Manager;
12 use Jet_Form_Builder\Form_Response\Response;
13 use Jet_Form_Builder\Form_Response\Types\Reload_Response;
14 use Jet_Form_Builder\Gateways\Gateway_Manager as GM;
15 use Jet_Form_Builder\Gateways\Scenarios_Abstract\Scenarios_Manager_Abstract;
16
17 /**
18 *
19 * Class Base_Gateway
20 *
21 * @package Jet_Form_Builder\Gateways
22 */
23 abstract class Base_Gateway extends Legacy_Base_Gateway {
24
25 const GATEWAY_META_KEY = '_jet_gateway_data';
26 const PAYMENT_TYPE_INITIAL = 'initial';
27 const PAYMENT_TYPE_RENEWAl = 'renewal';
28
29 const SUCCESS_TYPE = 'success';
30 const FAILED_TYPE = 'cancel';
31
32 /** @var Redirect_To_Page */
33 protected $redirect;
34 protected $order_token;
35
36 protected $payment_instance = array();
37 protected $token_query_name;
38
39 protected $removed_query_args_on_payment = array(
40 GM::PAYMENT_TYPE_PARAM,
41 Scenarios_Manager_Abstract::QUERY_VAR,
42 'session_id',
43 'order_token',
44 'status',
45 'PayerID',
46 );
47
48 public function rep_item_id() {
49 return $this->get_id();
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 protected function options_list() {
67 return array();
68 }
69
70 abstract protected function retrieve_gateway_meta();
71
72 public function custom_labels(): array {
73 return array();
74 }
75
76 public function get_payment() {
77 return $this->payment_instance;
78 }
79
80 public function additional_editor_data(): array {
81 return array(
82 'version' => 0,
83 );
84 }
85
86 public function try_run_on_catch() {
87 try {
88 $this->set_payment_token();
89 $this->set_gateway_from_post_meta();
90
91 /**
92 * Init actions to migrate in events
93 */
94 $this->init_actions();
95
96 $this->set_form_gateways_meta();
97 $this->set_payment();
98 $this->on_success_payment();
99
100 } catch ( Gateway_Exception $exception ) {
101 return;
102 } catch ( Skip_Exception $e ) {
103 return;
104 }
105 }
106
107 private function init_actions() {
108 $form_id = (int) ( $this->data['form_id'] ?? 0 );
109
110 jet_fb_action_handler()->set_form_id( $form_id );
111 }
112
113 /**
114 * @return $this
115 * @throws Skip_Exception
116 */
117 public function set_payment_token() {
118 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
119 if ( empty( $this->token_query_name ) || empty( $_GET[ $this->token_query_name ] ) ) {
120 throw new Skip_Exception( 'Empty payment token.' );
121 }
122
123 $this->payment_token = $this->get_payment_token();
124
125 if ( empty( $this->payment_token ) ) {
126 throw new Skip_Exception( 'Invalid payment token.' );
127 }
128
129 return $this;
130 }
131
132
133 /**
134 * @return $this
135 */
136 public function set_form_gateways_meta() {
137 return $this->set_form_meta( $this->with_global_settings() );
138 }
139
140 public function get_payment_token() {
141 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
142 return sanitize_text_field( wp_unslash( $_GET[ $this->token_query_name ] ?? '' ) );
143 }
144
145 /**
146 * @return int[]
147 */
148 public function get_actions_before() {
149 if ( ! $this->gateway( 'notifications_before' ) ) {
150 return array();
151 }
152
153 return array_filter(
154 $this->gateway( 'notifications_before' ),
155 function ( $action ) {
156 return $action['active'];
157 }
158 );
159 }
160
161 /**
162 * @return string
163 * @deprecated since 2.0.0
164 */
165 public function get_result_message( $status ) {
166 if ( ! $status || in_array( $status, $this->failed_statuses() ) ) {
167 $message = Manager::dynamic_error( $this->get_meta_message( 'failed' ) );
168 } else {
169 $message = Manager::dynamic_success( $this->get_meta_message( 'success' ) );
170 }
171 $message = stripcslashes( $message );
172
173 return $message;
174 }
175
176 /**
177 * @return string
178 */
179 public function get_meta_message( $type ) {
180
181 return isset( $this->gateways_meta['messages'][ $type ] )
182 ? $this->apply_macros( $this->gateways_meta['messages'][ $type ] )
183 : Gateway_Manager::instance()->default_messages()[ $type ];
184 }
185
186
187 /**
188 * @throws Gateway_Exception
189 */
190 public function set_current_gateway_options() {
191 if ( ! empty( $this->options ) ) {
192 return $this;
193 }
194
195 foreach ( $this->options_list() as $name => $option ) {
196 $is_required = isset( $option['required'] )
197 ? filter_var( $option['required'], FILTER_VALIDATE_BOOLEAN )
198 : true;
199
200 $default_val = $option['default'] ?? false;
201
202 if ( $is_required && ! $this->current_gateway( $name ) && false === $default_val ) {
203 throw new Gateway_Exception( 'Invalid gateway options', $name );
204 }
205
206 $this->options[ $name ] = $this->isset_current_gateway( $name )
207 ? sanitize_text_field( $this->current_gateway( $name ) )
208 : $default_val;
209 }
210
211 return $this;
212 }
213
214
215 public function get_refer_url( $type, array $additional_args = array() ) {
216 return add_query_arg(
217 array_merge(
218 array( GM::PAYMENT_TYPE_PARAM => $this->get_id() ),
219 $additional_args
220 ),
221 jet_fb_handler()->refer
222 );
223 }
224
225
226 public function options( $param = false ) {
227 $labels = array();
228
229 if ( ! $param ) {
230 return $this->options_list();
231 }
232 foreach ( $this->options_list() as $name => $option ) {
233 $labels[ $name ] = $option[ $param ] ?? '';
234 }
235
236 return $labels;
237 }
238
239
240 public function with_global_settings() {
241 return Gateway_Manager::instance()->with_global_settings( $this->retrieve_gateway_meta(), $this->get_id() );
242 }
243
244
245 /**
246 * Gateway part starts.
247 *
248 * @param bool $if_empty
249 *
250 * @return false|mixed
251 */
252
253 public function get_current_gateway( $if_empty = false ) {
254 return $this->gateway( $this->get_id(), $if_empty );
255 }
256
257 public function current_gateway( $prop = '', $if_empty = false ) {
258 $gateway = $this->get_current_gateway( $if_empty );
259
260 return $prop ? $gateway[ $prop ] ?? $if_empty : $gateway;
261 }
262
263 public function isset_current_gateway( $prop ) {
264 $gateway = $this->get_current_gateway( false );
265
266 return isset( $gateway[ $prop ] );
267 }
268
269 public function gateway( $prop = '', $if_empty = false ) {
270 return $this->gateways_meta[ $prop ] ?? $if_empty;
271 }
272
273 /**
274 * Gateway part ends.
275 */
276
277 public function get_scenario_meta(): array {
278 $gateway = $this->get_current_gateway( array() );
279
280 return $gateway['scenario'] ?? array();
281 }
282
283 public function get_current_scenario_id() {
284 $scenario = $this->get_scenario_meta();
285
286 return empty( $scenario['id'] )
287 ? Paypal\Scenarios_Logic\Pay_Now::scenario_id()
288 : $scenario['id'];
289 }
290
291 public function get_current_scenario(): array {
292 $scenario_id = $this->get_current_scenario_id();
293 $scenario = $this->get_scenario_meta();
294
295 return $scenario[ $scenario_id ] ?? array();
296 }
297
298 /**
299 * @param string $prop
300 * @param false $if_empty
301 *
302 * @return mixed
303 */
304 public function current_scenario( $prop = '', $if_empty = false ) {
305 $scenario = $this->get_current_scenario();
306
307 return $prop ? $scenario[ $prop ] ?? $if_empty : $scenario;
308 }
309
310
311 public function isset_gateway( $prop ): bool {
312 return isset( $this->gateways_meta[ $prop ] );
313 }
314
315 public function property( $prop ) {
316 return $this->$prop ?? false;
317 }
318
319 public function set_form_meta( $gateways_meta ): Base_Gateway {
320 Migrate_Legacy_Data::migrate( $gateways_meta );
321
322 $this->gateways_meta = $gateways_meta;
323
324 return $this;
325 }
326
327 /**
328 * Execute actions or something else when payment is success
329 *
330 * @return void [description]
331 */
332 protected function try_do_actions() {
333 try {
334 if ( in_array( $this->data['status'], $this->failed_statuses() ) ) {
335 $this->process_status( 'failed' );
336 } else {
337 $this->process_status( 'success' );
338 }
339 } catch ( Action_Exception $exception ) {
340 $this->send_response(
341 array(
342 'status' => $exception->get_form_status(),
343 )
344 );
345 }
346 }
347
348 public function send_response( $args = array() ) {
349 $redirect = jet_fb_action_handler()->response_data['redirect'] ?? '';
350
351 if ( ! empty( $redirect ) ) {
352 // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
353 wp_redirect( $redirect );
354 die();
355 }
356
357 ( new Response( $this->get_response_manager() ) )->init( $args )->send();
358 }
359
360 private function get_response_manager() {
361 return new Reload_Response(
362 array(
363 'refer' => Http_Tools::get_form_refer_url(),
364 'remove_args' => $this->removed_query_args_on_payment,
365 )
366 );
367 }
368
369 }
370