| 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 |
if ( ! $this->is_price_field_protection_enabled() ) { |
| 246 |
$number = jet_fb_context()->get_value( $this->price_field ); |
| 247 |
|
| 248 |
return $this->get_price( is_numeric( $number ) ? $number : 0 ); |
| 249 |
} |
| 250 |
|
| 251 |
$number = ( new Trusted_Price_Resolver( (int) jet_fb_handler()->form_id ) ) |
| 252 |
->resolve( $this->price_field ); |
| 253 |
|
| 254 |
return $this->get_price( $number ); |
| 255 |
} |
| 256 |
|
| 257 |
public function is_price_field_protection_enabled(): bool { |
| 258 |
return filter_var( |
| 259 |
$this->gateway( |
| 260 |
'protect_price_field', |
| 261 |
$this->current_scenario( 'protect_price_field', false ) |
| 262 |
), |
| 263 |
FILTER_VALIDATE_BOOLEAN |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
protected function get_price( $price ) { |
| 268 |
return (float) $price; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* @return array|object|void|null |
| 273 |
* @deprecated 2.0.0 |
| 274 |
*/ |
| 275 |
protected function get_form_data() { |
| 276 |
return $this->get_form_by_payment_token( $this->payment_token ); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Apply macros in string |
| 281 |
* |
| 282 |
* @param null $content |
| 283 |
* |
| 284 |
* @return string [description] |
| 285 |
*/ |
| 286 |
public function apply_macros( $content = null ) { |
| 287 |
return preg_replace_callback( |
| 288 |
'/%(.*?)%/', |
| 289 |
function ( $matches ) { |
| 290 |
switch ( $matches[1] ) { |
| 291 |
case 'gateway_amount': |
| 292 |
if ( empty( $this->data['amount'] ) ) { |
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
return $this->data['amount']['value'] ?? 0 . ' ' . $this->data['amount']['currency_code'] ?? ''; |
| 297 |
|
| 298 |
case 'gateway_status': |
| 299 |
return $this->data['status'] ?? ''; |
| 300 |
|
| 301 |
default: |
| 302 |
return $this->data['form_data'][ $matches[1] ] ?? ''; |
| 303 |
} |
| 304 |
}, |
| 305 |
$content |
| 306 |
); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Returns form data by payment ID |
| 311 |
* |
| 312 |
* @param null $payment |
| 313 |
* |
| 314 |
* @return array|object|void|null [description] |
| 315 |
* @deprecated since 2.0.0 |
| 316 |
*/ |
| 317 |
public function get_form_by_payment_token( $payment = null ) { |
| 318 |
|
| 319 |
if ( ! $payment ) { |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
global $wpdb; |
| 324 |
$sql = $wpdb->prepare( |
| 325 |
"SELECT * FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value LIKE %s", |
| 326 |
self::GATEWAY_META_KEY, |
| 327 |
'%' . $wpdb->esc_like( $payment ) . '%' |
| 328 |
); |
| 329 |
|
| 330 |
// phpcs:ignore WordPress.DB |
| 331 |
return $wpdb->get_row( $sql, ARRAY_A ); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* @param Action_Handler $action_handler |
| 336 |
* |
| 337 |
* @return void |
| 338 |
* @throws Gateway_Exception |
| 339 |
* @deprecated 2.0.0 |
| 340 |
*/ |
| 341 |
public function after_actions( Action_Handler $action_handler ) { |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Prevent unnecessary notifications processing before form is send. |
| 346 |
* |
| 347 |
* @return void [description] |
| 348 |
* @deprecated 2.0.0 |
| 349 |
*/ |
| 350 |
public function before_actions() { |
| 351 |
$this->set_form_meta( GM::instance()->gateways() ); |
| 352 |
$default_actions = jet_fb_action_handler()->get_all(); |
| 353 |
|
| 354 |
$action_order = (int) $this->gateway( 'action_order' ); |
| 355 |
|
| 356 |
foreach ( $default_actions as $index => $action ) { |
| 357 |
if ( 'insert_post' !== $action->get_id() || $action_order !== $action->_id ) { |
| 358 |
continue; |
| 359 |
} |
| 360 |
/** @var Events_List|false $list */ |
| 361 |
$list = $action->get_events(); |
| 362 |
|
| 363 |
if ( ! $list ) { |
| 364 |
$list = new Events_List(); |
| 365 |
} |
| 366 |
|
| 367 |
$list->push( Default_Process_Event::class ); |
| 368 |
break; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* For backward compatibility with JetAppointment & JetBooking |
| 374 |
* |
| 375 |
* @param array $request |
| 376 |
*/ |
| 377 |
public function set_form_data( array $request ) { |
| 378 |
if ( ! is_array( $this->data ) ) { |
| 379 |
$this->data = array(); |
| 380 |
} |
| 381 |
$this->data['form_data'] = $request; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
} |
| 386 |
|