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