PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.1.1
JetFormBuilder — Dynamic Blocks Form Builder v3.6.1.1
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.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 All 134 releases
jetformbuilder / modules / gateways / legacy-base-gateway.php

legacy-base-gateway.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.1.1, at modules/gateways/legacy-base-gateway.php

365 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace JFB_Modules\Gateways;
5
6 use Jet_Form_Builder\Actions\Action_Handler;
7 use Jet_Form_Builder\Actions\Events\Default_Process\Default_Process_Event;
8 use Jet_Form_Builder\Actions\Events\Gateway_Failed\Gateway_Failed_Event;
9 use Jet_Form_Builder\Actions\Events\Gateway_Success\Gateway_Success_Event;
10 use Jet_Form_Builder\Actions\Events_List;
11 use Jet_Form_Builder\Classes\Tools;
12 use Jet_Form_Builder\Exceptions\Action_Exception;
13 use Jet_Form_Builder\Exceptions\Gateway_Exception;
14 use JFB_Modules\Gateways\Module as GM;
15
16 // If this file is called directly, abort.
17 if ( ! defined( 'WPINC' ) ) {
18 die;
19 }
20
21 abstract class Legacy_Base_Gateway {
22
23 const GATEWAY_META_KEY = '_jet_gateway_data';
24
25 /**
26 * @var string|int
27 *
28 * @deprecated 2.0.0
29 */
30 protected $payment_id;
31 protected $payment_token;
32 protected $data;
33 protected $order_id;
34 protected $gateways_meta;
35 protected $price_field;
36 protected $price;
37 protected $options;
38
39 /**
40 * @deprecated 2.0.0
41 */
42 protected function set_payment() {
43 $this->payment_instance = $this->retrieve_payment_instance();
44 }
45
46 /**
47 * @deprecated 2.0.0
48 */
49 protected function set_gateway_data_on_result() {
50 // TODO: Implement set_gateway_data_on_result() method.
51 }
52
53 /**
54 * Process status notification and enqueue message
55 *
56 * @param string $type [description]
57 *
58 * @return void [type] [description]
59 * @throws Action_Exception
60 * @deprecated 2.0.0
61 */
62 public function process_status( $type = 'success' ) {
63 $id = $this->data['form_id'] ?? 0;
64 jet_fb_action_handler()->add_request( $this->data['form_data'] );
65
66 switch ( $type ) {
67 case 'success':
68 jet_fb_events()->execute( Gateway_Success_Event::class, $id );
69 break;
70 case 'failed':
71 default:
72 jet_fb_events()->execute( Gateway_Failed_Event::class, $id );
73 break;
74 }
75 }
76
77 /**
78 * Store payment status into order and show success/failed message
79 *
80 * @return void [description]
81 * @throws Gateway_Exception
82 * @deprecated 2.0.0
83 */
84 public function on_success_payment() {
85 $this->set_current_gateway_options();
86 $this->set_gateway_data_on_result();
87
88 $this->try_do_actions();
89
90 /**
91 * By default save 'data' property to payment/subscription post meta
92 */
93 $this->save_gateway_before_send_response();
94
95 $this->send_response(
96 array(
97 'status' => $this->get_result_message( $this->data['status'] ),
98 )
99 );
100 }
101
102 /**
103 * @deprecated 2.0.0
104 */
105 public function save_gateway_before_send_response() {
106 $this->data['date'] = date_i18n( 'F j, Y, H:i' );
107 $this->data['gateway'] = $this->get_name();
108
109 /**
110 * JSON_UNESCAPED_UNICODE - for valid encode cyrillic
111 */
112 $json = wp_json_encode( $this->data, JSON_UNESCAPED_UNICODE );
113
114 update_post_meta( $this->payment_id, static::GATEWAY_META_KEY, $json );
115 }
116
117 /**
118 * @return $this
119 * @deprecated 2.0.0
120 */
121 public function set_gateway_from_post_meta() {
122 $row_data = $this->get_form_data();
123
124 $this->payment_id = $row_data['post_id'];
125 $this->data = Tools::decode_unserializable( $row_data['meta_value'] );
126
127 return $this;
128 }
129
130 /**
131 * @return false
132 * @deprecated 2.0.0
133 */
134 // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
135 protected function query_order_token( $order_id, $form_id ) {
136 return false;
137 }
138
139 /**
140 * @throws Gateway_Exception
141 * @deprecated 2.0.0
142 */
143 public function set_order_token() {
144 $this->order_token = $this->query_order_token( $this->order_id, jet_fb_handler()->form_id );
145
146 if ( ! $this->order_token ) {
147 throw new Gateway_Exception( 'Invalid token', esc_html( $this->order_token ) );
148 }
149 }
150
151 public function get_order_token() {
152 return $this->order_token;
153 }
154
155
156 /**
157 * @return int
158 * @deprecated 2.0.0
159 */
160 public function get_initialize_action_id() {
161 return (int) $this->gateway( 'action_order', 0 );
162 }
163
164 /**
165 * @return int|mixed
166 * @deprecated 2.0.0
167 */
168 public function get_insert_post_action_id() {
169 return jet_fb_action_handler()->get_inserted_post_id( $this->get_initialize_action_id() );
170 }
171
172 /**
173 * @throws Gateway_Exception
174 * @deprecated 2.0.0
175 */
176 public function set_order_id() {
177 if ( ! $this->get_insert_post_action_id() ) {
178 throw new Gateway_Exception( 'There is not inserted_post_id' );
179 }
180
181 $this->order_id = $this->get_insert_post_action_id();
182 }
183
184 /**
185 * @return mixed
186 * @deprecated 1.5.0
187 */
188 public function get_order_id() {
189 return $this->order_id;
190 }
191
192
193 /**
194 * @throws Gateway_Exception
195 */
196 public function set_price_field() {
197 $this->price_field = sanitize_key( $this->gateway( 'price_field', '' ) );
198
199 $this->price_field = apply_filters(
200 'jet-form-builder/gateways/price-field',
201 $this->price_field,
202 jet_fb_action_handler()
203 );
204
205 if ( ! $this->price_field ) {
206 throw new Gateway_Exception( 'Invalid price field' );
207 }
208 }
209
210 /**
211 * @throws Gateway_Exception
212 */
213 public function set_price_from_filed() {
214 $this->price = $this->get_price_from_request();
215
216 if ( ! $this->price ) {
217 throw new Gateway_Exception( 'Empty price field' );
218 }
219 }
220
221 public function get_price_var() {
222 return $this->price;
223 }
224
225 /**
226 * @throws Gateway_Exception
227 */
228 public function set_gateway_data() {
229 if ( ! $this->gateways_meta ) {
230 /** for backward compatibility */
231 $this->gateways_meta = GM::instance()->gateways();
232 }
233
234 $this->set_order_id();
235 $this->set_price_field();
236 $this->set_price_from_filed();
237 $this->set_current_gateway_options();
238 $this->set_order_token();
239
240 /** for backward compatibility */
241 return true;
242 }
243
244 protected function get_price_from_request() {
245 $number = jet_fb_context()->get_value( $this->price_field );
246
247 return $this->get_price( is_numeric( $number ) ? $number : 0 );
248 }
249
250 protected function get_price( $price ) {
251 return (float) $price;
252 }
253
254 /**
255 * @return array|object|void|null
256 * @deprecated 2.0.0
257 */
258 protected function get_form_data() {
259 return $this->get_form_by_payment_token( $this->payment_token );
260 }
261
262 /**
263 * Apply macros in string
264 *
265 * @param null $content
266 *
267 * @return string [description]
268 */
269 public function apply_macros( $content = null ) {
270 return preg_replace_callback(
271 '/%(.*?)%/',
272 function ( $matches ) {
273 switch ( $matches[1] ) {
274 case 'gateway_amount':
275 if ( empty( $this->data['amount'] ) ) {
276 return 0;
277 }
278
279 return $this->data['amount']['value'] ?? 0 . ' ' . $this->data['amount']['currency_code'] ?? '';
280
281 case 'gateway_status':
282 return $this->data['status'] ?? '';
283
284 default:
285 return $this->data['form_data'][ $matches[1] ] ?? '';
286 }
287 },
288 $content
289 );
290 }
291
292 /**
293 * Returns form data by payment ID
294 *
295 * @param null $payment
296 *
297 * @return array|object|void|null [description]
298 * @deprecated since 2.0.0
299 */
300 public function get_form_by_payment_token( $payment = null ) {
301
302 if ( ! $payment ) {
303 return;
304 }
305
306 global $wpdb;
307 $sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key = '" . self::GATEWAY_META_KEY . "' AND meta_value LIKE '%$payment%';";
308
309 // phpcs:ignore WordPress.DB
310 return $wpdb->get_row( $sql, ARRAY_A );
311 }
312
313 /**
314 * @param Action_Handler $action_handler
315 *
316 * @return void
317 * @throws Gateway_Exception
318 * @deprecated 2.0.0
319 */
320 public function after_actions( Action_Handler $action_handler ) {
321 }
322
323 /**
324 * Prevent unnecessary notifications processing before form is send.
325 *
326 * @return void [description]
327 * @deprecated 2.0.0
328 */
329 public function before_actions() {
330 $this->set_form_meta( GM::instance()->gateways() );
331 $default_actions = jet_fb_action_handler()->get_all();
332
333 $action_order = (int) $this->gateway( 'action_order' );
334
335 foreach ( $default_actions as $index => $action ) {
336 if ( 'insert_post' !== $action->get_id() || $action_order !== $action->_id ) {
337 continue;
338 }
339 /** @var Events_List|false $list */
340 $list = $action->get_events();
341
342 if ( ! $list ) {
343 $list = new Events_List();
344 }
345
346 $list->push( Default_Process_Event::class );
347 break;
348 }
349 }
350
351 /**
352 * For backward compatibility with JetAppointment & JetBooking
353 *
354 * @param array $request
355 */
356 public function set_form_data( array $request ) {
357 if ( ! is_array( $this->data ) ) {
358 $this->data = array();
359 }
360 $this->data['form_data'] = $request;
361 }
362
363
364 }
365