Guest User

Paypal Class

a guest
Nov 19th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.58 KB | None | 0 0
  1. <?php
  2. namespace Helpers;
  3.  
  4. use Helpers\HTTPRequest;
  5. use Models\Payment_model;
  6. class PP {
  7.  
  8.     const API_USERNAME = "";
  9.     const API_PASSWORD = "";
  10.     const API_SIGNATURE = "";
  11.     const PP_RETURN = "http://hsfarm.myftp.org/splashwi-homepage/clientarea/pay_pingback";
  12.     const PP_CANCEL = "http://hsfarm.myftp.org/splashwi-homepage/clientarea/pay_pingback";
  13.  
  14.     public $endpoint;
  15.     public $host;
  16.     public $gate;
  17.  
  18.     private $config;
  19.  
  20.     function __construct($config, $real = false) {
  21.         $endpoint = '/nvp';
  22.  
  23.         $this->config = $config;
  24.     }
  25.  
  26.     /**
  27.      * @return HTTPRequest
  28.      */
  29.     public function response($data){
  30.         $endpoint = '/nvp';
  31.  
  32.         $host = $this->config->pp_host;
  33.         $gate = $this->config->pp_gate;
  34.  
  35.         /*$host = "api-3t.sandbox.paypal.com";
  36.         $gate = 'https://www.sandbox.paypal.com/cgi-bin/webscr?';*/
  37.         $r = new \Helpers\HTTPRequest($host, $endpoint, 'POST', true);
  38.         $result = $r->connect($data);
  39.         if ($result<400) return $r;
  40.         return false;
  41.     }
  42.  
  43.     public function buildQuery($data = array()){
  44.         $data['USER'] = $this->config->username;
  45.         $data['PWD'] = $this->config->publickey;
  46.         $data['SIGNATURE'] = $this->config->privatekey;
  47.         $data['VERSION'] = '52.0';
  48.         $query = http_build_query($data);
  49.         return $query;
  50.     }
  51.  
  52.  
  53.     /**
  54.      * Main payment function
  55.      *
  56.      * If OK, the customer is redirected to PayPal gateway
  57.      * If error, the error info is returned
  58.      *
  59.      * @param float $amount Amount (2 numbers after decimal point)
  60.      * @param string $desc Item description
  61.      * @param string $invoice Invoice number (can be omitted)
  62.      * @param string $currency 3-letter currency code (USD, GBP, CZK etc.)
  63.      *
  64.      * @return array error info
  65.      */
  66.     public function doExpressCheckout($amount, $desc, $initial='', $invoice='', $currency='EUR'){
  67.         if(!empty($initial) && $initial == "init") {
  68.             $add = "_init";
  69.         } else {
  70.             $add = "";
  71.         }
  72.  
  73.         $host = $_SERVER["HTTP_HOST"];
  74.         $data = array(
  75.         'PAYMENTACTION' =>'Sale',
  76.         'AMT' =>$amount,
  77.         'RETURNURL' => 'http://'.$host.DIR.'paypal_pingback',
  78.         'CANCELURL'  => 'http://'.$host.DIR.'paypal_pingback',
  79.         'DESC'=>$desc,
  80.         'NOSHIPPING'=>"1",
  81.         'ALLOWNOTE'=>"1",
  82.         'CURRENCYCODE'=>$currency,
  83.         'METHOD' =>'SetExpressCheckout');
  84.  
  85.         $data['CUSTOM'] = $amount.'|'.$currency.'|'.$invoice;
  86.         if ($invoice) $data['INVNUM'] = $invoice;
  87.  
  88.         $query = PP::buildQuery($data);
  89.  
  90.         $result = PP::response($query);
  91.  
  92.         if (!$result) return false;
  93.         $response = $result->getContent();
  94.         $return = self::responseParse($response);
  95.  
  96.         if ($return['ACK'] == 'Success') {
  97.             $gate = 'https://www.paypal.com/cgi-bin/webscr?';
  98.             //$gate = 'https://www.sandbox.paypal.com/cgi-bin/webscr?';
  99.  
  100.             echo 'Du wirst zu PayPal weitergeleitet...';
  101.             echo '<meta http-equiv="refresh" content="0; URL='.$gate.'cmd=_express-checkout&useraction=commit&token='.$return['TOKEN'].'">';
  102.             //header('Location: '.$gate.'cmd=_express-checkout&useraction=commit&token='.$return['TOKEN'].'');
  103.             die();
  104.         }
  105.         return($return);
  106.     }
  107.  
  108.     public function getCheckoutDetails($token){
  109.         $data = array(
  110.         'TOKEN' => $token,
  111.         'METHOD' =>'GetExpressCheckoutDetails');
  112.         $query = $this->buildQuery($data);
  113.  
  114.         $result = $this->response($query);
  115.  
  116.         if (!$result) return false;
  117.         $response = $result->getContent();
  118.         $return = $this->responseParse($response);
  119.         return($return);
  120.     }
  121.     public function doPayment(){
  122.         $token = $_GET['token'];
  123.         $payer = $_GET['PayerID'];
  124.         $details = $this->getCheckoutDetails($token);
  125.         if (!$details) return false;
  126.         list($amount,$currency,$invoice) = explode('|',$details['CUSTOM']);
  127.         $data = array(
  128.         'PAYMENTACTION' => 'Sale',
  129.         'PAYERID' => $payer,
  130.         'TOKEN' =>$token,
  131.         'AMT' => $amount,
  132.         'CURRENCYCODE'=>$currency,
  133.         'METHOD' =>'DoExpressCheckoutPayment');
  134.         $query = $this->buildQuery($data);
  135.  
  136.         $result = $this->response($query);
  137.  
  138.         if (!$result) return false;
  139.         $response = $result->getContent();
  140.         $return = $this->responseParse($response);
  141.  
  142.         /*
  143.          * [AMT] => 10.00
  144.          * [CURRENCYCODE] => USD
  145.          * [PAYMENTSTATUS] => Completed
  146.          * [PENDINGREASON] => None
  147.          * [REASONCODE] => None
  148.          */
  149.  
  150.         return($return);
  151.     }
  152.  
  153.     private function getScheme() {
  154.         $scheme = 'http';
  155.         if (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') {
  156.             $scheme .= 's';
  157.         }
  158.         return $scheme;
  159.     }
  160.  
  161.     private function responseParse($resp){
  162.         $a=explode("&", $resp);
  163.         $out = array();
  164.         foreach ($a as $v){
  165.             $k = strpos($v, '=');
  166.             if ($k) {
  167.                 $key = trim(substr($v,0,$k));
  168.                 $value = trim(substr($v,$k+1));
  169.                 if (!$key) continue;
  170.                 $out[$key] = urldecode($value);
  171.             } else {
  172.                 $out[] = $v;
  173.             }
  174.         }
  175.         return $out;
  176.     }
  177. }
Add Comment
Please, Sign In to add comment