PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/paypal/paypal.php

https://github.com/psykomo/kutump-enhanced
PHP | 283 lines | 205 code | 42 blank | 36 comment | 22 complexity | dfa77c4da5ac29e95af566a7d5f1401e MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. +-------------------------------------------------------------------------------+
  4. | Copyright 2008 Peter Reisinger - p.reisinger@gmail.com |
  5. | |
  6. | This program is free software: you can redistribute it and/or modify |
  7. | it under the terms of the GNU General Public License as published by |
  8. | the Free Software Foundation, either version 3 of the License, or |
  9. | (at your option) any later version. |
  10. | |
  11. | This program is distributed in the hope that it will be useful, |
  12. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  13. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  14. | GNU General Public License for more details. |
  15. | |
  16. | You should have received a copy of the GNU General Public License |
  17. | along with this program. If not, see <http://www.gnu.org/licenses/>. |
  18. +-------------------------------------------------------------------------------+
  19. */
  20. abstract class PayPalNVP
  21. {
  22. //path to main ini files
  23. const INI_FILES = "/etc/nvp/";
  24. //name of this action
  25. private static $name = "PayPalNVP";
  26. //holds all name value pairs - NVP
  27. private $default = array();
  28. //set main NVP and calling class's NVP
  29. protected function __construct($action)
  30. {
  31. try {
  32. $this->checkFile(self::INI_FILES.self::$name.".ini");
  33. $this->checkFile(self::INI_FILES.$action.".ini");
  34. } catch (Exception $e) {
  35. echo "Message: ".$e->getMessage();
  36. }
  37. $this->default[self::$name] = parse_ini_file(dirname(__FILE__).self::INI_FILES.self::$name.".ini", false);
  38. $this->default[$action] = parse_ini_file(dirname(__FILE__).self::INI_FILES.$action.".ini", false);
  39. }
  40. //used by children classes to set NVP
  41. protected function setDefault($action, $key, $value)
  42. {
  43. $this->default[$action][$key] = $value;
  44. }
  45. //used by constructor
  46. private function checkFile($file)
  47. {
  48. if (!(file_exists(dirname(__FILE__).$file) && is_readable(dirname(__FILE__).$file))) {
  49. throw new Exception("Error ".dirname(__FILE__).$file." must exist and be readable");
  50. }
  51. }
  52. //set main NVP
  53. public function setPayPalNVP ($key, $value)
  54. {
  55. $this->default['PayPalNVP'][$key] = $value;
  56. }
  57. //used by PPHttpPost, to get needed values
  58. protected function getNVP ($action, $key = null)
  59. {
  60. if ($key == null) {
  61. return($this->default[$action]);
  62. } else {
  63. if (array_key_exists($key, $this->default[$action])) {
  64. return($this->default[$action][$key]);
  65. } else {
  66. throw new Exception("Value ".$key." cannot be found.");
  67. }
  68. }
  69. }
  70. protected function getEnvironment()
  71. {
  72. $environment = $this->getNVP(self::$name, "environment");
  73. if ($environment == 'live') {
  74. return "";
  75. } else {
  76. return $environment.".";
  77. }
  78. }
  79. protected function PPHttpPost($methodName_)
  80. {
  81. $API_UserName = urlencode($this->getNVP(self::$name, "API_UserName"));
  82. $API_Password = urlencode($this->getNVP(self::$name, "API_Password"));
  83. $API_Signature = urlencode($this->getNVP(self::$name, "API_Signature"));
  84. $API_Endpoint = "https://api-3t.".$this->getEnvironment()."paypal.com/nvp";
  85. $version = urlencode($this->getNVP(self::$name, "version"));
  86. $nvpStr = '';
  87. $NVP = $this->getNVP($methodName_);
  88. foreach ($NVP as $key=>$value) {
  89. $nvpStr .= '&'.$key.'='.urlencode($value);
  90. }
  91. // setting the curl parameters.
  92. $ch = curl_init();
  93. curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
  94. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  95. // turning off the server and peer verification(TrustManager Concept).
  96. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  97. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  98. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  99. curl_setopt($ch, CURLOPT_POST, 1);
  100. // NVPRequest for submitting to server
  101. $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr";
  102. // setting the nvpreq as POST FIELD to curl
  103. curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
  104. // getting response from server
  105. $httpResponse = curl_exec($ch);
  106. if(!$httpResponse) {
  107. exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
  108. }
  109. // Extract the RefundTransaction response details
  110. $httpResponseAr = explode("&", $httpResponse);
  111. $httpParsedResponseAr = array();
  112. foreach ($httpResponseAr as $i => $value) {
  113. $tmpAr = explode("=", $value);
  114. if(sizeof($tmpAr) > 1) {
  115. $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
  116. }
  117. }
  118. if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
  119. exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
  120. }
  121. return $httpParsedResponseAr;
  122. }
  123. abstract public function setNVP($key, $value);
  124. abstract public function getResponse();
  125. }
  126. class SetExpressCheckout extends PayPalNVP
  127. {
  128. private static $name = "SetExpressCheckout";
  129. public function __construct($amount = null)
  130. {
  131. parent::__construct(self::$name);
  132. if (!is_null($amount)) {
  133. $this->setNVP("AMT", $amount);
  134. }
  135. }
  136. public function setNVP($key, $value)
  137. {
  138. $this->setDefault(self::$name, $key, $value);
  139. }
  140. public function getResponse()
  141. {
  142. $response = $this->PPHttpPost(self::$name);
  143. if("Success" == $response["ACK"]) {
  144. $token = $response['TOKEN'];
  145. $nvp = $this->getNVP(self::$name);
  146. $nvpStr = '';
  147. foreach ($nvp as $key=>$value) {
  148. $nvpStr .= '&'.$key.'='.urlencode($value);
  149. }
  150. Header("Location: https://www.".$this->getEnvironment()."paypal.com/webscr?cmd=_express-checkout&token=$token$nvpStr");
  151. //Header("Location: https://www.".$this->getEnvironment()."paypal.com/webscr?cmd=_cart&token=$token$nvpStr");
  152. exit;
  153. } else {
  154. exit ('SetExpressCheckout failed: ' .print_r($response));
  155. }
  156. }
  157. public static function request($amount = null)
  158. {
  159. $request = new self($amount);
  160. return($request->getResponse());
  161. }
  162. }
  163. class GetExpressCheckoutDetails extends PayPalNVP
  164. {
  165. private static $name = "GetExpressCheckoutDetails";
  166. public function __construct()
  167. {
  168. parent::__construct(self::$name);
  169. $this->setNVP("TOKEN", $_GET['token']);
  170. }
  171. public function setNVP($key, $value)
  172. {
  173. $this->setDefault(self::$name, $key, $value);
  174. }
  175. public function getResponse()
  176. {
  177. return($this->PPHttpPost(self::$name));
  178. }
  179. public static function request()
  180. {
  181. $request = new self();
  182. return($request->getResponse());
  183. }
  184. }
  185. class DoExpressCheckoutPayment extends PayPalNVP
  186. {
  187. private static $name = "DoExpressCheckoutPayment";
  188. public function __construct($amount = null)
  189. {
  190. parent::__construct(self::$name);
  191. $this->setNVP("TOKEN", urlencode($_GET['token']));
  192. $this->setNVP("PAYERID", urlencode($_GET['PayerID']));
  193. if (!is_null($amount)) {
  194. $this->setNVP("AMT", $amount);
  195. }
  196. }
  197. public function setNVP($key, $value)
  198. {
  199. $this->setDefault(self::$name, $key, $value);
  200. }
  201. public function getResponse()
  202. {
  203. return($this->PPHttpPost(self::$name));
  204. }
  205. public static function request($amount = null)
  206. {
  207. $request = new self($amount);
  208. return($request->getResponse());
  209. }
  210. }
  211. class CreateRecurringPaymentsProfile extends PayPalNVP
  212. {
  213. private static $name = "CreateRecurringPaymentsProfile";
  214. //desc = description, has to be same as in SetExpressCheckout
  215. public function __construct($desc, $amount = null)
  216. {
  217. parent::__construct(self::$name);
  218. $this->setNVP("TOKEN", urlencode($_GET['token']));
  219. //UTC time date("Y-m-d\TH:i:s\Z", mktime(0,0,0,date("m"), date("d")+1, date("y")));
  220. //GMT time gmdate("M d Y H:i:s", mktime(0,0,0,date("m"), date("d")+1, date("y"))); !!! not working need to find out why
  221. $this->setNVP("PROFILESTARTDATE", date("Y-m-d\TH:i:s\Z", mktime(0,0,0,date("m"), date("d")+1, date("y"))));
  222. $this->setNVP("DESC", $desc);
  223. if (!is_null($amount)) {
  224. $this->setNVP("AMT", $amount);
  225. }
  226. }
  227. public function setNVP($key, $value)
  228. {
  229. $this->setDefault(self::$name, $key, $value);
  230. }
  231. public function getResponse()
  232. {
  233. return($this->PPHttpPost(self::$name));
  234. }
  235. public static function request($desc, $amount = null)
  236. {
  237. $request = new self($desc, $amount);
  238. return($request->getResponse());
  239. }
  240. }
  241. ?>