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