Sisow_Helper_Sisow

The Woocommerce Sisow Payment Methods Sisow Helper class.

Defined (1)

The class is defined in the following location(s).

/includes/classes/Sisow/Helper/Sisow.php  
  1. class Sisow_Helper_Sisow 
  2. protected static $issuers; 
  3. protected static $lastcheck; 
  4.  
  5. private $response; 
  6.  
  7. // Merchant data 
  8. private $merchantId; 
  9. private $merchantKey; 
  10. private $shopId; 
  11.  
  12. // Transaction data 
  13. public $payment; // empty=iDEAL; sofort=DIRECTebanking; mistercash=MisterCash; ... 
  14. public $issuerId; // mandatory; sisow bank code 
  15. public $purchaseId; // mandatory; max 16 alphanumeric 
  16. public $entranceCode; // max 40 strict alphanumeric (letters and numbers only) 
  17. public $description; // mandatory; max 32 alphanumeric 
  18. public $amount; // mandatory; min 0.45 
  19. public $notifyUrl; 
  20. public $returnUrl; // mandatory 
  21. public $cancelUrl; 
  22. public $callbackUrl; 
  23.  
  24. // Status data 
  25. public $status; 
  26. public $timeStamp; 
  27. public $consumerAccount; 
  28. public $consumerName; 
  29. public $consumerCity; 
  30.  
  31. // Invoice data 
  32. public $invoiceNo; 
  33. public $documentId; 
  34. public $documentUrl; 
  35.  
  36. // Result/check data 
  37. public $trxId; 
  38. public $issuerUrl; 
  39.  
  40. // Error data 
  41. public $errorCode; 
  42. public $errorMessage; 
  43.  
  44. // Status 
  45. const statusSuccess = "Success"; 
  46. const statusCancelled = "Cancelled"; 
  47. const statusExpired = "Expired"; 
  48. const statusFailure = "Failure"; 
  49. const statusOpen = "Open"; 
  50.  
  51. public function __construct($merchantid, $merchantkey, $shopid = 0) { 
  52. $this->merchantId = $merchantid; 
  53. $this->merchantKey = $merchantkey; 
  54. $this->shopId = $shopid; 
  55.  
  56. private function error() { 
  57. $this->errorCode = $this->parse("errorcode"); 
  58. $this->errorMessage = urldecode($this->parse("errormessage")); 
  59.  
  60. private function parse($search, $xml = false) { 
  61. if ($xml === false) { 
  62. $xml = $this->response; 
  63. if (($start = strpos($xml, "<" . $search . ">")) === false) { 
  64. return false; 
  65. $start += strlen($search) + 2; 
  66. if (($end = strpos($xml, "</" . $search . ">", $start)) === false) { 
  67. return false; 
  68. return substr($xml, $start, $end - $start); 
  69.  
  70. public function send($method, array $keyvalue = NULL, $return = 1) { 
  71. $url = "https://www.sisow.nl/Sisow/iDeal/RestHandler.ashx/" . $method; 
  72. $options = array( 
  73. CURLOPT_POST => 1,  
  74. CURLOPT_HEADER => 0,  
  75. CURLOPT_URL => $url,  
  76. CURLOPT_FRESH_CONNECT => 1,  
  77. CURLOPT_RETURNTRANSFER => $return,  
  78. CURLOPT_FORBID_REUSE => 1,  
  79. CURLOPT_TIMEOUT => 120,  
  80. CURLOPT_SSL_VERIFYPEER => 0,  
  81. CURLOPT_POSTFIELDS => $keyvalue == NULL ? "" : http_build_query($keyvalue, '', '&')); 
  82. $ch = curl_init(); 
  83. curl_setopt_array($ch, $options); 
  84. $this->response = curl_exec($ch); 
  85. curl_close($ch);  
  86. if (!$this->response) { 
  87. return false; 
  88. return true; 
  89.  
  90. private function getDirectory() { 
  91. $diff = 24 * 60 *60; 
  92. if (self::$lastcheck) 
  93. $diff = time() - self::$lastcheck; 
  94. if ($diff < 24 *60 *60) 
  95. return 0; 
  96. if (!$this->send("DirectoryRequest")) 
  97. return -1; 
  98. $search = $this->parse("directory"); 
  99. if (!$search) { 
  100. $this->error(); 
  101. return -2; 
  102. self::$issuers = array(); 
  103. $iss = explode("<issuer>", str_replace("</issuer>", "", $search)); 
  104. foreach ($iss as $k => $v) { 
  105. $issuerid = $this->parse("issuerid", $v); 
  106. $issuername = $this->parse("issuername", $v); 
  107. if ($issuerid && $issuername) { 
  108. self::$issuers[$issuerid] = $issuername; 
  109. self::$lastcheck = time(); 
  110. return 0; 
  111.  
  112. // DirectoryRequest 
  113. public function DirectoryRequest(&$output, $select = false, $test = false) { 
  114. if ($test === true) { 
  115. // kan ook via de gateway aangevraagd worden, maar is altijd hetzelfde 
  116. if ($select === true) { 
  117. $output = "<select id=\"sisowbank\" name=\"issuerid\">"; 
  118. $output .= "<option value=\"99\">Sisow Bank (test)</option>"; 
  119. $output .= "</select>"; 
  120. else { 
  121. $output = array("99" => "Sisow Bank (test)"); 
  122. return 0; 
  123. $output = false; 
  124. $ex = $this->getDirectory(); 
  125. if ($ex < 0) { 
  126. return $ex; 
  127. if ($select === true) { 
  128. $output = "<select id=\"sisowbank\" name=\"issuerid\">"; 
  129. else { 
  130. $output = array(); 
  131. foreach (self::$issuers as $k => $v) { 
  132. if ($select === true) { 
  133. $output .= "<option value=\"" . $k . "\">" . $v . "</option>"; 
  134. else { 
  135. $output[$k] = $v; 
  136. if ($select === true) { 
  137. $output .= "</select>"; 
  138. return 0; 
  139.  
  140. // TransactionRequest 
  141. public function TransactionRequest($keyvalue = NULL) { 
  142. $this->trxId = $this->issuerUrl = ""; 
  143. if (!$this->merchantId) { 
  144. $this->errorMessage = 'No Merchant ID'; 
  145. return -1; 
  146. if (!$this->merchantKey) { 
  147. $this->errorMessage = 'No Merchant Key'; 
  148. return -2; 
  149. if (!$this->purchaseId) { 
  150. $this->errorMessage = 'No purchaseid'; 
  151. return -3; 
  152. if ($this->amount < 0.45) { 
  153. $this->errorMessage = 'Amount < 0.45'; 
  154. return -4; 
  155. if (!$this->description) { 
  156. $this->errorMessage = 'No description'; 
  157. return -5; 
  158. if (!$this->returnUrl) { 
  159. $this->errorMessage = 'No returnurl'; 
  160. return -6; 
  161. if (!$this->issuerId && !$this->payment) { 
  162. $this->errorMessage = 'No issuerid or no payment method'; 
  163. return -7; 
  164.  
  165. if(!$this->verifyOrder($keyvalue) && ($this->payment == 'focum' || $this->payment == 'klarna' || $this->payment == 'klarnaacc')) 
  166. return -10; 
  167.  
  168. if (!$this->entranceCode) 
  169. $this->entranceCode = $this->purchaseId; 
  170.  
  171. $pars = array(); 
  172.  
  173. if(strlen($keyvalue['billing_countrycode']) == 2) 
  174. $pars["locale"] = $this->setLocale($keyvalue['billing_countrycode']); 
  175. else 
  176. $pars["locale"] = $this->setLocale(""); 
  177.  
  178. $pars["merchantid"] = $this->merchantId; 
  179. $pars["shopid"] = $this->shopId; 
  180. $pars["payment"] = $this->payment; 
  181. $pars["issuerid"] = $this->issuerId; 
  182. $pars["purchaseid"] = $this->purchaseId;  
  183. $pars["amount"] = round($this->amount * 100); 
  184. $pars["description"] = $this->description; 
  185. $pars["entrancecode"] = $this->entranceCode; 
  186. $pars["returnurl"] = $this->returnUrl; 
  187. $pars["cancelurl"] = $this->cancelUrl; 
  188. $pars["callbackurl"] = $this->callbackUrl; 
  189. $pars["notifyurl"] = $this->notifyUrl; 
  190.  
  191. $pars["sha1"] = sha1($this->purchaseId . $this->entranceCode . round($this->amount * 100) . $this->shopId . $this->merchantId . $this->merchantKey); 
  192. if ($keyvalue) { 
  193. foreach ($keyvalue as $k => $v) { 
  194. $pars[$k] = $v; 
  195.  
  196. if (!$this->send("TransactionRequest", $pars)) 
  197. return -8; 
  198. $this->trxId = $this->parse("trxid"); 
  199. $this->issuerUrl = urldecode($this->parse("issuerurl")); 
  200. $this->invoiceNo = $this->parse("invoiceno"); 
  201. $this->documentId = $this->parse("documentid"); 
  202. $this->documentUrl = $this->parse("documenturl"); 
  203. if (!$this->issuerUrl) { 
  204. $this->error(); 
  205. return -9; 
  206. return 0; 
  207.  
  208. // StatusRequest 
  209. public function StatusRequest($trxid = false) { 
  210. if ($trxid === false) 
  211. $trxid = $this->trxId; 
  212. if (!$this->merchantId) 
  213. return -1; 
  214. if (!$this->merchantKey) 
  215. return -2; 
  216. if (!$trxid) 
  217. return -3; 
  218. $this->trxId = $trxid; 
  219. $pars = array(); 
  220. $pars["merchantid"] = $this->merchantId; 
  221. $pars["shopid"] = $this->shopId; 
  222. $pars["trxid"] = $this->trxId; 
  223. $pars["sha1"] = sha1($this->trxId . $this->shopId . $this->merchantId . $this->merchantKey); 
  224. if (!$this->send("StatusRequest", $pars)) 
  225. return -4; 
  226. $this->status = $this->parse("status"); 
  227. if (!$this->status) { 
  228. $this->error(); 
  229. return -5; 
  230. $this->timeStamp = $this->parse("timestamp"); 
  231. $this->amount = $this->parse("amount") / 100.0; 
  232. $this->consumerAccount = $this->parse("consumeraccount"); 
  233. $this->consumerName = $this->parse("consumername"); 
  234. $this->consumerCity = $this->parse("consumercity"); 
  235. $this->purchaseId = $this->parse("purchaseid"); 
  236. $this->description = $this->parse("description"); 
  237. $this->entranceCode = $this->parse("entrancecode"); 
  238. return 0; 
  239.  
  240. // RefundRequest 
  241. public function RefundRequest($trxid, $amount) { 
  242. $this->amount = $amount; 
  243.  
  244. $pars = array(); 
  245. $pars["merchantid"] = $this->merchantId; 
  246. $pars["trxid"] = $trxid; 
  247. $pars["amount"] = $amount * 100.0; 
  248. $pars["sha1"] = sha1($trxid . $this->merchantId . $this->merchantKey); 
  249. if (!$this->send("RefundRequest", $pars)) 
  250. return -1; 
  251. $this->documentId = $this->parse("refundid"); 
  252. if (!$this->documentId) { 
  253. $this->error(); 
  254. return -2; 
  255. return $this->documentId; 
  256.  
  257. // InvoiceRequest 
  258. public function InvoiceRequest($trxid, $keyvalue = NULL) { 
  259. $pars = array(); 
  260. $pars["merchantid"] = $this->merchantId; 
  261. $pars["trxid"] = $trxid; 
  262. $pars["sha1"] = sha1($trxid . $this->merchantId . $this->merchantKey); 
  263. if ($keyvalue) { 
  264. foreach ($keyvalue as $k => $v) { 
  265. $pars[$k] = $v; 
  266. if (!$this->send("InvoiceRequest", $pars)) 
  267. return -1; 
  268. $this->invoiceNo = $this->parse("invoiceno"); 
  269. $this->documentUrl = $this->parse("documenturl"); 
  270.  
  271. if (!$this->invoiceNo) { 
  272. $this->error(); 
  273. return -2; 
  274.  
  275. $this->documentId = $this->parse("documentid"); 
  276. return 0; //$this->invoiceNo; 
  277.  
  278. // CancelReservationRequest 
  279. public function CancelReservationRequest($trxid) { 
  280. $pars = array(); 
  281. $pars["merchantid"] = $this->merchantId; 
  282. $pars["trxid"] = $trxid; 
  283. $pars["sha1"] = sha1($trxid . $this->merchantId . $this->merchantKey); 
  284. if (!$this->send("CancelReservationRequest", $pars)) 
  285. return -1; 
  286. return 0; 
  287.  
  288. // CreditInvoiceRequest 
  289. public function CreditInvoiceRequest($trxid, $keyvalue = NULL) { 
  290. $pars = array(); 
  291. $pars["merchantid"] = $this->merchantId; 
  292. $pars["trxid"] = $trxid; 
  293. $pars["sha1"] = sha1($trxid . $this->merchantId . $this->merchantKey); 
  294. if ($keyvalue) { 
  295. foreach ($keyvalue as $k => $v) { 
  296. $pars[$k] = $v; 
  297. if (!$this->send("CreditInvoiceRequest", $pars)) 
  298. return -1; 
  299. $this->invoiceNo = $this->parse("invoiceno"); 
  300. $this->documentUrl = $this->parse("documenturl"); 
  301. if (!$this->invoiceNo) { 
  302. $this->error(); 
  303. return -2; 
  304. $this->documentId = $this->parse("documentid"); 
  305. return 0; //$this->invoiceNo; 
  306.  
  307. private function verifyOrder($pars) 
  308. if(!array_key_exists('product_total_1', $pars)) 
  309. $this->errorCode = "ORDERROWS"; 
  310. $this->errorMessage = "No orderrows found"; 
  311. return false; 
  312.  
  313. $rowtotal = 0; 
  314.  
  315. foreach($pars as $key => $value) 
  316. if (strpos($key, 'product_total_') !== false) { 
  317. $rowtotal += $pars[$key]; 
  318.  
  319. $diff = $rowtotal - round(($this->amount * 100.0)); 
  320. if($diff < 0) 
  321. $diff = $diff * -1; 
  322.  
  323. if($diff > 1) 
  324. $this->errorCode = "ORDERTOTAL"; 
  325. $this->errorMessage = "Order total(".($this->amount * 100.0).") don't match RowTotal (".$rowtotal.")"; 
  326. return false; 
  327.  
  328. return true; 
  329.  
  330. public function setLocale($countryIso) 
  331. $supported = array("US"); 
  332.  
  333. switch($this->payment) 
  334. case "paypalec": 
  335. $supported = array('AU', 'AT', 'BE', 'BR', 'CA', 'CH', 'CN', 'DE', 'ES', 'GB', 'FR', 'IT', 'NL', 'PL', 'PT', 'RU', 'US'); 
  336. break; 
  337. case "mistercash": 
  338. $supported = array('NL', 'BE', 'DE', 'IT', 'ES', 'PT', 'BR', 'SE', 'FR'); 
  339. break; 
  340. case "creditcard": 
  341. $supported = array('NL', 'BE', 'DE', 'IT', 'ES', 'PT', 'BR', 'SE', 'FR'); 
  342. break; 
  343. case "maestro": 
  344. $supported = array('NL', 'BE', 'DE', 'IT', 'ES', 'PT', 'BR', 'SE', 'FR'); 
  345. break; 
  346. case "mastercard": 
  347. $supported = array('NL', 'BE', 'DE', 'IT', 'ES', 'PT', 'BR', 'SE', 'FR'); 
  348. break; 
  349. case "visa": 
  350. $supported = array('NL', 'BE', 'DE', 'IT', 'ES', 'PT', 'BR', 'SE', 'FR'); 
  351. break; 
  352. default: 
  353. return "NL"; 
  354. break; 
  355.  
  356. $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); 
  357. $lang = strtoupper($lang); 
  358.  
  359. $lang = (!isset($lang) || $lang == "") ? $countryIso : $lang; 
  360.  
  361. if($lang == "") 
  362. return "US"; 
  363. if(in_array($lang, $supported)) 
  364. return $lang; 
  365. else 
  366. return 'US';