PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.1
JetFormBuilder — Dynamic Blocks Form Builder v3.0.1
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 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 / scenarios-abstract / scenario-logic-base.php
jetformbuilder / includes / gateways / scenarios-abstract Last commit date
scenario-logic-base.php 3 years ago scenario-logic-repository.php 3 years ago scenario-view-base.php 3 years ago scenarios-manager-abstract.php 3 years ago scenarios-view-repository.php 3 years ago
scenario-logic-base.php
310 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Gateways\Scenarios_Abstract;
5
6 use Jet_Form_Builder\Actions\Events\Gateway_Failed\Gateway_Failed_Event;
7 use Jet_Form_Builder\Actions\Events\Gateway_Success\Gateway_Success_Event;
8 use Jet_Form_Builder\Actions\Methods\Form_Record\Query_Views\Record_Fields_View;
9 use Jet_Form_Builder\Actions\Methods\Form_Record\Tools;
10 use Jet_Form_Builder\Actions\Types\Redirect_To_Page;
11 use Jet_Form_Builder\Actions\Types\Save_Record;
12 use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception;
13 use Jet_Form_Builder\Exceptions\Action_Exception;
14 use Jet_Form_Builder\Exceptions\Gateway_Exception;
15 use Jet_Form_Builder\Exceptions\Repository_Exception;
16 use Jet_Form_Builder\Form_Messages\Manager;
17 use Jet_Form_Builder\Gateways\Gateway_Manager;
18 use Jet_Form_Builder\Gateways\Scenario_Item;
19 use Jet_Form_Builder\Gateways\Paypal\Scenarios_Manager;
20
21 abstract class Scenario_Logic_Base implements Scenario_Item {
22
23 protected $queried_token;
24 protected $queried_row = array();
25 protected $api_response;
26
27 abstract public function after_actions();
28
29 /**
30 * @throws Gateway_Exception
31 */
32 abstract public function process_after();
33
34 abstract protected function query_token();
35
36 abstract protected function query_scenario_row();
37
38 abstract public function get_failed_statuses();
39
40 public static function rep_item_id() {
41 return static::scenario_id();
42 }
43
44 public function on_catch() {
45 // complete payment/subscription
46 try {
47 $this->process_after();
48 $status = $this->get_process_status();
49
50 } catch ( Gateway_Exception $exception ) {
51 $status = 'failed';
52 }
53
54 // run actions
55 try {
56 $action_error = false;
57 $this->process_status( $status );
58
59 } catch ( Action_Exception $exception ) {
60 $action_error = $exception->getMessage();
61 }
62 // reset current action id, for save results
63 jet_fb_action_handler()->set_current_action( false );
64
65 do_action( 'jet-form-builder/gateways/before-send', $status, $action_error, $this );
66
67 jet_fb_gateway_current()->send_response(
68 array(
69 'status' => $action_error ? $action_error : $this->get_result_message( $status ),
70 )
71 );
72 }
73
74 public function before_actions() {
75 }
76
77 /**
78 * @param $type
79 *
80 * @return string
81 */
82 public function get_referrer_url( string $type ) {
83 return add_query_arg(
84 array(
85 Gateway_Manager::PAYMENT_TYPE_PARAM => jet_fb_gateway_current()->get_id(),
86 Scenarios_Manager::QUERY_VAR => static::scenario_id(),
87 ),
88 jet_fb_handler()->refer
89 );
90 }
91
92 /**
93 * @return array
94 */
95 public function init_request(): array {
96 if ( ! empty( jet_fb_action_handler()->request_data ) ) {
97 return jet_fb_action_handler()->request_data;
98 }
99 $record = $this->get_scenario_row( 'record' );
100 $values = Tools::apply_record( $record );
101
102 // For backward compatibility with JetAppointment & JetBooking
103 jet_fb_gateway_current()->set_form_data( $values );
104
105 return $values;
106 }
107
108 public function init_actions() {
109 $form_id = (int) $this->get_scenario_row( 'form_id', 0 );
110
111 jet_fb_action_handler()->set_form_id( $form_id );
112 }
113
114 /**
115 * Process status notification and enqueue message
116 *
117 * @param string $type [description]
118 *
119 * @throws Action_Exception
120 */
121 public function process_status( $type = 'success' ) {
122 // save form request to Action_Handler & current gateway controller
123 $form_id = (int) $this->get_scenario_row( 'form_id', 0 );
124
125 switch ( $type ) {
126 case 'success':
127 jet_fb_events()->execute( Gateway_Success_Event::class, $form_id );
128 break;
129 case 'failed':
130 default:
131 jet_fb_events()->execute( Gateway_Failed_Event::class, $form_id );
132 break;
133 }
134 }
135
136 public function get_gateways_meta() {
137 $form_id = (int) $this->get_scenario_row( 'form_id', 0 );
138
139 return jet_form_builder()->post_type->get_gateways( $form_id );
140 }
141
142 public function scenario_row( $props = array() ) {
143 if ( ! empty( $props ) ) {
144 $this->set_scenario_row( $props );
145 }
146
147 return $this->get_scenario_row();
148 }
149
150 public function get_scenario_row( $key = '', $if_empty = false ) {
151 $this->maybe_query_scenario_row();
152
153 return $key ? ( $this->queried_row[ $key ] ?? $if_empty ) : $this->queried_row;
154 }
155
156 public function set_scenario_row( $props ): Scenario_Logic_Base {
157 $this->maybe_query_scenario_row();
158
159 $this->queried_row = array_merge( $this->queried_row, $props );
160
161 return $this;
162 }
163
164 protected function maybe_query_scenario_row(): Scenario_Logic_Base {
165 if ( empty( $this->queried_row ) ) {
166 $this->queried_row = $this->query_scenario_row();
167 }
168
169 return $this;
170 }
171
172 public function get_process_status() {
173 return $this->get_status_type( $this->get_scenario_row( 'status' ) );
174 }
175
176 public function get_queried_token() {
177 if ( ! $this->queried_token ) {
178 $this->queried_token = $this->query_token();
179 }
180
181 return $this->queried_token;
182 }
183
184 public function add_redirect( $links ) {
185 foreach ( $links as $link ) {
186 if ( empty( $link['rel'] ) || 'approve' !== $link['rel'] ) {
187 continue;
188 }
189
190 jet_fb_action_handler()->add_response( array( 'redirect' => $link['href'] ) );
191 break;
192 }
193 }
194
195 /**
196 * @param string $key
197 * @param string $if_empty
198 *
199 * @return mixed
200 */
201 public function get_setting( string $key, $if_empty = '' ) {
202 return jet_fb_gateway_current()->current_scenario( $key, $if_empty );
203 }
204
205 /**
206 * @return array|false|mixed
207 */
208 public function get_settings() {
209 return jet_fb_gateway_current()->current_scenario();
210 }
211
212 /**
213 * @return string
214 */
215 private function get_context_key() {
216 return jet_fb_gateway_current()->get_id() . '__' . static::scenario_id();
217 }
218
219 /**
220 * @param array $context
221 *
222 * @return $this
223 * @throws Repository_Exception
224 */
225 public function add_context( array $context ) {
226 jet_fb_action_handler()->add_context( $this->get_context_key(), $context );
227
228 return $this;
229 }
230
231 /**
232 * @param string $property
233 *
234 * @return array|false|mixed
235 * @throws Repository_Exception
236 */
237 public function get_context( string $property = '' ) {
238 return jet_fb_action_handler()->get_context( $this->get_context_key(), $property );
239 }
240
241 public function apply_macros( string $message ): string {
242 return preg_replace_callback(
243 '/%(.*?)%/',
244 function ( $matches ) {
245 switch ( $matches[1] ) {
246 case 'gateway_amount':
247 $amount = $this->get_scenario_row( 'amount_value', 0 );
248 if ( empty( $amount ) ) {
249 return 0;
250 }
251
252 return $amount . ' ' . $this->get_scenario_row( 'amount_code' );
253
254 case 'gateway_status':
255 return $this->get_scenario_row( 'status' );
256
257 default:
258 return jet_fb_action_handler()->request_data[ $matches[1] ] ?? '';
259 }
260 },
261 $message
262 );
263 }
264
265 /**
266 *
267 * @param $status
268 *
269 * @return string
270 */
271 public function get_result_message( $status ): string {
272 $gateway = jet_fb_gateway_current();
273 $message = $status;
274
275 switch ( $this->get_status_type( $status ) ) {
276 case 'failed':
277 $message = Manager::dynamic_error( $gateway->get_meta_message( 'failed' ) );
278 break;
279 case 'success':
280 $message = Manager::dynamic_success( $gateway->get_meta_message( 'success' ) );
281 break;
282 }
283 $message = stripcslashes( $message );
284
285 return $message;
286 }
287
288 /**
289 * @param $status
290 *
291 * @return string
292 */
293 public function get_meta_message( $status ): string {
294 $gateway = jet_fb_gateway_current();
295
296 return $gateway->get_meta_message( $this->get_status_type( $status ) );
297 }
298
299 public function get_status_type( $status ): string {
300 if ( in_array( (string) $status, array( 'success', 'failed' ), true ) ) {
301 return $status;
302 }
303
304 return ( ! $status || in_array( $status, $this->get_failed_statuses(), true ) )
305 ? 'failed'
306 : 'success';
307 }
308
309 }
310