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