PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/event-espresso.3.1.23.P/gateways/ideal/ideal.class.php

https://bitbucket.org/anneivycat/ebcookhouse
PHP | 466 lines | 351 code | 98 blank | 17 comment | 33 complexity | 8111781dfaf211de7d72e21c296b09cb MD5 | raw file
  1. <?php
  2. /*-----------------------------------------------------------------------
  3. Start : 24 februari 2009
  4. Door : Mollie B.V. (RDF) © 2009
  5. Versie : 1.12 (gebaseerd op de Mollie iDEAL class van
  6. Concepto IT Solution - http://www.concepto.nl/)
  7. Laatste aanpassing : 15-09-2010
  8. Aard v. aanpassing : cURL ondersteuning toegevoegd
  9. Door : RDF
  10. -----------------------------------------------------------------------*/
  11. class iDEAL_Payment
  12. {
  13. const MIN_TRANS_AMOUNT = 118;
  14. protected $partner_id = null;
  15. protected $profile_key = null;
  16. protected $testmode = false;
  17. protected $bank_id = null;
  18. protected $amount = 0;
  19. protected $description = null;
  20. protected $return_url = null;
  21. protected $report_url = null;
  22. protected $bank_url = null;
  23. protected $payment_url = null;
  24. protected $transaction_id = null;
  25. protected $paid_status = false;
  26. protected $consumer_info = array();
  27. protected $error_message = '';
  28. protected $error_code = 0;
  29. protected $api_host = 'ssl://secure.mollie.nl';
  30. protected $api_port = 443;
  31. public function __construct ($partner_id, $api_host = 'ssl://secure.mollie.nl', $api_port = 443)
  32. {
  33. $this->partner_id = $partner_id;
  34. $this->api_host = $api_host;
  35. $this->api_port = $api_port;
  36. }
  37. // Haal de lijst van beschikbare banken
  38. public function getBanks()
  39. {
  40. $query_variables = array (
  41. 'a' => 'banklist',
  42. 'partner_id' => $this->partner_id,
  43. );
  44. if ($this->testmode) {
  45. $query_variables['testmode'] = 'true';
  46. }
  47. $banks_xml = $this->_sendRequest (
  48. $this->api_host,
  49. $this->api_port,
  50. '/xml/ideal/',
  51. http_build_query($query_variables, '', '&')
  52. );
  53. if (empty($banks_xml)) {
  54. return false;
  55. }
  56. $banks_object = $this->_XMLtoObject($banks_xml);
  57. if (!$banks_object or $this->_XMlisError($banks_object)) {
  58. return false;
  59. }
  60. $banks_array = array();
  61. foreach ($banks_object->bank as $bank) {
  62. $banks_array["{$bank->bank_id}"] = "{$bank->bank_name}";
  63. }
  64. return $banks_array;
  65. }
  66. // Zet een betaling klaar bij de bank en maak de betalings URL beschikbaar
  67. public function createPayment ($bank_id, $amount, $description, $return_url, $report_url)
  68. {
  69. if (!$this->setBankId($bank_id) or
  70. !$this->setDescription($description) or
  71. !$this->setAmount($amount) or
  72. !$this->setReturnUrl($return_url) or
  73. !$this->setReportUrl($report_url))
  74. {
  75. $this->error_message = "De opgegeven betalings gegevens zijn onjuist of incompleet.";
  76. return false;
  77. }
  78. $query_variables = array (
  79. 'a' => 'fetch',
  80. 'partnerid' => $this->getPartnerId(),
  81. 'bank_id' => $this->getBankId(),
  82. 'amount' => $this->getAmount(),
  83. 'description' => $this->getDescription(),
  84. 'reporturl' => $this->getReportURL(),
  85. 'returnurl' => $this->getReturnURL(),
  86. );
  87. if ($this->profile_key)
  88. $query_variables['profile_key'] = $this->profile_key;
  89. $create_xml = $this->_sendRequest(
  90. $this->api_host,
  91. $this->api_port,
  92. '/xml/ideal/',
  93. http_build_query($query_variables, '', '&')
  94. );
  95. if (empty($create_xml)) {
  96. return false;
  97. }
  98. $create_object = $this->_XMLtoObject($create_xml);
  99. if (!$create_object or $this->_XMLisError($create_object)) {
  100. return false;
  101. }
  102. $this->transaction_id = (string) $create_object->order->transaction_id;
  103. $this->bank_url = (string) $create_object->order->URL;
  104. return true;
  105. }
  106. // Kijk of er daadwerkelijk betaald is
  107. public function checkPayment ($transaction_id)
  108. {
  109. if (!$this->setTransactionId($transaction_id)) {
  110. $this->error_message = "Er is een onjuist transactie ID opgegeven";
  111. return false;
  112. }
  113. $query_variables = array (
  114. 'a' => 'check',
  115. 'partnerid' => $this->partner_id,
  116. 'transaction_id' => $this->getTransactionId(),
  117. );
  118. if ($this->testmode) {
  119. $query_variables['testmode'] = 'true';
  120. }
  121. $check_xml = $this->_sendRequest(
  122. $this->api_host,
  123. $this->api_port,
  124. '/xml/ideal/',
  125. http_build_query($query_variables, '', '&')
  126. );
  127. if (empty($check_xml))
  128. return false;
  129. $check_object = $this->_XMLtoObject($check_xml);
  130. if (!$check_object or $this->_XMLisError($check_object)) {
  131. return false;
  132. }
  133. $this->paid_status = (bool) ($check_object->order->payed == 'true');
  134. $this->amount = (int) $check_object->order->amount;
  135. $this->consumer_info = (isset($check_object->order->consumer)) ? (array) $check_object->order->consumer : array();
  136. return true;
  137. }
  138. public function CreatePaymentLink ($description, $amount)
  139. {
  140. if (!$this->setDescription($description) or !$this->setAmount($amount))
  141. {
  142. $this->error_message = "U moet een omschrijving én bedrag (in centen) opgeven voor de iDEAL link. Tevens moet het bedrag minstens " . self::MIN_TRANS_AMOUNT . ' eurocent zijn. U gaf ' . (int) $amount . ' cent op.';
  143. return false;
  144. }
  145. $query_variables = array (
  146. 'a' => 'create-link',
  147. 'partnerid' => $this->partner_id,
  148. 'amount' => $this->getAmount(),
  149. 'description' => $this->getDescription(),
  150. );
  151. $create_xml = $this->_sendRequest(
  152. $this->api_host,
  153. $this->api_port,
  154. '/xml/ideal/',
  155. http_build_query($query_variables, '', '&')
  156. );
  157. $create_object = $this->_XMLtoObject($create_xml);
  158. if (!$create_object or $this->_XMLisError($create_object)) {
  159. return false;
  160. }
  161. $this->payment_url = (string) $create_object->link->URL;
  162. }
  163. /*
  164. PROTECTED FUNCTIONS
  165. */
  166. protected function _sendRequest ($host, $port, $path, $data)
  167. {
  168. if (function_exists('curl_init')) {
  169. return $this->_sendRequestCurl($host, $port, $path, $data);
  170. }
  171. else {
  172. return $this->_sendRequestFsock($host, $port, $path, $data);
  173. }
  174. }
  175. protected function _sendRequestFsock ($host, $port, $path, $data)
  176. {
  177. $hostname = str_replace('ssl://', '', $host);
  178. $fp = @fsockopen($host, $port, $errno, $errstr);
  179. $buf = '';
  180. if (!$fp)
  181. {
  182. $this->error_message = 'Kon geen verbinding maken met server: ' . $errstr;
  183. $this->error_code = 0;
  184. return false;
  185. }
  186. @fputs($fp, "POST $path HTTP/1.0\n");
  187. @fputs($fp, "Host: $hostname\n");
  188. @fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
  189. @fputs($fp, "Content-length: " . strlen($data) . "\n");
  190. @fputs($fp, "Connection: close\n\n");
  191. @fputs($fp, $data);
  192. while (!feof($fp)) {
  193. $buf .= fgets($fp, 128);
  194. }
  195. fclose($fp);
  196. if (empty($buf))
  197. {
  198. $this->error_message = 'Zero-sized reply';
  199. return false;
  200. }
  201. else {
  202. list($headers, $body) = preg_split("/(\r?\n){2}/", $buf, 2);
  203. }
  204. return $body;
  205. }
  206. protected function _sendRequestCurl ($host, $port, $path, $data)
  207. {
  208. $host = str_replace('ssl://', 'https://', $host);
  209. $ch = curl_init();
  210. curl_setopt($ch, CURLOPT_URL, $host . $path);
  211. curl_setopt($ch, CURLOPT_PORT, $port);
  212. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  213. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  214. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  215. curl_setopt($ch, CURLOPT_TIMEOUT, 45);
  216. curl_setopt($ch, CURLOPT_HEADER, false);
  217. curl_setopt($ch, CURLOPT_POST, true);
  218. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  219. $body = curl_exec($ch);
  220. curl_close($ch);
  221. return $body;
  222. }
  223. protected function _XMLtoObject ($xml)
  224. {
  225. try
  226. {
  227. $xml_object = new SimpleXMLElement($xml);
  228. if ($xml_object == false)
  229. {
  230. $this->error_message = "Kon XML resultaat niet verwerken";
  231. return false;
  232. }
  233. }
  234. catch (Exception $e) {
  235. return false;
  236. }
  237. return $xml_object;
  238. }
  239. protected function _XMLisError($xml)
  240. {
  241. if (isset($xml->item))
  242. {
  243. $attributes = $xml->item->attributes();
  244. if ($attributes['type'] == 'error')
  245. {
  246. $this->error_message = (string) $xml->item->message;
  247. $this->error_code = (string) $xml->item->errorcode;
  248. return true;
  249. }
  250. }
  251. return false;
  252. }
  253. /* Getters en setters */
  254. public function setProfileKey($profile_key)
  255. {
  256. if (is_null($profile_key))
  257. return false;
  258. return ($this->profile_key = $profile_key);
  259. }
  260. public function getProfileKey()
  261. {
  262. return $this->profile_key;
  263. }
  264. public function setPartnerId ($partner_id)
  265. {
  266. if (!is_numeric($partner_id)) {
  267. return false;
  268. }
  269. return ($this->partner_id = $partner_id);
  270. }
  271. public function getPartnerId ()
  272. {
  273. return $this->partner_id;
  274. }
  275. public function setTestmode ($enable = true)
  276. {
  277. return ($this->testmode = $enable);
  278. }
  279. public function setBankId ($bank_id)
  280. {
  281. if (!is_numeric($bank_id))
  282. return false;
  283. return ($this->bank_id = $bank_id);
  284. }
  285. public function getBankId ()
  286. {
  287. return $this->bank_id;
  288. }
  289. public function setAmount ($amount)
  290. {
  291. if (!preg_match('~^[0-9]+$~', $amount)) {
  292. return false;
  293. }
  294. if (self::MIN_TRANS_AMOUNT > $amount) {
  295. return false;
  296. }
  297. return ($this->amount = $amount);
  298. }
  299. public function getAmount ()
  300. {
  301. return $this->amount;
  302. }
  303. public function setDescription ($description)
  304. {
  305. $description = substr($description, 0, 29);
  306. return ($this->description = $description);
  307. }
  308. public function getDescription ()
  309. {
  310. return $this->description;
  311. }
  312. public function setReturnURL ($return_url)
  313. {
  314. if (!preg_match('|(\w+)://([^/:]+)(:\d+)?(.*)|', $return_url))
  315. return false;
  316. return ($this->return_url = $return_url);
  317. }
  318. public function getReturnURL ()
  319. {
  320. return $this->return_url;
  321. }
  322. public function setReportURL ($report_url)
  323. {
  324. if (!preg_match('|(\w+)://([^/:]+)(:\d+)?(.*)|', $report_url)) {
  325. return false;
  326. }
  327. return ($this->report_url = $report_url);
  328. }
  329. public function getReportURL ()
  330. {
  331. return $this->report_url;
  332. }
  333. public function setTransactionId ($transaction_id)
  334. {
  335. if (empty($transaction_id))
  336. return false;
  337. return ($this->transaction_id = $transaction_id);
  338. }
  339. public function getTransactionId ()
  340. {
  341. return $this->transaction_id;
  342. }
  343. public function getBankURL ()
  344. {
  345. return $this->bank_url;
  346. }
  347. public function getPaymentURL ()
  348. {
  349. return (string) $this->payment_url;
  350. }
  351. public function getPaidStatus ()
  352. {
  353. return $this->paid_status;
  354. }
  355. public function getConsumerInfo ()
  356. {
  357. return $this->consumer_info;
  358. }
  359. public function getErrorMessage ()
  360. {
  361. return $this->error_message;
  362. }
  363. public function getErrorCode ()
  364. {
  365. return $this->error_code;
  366. }
  367. }