PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.2.3
JetFormBuilder — Dynamic Blocks Form Builder v3.2.3
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 / modules / gateways / paypal / controller.php
jetformbuilder / modules / gateways / paypal Last commit date
api-actions 2 years ago rest-endpoints 2 years ago scenarios-connectors 2 years ago scenarios-logic 2 years ago scenarios-views 2 years ago tab-handlers 2 years ago controller.php 2 years ago scenarios-logic-manager.php 2 years ago scenarios-manager.php 2 years ago scenarios-view-manager.php 2 years ago
controller.php
195 lines
1 <?php
2
3 namespace JFB_Modules\Gateways\Paypal;
4
5 use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager;
6 use Jet_Form_Builder\Classes\Tools;
7 use Jet_Form_Builder\Exceptions\Gateway_Exception;
8 use JFB_Modules\Gateways\Module;
9 use JFB_Modules\Gateways\Paypal\Api_Actions\Get_Token;
10
11 // If this file is called directly, abort.
12 if ( ! defined( 'WPINC' ) ) {
13 die;
14 }
15
16 class Controller extends \Jet_Form_Builder\Gateways\Base_Scenario_Gateway {
17
18 public function __construct() {
19 // install tab handlers for Settings page
20 Tab_Handler_Manager::instance()->install( new Tab_Handlers\Paypal_Handler() );
21 }
22
23 const ID = 'paypal';
24
25 public $data = false;
26 public $message = false;
27 public $redirect = false;
28
29 protected $token_query_name = 'token';
30
31 /**
32 * Returns current gateway ID
33 *
34 * @return [type] [description]
35 */
36 public function get_id() {
37 return self::ID;
38 }
39
40 /**
41 * Returns current gateway name
42 *
43 * @return [type] [description]
44 */
45 public function get_name() {
46 return _x( 'PayPal Checkout', 'Paypal gateways editor data', 'jet-form-builder' );
47 }
48
49 protected function options_list() {
50 return array(
51 'client_id' => array(
52 'label' => _x( 'Client ID', 'Paypal gateways editor data', 'jet-form-builder' ),
53 ),
54 'secret' => array(
55 'label' => _x( 'Secret Key', 'Paypal gateways editor data', 'jet-form-builder' ),
56 ),
57 'currency' => array(
58 'required' => false,
59 ),
60 'use_global' => array(
61 'label' => _x( 'Use Global Settings', 'Paypal gateways editor data', 'jet-form-builder' ),
62 'required' => false,
63 ),
64 'gateway_type' => array(
65 'label' => _x( 'Gateway Action', 'Paypal gateways editor data', 'jet-form-builder' ),
66 'default' => Scenarios_Logic\Pay_Now::scenario_id(),
67 ),
68 );
69 }
70
71 public function custom_labels(): array {
72 return array(
73 'scenario' => Scenarios_Manager::instance()->view()->get_editor_labels(),
74 );
75 }
76
77 public function additional_editor_data(): array {
78 return array_merge(
79 array(
80 'version' => 1,
81 'scenarios' => Tools::with_placeholder(
82 Scenarios_Manager::instance()->view()->get_items_list(),
83 __( 'Choose scenario...', 'jet-form-builder' )
84 ),
85 ),
86 Scenarios_Manager::instance()->view()->get_editor_data()
87 );
88 }
89
90 public static function get_credentials() {
91 return Module::instance()->get_global_settings( self::ID );
92 }
93
94 public static function get_credentials_by_form( $form_id ) {
95 if ( ! $form_id ) {
96 return self::get_credentials();
97 }
98 $credits = Module::instance()->get_form_gateways_by_id( $form_id )[ self::ID ] ?? array();
99
100 if ( ! empty( $credits['client_id'] ) &&
101 ! empty( $credits['secret'] ) &&
102 empty( $credits['use_global'] )
103 ) {
104 return $credits;
105 }
106
107 return self::get_credentials();
108 }
109
110 /**
111 * Returns auth token for current client_id and secret combination
112 *
113 * @return mixed|void [description]
114 * @throws Gateway_Exception
115 */
116 public function get_current_token() {
117 $client_id = $this->current_gateway( 'client_id' );
118 $secret = $this->current_gateway( 'secret' );
119
120 return self::get_token_with_credits( $client_id, $secret );
121 }
122
123 /**
124 * @param $form_id
125 *
126 * @return mixed|string
127 * @throws Gateway_Exception
128 */
129 public static function get_token_by_form_id( $form_id ) {
130 if ( ! $form_id ) {
131 return self::get_token_global();
132 }
133
134 $paypal = self::get_credentials_by_form( $form_id );
135
136 return self::get_token_with_credits(
137 $paypal['client_id'] ?? '',
138 $paypal['secret'] ?? ''
139 );
140 }
141
142 /**
143 * @return mixed|string
144 * @throws Gateway_Exception
145 */
146 public static function get_token_global() {
147 $credits = self::get_credentials();
148
149 $secret = $credits['secret'] ?? false;
150 $client_id = $credits['client_id'] ?? false;
151
152 return self::get_token_with_credits( $client_id, $secret );
153 }
154
155 /**
156 * @param $client_id
157 * @param $secret
158 *
159 * @return mixed|string
160 * @throws Gateway_Exception
161 */
162 public static function get_token_with_credits( $client_id, $secret ) {
163 if ( ! $client_id || ! $secret ) {
164 throw new Gateway_Exception(
165 'Empty `client_id` or `secret_key`.',
166 esc_html( $client_id ),
167 esc_html( $secret )
168 );
169 }
170 $hash = 'jet_fb_pp_token_' . md5( $client_id . $secret );
171 $token = get_transient( $hash );
172
173 if ( $token ) {
174 return $token;
175 }
176
177 $request = ( new Get_Token() )
178 ->set_credentials( $client_id, $secret );
179
180 $response = $request->send_request();
181
182 if ( empty( $response['access_token'] ) ) {
183 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
184 throw new Gateway_Exception( esc_html( $response['error_description'] ), $response, $request->get_request_args() );
185 }
186
187 $token = $response['access_token'];
188
189 set_transient( $hash, $token, $response['expires_in'] * 0.9 );
190
191 return $token;
192 }
193
194 }
195