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