Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Helpers;
- use Helpers\HTTPRequest;
- use Models\Payment_model;
- class PP {
- const API_USERNAME = "";
- const API_PASSWORD = "";
- const API_SIGNATURE = "";
- const PP_RETURN = "http://hsfarm.myftp.org/splashwi-homepage/clientarea/pay_pingback";
- const PP_CANCEL = "http://hsfarm.myftp.org/splashwi-homepage/clientarea/pay_pingback";
- public $endpoint;
- public $host;
- public $gate;
- private $config;
- function __construct($config, $real = false) {
- $endpoint = '/nvp';
- $this->config = $config;
- }
- /**
- * @return HTTPRequest
- */
- public function response($data){
- $endpoint = '/nvp';
- $host = $this->config->pp_host;
- $gate = $this->config->pp_gate;
- /*$host = "api-3t.sandbox.paypal.com";
- $gate = 'https://www.sandbox.paypal.com/cgi-bin/webscr?';*/
- $r = new \Helpers\HTTPRequest($host, $endpoint, 'POST', true);
- $result = $r->connect($data);
- if ($result<400) return $r;
- return false;
- }
- public function buildQuery($data = array()){
- $data['USER'] = $this->config->username;
- $data['PWD'] = $this->config->publickey;
- $data['SIGNATURE'] = $this->config->privatekey;
- $data['VERSION'] = '52.0';
- $query = http_build_query($data);
- return $query;
- }
- /**
- * Main payment function
- *
- * If OK, the customer is redirected to PayPal gateway
- * If error, the error info is returned
- *
- * @param float $amount Amount (2 numbers after decimal point)
- * @param string $desc Item description
- * @param string $invoice Invoice number (can be omitted)
- * @param string $currency 3-letter currency code (USD, GBP, CZK etc.)
- *
- * @return array error info
- */
- public function doExpressCheckout($amount, $desc, $initial='', $invoice='', $currency='EUR'){
- if(!empty($initial) && $initial == "init") {
- $add = "_init";
- } else {
- $add = "";
- }
- $host = $_SERVER["HTTP_HOST"];
- $data = array(
- 'PAYMENTACTION' =>'Sale',
- 'AMT' =>$amount,
- 'RETURNURL' => 'http://'.$host.DIR.'paypal_pingback',
- 'CANCELURL' => 'http://'.$host.DIR.'paypal_pingback',
- 'DESC'=>$desc,
- 'NOSHIPPING'=>"1",
- 'ALLOWNOTE'=>"1",
- 'CURRENCYCODE'=>$currency,
- 'METHOD' =>'SetExpressCheckout');
- $data['CUSTOM'] = $amount.'|'.$currency.'|'.$invoice;
- if ($invoice) $data['INVNUM'] = $invoice;
- $query = PP::buildQuery($data);
- $result = PP::response($query);
- if (!$result) return false;
- $response = $result->getContent();
- $return = self::responseParse($response);
- if ($return['ACK'] == 'Success') {
- $gate = 'https://www.paypal.com/cgi-bin/webscr?';
- //$gate = 'https://www.sandbox.paypal.com/cgi-bin/webscr?';
- echo 'Du wirst zu PayPal weitergeleitet...';
- echo '<meta http-equiv="refresh" content="0; URL='.$gate.'cmd=_express-checkout&useraction=commit&token='.$return['TOKEN'].'">';
- //header('Location: '.$gate.'cmd=_express-checkout&useraction=commit&token='.$return['TOKEN'].'');
- die();
- }
- return($return);
- }
- public function getCheckoutDetails($token){
- $data = array(
- 'TOKEN' => $token,
- 'METHOD' =>'GetExpressCheckoutDetails');
- $query = $this->buildQuery($data);
- $result = $this->response($query);
- if (!$result) return false;
- $response = $result->getContent();
- $return = $this->responseParse($response);
- return($return);
- }
- public function doPayment(){
- $token = $_GET['token'];
- $payer = $_GET['PayerID'];
- $details = $this->getCheckoutDetails($token);
- if (!$details) return false;
- list($amount,$currency,$invoice) = explode('|',$details['CUSTOM']);
- $data = array(
- 'PAYMENTACTION' => 'Sale',
- 'PAYERID' => $payer,
- 'TOKEN' =>$token,
- 'AMT' => $amount,
- 'CURRENCYCODE'=>$currency,
- 'METHOD' =>'DoExpressCheckoutPayment');
- $query = $this->buildQuery($data);
- $result = $this->response($query);
- if (!$result) return false;
- $response = $result->getContent();
- $return = $this->responseParse($response);
- /*
- * [AMT] => 10.00
- * [CURRENCYCODE] => USD
- * [PAYMENTSTATUS] => Completed
- * [PENDINGREASON] => None
- * [REASONCODE] => None
- */
- return($return);
- }
- private function getScheme() {
- $scheme = 'http';
- if (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') {
- $scheme .= 's';
- }
- return $scheme;
- }
- private function responseParse($resp){
- $a=explode("&", $resp);
- $out = array();
- foreach ($a as $v){
- $k = strpos($v, '=');
- if ($k) {
- $key = trim(substr($v,0,$k));
- $value = trim(substr($v,$k+1));
- if (!$key) continue;
- $out[$key] = urldecode($value);
- } else {
- $out[] = $v;
- }
- }
- return $out;
- }
- }
Add Comment
Please, Sign In to add comment