paypal
4 years ago
base-gateway.php
4 years ago
gateway-manager.php
4 years ago
gateways-editor-data.php
4 years ago
gateway-manager.php
234 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Gateways; |
| 4 | |
| 5 | use Jet_Form_Builder\Actions\Action_Handler; |
| 6 | use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 7 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 8 | use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 9 | use Jet_Form_Builder\Gateways\Paypal; |
| 10 | use Jet_Form_Builder\Plugin; |
| 11 | |
| 12 | /** |
| 13 | * @method static Gateway_Manager instance() |
| 14 | * |
| 15 | * Class Gateway_Manager |
| 16 | * @package Jet_Form_Builder\Gateways |
| 17 | */ |
| 18 | class Gateway_Manager { |
| 19 | |
| 20 | const BEFORE_ACTIONS_CALLABLE = 'before_send_actions'; |
| 21 | const AFTER_ACTIONS_CALLABLE = 'after_send_actions'; |
| 22 | |
| 23 | const PAYMENT_TYPE_PARAM = 'jet_form_gateway'; |
| 24 | |
| 25 | use Instance_Trait; |
| 26 | use Gateways_Editor_Data; |
| 27 | |
| 28 | private $_gateways = array(); |
| 29 | private $gateways_form_data = array(); |
| 30 | |
| 31 | public $message = null; |
| 32 | public $data = null; |
| 33 | |
| 34 | /** |
| 35 | * Register gateways components |
| 36 | */ |
| 37 | public function __construct() { |
| 38 | add_action( 'init', array( $this, 'register_gateways' ) ); |
| 39 | |
| 40 | $this->catch_payment_result(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns all registered gateways |
| 45 | * |
| 46 | * @return void [description] |
| 47 | */ |
| 48 | public function register_gateways() { |
| 49 | |
| 50 | $gateways = array( |
| 51 | new Paypal\Controller(), |
| 52 | ); |
| 53 | |
| 54 | foreach ( $gateways as $gateway ) { |
| 55 | $this->register_gateway( $gateway ); |
| 56 | } |
| 57 | |
| 58 | do_action( 'jet-form-builder/gateways/register', $this ); |
| 59 | } |
| 60 | |
| 61 | public function register_gateway( Base_Gateway $gateway ) { |
| 62 | $this->_gateways[] = $gateway; |
| 63 | } |
| 64 | |
| 65 | public function before_send_actions( $action_handler ) { |
| 66 | $gateways = $this->get_form_gateways_by_id( $action_handler->form_id ); |
| 67 | |
| 68 | if ( empty( $gateways ) || empty( $gateways['gateway'] ) || 'none' === $gateways['gateway'] ) { |
| 69 | return; |
| 70 | } |
| 71 | $this->save_gateways_form_data( $gateways ); |
| 72 | |
| 73 | $controller = $this->get_gateway_controller( $gateways['gateway'] ); |
| 74 | |
| 75 | if ( $controller ) { |
| 76 | $controller->before_actions( $action_handler, |
| 77 | $this->get_actions_before( $action_handler ) |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public function after_send_actions( $action_handler ) { |
| 83 | if ( empty( $this->gateways_form_data ) ) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | $controller = $this->get_gateway_controller( $this->gateways_form_data['gateway'] ); |
| 88 | |
| 89 | if ( $controller ) { |
| 90 | $controller->after_actions( $action_handler ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public function add_data( $data ) { |
| 95 | $this->data = $data; |
| 96 | } |
| 97 | |
| 98 | public function save_gateways_form_data( $data ) { |
| 99 | $id = $data['gateway']; |
| 100 | |
| 101 | if ( isset( $data[ $id ]['use_global'] ) && $data[ $id ]['use_global'] ) { |
| 102 | $data[ $id ] = array_merge( $data[ $id ], Tab_Handler_Manager::instance()->tab( $id )->on_load() ); |
| 103 | } |
| 104 | |
| 105 | $this->gateways_form_data = $data; |
| 106 | } |
| 107 | |
| 108 | public function add_message( $message ) { |
| 109 | |
| 110 | $this->message = $message; |
| 111 | |
| 112 | if ( ! $this->data || ! isset( $this->data['form_id'] ) ) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | $form_id = $this->data['form_id']; |
| 117 | |
| 118 | add_filter( 'jet-form-builder/pre-render/' . $form_id, function ( $res ) use ( $form_id ) { |
| 119 | echo $this->apply_macros( $this->message ); |
| 120 | |
| 121 | return true; |
| 122 | } ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Catch processed payment results |
| 127 | * |
| 128 | * @return void [description] |
| 129 | */ |
| 130 | public function catch_payment_result() { |
| 131 | if ( ! isset( $_GET[ self::PAYMENT_TYPE_PARAM ] ) || ! Plugin::instance()->allow_gateways ) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | add_action( 'parse_request', array( $this, 'on_has_gateway_request' ) ); |
| 136 | } |
| 137 | |
| 138 | public function on_has_gateway_request() { |
| 139 | $gateway_type = esc_attr( $_GET[ self::PAYMENT_TYPE_PARAM ] ); |
| 140 | $controller = $this->get_gateway_controller( $gateway_type ); |
| 141 | |
| 142 | if ( ! ( $controller instanceof Base_Gateway ) ) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | $this->try_run_gateway_controller( $controller ); |
| 147 | } |
| 148 | |
| 149 | public function try_run_gateway_controller( Base_Gateway $controller ) { |
| 150 | try { |
| 151 | $controller->set_payment_token(); |
| 152 | $controller->set_gateways_meta(); |
| 153 | $controller->set_form_gateways_meta(); |
| 154 | $controller->set_payment_instance(); |
| 155 | $controller->on_success_payment(); |
| 156 | |
| 157 | } catch ( Gateway_Exception $exception ) { |
| 158 | //do_action( 'qm/debug', var_export( [ $exception->getMessage(), $exception->getTraceAsString() ], true ) ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | public function get_gateway_controller( $type = false ) { |
| 164 | if ( ! $type ) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | foreach ( $this->_gateways as $gateway ) { |
| 169 | if ( $gateway->get_id() === $type ) { |
| 170 | return $gateway; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns gatewyas config for current form |
| 179 | * |
| 180 | * @param [type] $post_id [description] |
| 181 | * |
| 182 | * @return [type] [description] |
| 183 | */ |
| 184 | public function get_form_gateways_by_id( $form_id = null ) { |
| 185 | |
| 186 | if ( ! $form_id ) { |
| 187 | $form_id = get_the_ID(); |
| 188 | } |
| 189 | $default = array( 'gateway' => 'none' ); |
| 190 | |
| 191 | $meta = Plugin::instance()->post_type->get_gateways( $form_id ); |
| 192 | |
| 193 | return is_array( $meta ) ? $meta : $default; |
| 194 | } |
| 195 | |
| 196 | public function gateways() { |
| 197 | return $this->gateways_form_data; |
| 198 | } |
| 199 | |
| 200 | public function get_actions_before( Action_Handler $action_handler ) { |
| 201 | $withFilter = function ( $actions = array() ) use ( $action_handler ) { |
| 202 | return apply_filters( |
| 203 | 'jet-form-builder/gateways/notifications-before', |
| 204 | $actions, |
| 205 | $action_handler->get_all() |
| 206 | ); |
| 207 | }; |
| 208 | |
| 209 | if ( empty( $this->gateways() ) || empty( $this->gateways()['notifications_before'] ) ) { |
| 210 | return $withFilter(); |
| 211 | } |
| 212 | |
| 213 | $actions_ids = array_filter( |
| 214 | $this->gateways()['notifications_before'], |
| 215 | function ( $action ) { |
| 216 | return $action['active']; |
| 217 | } |
| 218 | ); |
| 219 | |
| 220 | return $withFilter( $actions_ids ); |
| 221 | } |
| 222 | |
| 223 | public function has_gateway( $form_id ) { |
| 224 | $gateways = $this->get_form_gateways_by_id( $form_id ); |
| 225 | |
| 226 | if ( empty( $gateways ) || empty( $gateways['gateway'] ) || 'none' === $gateways['gateway'] ) { |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | } |
| 234 |