PluginProbe
Booking Package / trunk
Booking Package vtrunk
1.7.28 1.7.27 1.7.26 1.7.24 1.7.25 1.7.23 1.7.22 1.7.21 1.7.20 1.7.19 1.7.18 1.7.17 1.7.16 1.7.15 1.7.14 1.7.13 1.7.12 1.7.11 1.7.10 trunk 1.5.30 1.5.31 1.5.32 1.5.33 1.5.35 All 183 releases
booking-package / lib / CreditCard.php

CreditCard.php in Booking Package trunk, at lib/CreditCard.php

1,251 lines 46.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if(!defined('ABSPATH')){
3 exit;
4 }
5
6 class booking_package_CreditCard {
7
8 public $response = array();
9
10 public $pluginName = null;
11
12 public $lang = null;
13
14 public $prefix = null;
15
16 public $request_timeout = 30;
17
18 public function __construct($pluginName, $prefix){
19
20 $this->pluginName = $pluginName;
21 $this->prefix = $prefix;
22
23 }
24
25 public function createCustomer($payId, $public_key, $secret, $token, $calendarAccount, $subscription, $user, $payment_live, $payment_active){
26
27 $response = array("customer" => array(), "subscription" => array());
28 if($payId == "stripe"){
29
30 /*
31 $createCustomer = true;
32 if(isset($user['subscription_list']['customer_id_for_stripe'])){
33
34 $customer = $this->getCustomerForStripe($user['subscription_list']['customer_id_for_stripe'], $secret);
35 if(is_array($customer)){
36 #var_dump($customer);
37 $createCustomer = false;
38
39 }
40
41 }
42
43 if($createCustomer === true){
44
45 $response = $this->createCustomerForStripe($payId, $public_key, $secret, $token, $calendarAccount, $subscription, $user);
46
47 }else{
48
49 $response['subscription'] = $this->createSubscriptionsForStripe($user['subscription_list']['customer_id_for_stripe'], $secret, $subscription, $user);
50
51 }
52 **/
53
54 $response = $this->createCustomerForStripe($payId, $public_key, $secret, $token, $calendarAccount, $subscription, $user);
55
56 }
57
58 return $response;
59
60 }
61
62 public function createCustomerForStripe($payId, $public_key, $secret, $token, $calendarAccount, $subscription, $user){
63
64 $params = array('source' => $token, 'email' => $user['user_email'], 'description' => 'User: '.$user['user_login'].' Calendar: '.$calendarAccount['name']);
65 $response = null;
66
67 $args = array(
68 'method' => 'POST',
69 'timeout' => $this->request_timeout,
70 'body' => $params,
71 'headers' => array(
72 'Authorization' => 'Basic ' . base64_encode($secret . ':')
73 )
74 );
75 $response = wp_remote_request("https://api.stripe.com/v1/customers", $args);
76 $httpCode = wp_remote_retrieve_response_code($response);
77 $customer = json_decode(wp_remote_retrieve_body($response), true);
78
79 /**
80 $ch = curl_init();
81 curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/customers");
82 curl_setopt($ch, CURLOPT_USERPWD, $secret.":");
83 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
84 curl_setopt($ch, CURLOPT_POST, 1);
85
86 ob_start();
87 $response = curl_exec($ch);
88 $response = ob_get_contents();
89 ob_end_clean();
90 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
91 curl_close ($ch);
92 $customer = json_decode($response, true);
93 **/
94
95 if ($httpCode < 400) {
96
97 $subscription = $this->createSubscriptionsForStripe($customer['id'], $secret, $subscription, $user);
98 $stripe = array("customer" => $customer, "subscription" => $subscription);
99 return $stripe;
100
101 } else {
102
103 $message = $this->httpCodeError($httpCode, $payId);
104 return $message;
105
106 }
107
108 }
109
110 public function getCustomerForStripe($id, $secret){
111
112 $args = array(
113 'method' => 'GET',
114 'timeout' => $this->request_timeout,
115 'headers' => array(
116 'Authorization' => 'Basic ' . base64_encode($secret . ':')
117 )
118 );
119 $response = wp_remote_request("https://api.stripe.com/v1/customers/" . $id, $args);
120 $httpCode = wp_remote_retrieve_response_code($response);
121 $customer = json_decode(wp_remote_retrieve_body($response), true);
122
123 /**
124 $ch = curl_init();
125 curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/customers/".$id);
126 curl_setopt($ch, CURLOPT_USERPWD, $secret.":");
127 #curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
128 curl_setopt($ch, CURLOPT_POST, 0);
129
130 ob_start();
131 $response = curl_exec($ch);
132 $response = ob_get_contents();
133 ob_end_clean();
134 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
135 curl_close ($ch);
136 $customer = json_decode($response, true);
137 **/
138
139 if ($httpCode < 400) {
140
141 #var_dump($customer);
142 return $customer;
143
144 } else {
145
146 return false;
147
148 }
149
150 }
151
152 public function createSubscriptionsForStripe($id, $secret, $subscription, $user){
153
154 $params = array("customer" => $id);
155 $plans = $subscription['plans'];
156 for($i = 0; $i < count($plans); $i++){
157
158 $params["items[".$i."][plan]"] = $plans[$i]["id"];
159
160 }
161 #var_dump($params);
162
163 $args = array(
164 'method' => 'POST',
165 'timeout' => $this->request_timeout,
166 'body' => $params,
167 'headers' => array(
168 'Authorization' => 'Basic ' . base64_encode($secret . ':')
169 )
170 );
171 $response = wp_remote_request("https://api.stripe.com/v1/subscriptions", $args);
172 $httpCode = wp_remote_retrieve_response_code($response);
173 $subscription = json_decode(wp_remote_retrieve_body($response), true);
174
175 /**
176 $ch = curl_init();
177 curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/subscriptions");
178 curl_setopt($ch, CURLOPT_USERPWD, $secret.":");
179 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
180 curl_setopt($ch, CURLOPT_POST, 1);
181
182 ob_start();
183 $response = curl_exec($ch);
184 $response = ob_get_contents();
185 ob_end_clean();
186 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
187 curl_close ($ch);
188 $subscription = json_decode($response, true);
189 **/
190
191 if ($httpCode < 400) {
192
193 return $subscription;
194
195 } else {
196
197 $message = $this->httpCodeError($httpCode, $payId);
198 return $message;
199
200 }
201
202 }
203
204 public function update_subscription($secret, $subscription){
205
206 if ($subscription['payType'] == 'stripe') {
207
208 $args = array(
209 'method' => 'GET',
210 'timeout' => $this->request_timeout,
211 'headers' => array(
212 'Authorization' => 'Basic ' . base64_encode($secret . ':')
213 )
214 );
215 $response = wp_remote_request("https://api.stripe.com/v1/subscriptions/" . $subscription['subscription_id_for_stripe'], $args);
216 $httpCode = wp_remote_retrieve_response_code($response);
217 $subscription = json_decode(wp_remote_retrieve_body($response), true);
218
219 /**
220 $ch = curl_init();
221 curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/subscriptions/".$subscription['subscription_id_for_stripe']);
222 curl_setopt($ch, CURLOPT_USERPWD, $secret.":");
223 curl_setopt($ch, CURLOPT_POST, 0);
224
225 ob_start();
226 $response = curl_exec($ch);
227 $response = ob_get_contents();
228 ob_end_clean();
229 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
230 curl_close ($ch);
231 $subscription = json_decode($response, true);
232 **/
233
234 if ($httpCode < 400) {
235
236 return $subscription;
237
238 } else {
239
240 $message = $this->httpCodeError($httpCode, $payId);
241 return $message;
242
243 }
244
245 }
246
247 }
248
249 public function deleteSubscription($subscription, $secret){
250
251 $response = array("status" => 0, "reload" => 1);
252 if($subscription['payType'] == "stripe"){
253
254 $args = array(
255 'method' => 'DELETE',
256 'timeout' => $this->request_timeout,
257 'headers' => array(
258 'Authorization' => 'Basic ' . base64_encode($secret . ':')
259 )
260 );
261 $response = wp_remote_request("https://api.stripe.com/v1/subscriptions/" . $subscription['subscription_id_for_stripe'], $args);
262 $httpCode = wp_remote_retrieve_response_code($response);
263 $delete = json_decode(wp_remote_retrieve_body($response), true);
264
265 /**
266 $ch = curl_init();
267 curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/subscriptions/".$subscription['subscription_id_for_stripe']);
268 curl_setopt($ch, CURLOPT_USERPWD, $secret.":");
269 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
270
271 ob_start();
272 $response = curl_exec($ch);
273 $response = ob_get_contents();
274 ob_end_clean();
275 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
276 curl_close ($ch);
277 $delete = json_decode($response, true);
278 **/
279
280 if ($httpCode < 400) {
281
282 if ($delete['status'] == 'canceled') {
283
284 $args = array(
285 'method' => 'DELETE',
286 'timeout' => $this->request_timeout,
287 'headers' => array(
288 'Authorization' => 'Basic ' . base64_encode($secret . ':')
289 )
290 );
291 $response = wp_remote_request("https://api.stripe.com/v1/customers/" . $subscription['customer_id_for_stripe'], $args);
292 $httpCode = wp_remote_retrieve_response_code($response);
293 $delete = json_decode(wp_remote_retrieve_body($response), true);
294
295 /**
296 $ch = curl_init();
297 curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/customers/".$subscription['customer_id_for_stripe']);
298 curl_setopt($ch, CURLOPT_USERPWD, $secret.":");
299 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
300
301 ob_start();
302 $response = curl_exec($ch);
303 $response = ob_get_contents();
304 ob_end_clean();
305 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
306 curl_close ($ch);
307 $delete = json_decode($response, true);
308 **/
309
310 if ($httpCode < 400) {
311
312 return $delete;
313
314 } else {
315
316 $message = $this->httpCodeError($httpCode, $payId);
317 return $message;
318
319 }
320
321 }
322
323 } else {
324
325 $message = $this->httpCodeError($httpCode, $payId);
326 return $message;
327
328 }
329
330 }
331
332 return $response;
333
334 }
335
336 public function pay($payId, $stripe_konbini, $stripe_paypay, $public_key, $secret, $token, $payment_live, $amont, $currency, $key, $name, $email, $bookingDate){
337
338 $response = array('error' => "8001");
339 if ($payId == 'stripe') {
340
341 $capture_method = get_option($this->prefix . 'stripe_capture_method', 'automatic');
342 if ($capture_method == 'automatic' && $stripe_konbini === 0 && $stripe_paypay === 0) {
343
344 $this->captureStripe($payId, $secret, $token);
345
346 }
347
348 $response = $this->commitStripe($payId, $stripe_konbini, $stripe_paypay, $secret, $token, $amont, $currency, $key, $name, $email, $bookingDate);
349
350 } else if ($payId == 'paypal') {
351
352 $access_token = $this->getAccessTokenPayPal($public_key, $secret, $payment_live);
353 $response = $this->getPaymentDetailsPayPal($token, $access_token, $payment_live, $key, $name, $email, $bookingDate);
354 #var_dump($response);
355
356 }
357
358 $this->response = $response;
359 return $response;
360
361 }
362
363 public function cancel($payId, $public_key, $secret, $payment_live, $chargeId){
364
365 #var_dump($payId);
366 $response = array('error' => "8002");
367 if($payId == 'stripe' || $payId == 'stripe_paypay'){
368
369 $response = $this->cancelStripe($payId, $secret, $chargeId);
370
371 }else if($payId == 'paypal'){
372
373 $access_token = $this->getAccessTokenPayPal($public_key, $secret, $payment_live);
374 $response = $this->refoundPayPal($chargeId, $access_token, $payment_live);
375
376 }
377
378 $this->response = $response;
379 return $response;
380
381 }
382
383 public function update($payId, $public_key, $secret, $chargeId, $bookingId, $payment_live){
384
385 $response = array('error' => "8002");
386 if($payId == 'paypal'){
387
388 $access_token = $this->getAccessTokenPayPal($public_key, $secret, $payment_live);
389 $response = $this->updatePaymentPayPal($chargeId, $bookingId, $access_token, $payment_live);
390
391 }
392
393 $this->response = $response;
394 return $response;
395
396 }
397
398 public function intentForStripe($secret, $amont, $currency) {
399
400 $params = array(
401 'amount' => $amont,
402 'currency' => $currency,
403 'capture_method' => 'manual',
404 /** 'payment_method_types' => array('card', 'konbini'), **/
405 'metadata' => array(
406 'integration_check' => 'accept_a_payment'
407 )
408 );
409
410 $args = array(
411 'method' => 'POST',
412 'timeout' => $this->request_timeout,
413 'body' => $params,
414 'headers' => array(
415 'Authorization' => 'Basic ' . base64_encode($secret . ':')
416 )
417 );
418 $response = wp_remote_request("https://api.stripe.com/v1/payment_intents", $args);
419 $httpCode = wp_remote_retrieve_response_code($response);
420 $response = json_decode(wp_remote_retrieve_body($response), true);
421 return $response;
422
423 }
424
425 public function intentForStripeExpressCheckout($secret, $amont, $currency) {
426
427 $params = array(
428 'amount' => $amont,
429 'currency' => $currency,
430 'capture_method' => 'manual',
431 'automatic_payment_methods' => ['enabled' => 'true'],
432 );
433
434 $args = array(
435 'method' => 'POST',
436 'timeout' => $this->request_timeout,
437 'body' => http_build_query($params),
438 'headers' => array(
439 'Authorization' => 'Basic ' . base64_encode($secret . ':')
440 )
441 );
442 $response = wp_remote_request("https://api.stripe.com/v1/payment_intents", $args);
443 $httpCode = wp_remote_retrieve_response_code($response);
444 $response = json_decode(wp_remote_retrieve_body($response), true);
445 return $response;
446
447 }
448
449 public function intentForStripePayPay($secret, $amont, $currency) {
450
451 $params = array(
452 'amount' => $amont,
453 'currency' => $currency,
454 'payment_method_types' => array('paypay'),
455 );
456
457 $args = array(
458 'method' => 'POST',
459 'timeout' => $this->request_timeout,
460 'body' => $params,
461 'headers' => array(
462 'Authorization' => 'Basic ' . base64_encode($secret . ':')
463 )
464 );
465 $response = wp_remote_request("https://api.stripe.com/v1/payment_intents", $args);
466 $httpCode = wp_remote_retrieve_response_code($response);
467 $response = json_decode(wp_remote_retrieve_body($response), true);
468 return $response;
469
470 }
471
472 public function intentForStripeKonbini($secret, $amont, $currency, $expiresDate) {
473
474 $params = array(
475 'amount' => $amont,
476 'currency' => $currency,
477 'capture_method' => 'automatic',
478 'payment_method_types' => array('konbini'),
479 'payment_method_options' => array(
480 'konbini' => array(
481 'expires_at' => $expiresDate,
482 ),
483 ),
484 'metadata' => array(
485 'integration_check' => 'accept_a_payment'
486 )
487 );
488
489 $args = array(
490 'method' => 'POST',
491 'timeout' => $this->request_timeout,
492 'body' => $params,
493 'headers' => array(
494 'Authorization' => 'Basic ' . base64_encode($secret . ':')
495 )
496 );
497 $response = wp_remote_request("https://api.stripe.com/v1/payment_intents", $args);
498 $httpCode = wp_remote_retrieve_response_code($response);
499 $response = json_decode(wp_remote_retrieve_body($response), true);
500 return $response;
501
502 }
503
504 public function updateIntentForStripe($secret, $amont, $id) {
505
506 $params = array(
507 'amount' => $amont,
508 );
509
510 $args = array(
511 'method' => 'POST',
512 'timeout' => $this->request_timeout,
513 'body' => $params,
514 'headers' => array(
515 'Authorization' => 'Basic ' . base64_encode($secret . ':')
516 )
517 );
518
519 $response = wp_remote_request("https://api.stripe.com/v1/payment_intents/" . $id, $args);
520 $httpCode = wp_remote_retrieve_response_code($response);
521 $response = json_decode(wp_remote_retrieve_body($response), true);
522 return $response;
523
524 }
525
526 public function captureStripe($payId, $secret, $token) {
527
528 $params = array();
529 $args = array(
530 'method' => 'POST',
531 'timeout' => $this->request_timeout,
532 /**'body' => $params, **/
533 'headers' => array(
534 'Authorization' => 'Basic ' . base64_encode($secret . ':')
535 )
536 );
537 $response = wp_remote_request('https://api.stripe.com/v1/payment_intents/' . $token . '/capture', $args);
538 $httpCode = wp_remote_retrieve_response_code($response);
539 $response = json_decode(wp_remote_retrieve_body($response), true);
540
541 /**
542 $ch = curl_init();
543 curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/payment_intents/" . $token . '/capture');
544 curl_setopt($ch, CURLOPT_USERPWD, $secret.":");
545 #curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
546 curl_setopt($ch, CURLOPT_POST, 1);
547
548 ob_start();
549 $response = curl_exec($ch);
550 $response = ob_get_contents();
551 ob_end_clean();
552 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
553 curl_close ($ch);
554 $response = json_decode($response, true);
555 **/
556
557 }
558
559 public function commitStripe($payId, $stripe_konbini, $stripe_paypay, $secret, $token, $amont, $currency, $key, $name, $email, $bookingDate){
560
561 $message = array();
562 #$params = array('source' => $token, 'amount' => $amont, 'currency' => $currency, 'description' => "Key: " . $key . " Name: " . $name);
563 $response = null;
564 $params = array('description' => "Key: " . $key . " Booking: " . $bookingDate . " Name: " . $name . " Emails: " . $email);
565
566 $args = array(
567 'method' => 'POST',
568 'timeout' => $this->request_timeout,
569 'body' => $params,
570 'headers' => array(
571 'Authorization' => 'Basic ' . base64_encode($secret . ':')
572 )
573 );
574 $response = wp_remote_request('https://api.stripe.com/v1/payment_intents/' . $token, $args);
575 $httpCode = wp_remote_retrieve_response_code($response);
576 $response = json_decode(wp_remote_retrieve_body($response), true);
577
578 /**
579 $ch = curl_init();
580 curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/payment_intents/" . $token);
581 curl_setopt($ch, CURLOPT_USERPWD, $secret.":");
582 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
583 curl_setopt($ch, CURLOPT_POST, 1);
584
585 ob_start();
586 $response = curl_exec($ch);
587 $response = ob_get_contents();
588 ob_end_clean();
589 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
590 curl_close ($ch);
591 $response = json_decode($response, true);
592 **/
593
594 #$id = $response['charges']['data'][0]['id'];
595 $id = $response['id'];
596 #var_dump($response);
597
598 if ($httpCode < 400) {
599
600 if (isset($id) && ($response['status'] == 'succeeded' || $response['status'] == 'requires_capture' || $stripe_konbini == 1 || $stripe_paypay == 1)) {
601
602 $message['cardToken'] = $id;
603
604 } else {
605
606 if($httpCode == 400){
607
608 $message['error'] = "The request was unacceptable, often due to missing a required parameter.";
609
610 }else if($httpCode == 401){
611
612 $message['error'] = "No valid API key provided.";
613
614 }else if($httpCode == 402){
615
616 $message['error'] = "The parameters were valid but the request failed.";
617
618 }else if($httpCode == 404){
619
620 $message['error'] = "The requested resource doesn't exist.";
621
622 }else if($httpCode == 409){
623
624 $message['error'] = "The request conflicts with another request (perhaps due to using the same idempotent key).";
625
626 }else if($httpCode == 429){
627
628 $message['error'] = "Too many requests hit the API too quickly. We recommend an exponential backoff of your requests.";
629
630 }else if($httpCode == 500 || $httpCode == 502 || $httpCode == 503 || $httpCode == 504){
631
632 $message['error'] = "Something went wrong on Stripe's end.";
633
634 }else{
635
636 $message['error'] = "8007";
637
638 }
639
640 #$message['error'] = $response['message'];
641
642 }
643
644 } else {
645
646 $message = $this->httpCodeError($httpCode, $payId);
647 if (isset($response['error']) && isset($response['error']['message'])) {
648
649 $message = array('code' => $httpCode, 'error' => $response['error']['message'], 'object' => $response);
650
651 }
652
653 }
654
655 return $message;
656
657 }
658
659 public function commitPayPal($payId, $secret, $token, $amont, $currency, $key, $name){
660
661 $message = array();
662 $bool = true;
663 if($bool == true){
664
665 $request = array(
666 'amount' => $amont,
667 'merchantAccountId' => $currency,
668 'paymentMethodNonce' => $token,
669 'orderId' => $key,
670 'options' => array(
671 'submitForSettlement' => true
672 )
673 );
674 $gateway = new Braintree_Gateway(array('accessToken' => $secret));
675 $result = $gateway->transaction()->sale($request);
676 if($result->success === true){
677
678 $message['cardToken'] = $result->transaction->id;
679 $message['paymentId'] = $result->transaction->paypalDetails->paymentId;
680 $message['payerId'] = $result->transaction->paypalDetails->payerId;
681
682 }else{
683
684 $message['error'] = $result->message;
685
686 }
687
688 return $message;
689
690 }else{
691
692 return array('error' => "8020");
693
694 }
695
696 }
697
698 public function getAccessTokenPayPal($public_key, $secret, $payment_live){
699
700 $sandbox = '';
701 if(intval($payment_live) == 0){
702
703 $sandbox = 'sandbox.';
704
705 }
706
707 $url = "https://api.".$sandbox."paypal.com/v1/oauth2/token";
708 $message = array();
709 $headers = array('Accept: application/json', 'Accept-Language: en_US');
710 $params = array('grant_type' => 'client_credentials');
711
712 $args = array(
713 'method' => 'POST',
714 'timeout' => $this->request_timeout,
715 'body' => $params,
716 'headers' => array(
717 'Authorization' => 'Basic ' . base64_encode($public_key . ':' . $secret)
718 )
719 );
720 $response = wp_remote_request($url, $args);
721 $httpCode = wp_remote_retrieve_response_code($response);
722 $response = json_decode(wp_remote_retrieve_body($response), true);
723
724 /**
725 $response = null;
726 $ch = curl_init();
727 curl_setopt($ch, CURLOPT_URL, $url);
728 curl_setopt($ch, CURLOPT_USERPWD, $public_key.":".$secret);
729 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
730 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
731 curl_setopt($ch, CURLOPT_POST, 1);
732
733 ob_start();
734 $response = curl_exec($ch);
735 $response = ob_get_contents();
736 ob_end_clean();
737 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
738 curl_close ($ch);
739 $response = json_decode($response, true);
740 **/
741 return $response['access_token'];
742
743 }
744
745 public function capturePayPal($id, $token, $payment_live) {
746
747 $sandbox = '';
748 if(intval($payment_live) == 0){
749
750 $sandbox = 'sandbox.';
751
752 }
753
754 $url = "https://api." . $sandbox . "paypal.com/v2/checkout/orders/" . $id . '/capture';
755 $message = array();
756 $headers = array('content-type: application/json', 'Authorization: Bearer '.$token);
757
758 $args = array(
759 'method' => 'POST',
760 'timeout' => $this->request_timeout,
761 'headers' => array(
762 'content-type' => 'application/json',
763 'Authorization' => 'Bearer ' . $token
764 )
765 );
766 $response = wp_remote_request($url, $args);
767 $httpCode = wp_remote_retrieve_response_code($response);
768 $response = json_decode(wp_remote_retrieve_body($response), true);
769
770 /**
771 $response = null;
772 $ch = curl_init();
773 curl_setopt($ch, CURLOPT_URL, $url);
774 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
775 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array()));
776 curl_setopt($ch, CURLOPT_POST, 1);
777
778 ob_start();
779 $response = curl_exec($ch);
780 $response = ob_get_contents();
781 ob_end_clean();
782 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
783 curl_close ($ch);
784 $response = json_decode($response, true);
785 **/
786
787 return $response;
788
789 }
790
791 public function addBookingIdOnPayPal($id, $bookingId, $token, $payment_live) {
792
793 $sandbox = '';
794 if(intval($payment_live) == 0){
795
796 $sandbox = 'sandbox.';
797
798 }
799
800 $url = "https://api." . $sandbox . "paypal.com/v2/checkout/orders/" . $id;
801 $message = array();
802 $headers = array('content-type: application/json', 'Authorization: Bearer '.$token);
803 $params = array(
804 array(
805 'op' => 'add',
806 'path' => "/purchase_units/@reference_id=='default'/custom_id",
807 'value' => $bookingId,
808 ),
809 );
810
811 $args = array(
812 'method' => 'PATCH',
813 'timeout' => $this->request_timeout,
814 'body' => json_encode($params),
815 'headers' => array(
816 'content-type' => 'application/json',
817 'Authorization' => 'Bearer ' . $token
818 )
819 );
820 $response = wp_remote_request($url, $args);
821 $httpCode = wp_remote_retrieve_response_code($response);
822 $response = json_decode(wp_remote_retrieve_body($response), true);
823
824 /**
825 $response = null;
826 $ch = curl_init();
827 curl_setopt($ch, CURLOPT_URL, $url);
828 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
829 //curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
830 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
831 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
832
833 ob_start();
834 $response = curl_exec($ch);
835 $response = ob_get_contents();
836 ob_end_clean();
837 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
838 curl_close ($ch);
839 **/
840
841 }
842
843 public function getPaymentDetailsPayPal($id, $token, $payment_live, $bookingId, $name, $email, $bookingDate){
844
845 $this->addBookingIdOnPayPal($id, 'Booking ID: ' . $bookingId . ', Name: ' . $name, $token, $payment_live);
846 $this->capturePayPal($id, $token, $payment_live);
847 $sandbox = '';
848 if(intval($payment_live) == 0){
849
850 $sandbox = 'sandbox.';
851
852 }
853
854 $url = "https://api.". $sandbox ."paypal.com/v1/payments/payment/".$id;
855 $url = "https://api." . $sandbox . "paypal.com/v2/checkout/orders/" . $id;
856 $message = array();
857 $headers = array('content-type: application/json', 'Authorization: Bearer '.$token);
858
859 $args = array(
860 'method' => 'GET',
861 'timeout' => $this->request_timeout,
862 'headers' => array(
863 'content-type' => 'application/json',
864 'Authorization' => 'Bearer ' . $token
865 )
866 );
867 $response = wp_remote_request($url, $args);
868 $httpCode = wp_remote_retrieve_response_code($response);
869 $response = json_decode(wp_remote_retrieve_body($response), true);
870
871 if ($response['status'] == 'COMPLETED') {
872
873 $message['cardToken'] = $response['id'];
874 if (isset($response['purchase_units'][0]['payments']['captures'][0]['id'])) {
875
876 $message['cardToken'] = $response['purchase_units'][0]['payments']['captures'][0]['id'];
877
878 }
879
880 } else {
881
882 $message['error'] = 'Payment through PayPal failed.';
883 $message['response'] = $response;
884
885 }
886
887 return $message;
888
889 }
890
891 public function updatePaymentPayPal($id, $bookingId, $token, $payment_live){
892
893 $sandbox = '';
894 if(intval($payment_live) == 0){
895
896 $sandbox = 'sandbox.';
897
898 }
899
900 $url = "https://api.".$sandbox."paypal.com/v1/payments/payment/".$id;
901 $message = array();
902 $headers = array('Content-Type: application/json', 'Authorization: Bearer '.$token);
903 $params = array(array('op' => 'replace', 'path' => '/transactions/0/custom', 'value' => 'Booking ID ' . $bookingId));
904
905 $args = array(
906 'method' => 'PATCH',
907 'timeout' => $this->request_timeout,
908 'body' => json_encode($params),
909 'headers' => array(
910 'content-type' => 'application/json',
911 'Authorization' => 'Bearer ' . $token
912 )
913 );
914 $response = wp_remote_request($url, $args);
915 $httpCode = wp_remote_retrieve_response_code($response);
916 $response = json_decode(wp_remote_retrieve_body($response), true);
917
918 /**
919 $response = null;
920 $ch = curl_init();
921 curl_setopt($ch, CURLOPT_URL, $url);
922 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
923 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
924 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
925
926 ob_start();
927 $response = curl_exec($ch);
928 $response = ob_get_contents();
929 ob_end_clean();
930 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
931 curl_close($ch);
932 **/
933
934 $message['httpCode'] = $httpCode;
935 $message['id'] = $id;
936 $message['params'] = $params;
937
938 return $message;
939
940 }
941
942 public function refoundPayPal($id, $token, $payment_live){
943
944 $sandbox = '';
945 if(intval($payment_live) == 0){
946
947 $sandbox = 'sandbox.';
948
949 }
950
951 $url = "https://api.".$sandbox."paypal.com/v1/payments/sale/".$id;
952 $message = array();
953 $headers = array('content-type: application/json', 'Accept: application/json', 'Authorization: Bearer '.$token);
954
955 $args = array(
956 'method' => 'GET',
957 'timeout' => $this->request_timeout,
958 'headers' => array(
959 'content-type' => 'application/json',
960 'Authorization' => 'Bearer ' . $token
961 )
962 );
963 $response = wp_remote_request($url, $args);
964 $httpCode = wp_remote_retrieve_response_code($response);
965 $response = json_decode(wp_remote_retrieve_body($response), true);
966
967 /**
968 $response = null;
969 $ch = curl_init();
970 curl_setopt($ch, CURLOPT_URL, $url);
971 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
972 curl_setopt($ch, CURLOPT_POST, 0);
973
974 ob_start();
975 $response = curl_exec($ch);
976 $response = ob_get_contents();
977 ob_end_clean();
978 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
979 curl_close($ch);
980 $response = json_decode($response, true);
981 **/
982
983 if ($httpCode == 200) {
984
985 $url = "https://api." . $sandbox . "paypal.com/v1/payments/sale/" . $id . "/refund";
986 $headers = array('content-type: application/json', 'Accept: application/json', 'Authorization: Bearer '.$token);
987 $params = array('amount' => array('currency' => $response['amount']['currency'], 'total' => $response['amount']['total']));
988
989 $args = array(
990 'method' => 'POST',
991 'timeout' => $this->request_timeout,
992 /**'body' => json_encode($params),**/
993 'headers' => array(
994 'content-type' => 'application/json',
995 'Authorization' => 'Bearer ' . $token
996 )
997 );
998 $response = wp_remote_request($url, $args);
999 $httpCode = wp_remote_retrieve_response_code($response);
1000 $response = json_decode(wp_remote_retrieve_body($response), true);
1001
1002 /**
1003 $response = null;
1004 $ch = curl_init();
1005 curl_setopt($ch, CURLOPT_URL, $url);
1006 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
1007 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1008 curl_setopt($ch, CURLOPT_POST, 1);
1009 #curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
1010 #curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
1011
1012 ob_start();
1013 $response = curl_exec($ch);
1014 $response = ob_get_contents();
1015 ob_end_clean();
1016 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1017 curl_close($ch);
1018 $response = json_decode($response, true);
1019 **/
1020
1021 $message['response'] = $response;
1022 if($httpCode != 201){
1023
1024 $message['status'] = 'error';
1025 $message['message'] = 'Refund failed';
1026
1027 }
1028
1029 }
1030
1031 return $message;
1032
1033 }
1034
1035
1036
1037 /**
1038 public function cancelStripe($payId, $secret, $chargeId){
1039
1040 $status = 'requires_capture';
1041 if (preg_match('/^pi_/', $chargeId)) {
1042
1043 $args = array(
1044 'method' => 'GET',
1045 'timeout' => $this->request_timeout,
1046 'headers' => array(
1047 'Authorization' => 'Basic ' . base64_encode($secret . ':')
1048 )
1049 );
1050 $response = wp_remote_request('https://api.stripe.com/v1/payment_intents/' . $chargeId, $args);
1051 $httpCode = wp_remote_retrieve_response_code($response);
1052 $response = json_decode(wp_remote_retrieve_body($response), true);
1053
1054 if ($response['status'] == 'succeeded') {
1055
1056 $status = 'succeeded';
1057 $chargeId = $response['charges']['data'][0]['id'];
1058
1059 }
1060
1061 } else {
1062
1063 $status = 'succeeded';
1064
1065 }
1066
1067 $response = null;
1068 $httpCode = null;
1069 $message = array();
1070 if ($status == 'requires_capture') {
1071
1072 $args = array(
1073 'method' => 'POST',
1074 'timeout' => $this->request_timeout,
1075 'headers' => array(
1076 'Authorization' => 'Basic ' . base64_encode($secret . ':')
1077 )
1078 );
1079 $response = wp_remote_request('https://api.stripe.com/v1/payment_intents/' . $chargeId . '/cancel', $args);
1080 $httpCode = wp_remote_retrieve_response_code($response);
1081
1082 } else if ($status == 'succeeded') {
1083
1084 $params = array('charge' => $chargeId);
1085
1086 $args = array(
1087 'method' => 'POST',
1088 'timeout' => $this->request_timeout,
1089 'body' => $params,
1090 'headers' => array(
1091 'Authorization' => 'Basic ' . base64_encode($secret . ':')
1092 )
1093 );
1094 $response = wp_remote_request('https://api.stripe.com/v1/refunds', $args);
1095 $httpCode = wp_remote_retrieve_response_code($response);
1096
1097 }
1098
1099 #$response = json_decode($response, true);
1100 $response = json_decode(wp_remote_retrieve_body($response), true);
1101 if ($httpCode < 400) {
1102
1103 if (isset($response['id']) && $response['status'] == 'succeeded') {
1104
1105 $message['cardToken'] = $response['id'];
1106
1107 }
1108
1109 } else {
1110
1111 $message = $this->httpCodeError($httpCode, $payId);
1112
1113 }
1114
1115 return $message;
1116
1117 }
1118 **/
1119
1120 public function cancelStripe($payId, $secret, $chargeId) {
1121
1122 $status = 'requires_capture';
1123 $originalId = $chargeId;
1124 if (preg_match('/^pi_/', $originalId)) {
1125 $args = array(
1126 'method' => 'GET',
1127 'timeout' => $this->request_timeout,
1128 'headers' => array(
1129 'Authorization' => 'Basic ' . base64_encode($secret . ':')
1130 )
1131 );
1132 $response = wp_remote_request('https://api.stripe.com/v1/payment_intents/' . $originalId, $args);
1133
1134 if (!is_wp_error($response)) {
1135
1136 $responseBody = json_decode(wp_remote_retrieve_body($response), true);
1137 if (isset($responseBody['status']) && $responseBody['status'] == 'succeeded') {
1138
1139 $status = 'succeeded';
1140
1141 }
1142
1143 } else {
1144
1145 return $this->httpCodeError(500, $payId);
1146
1147 }
1148 } else {
1149
1150 $status = 'succeeded';
1151
1152 }
1153
1154 $response = null;
1155 $httpCode = null;
1156 $message = array();
1157 if ($status == 'requires_capture') {
1158
1159 $args = array(
1160 'method' => 'POST',
1161 'timeout' => $this->request_timeout,
1162 'headers' => array(
1163 'Authorization' => 'Basic ' . base64_encode($secret . ':')
1164 )
1165 );
1166 $response = wp_remote_request('https://api.stripe.com/v1/payment_intents/' . $originalId . '/cancel', $args);
1167
1168 } else if ($status == 'succeeded') {
1169
1170 if (preg_match('/^pi_/', $originalId)) {
1171
1172 $params = array('payment_intent' => $originalId);
1173
1174 } else {
1175
1176 $params = array('charge' => $originalId);
1177
1178 }
1179
1180 $args = array(
1181 'method' => 'POST',
1182 'timeout' => $this->request_timeout,
1183 'body' => $params,
1184 'headers' => array(
1185 'Authorization' => 'Basic ' . base64_encode($secret . ':')
1186 )
1187 );
1188 $response = wp_remote_request('https://api.stripe.com/v1/refunds', $args);
1189
1190 }
1191
1192 if (!is_wp_error($response) && $response !== null) {
1193
1194 $httpCode = wp_remote_retrieve_response_code($response);
1195 $responseBody = json_decode(wp_remote_retrieve_body($response), true);
1196
1197 if ($httpCode < 400) {
1198
1199 $success_statuses = array('succeeded', 'canceled', 'pending');
1200 if (isset($responseBody['id']) && isset($responseBody['status']) && in_array($responseBody['status'], $success_statuses)) {
1201
1202 $message['cardToken'] = $responseBody['id'];
1203
1204 } else {
1205
1206 $message = $this->httpCodeError($httpCode, $payId);
1207
1208 }
1209
1210 } else {
1211
1212 $message = $this->httpCodeError($httpCode, $payId);
1213
1214 }
1215
1216 } else {
1217 $message = $this->httpCodeError(500, $payId);
1218 }
1219
1220 return $message;
1221 }
1222
1223 public function cancelPayPal($payId, $secret, $chargeId){
1224
1225 }
1226
1227 public function httpCodeError($httpCode, $payId){
1228
1229 $message = array('code' => $httpCode);
1230 if($httpCode == 400){
1231 $message['error'] = "The request was unacceptable, often due to missing a required parameter.";
1232 }else if($httpCode == 401){
1233 $message['error'] = "No valid API key provided.";
1234 }else if($httpCode == 402){
1235 $message['error'] = "The parameters were valid but the request failed.";
1236 }else if($httpCode == 404){
1237 $message['error'] = "The requested resource doesn't exist.";
1238 }else if($httpCode == 409){
1239 $message['error'] = "The request conflicts with another request (perhaps due to using the same idempotent key).";
1240 }else if($httpCode == 429){
1241 $message['error'] = "Too many requests hit the API too quickly. We recommend an exponential backoff of your requests.";
1242 }else{
1243 $message['error'] = "Something went wrong on Stripe's end.";
1244 }
1245
1246 return $message;
1247
1248 }
1249
1250 }
1251 ?>