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