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