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