PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.6
JetFormBuilder — Dynamic Blocks Form Builder v1.2.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 / includes / gateways / paypal / controller.php
jetformbuilder / includes / gateways / paypal Last commit date
controller.php 4 years ago
controller.php
319 lines
1 <?php
2
3 namespace Jet_Form_Builder\Gateways\Paypal;
4
5 use Jet_Form_Builder\Actions\Action_Handler;
6 use Jet_Form_Builder\Exceptions\Action_Exception;
7 use Jet_Form_Builder\Exceptions\Gateway_Exception;
8 use Jet_Form_Builder\Plugin;
9 use Jet_Form_Builder\Gateways\Base_Gateway;
10
11 class Controller extends Base_Gateway {
12
13 public $data = false;
14 public $message = false;
15 public $redirect = false;
16
17 protected $token_query_name = 'token';
18
19 /**
20 * Returns current gateway ID
21 *
22 * @return [type] [description]
23 */
24 public function get_id() {
25 return 'paypal';
26 }
27
28 /**
29 * Returns current gateway name
30 *
31 * @return [type] [description]
32 */
33 public function get_name() {
34 return __( 'PayPal Checkout', 'jet-form-builder' );
35 }
36
37 protected function options_list() {
38 return array(
39 'client_id' => array(
40 'label' => __( 'Client ID', 'jet-form-builder' )
41 ),
42 'secret' => array(
43 'label' => __( 'Secret Key', 'jet-form-builder' )
44 ),
45 'currency' => array(
46 'label' => __( 'Currency Code', 'jet-form-builder' )
47 ),
48 'use_global' => array(
49 'label' => __( 'Use Global Settings', 'jet-form-builder' )
50 )
51 );
52 }
53
54 public function failed_statuses() {
55 return array( 'VOIDED' );
56 }
57
58 protected function retrieve_gateway_meta() {
59 return Plugin::instance()->post_type->get_gateways( $this->data['form_id'] );
60 }
61
62 protected function retrieve_payment_instance() {
63 $meta = $this->gateways_meta[ $this->get_id() ];
64 $client_id = $meta['client_id'];
65 $secret = $meta['secret'];
66
67 $payment = $this->request(
68 'v2/checkout/orders/' . $this->payment_token . '/capture',
69 array(),
70 null,
71 $this->get_token( $client_id, $secret )
72 );
73
74 return json_decode( $payment, true );
75 }
76
77 protected function set_gateway_data_on_result() {
78 try {
79 $this->set_current_gateway_options();
80 $this->set_payment_status();
81 $this->set_payment_amount();
82 $this->set_payer();
83
84 } catch ( Gateway_Exception $exception ) {
85 return false;
86 }
87
88 return true;
89 }
90
91 private function set_payment_status() {
92 if ( empty( $this->payment_instance['status'] ) ) {
93 throw new Gateway_Exception( 'Empty payment status' );
94 }
95
96 $this->data['status'] = $this->payment_instance['status'];
97 }
98
99 private function set_payer() {
100 if ( ! empty( $this->payment_instance['payer'] ) ) {
101 $this->data['payer'] = array(
102 'first_name' => $this->payment_instance['payer']['name']['given_name'],
103 'last_name' => $this->payment_instance['payer']['name']['surname'],
104 'email' => $this->payment_instance['payer']['email_address'],
105 );
106 }
107 }
108
109 private function set_payment_amount() {
110 if ( ! empty( $this->payment_instance['purchase_units'][0]['payments']['captures'] ) ) {
111 $payment_unit = $this->payment_instance['purchase_units'][0]['payments']['captures'][0];
112 $this->data['amount'] = $payment_unit['amount'];
113 }
114 }
115
116
117 protected function query_order_token( $order_id, $form_id ) {
118 return $this->get_token(
119 $this->options['client_id'],
120 $this->options['secret']
121 );
122 }
123
124 /**
125 * Process gateway payment
126 *
127 * @param $action_handler
128 *
129 * @return void [type] [description]
130 */
131 public function after_actions( Action_Handler $action_handler ) {
132
133 if ( ! $this->set_gateway_data( $action_handler ) ) {
134 return;
135 }
136
137 $order = $this->request(
138 'v2/checkout/orders',
139 array(),
140 array(
141 'intent' => 'CAPTURE',
142 'application_context' => array(
143 'landing_page' => 'BILLING',
144 'user_action' => 'PAY_NOW',
145 'brand_name' => get_option( 'blogname' ),
146 'return_url' => $this->get_refer_url( self::SUCCESS_TYPE ),
147 'cancel_url' => $this->get_refer_url( self::FAILED_TYPE ),
148 ),
149 'purchase_units' => array(
150 array(
151 'custom_id' => $action_handler->form_id . '-' . $this->order_id,
152 'amount' => array(
153 'currency_code' => $this->options['currency'],
154 'value' => $this->price,
155 ),
156 ),
157 ),
158 )
159 );
160
161 if ( empty( $order ) || is_wp_error( $order ) ) {
162 return;
163 }
164
165 $order = json_decode( $order, true );
166
167 if ( ! $order ) {
168 return;
169 }
170
171 if ( empty( $order['id'] ) ) {
172 throw ( new Action_Exception( $order['message'], $order ) )->dynamic_error();
173 }
174
175 update_post_meta(
176 $this->order_id,
177 self::GATEWAY_META_KEY,
178 json_encode( array(
179 'payment_id' => $order['id'],
180 'order_id' => $this->order_id,
181 'form_id' => $action_handler->form_id,
182 'form_data' => $action_handler->request_data,
183 ), JSON_UNESCAPED_UNICODE )
184 );
185
186 $this->maybe_redirect_to_checkout( $order );
187 }
188
189 private function maybe_redirect_to_checkout( $order ) {
190 foreach ( $order['links'] as $link ) {
191 if ( ! empty( $link['rel'] ) && 'approve' === $link['rel'] ) {
192 $this->action_handler->add_response( array(
193 'redirect' => $link['href']
194 ) );
195 }
196 }
197 }
198
199 /**
200 * Return API url
201 *
202 * @param $endpoint
203 *
204 * @return string
205 */
206 public function api_url( $endpoint ) {
207
208 $sandbox = apply_filters( 'jet-form-builder/gateways/paypal/sandbox-mode', false );
209
210 if ( $sandbox ) {
211 $url = 'https://api-m.sandbox.paypal.com/';
212 } else {
213 $url = 'https://api-m.paypal.com/';
214 }
215
216 return esc_url( $url . $endpoint );
217 }
218
219 /**
220 * Returns auth token for current client_id and secret combination
221 *
222 * @param bool $client_id
223 * @param bool $secret
224 *
225 * @return mixed|void [description]
226 * @throws Action_Exception
227 */
228 public function get_token( $client_id = false, $secret = false ) {
229 $hash = md5( $client_id . $secret );
230 $token = get_transient( $hash );
231
232 if ( $token ) {
233 return $token;
234 }
235
236 if ( ! $client_id || ! $secret ) {
237 return;
238 }
239
240 $key = base64_encode( $client_id . ':' . $secret );
241
242 $response = $this->request(
243 'v1/oauth2/token',
244 array(
245 'Accept' => 'application/json',
246 'Accept-Language' => get_locale(),
247 'Authorization' => 'Basic ' . $key,
248 ),
249 'grant_type=client_credentials'
250 );
251
252 $response = json_decode( $response, true );
253
254 if ( empty( $response['access_token'] ) ) {
255 throw ( new Action_Exception( $response['error_description'] ) )->dynamic_error();
256 }
257
258 $token = $response['access_token'];
259
260 set_transient( $hash, $token, 3 * HOUR_IN_SECONDS );
261
262 return $token;
263
264 }
265
266 /**
267 * Make a request
268 *
269 * @param null $endpoint
270 * @param array $headers [description]
271 * @param array $body [description]
272 *
273 * @param bool $token
274 * @param string $method
275 *
276 * @return string [type] [description]
277 * @throws Action_Exception
278 */
279 public function request( $endpoint = null, $headers = array(), $body = array(), $token = false, $method = 'post' ) {
280
281 $url = $this->api_url( $endpoint );
282
283 if ( empty( $headers ) ) {
284
285 $headers = array(
286 'Content-Type' => 'application/json',
287 'Accept-Language' => get_locale(),
288 'Authorization' => 'Bearer ' . ( $token ? $token : $this->order_token ),
289 );
290 }
291
292 if ( is_array( $body ) ) {
293
294 if ( version_compare( phpversion(), '7.1', '>=' ) ) {
295 ini_set( 'precision', 17 );
296 ini_set( 'serialize_precision', -1 );
297 }
298
299 $body = json_encode( $body );
300 }
301
302 $args = array(
303 'timeout' => 45,
304 'headers' => $headers,
305 );
306
307 if ( 'post' === $method ) {
308 $args['method'] = 'POST';
309 $args['body'] = $body;
310 $response = wp_remote_post( $url, $args );
311 } else {
312 $response = wp_remote_get( $url, $args );
313 }
314
315 return wp_remote_retrieve_body( $response );
316 }
317
318 }
319