Advertisement
Guest User

notify

a guest
Mar 22nd, 2018
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.56 KB | None | 0 0
  1. <?php
  2. require 'PayPal-PHP-SDK/autoload.php';
  3. require '../../../config-and-databases.php';
  4.  
  5. require '../../class.smtp.php';
  6. require '../../class.phpmailer.php';
  7. require '../../configfile.php';
  8.  
  9. require '../check.php';
  10. require '../validate.php';
  11.  
  12. $apiContext = new \PayPal\Rest\ApiContext(
  13.     new \PayPal\Auth\OAuthTokenCredential($paypal_ClientID,$paypal_ClientSecret)
  14. );
  15. if(isset($paypal_mode) && $paypal_mode=='live')
  16. $apiContext->setConfig(
  17.     array(
  18.         'mode' => 'live',
  19.     )
  20. );
  21.  
  22. use PayPal\Api\Amount;
  23. use PayPal\Api\Details;
  24. use PayPal\Api\Payment;
  25. use PayPal\Api\PaymentExecution;
  26. use PayPal\Api\Transaction;
  27. use PayPal\Rest\ApiContext;
  28. use PayPal\Api\Authorization;
  29. use PayPal\Api\Capture;
  30.  
  31. if (!isset($_GET['success']))
  32.     exit;
  33. if (!isset($_GET['paymentId']))
  34.     exit;
  35. if (!isset($_GET['PayerID']))
  36.     exit;
  37. if (!isset($_GET['token']))
  38.     exit;
  39.    
  40. if ($_GET['success']!='false' && $_GET['success']!='true')
  41.     exit;
  42.  
  43. $paymentId = $_GET['paymentId'];
  44. $token = $_GET['token'];
  45. $PayerID = $_GET['PayerID'];
  46.        
  47. if($filecurs=fopen('paiments.log', 'a'))
  48. {}
  49. else
  50.     die('Unable to write or create the file: '.$file);
  51.  
  52. function filewritestream($content)
  53. {
  54.     global $filecurs;
  55.     if(fwrite($filecurs,$content) === false)
  56.         die('Unable to write the file: '.$file);
  57. }
  58.  
  59. $debug=true;
  60.  
  61. $actioninerror=array();
  62.      
  63. if($debug)
  64. {
  65.     filewritestream(json_encode($_POST));
  66.     filewritestream(json_encode($_SERVER));
  67. }
  68. $data2 = '[{"details":"VPS2016 Basic: 12 Month","price":948,"quantity":"1","drop":[]},{"details":" - Backup: 12 Month","price":588,"quantity":"1"},{"details":" - Backup remote: 12 Month","price":588,"quantity":"1"}]';
  69.  
  70. if (isset($_GET['success']) && $_GET['success'] == 'true') {
  71.  
  72.         //load the base info
  73.         $reply = pg_query_params($postgres_link,'SELECT id,actiononpayment,customer,details FROM invoice WHERE paymentmethod=$1 AND paymentcode=$2',array('paypal',$token)) or die(pg_last_error());
  74.         $data = pg_fetch_array($reply);
  75.  
  76.  
  77.         $productslist=json_decode($data['details'],true);
  78.         /*cho '<pre>';
  79.         echo print_r($productslist);
  80.         echo '</pre>';*/
  81.         $internaltotal=0;
  82.         if($productslist!==NULL)
  83.         foreach($productslist as $product)
  84.             if(isset($product['details']) && isset($product['price']) && isset($product['quantity']))
  85.                 $internaltotal+=$product['price']*$product['quantity'];
  86.  
  87.         //insert into database
  88.         $payment = Payment::get($paymentId, $apiContext);
  89.         $execution = new PaymentExecution();
  90.         $execution->setPayerId($PayerID);
  91.  
  92.  
  93.     try {
  94.         $result = $payment->execute($execution, $apiContext);
  95.  
  96.         echo '<pre>';
  97.         var_dump($result);
  98.         var_dump($result->state);
  99.         echo '</pre>';
  100.         filewritestream($result);
  101.        
  102.         //Capture the payment
  103.         $arrayAuth = array("Executed Payment" => array("Payment id" => $payment->getId(), "Execution" => $execution, "Result" => $result));
  104.         $payment = Payment::get($paymentId, $apiContext);
  105.         $arrayAuth["Payment"] = $payment;
  106.         $arrayAuth["Exito"] = "";
  107.  
  108.  
  109.  
  110.         $authorization = Authorization::get($paymentId, $apiContext);
  111.         $amt = new Amount();
  112.         //Cause 1$ test2
  113.         $amt->setCurrency("USD")->setTotal(1);
  114.         //$amt->setCurrency("USD")->setTotal($internaltotal);
  115.         echo "<b style='color:green; font-size:25px;'>Transaccion realizada con exito<br></b>";
  116.  
  117.         $capture = new Capture();
  118.         $capture->setAmount($amt);
  119.         $getCapture = $authorization->capture($capture, $apiContext);
  120.  
  121.  
  122.         //do the internal code
  123.         if($data['actiononpayment']!='')
  124.         {
  125.             $actions=json_decode($data['actiononpayment'],true);
  126.             if(paimentActionIsValid($actions,$data))
  127.             {
  128.                 $returnCode=paimentValidate($actions,$data);
  129.                 pg_query_params($postgres_link,'UPDATE invoice SET paymentstat=$1 WHERE id=$2',array($returnCode,$transactionId)) or die(pg_last_error());
  130.                 if($returnCode!=1)
  131.                 {
  132.                     filewritestream(json_encode($_POST)."\n");
  133.                     filewritestream('paiment problem at apply: '.$transactionId."\n");
  134.                 }
  135.                 else if($debug)
  136.                     filewritestream('paiment ok: '.$transactionId.' '.$data['actiononpayment']."\n");
  137.                    
  138.                 if(count($actioninerror)>0)
  139.                 {
  140.                     $mail->addAddress($admin_email);
  141.                     $mail->Subject = 'For the invoice: '.$data['id'].' is unable to understand';
  142.                     $mail->Body = 'For the invoice: '.$data['id'].' is unable to understand: '.implode(',',array_keys($actioninerror));
  143.                     if (!$mail->send())
  144.                         echo 'Mailer error: '.$mail->ErrorInfo."\n";
  145.                     else
  146.                         echo 'Mail send'."\n";
  147.                 }
  148.                 foreach($vps_regenconfig_and_restart as $vpsid=>$falsevalue)
  149.                 {
  150.                     $reply = pg_query_params($postgres_link,'SELECT host FROM vps WHERE id=$1',array($vpsid)) or die(pg_last_error());
  151.                     if($data = pg_fetch_array($reply))
  152.                     {
  153.                         $file = fopen('/tmp/lock-vps-'.$vpsid,"w+");
  154.                         if(!flock($file,LOCK_EX|LOCK_NB))
  155.                         {
  156.                             fclose($file);
  157.                             echo 'Action already in progress for vps '.$vpsid."\n";
  158.                         }
  159.                         else
  160.                         {
  161.                             if(!setconfigfile($vpsid))
  162.                             {
  163.                                 filewritestream('set config file wrong:'.$vpsid."\n");
  164.                                 flock($file,LOCK_UN);
  165.                                 fclose($file);
  166.                                 echo 'Erro to set config file for vps '.$vpsid."\n";
  167.                             }
  168.                             $replyhardware = pg_query_params($postgres_link,'SELECT host FROM hardware WHERE id=$1',array($data['host'])) or die(pg_last_error());
  169.                             if($datahardware = pg_fetch_array($replyhardware))
  170.                             {
  171.                                 filewritestream('restart try: '.$vpsid."\n");
  172.                                 exec($restart_command.' '.$vpsid.' '.$datahardware['host'],$output,$returncode);
  173.                                 if($returncode!=0)
  174.                                     echo implode("\n",$output);
  175.                             }
  176.                             else
  177.                                 filewritestream('restart wrong:'.$vpsid."\n");
  178.                             fclose($file);
  179.                         }
  180.                     }
  181.                     else
  182.                         filewritestream('vps not found:'.$vpsid."\n");
  183.                 }
  184.             }
  185.             else
  186.                 filewritestream('paimentActionIsValid:'.json_encode($actioninerror)."\n".$data['actiononpayment']."\n");
  187.         }
  188.  
  189.     } catch (PayPal\Exception\PayPalConnectionException $ex) {
  190.         echo "<b style='color:red; font-size:25px;'>Error Vago<br>
  191.            Message: " . $ex->getCode() . "</b>";
  192.  
  193.         echo $ex->getData();
  194.         echo $ex->getMessage();
  195.         echo '<pre>';
  196.         echo print_r($ex->getTrace());
  197.         echo '</pre>';
  198.  
  199.     }
  200. } else {
  201.     echo ("<b style='color:green; font-size:25px;'>User Cancelled the Approval</b>");
  202. }
  203.  
  204. fclose($filecurs);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement