PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.1.6
JetFormBuilder — Dynamic Blocks Form Builder v3.1.6
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
192 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'] ) && ! empty( $credits['secret'] ) ) {
101 return $credits;
102 }
103
104 return self::get_credentials();
105 }
106
107 /**
108 * Returns auth token for current client_id and secret combination
109 *
110 * @return mixed|void [description]
111 * @throws Gateway_Exception
112 */
113 public function get_current_token() {
114 $client_id = $this->current_gateway( 'client_id' );
115 $secret = $this->current_gateway( 'secret' );
116
117 return self::get_token_with_credits( $client_id, $secret );
118 }
119
120 /**
121 * @param $form_id
122 *
123 * @return mixed|string
124 * @throws Gateway_Exception
125 */
126 public static function get_token_by_form_id( $form_id ) {
127 if ( ! $form_id ) {
128 return self::get_token_global();
129 }
130
131 $paypal = self::get_credentials_by_form( $form_id );
132
133 return self::get_token_with_credits(
134 $paypal['client_id'] ?? '',
135 $paypal['secret'] ?? ''
136 );
137 }
138
139 /**
140 * @return mixed|string
141 * @throws Gateway_Exception
142 */
143 public static function get_token_global() {
144 $credits = self::get_credentials();
145
146 $secret = $credits['secret'] ?? false;
147 $client_id = $credits['client_id'] ?? false;
148
149 return self::get_token_with_credits( $client_id, $secret );
150 }
151
152 /**
153 * @param $client_id
154 * @param $secret
155 *
156 * @return mixed|string
157 * @throws Gateway_Exception
158 */
159 public static function get_token_with_credits( $client_id, $secret ) {
160 if ( ! $client_id || ! $secret ) {
161 throw new Gateway_Exception(
162 'Empty `client_id` or `secret_key`.',
163 esc_html( $client_id ),
164 esc_html( $secret )
165 );
166 }
167 $hash = 'jet_fb_pp_token_' . md5( $client_id . $secret );
168 $token = get_transient( $hash );
169
170 if ( $token ) {
171 return $token;
172 }
173
174 $request = ( new Get_Token() )
175 ->set_credentials( $client_id, $secret );
176
177 $response = $request->send_request();
178
179 if ( empty( $response['access_token'] ) ) {
180 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
181 throw new Gateway_Exception( esc_html( $response['error_description'] ), $response, $request->get_request_args() );
182 }
183
184 $token = $response['access_token'];
185
186 set_transient( $hash, $token, $response['expires_in'] * 0.9 );
187
188 return $token;
189 }
190
191 }
192