paypal
5 years ago
base-gateway.php
5 years ago
gateway-manager.php
5 years ago
gateways-editor-data.php
5 years ago
paypal.php
5 years ago
base-gateway.php
417 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Gateways; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Actions\Action_Handler; |
| 8 | use Jet_Form_Builder\Classes\Tools; |
| 9 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 10 | use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 11 | use Jet_Form_Builder\Form_Messages\Manager; |
| 12 | use Jet_Form_Builder\Form_Response\Response; |
| 13 | use Jet_Form_Builder\Form_Response\Types\Reload_Response; |
| 14 | use Jet_Form_Builder\Gateways\Gateway_Manager as GM; |
| 15 | |
| 16 | abstract class Base_Gateway { |
| 17 | |
| 18 | const GATEWAY_META_KEY = '_jet_gateway_data'; |
| 19 | |
| 20 | const SUCCESS_TYPE = 'success'; |
| 21 | const FAILED_TYPE = 'cancel'; |
| 22 | |
| 23 | protected $payment_id; |
| 24 | protected $payment_token; |
| 25 | protected $data; |
| 26 | protected $action_handler; |
| 27 | protected $order_id; |
| 28 | protected $gateways_meta; |
| 29 | protected $price_field; |
| 30 | protected $price; |
| 31 | protected $options; |
| 32 | protected $redirect; |
| 33 | protected $order_token; |
| 34 | |
| 35 | protected $payment_instance = array(); |
| 36 | protected $token_query_name; |
| 37 | |
| 38 | protected $removed_query_args_on_payment = array( |
| 39 | GM::PAYMENT_TYPE_PARAM, |
| 40 | 'order_token', |
| 41 | 'status', |
| 42 | 'PayerID' |
| 43 | ); |
| 44 | |
| 45 | /** |
| 46 | * Returns current gateway ID |
| 47 | * |
| 48 | * @return [type] [description] |
| 49 | */ |
| 50 | abstract public function get_id(); |
| 51 | |
| 52 | /** |
| 53 | * Returns current gateway name |
| 54 | * |
| 55 | * @return [type] [description] |
| 56 | */ |
| 57 | abstract public function get_name(); |
| 58 | |
| 59 | abstract protected function options_list(); |
| 60 | |
| 61 | abstract public function after_actions( Action_Handler $action_handler ); |
| 62 | |
| 63 | abstract protected function set_gateway_data_on_result(); |
| 64 | |
| 65 | abstract protected function failed_statuses(); |
| 66 | |
| 67 | abstract protected function retrieve_payment_instance(); |
| 68 | |
| 69 | abstract protected function retrieve_gateway_meta(); |
| 70 | |
| 71 | public function set_payment_instance() { |
| 72 | $this->payment_instance = $this->retrieve_payment_instance(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Store payment status into order and show success/failed message |
| 77 | * @return [type] [description] |
| 78 | */ |
| 79 | public function on_success_payment() { |
| 80 | if ( ! $this->set_gateway_data_on_result() ) { |
| 81 | return; |
| 82 | } |
| 83 | $this->data['date'] = date_i18n( 'F j, Y, H:i' ); |
| 84 | |
| 85 | $this->data['gateway'] = $this->get_name(); |
| 86 | |
| 87 | update_post_meta( $this->payment_id, self::GATEWAY_META_KEY, json_encode( $this->data ) ); |
| 88 | |
| 89 | $this->try_do_actions(); |
| 90 | |
| 91 | $this->send_response( array( |
| 92 | 'status' => $this->get_status_on_payment( $this->data['status'] ), |
| 93 | ) ); |
| 94 | } |
| 95 | |
| 96 | public function get_status_on_payment( $status ) { |
| 97 | if ( in_array( $status, $this->failed_statuses() ) ) { |
| 98 | return Manager::dynamic_error( $this->get_meta_message( 'failed' ) ); |
| 99 | } |
| 100 | |
| 101 | return Manager::dynamic_success( $this->get_meta_message( 'success' ) ); |
| 102 | } |
| 103 | |
| 104 | public function get_meta_message( $type ) { |
| 105 | if ( isset( $this->gateways_meta['messages'] ) && isset( $this->gateways_meta['messages'][ $type ] ) ) { |
| 106 | return $this->apply_macros( $this->gateways_meta['messages'][ $type ] ); |
| 107 | } |
| 108 | |
| 109 | return Gateway_Manager::instance()->default_messages()[ $type ]; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Execute actions or something else when payment is success |
| 114 | * |
| 115 | * @return [type] [description] |
| 116 | */ |
| 117 | protected function try_do_actions() { |
| 118 | try { |
| 119 | if ( in_array( $this->data['status'], $this->failed_statuses() ) ) { |
| 120 | $this->process_status( 'failed' ); |
| 121 | } else { |
| 122 | $this->process_status( 'success' ); |
| 123 | } |
| 124 | } catch ( Action_Exception $exception ) { |
| 125 | $this->send_response( array( |
| 126 | 'status' => $exception->get_form_status(), |
| 127 | ) ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | private function send_response( $args = array() ) { |
| 132 | ( new Response( $this->get_response_manager() ) )->init( $args )->send(); |
| 133 | } |
| 134 | |
| 135 | private function get_response_manager() { |
| 136 | return new Reload_Response( array( |
| 137 | 'refer' => $this->data['form_data']['__refer'], |
| 138 | 'remove_args' => $this->removed_query_args_on_payment, |
| 139 | ) ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Process status notification and enqueue message |
| 144 | * |
| 145 | * @param string $type [description] |
| 146 | * |
| 147 | * @return void [type] [description] |
| 148 | * @throws Action_Exception |
| 149 | */ |
| 150 | public function process_status( $type = 'success' ) { |
| 151 | |
| 152 | $settings = $this->gateways_meta; |
| 153 | |
| 154 | $keep_these = isset( $settings[ 'notifications_' . $type ] ) ? $settings[ 'notifications_' . $type ] : array(); |
| 155 | |
| 156 | if ( ! empty( $keep_these ) ) { |
| 157 | $notifications = new Action_Handler( $this->data['form_id'] ); |
| 158 | $notifications->add_request( $this->data['form_data'] ); |
| 159 | |
| 160 | $notifications->unregister_action( 'redirect_to_page' ); |
| 161 | |
| 162 | $all = $notifications->get_all(); |
| 163 | |
| 164 | if ( empty( $all ) ) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | foreach ( $all as $index => $notification ) { |
| 169 | if ( ! array_key_exists( $index, $keep_these ) ) { |
| 170 | $notifications->unregister_action( $index ); |
| 171 | } |
| 172 | } |
| 173 | $notifications->run_actions(); |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | final public function set_payment_token() { |
| 179 | if ( empty( $this->token_query_name ) || empty( $_GET[ $this->token_query_name ] ) ) { |
| 180 | throw new Gateway_Exception( 'Empty payment token.' ); |
| 181 | } |
| 182 | |
| 183 | $this->payment_token = $this->get_payment_token(); |
| 184 | |
| 185 | if ( empty( $this->payment_token ) ) { |
| 186 | throw new Gateway_Exception( 'Invalid payment token.' ); |
| 187 | } |
| 188 | |
| 189 | return $this; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | final public function set_gateways_meta() { |
| 194 | $row_data = $this->get_form_data(); |
| 195 | |
| 196 | $this->payment_id = $row_data['post_id']; |
| 197 | $this->data = Tools::decode_unserializable( $row_data['meta_value'] ); |
| 198 | |
| 199 | return $this; |
| 200 | } |
| 201 | |
| 202 | final public function set_form_gateways_meta() { |
| 203 | $this->gateways_meta = $this->retrieve_gateway_meta(); |
| 204 | |
| 205 | return $this; |
| 206 | } |
| 207 | |
| 208 | final protected function set_order_token() { |
| 209 | $this->order_token = $this->query_order_token( $this->order_id, $this->action_handler->form_id ); |
| 210 | |
| 211 | if ( ! $this->order_token ) { |
| 212 | throw new Gateway_Exception( 'Invalid token', $this->order_token ); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | public function get_payment_token() { |
| 217 | return esc_attr( $_GET[ $this->token_query_name ] ); |
| 218 | } |
| 219 | |
| 220 | public function get_payment_id() { |
| 221 | return absint( explode( '-', $this->payment_token )[0] ); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Prevent unnecessary notifications processing before form is send. |
| 226 | * |
| 227 | * @param $action_handler |
| 228 | * |
| 229 | * @param $keep_these |
| 230 | * |
| 231 | * @return void [description] |
| 232 | */ |
| 233 | public function before_actions( $action_handler, $keep_these ) { |
| 234 | |
| 235 | if ( empty( $action_handler->get_all() ) ) { |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | foreach ( $action_handler->get_all() as $index => $action ) { |
| 240 | |
| 241 | if ( 'insert_post' === $action->get_id() ) { |
| 242 | continue; |
| 243 | } |
| 244 | |
| 245 | if ( 'redirect_to_page' === $action->get_id() ) { |
| 246 | $action_handler->unregister_action( $action->get_id() ); |
| 247 | $this->redirect = $action; |
| 248 | } |
| 249 | |
| 250 | if ( ! array_key_exists( $index, $keep_these ) ) { |
| 251 | $action_handler->unregister_action( $index ); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Returns form data by payment ID |
| 258 | * |
| 259 | * @param [type] $payment [description] |
| 260 | * |
| 261 | * @return [type] [description] |
| 262 | */ |
| 263 | public function get_form_by_payment_token( $payment = null ) { |
| 264 | |
| 265 | if ( ! $payment ) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | global $wpdb; |
| 270 | $sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key = '" . self::GATEWAY_META_KEY . "' AND meta_value LIKE '%$payment%';"; |
| 271 | |
| 272 | return $wpdb->get_row( $sql, ARRAY_A ); |
| 273 | } |
| 274 | |
| 275 | protected function set_order_id() { |
| 276 | $inserted_id = empty( $this->gateways_meta['action_order'] ) ? 0 : $this->gateways_meta['action_order']; |
| 277 | $inserted_id = $this->action_handler->get_inserted_post_id( $inserted_id ); |
| 278 | |
| 279 | if ( ! $inserted_id ) { |
| 280 | throw new Gateway_Exception( 'There is not inserted_post_id' ); |
| 281 | } |
| 282 | |
| 283 | $this->order_id = $inserted_id; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | protected function set_price_field() { |
| 288 | if ( isset( $this->gateways_meta['price_field'] ) && ! empty( $this->gateways_meta['price_field'] ) ) { |
| 289 | $this->price_field = esc_attr( $this->gateways_meta['price_field'] ); |
| 290 | } |
| 291 | |
| 292 | $this->price_field = apply_filters( 'jet-form-builder/gateways/price-field', $this->price_field, $this->action_handler ); |
| 293 | |
| 294 | if ( ! $this->price_field ) { |
| 295 | throw new Gateway_Exception( 'Invalid price field' ); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | protected function set_price_from_filed() { |
| 300 | if ( isset( $this->action_handler->request_data[ $this->price_field ] ) ) { |
| 301 | $this->price = $this->get_price( $this->action_handler->request_data[ $this->price_field ] ); |
| 302 | } |
| 303 | |
| 304 | if ( ! $this->price ) { |
| 305 | throw new Gateway_Exception( 'Empty price field' ); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | protected function set_current_gateway_options() { |
| 310 | $gateway = $this->gateways_meta[ $this->get_id() ]; |
| 311 | |
| 312 | foreach ( $this->options_list() as $name => $option ) { |
| 313 | if ( ! isset( $gateway[ $name ] ) || empty( $gateway[ $name ] ) ) { |
| 314 | throw new Gateway_Exception( 'Invalid gateway options' ); |
| 315 | } |
| 316 | $this->options[ $name ] = esc_attr( $gateway[ $name ] ); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | protected function set_gateway_data( Action_Handler $action_handler ) { |
| 321 | $this->gateways_meta = GM::instance()->gateways(); |
| 322 | $this->action_handler = $action_handler; |
| 323 | |
| 324 | try { |
| 325 | $this->set_order_id(); |
| 326 | $this->set_price_field(); |
| 327 | $this->set_price_from_filed(); |
| 328 | $this->set_current_gateway_options(); |
| 329 | $this->set_order_token(); |
| 330 | |
| 331 | } catch ( Gateway_Exception $exception ) { |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | protected function get_price( $price ) { |
| 339 | return absint( $price ); |
| 340 | } |
| 341 | |
| 342 | protected function get_refer_url( $type, array $additional_args = array() ) { |
| 343 | |
| 344 | $success_redirect = ! empty( $this->gateways_meta['use_success_redirect'] ) ? $this->gateways_meta['use_success_redirect'] : false; |
| 345 | $success_redirect = filter_var( $success_redirect, FILTER_VALIDATE_BOOLEAN ); |
| 346 | $refer = $this->action_handler->request_data['__refer']; |
| 347 | |
| 348 | if ( $success_redirect && $this->redirect && 'success' === $type ) { |
| 349 | $settings = $this->redirect->settings; |
| 350 | |
| 351 | $type = ! empty( $settings['redirect_type'] ) ? $settings['redirect_type'] : 'static_page'; |
| 352 | |
| 353 | if ( 'static_page' === $type ) { |
| 354 | $to_page = ! empty( $settings['redirect_page'] ) ? $settings['redirect_page'] : false; |
| 355 | $refer = ! empty( $to_page ) ? get_permalink( $to_page ) : false; |
| 356 | } else { |
| 357 | $refer = ! empty( $settings['redirect_url'] ) ? $settings['redirect_url'] : false; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return add_query_arg( |
| 362 | array_merge( |
| 363 | array( GM::PAYMENT_TYPE_PARAM => $this->get_id() ), |
| 364 | $additional_args |
| 365 | ), |
| 366 | $refer |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | abstract protected function query_order_token( $order_id, $form_id ); |
| 371 | |
| 372 | protected function get_form_data() { |
| 373 | return $this->get_form_by_payment_token( $this->payment_token ); |
| 374 | } |
| 375 | |
| 376 | public function options( $param = false ) { |
| 377 | $labels = array(); |
| 378 | |
| 379 | if ( ! $param ) { |
| 380 | return $this->options_list(); |
| 381 | } |
| 382 | foreach ( $this->options_list() as $name => $option ) { |
| 383 | $labels[ $name ] = $option[ $param ]; |
| 384 | } |
| 385 | |
| 386 | return $labels; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Apply macros in string |
| 391 | * |
| 392 | * @return [type] [description] |
| 393 | */ |
| 394 | public function apply_macros( $string = null ) { |
| 395 | |
| 396 | return preg_replace_callback( '/%(.*?)%/', function ( $matches ) { |
| 397 | switch ( $matches[1] ) { |
| 398 | case 'gateway_amount': |
| 399 | $amount = ! empty( $this->data['amount'] ) ? $this->data['amount'] : false; |
| 400 | |
| 401 | return ! empty( $amount ) ? $amount['value'] . ' ' . $amount['currency_code'] : 0; |
| 402 | |
| 403 | case 'gateway_status': |
| 404 | return ! empty( $this->data['status'] ) ? $this->data['status'] : ''; |
| 405 | |
| 406 | default: |
| 407 | $form_data = ! empty( $this->data['form_data'] ) ? $this->data['form_data'] : array(); |
| 408 | |
| 409 | return ! empty( $form_data[ $matches[1] ] ) ? $form_data[ $matches[1] ] : ''; |
| 410 | } |
| 411 | |
| 412 | }, $string ); |
| 413 | |
| 414 | } |
| 415 | |
| 416 | |
| 417 | } |