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