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