PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.4.3
JetFormBuilder — Dynamic Blocks Form Builder v1.4.3
3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / gateways / gateway-manager.php
jetformbuilder / includes / gateways Last commit date
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
220 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(
77 $action_handler,
78 $this->get_actions_before( $action_handler )
79 );
80 }
81 }
82
83 public function after_send_actions( $action_handler ) {
84 if ( empty( $this->gateways_form_data ) ) {
85 return;
86 }
87
88 $controller = $this->get_gateway_controller( $this->gateways_form_data['gateway'] );
89
90 if ( $controller ) {
91 $controller->after_actions( $action_handler );
92 }
93 }
94
95 public function add_data( $data ) {
96 $this->data = $data;
97 }
98
99 public function save_gateways_form_data( $data ) {
100 $id = $data['gateway'];
101
102 if ( isset( $data[ $id ]['use_global'] ) && $data[ $id ]['use_global'] ) {
103 $data[ $id ] = array_merge( $data[ $id ], Tab_Handler_Manager::instance()->tab( $id )->on_load() );
104 }
105
106 $this->gateways_form_data = $data;
107 }
108
109 /**
110 * Catch processed payment results
111 *
112 * @return void [description]
113 */
114 public function catch_payment_result() {
115 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
116 if ( ! isset( $_GET[ self::PAYMENT_TYPE_PARAM ] ) || ! Plugin::instance()->allow_gateways ) {
117 return;
118 }
119
120 add_action( 'parse_request', array( $this, 'on_has_gateway_request' ) );
121 }
122
123 public function on_has_gateway_request() {
124 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
125 $gateway_type = sanitize_key( wp_unslash( $_GET[ self::PAYMENT_TYPE_PARAM ] ?? '' ) );
126 $controller = $this->get_gateway_controller( $gateway_type );
127
128 if ( ! ( $controller instanceof Base_Gateway ) ) {
129 return;
130 }
131
132 $this->try_run_gateway_controller( $controller );
133 }
134
135 public function try_run_gateway_controller( Base_Gateway $controller ) {
136 try {
137 $controller->set_payment_token();
138 $controller->set_gateways_meta();
139 $controller->set_form_gateways_meta();
140 $controller->set_payment_instance();
141 $controller->on_success_payment();
142
143 } catch ( Gateway_Exception $exception ) {
144 return;
145 }
146 }
147
148
149 public function get_gateway_controller( $type = false ) {
150 if ( ! $type ) {
151 return false;
152 }
153
154 foreach ( $this->_gateways as $gateway ) {
155 if ( $gateway->get_id() === $type ) {
156 return $gateway;
157 }
158 }
159
160 return false;
161 }
162
163 /**
164 * Returns gatewyas config for current form
165 *
166 * @param [type] $post_id [description]
167 *
168 * @return [type] [description]
169 */
170 public function get_form_gateways_by_id( $form_id = null ) {
171
172 if ( ! $form_id ) {
173 $form_id = get_the_ID();
174 }
175 $default = array( 'gateway' => 'none' );
176
177 $meta = Plugin::instance()->post_type->get_gateways( $form_id );
178
179 return is_array( $meta ) ? $meta : $default;
180 }
181
182 public function gateways() {
183 return $this->gateways_form_data;
184 }
185
186 public function get_actions_before( Action_Handler $action_handler ) {
187 $withFilter = function ( $actions = array() ) use ( $action_handler ) {
188 return apply_filters(
189 'jet-form-builder/gateways/notifications-before',
190 $actions,
191 $action_handler->get_all()
192 );
193 };
194
195 if ( empty( $this->gateways() ) || empty( $this->gateways()['notifications_before'] ) ) {
196 return $withFilter();
197 }
198
199 $actions_ids = array_filter(
200 $this->gateways()['notifications_before'],
201 function ( $action ) {
202 return $action['active'];
203 }
204 );
205
206 return $withFilter( $actions_ids );
207 }
208
209 public function has_gateway( $form_id ) {
210 $gateways = $this->get_form_gateways_by_id( $form_id );
211
212 if ( empty( $gateways ) || empty( $gateways['gateway'] ) || 'none' === $gateways['gateway'] ) {
213 return false;
214 }
215
216 return true;
217 }
218
219 }
220