tpayPaymentSzkwal

Class PaymentSzkwal.

Defined (1)

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

/includes/lib/src/_class_tpay/paymentSzkwal.php  
  1. class PaymentSzkwal 
  2. const APILOGIN = 'api_login'; 
  3. const APIPASS = 'api_password'; 
  4. const DATE = 'Y-m-d'; 
  5. const TITLE = 'title'; 
  6. const INVALIDRESPONSE = 'Invalid server response'; 
  7. const AMOUNT = 'amount'; 
  8. /** 
  9. * API login 
  10. * @var string 
  11. */ 
  12. protected $apiLogin = '[SZKWAL_LOGIN]'; 
  13. /** 
  14. * API password 
  15. * @var string 
  16. */ 
  17. protected $apiPass = '[SZKWAL_API_PASSWORD]'; 
  18. /** 
  19. * API hash 
  20. * @var string 
  21. */ 
  22. protected $apiHash = '[SZKWAL_API_HASH]'; 
  23. /** 
  24. * API partner unique address 
  25. * @var string 
  26. */ 
  27. protected $partnerUniqueAddress = '[SZKWAL_PARTNER_ADDRESS]'; 
  28. /** 
  29. * API title format 
  30. * @var string 
  31. */ 
  32. protected $titleFormat = '[SZKWAL_TITLE_FORMAT]'; 
  33. /** 
  34. * tpay payment url 
  35. * @var string 
  36. */ 
  37. protected $apiUrl = 'https://szkwal.tpay.com/'; 
  38. /** 
  39. * The list of possible errors returning from tpay servive 
  40. * @var array 
  41. */ 
  42. protected $errorCodes = array( 
  43. 'ERR01' => 'authorization failed',  
  44. 'ERR02' => 'required input empty',  
  45. 'ERR03' => 'incorrect title format',  
  46. 'ERR04' => 'title busy',  
  47. 'ERR05' => 'incorrect hash',  
  48. 'ERR06' => 'no such client',  
  49. 'ERR07' => 'malformed CSV',  
  50. 'ERR08' => 'no such package',  
  51. 'ERR09' => 'incorrect host',  
  52. 'ERR10' => 'incorrect email',  
  53. 'ERR11' => 'incorrect dates',  
  54. 'ERR12' => 'incorrect amount',  
  55. 'ERR13' => 'no such method',  
  56. 'ERR14' => 'Insufficient funds',  
  57. 'ERR15' => 'Incorrect client account number',  
  58. 'ERR99' => 'other error',  
  59. ); 
  60.  
  61. /** 
  62. * PaymentSzkwal class constructor 
  63. * @param string|bool $apiLogin API login 
  64. * @param string|bool $apiPass API password 
  65. * @param string|bool $apiHash API hash 
  66. * @param string|bool $partnerUniqueAddress API partner unique address 
  67. * @param string|bool $titleFormat API title format 
  68. */ 
  69. public function __construct($apiLogin = false, $apiPass = false, $apiHash = false,  
  70. $partnerUniqueAddress = false, $titleFormat = false) 
  71. if ($apiLogin !== false) { 
  72. $this->apiLogin = $apiLogin; 
  73. if ($apiPass !== false) { 
  74. $this->apiPass = $apiPass; 
  75. if ($apiHash !== false) { 
  76. $this->apiHash = $apiHash; 
  77. if ($partnerUniqueAddress !== false) { 
  78. $this->partnerUniqueAddress = $partnerUniqueAddress; 
  79. if ($titleFormat !== false) { 
  80. $this->titleFormat = $titleFormat; 
  81.  
  82. require_once(dirname(__FILE__) . '/util.php'); 
  83.  
  84. Util::loadClass('curl'); 
  85. Util::loadClass('exception'); 
  86. Util::loadClass('validate'); 
  87. Util::loadClass('lang'); 
  88. Util::checkVersionPHP(); 
  89.  
  90. /** 
  91. * Register a new client (method RegisterClient) - or update his data with UpdateClient . This method has to 
  92. * be used before Client makes his first payment. Bellow we will register a new customer Jan Nowak and his 
  93. * dedicated constant payment title, together with his bank account number. If Client will make a payment with 
  94. * incorrect title, SZKWAL can automatically connect the payment from that bank account to this Client. 
  95. * @param string $clientName customer name; up to 96 alphanumeric characters; 
  96. * @param string $clientEmail customer e-mail; up to 128 alphanumeric characters, must be a valid e-mail address; 
  97. * @param int $clientPhone customer phone; up to 32 numeric characters; 
  98. * @param string $crc optional field sent in notifications; up to 64 characters; 
  99. * @param int $clientAccount client account number; 26 digits 
  100. * @throws TException 
  101. * @return array 
  102. */ 
  103. public function registerClient($clientName, $clientEmail, $clientPhone, $crc, $clientAccount) 
  104.  
  105. $title = $this->generateTitle(); 
  106.  
  107. Util::log('SZKWal register client sha1 params', print_r(array( 
  108. 'cli_name' => $clientName,  
  109. 'cli_mail' => $clientEmail,  
  110. 'cli_phone' => $clientPhone,  
  111. static::TITLE => $title,  
  112. 'crc' => $crc,  
  113. 'account' => $clientAccount,  
  114. 'apiHash' => $this->apiHash,  
  115. ), true)); 
  116. $sha1 = sha1($clientName . $clientEmail . $clientPhone . $title . $crc . $clientAccount . $this->apiHash); 
  117.  
  118. $postData = array( 
  119. static::APILOGIN => $this->apiLogin,  
  120. static::APIPASS => $this->apiPass,  
  121. 'cli_name' => $clientName,  
  122. 'cli_email' => $clientEmail,  
  123. 'cli_phone' => $clientPhone,  
  124. static::TITLE => $title,  
  125. 'crc' => $crc,  
  126. 'cli_account' => $clientAccount,  
  127. 'hash' => $sha1,  
  128. ); 
  129.  
  130. Validate::validateConfig(Validate::PAYMENT_TYPE_SZKWAL, $postData); 
  131.  
  132. $res = $this->request('RegisterClient', $postData); 
  133.  
  134. $this->checkError($res); 
  135. preg_match_all('/<cli_id>([0-9]*)<\/cli_id>/', $res, $matchesCliId); 
  136.  
  137. if (isset($matchesCliId[1]) && isset($matchesCliId[1][0])) { 
  138. $clientID = (int)$matchesCliId[1][0]; 
  139. } else { 
  140. throw new TException(static::INVALIDRESPONSE); 
  141.  
  142. return array( 
  143. 'client_id' => $clientID,  
  144. static::TITLE => $title,  
  145. ); 
  146.  
  147. /** 
  148. * Generate new unique title 
  149. * @return string 
  150. * @todo 
  151. */ 
  152. public static function generateTitle() 
  153. return 'KIP' . substr(time(), 1); 
  154.  
  155. /** 
  156. * Send API request 
  157. * @param string $method method name 
  158. * @param array $postData post data 
  159. * @return mixed 
  160. */ 
  161. protected function request($method, $postData) 
  162. $url = $this->apiUrl . $this->partnerUniqueAddress . '/' . $method; 
  163.  
  164. return Curl::doCurlRequest($url, $postData); 
  165.  
  166. /** 
  167. * Check for error presence in response 
  168. * @param string $response 
  169. * @throws TException 
  170. */ 
  171. protected function checkError($response) 
  172. preg_match_all('/<error_code>(ERR[0-9]*)<\/error_code>/', $response, $matchesError); 
  173. if (isset($matchesError[1]) && isset($matchesError[1][0])) { 
  174. $errorCode = $matchesError[1][0]; 
  175. throw new TException($this->errorCodes[$errorCode]); 
  176.  
  177. /** 
  178. * Create HTML confirmation block with transaction info and merchant data 
  179. * @param string $title transaction title 
  180. * @param bool|float $amount transaction amount 
  181. * @param string $staticFilesURL static file URL 
  182. * @param string $merchantData merchant data to display 
  183. * @return string 
  184. * @throws TException 
  185. */ 
  186. public function getConfirmationBlock($title, $amount = false, $staticFilesURL = '', $merchantData = '') 
  187. $data = array( 
  188. static::TITLE => $title,  
  189. 'banks' => $this->getBanks(),  
  190. static::AMOUNT => $amount,  
  191. 'static_files_url' => $staticFilesURL,  
  192. 'merchant_data' => $merchantData 
  193. ); 
  194.  
  195. return Util::parseTemplate('szkwal/_tpl/confirmation', $data); 
  196.  
  197. /** 
  198. * Method used to receive information about all available bank channels. 
  199. * @throws TException 
  200. * @return mixed 
  201. */ 
  202. public function getBanks() 
  203. $postData = array( 
  204. static::APILOGIN => $this->apiLogin,  
  205. static::APIPASS => $this->apiPass,  
  206. ); 
  207. $res = $this->request('GetBanksData', $postData); 
  208. $this->checkError($res); 
  209.  
  210. preg_match_all('/<bank_list>(.*)<\/bank_list>/', $res, $matches); 
  211. if (isset($matches[1]) && isset($matches[1][0])) { 
  212. $data = json_decode($matches[1][0], true); 
  213. foreach ($data as &$d) { 
  214. $d['availability'] = json_decode($d['availability'], true); 
  215. } else { 
  216. throw new TException(static::INVALIDRESPONSE); 
  217.  
  218. return $data; 
  219.  
  220. /** 
  221. * Method used to change result URL where payment notifications will be send. 
  222. * @param string|bool $notifyURL notify url 
  223. * @throws TException 
  224. * @return bool 
  225. */ 
  226. public function changeSellerData($notifyURL) 
  227. $sha1 = sha1($notifyURL . $this->apiHash); 
  228. $postData = array( 
  229. static::APILOGIN => $this->apiLogin,  
  230. static::APIPASS => $this->apiPass,  
  231. 'notify_url' => $notifyURL,  
  232. 'hash' => $sha1,  
  233. ); 
  234. $res = $this->request('ChangeSellerData', $postData); 
  235. $this->checkError($res); 
  236.  
  237. if (strpos($res, '<result>correct</result>') !== -1) { 
  238. return true; 
  239. } else { 
  240. throw new TException(static::INVALIDRESPONSE); 
  241.  
  242. /** 
  243. * Method used to block/unblock payments for specific client. 
  244. * @param string $title client title according to agreed format 
  245. * @param int $status Type 1 to enable client, 0 to disable 
  246. * @return bool 
  247. */ 
  248. public function clientStatus($title, $status) 
  249. $sha1 = sha1($title . $status . $this->apiHash); 
  250.  
  251. $postData = array( 
  252. static::APILOGIN => $this->apiLogin,  
  253. static::APIPASS => $this->apiPass,  
  254. static::TITLE => $title,  
  255. 'status' => $status,  
  256. 'hash' => $sha1,  
  257. ); 
  258.  
  259. return $this->request('ClientStatus', $postData); 
  260.  
  261. /** 
  262. * Method sed to acquire report of incoming payments. Depending on input parameters, the function can 
  263. * return a list of payments for one client (providing cli_id or title) or list of all payments in the specified 
  264. * period. 
  265. * @param int $clientId 
  266. * @param string $title 
  267. * @param int $startTime time in unix timestamp format 
  268. * @param int|bool $endTime time in unix timestamp format, if false than now 
  269. * @return array 
  270. */ 
  271. public function paymentsReport($clientId, $title, $startTime, $endTime = false) 
  272. $postData = array( 
  273. static::APILOGIN => $this->apiLogin,  
  274. static::APIPASS => $this->apiPass,  
  275. ); 
  276. $postData['from'] = date(static::DATE, $startTime); 
  277. if ($clientId !== false) { 
  278. $postData['cli_id'] = $clientId; 
  279. if ($title !== false) { 
  280. $postData[static::TITLE] = $title; 
  281. $endTime = ($endTime !== false) ? $endTime : time(); 
  282. $postData['to'] = date(static::DATE, $endTime); 
  283.  
  284. $postData['hash'] = sha1( 
  285. $postData['cli_id'] . $postData[static::TITLE] . $postData['from'] . $postData['to'] . $this->apiHash 
  286. ); 
  287.  
  288. return $this->request('PaymentsReport', $postData); 
  289.  
  290. /** 
  291. * Simulate user payment in test mode 
  292. * @param string $title client/transaction title 
  293. * @param float $amount amount 
  294. * @return mixed 
  295. */ 
  296. public function registerIncome($title, $amount) 
  297. $postData = array( 
  298. static::APILOGIN => $this->apiLogin,  
  299. static::APIPASS => $this->apiPass,  
  300. static::TITLE => $title,  
  301. static::AMOUNT => $amount,  
  302. ); 
  303.  
  304. $postData['hash'] = sha1($title . $amount . $this->apiHash); 
  305.  
  306. return $this->request('RegisterIncome', $postData); 
  307.  
  308. /** 
  309. * Generate monthly report 
  310. * @param int $startTime time in unix timestamp format 
  311. * @param int|bool $endTime time in unix timestamp format, if false than now 
  312. * @return array 
  313. */ 
  314. public function monthlyReport($startTime, $endTime = false) 
  315. return $this->generateReport('MonthlyReport', $startTime, $endTime); 
  316.  
  317. /** 
  318. * Generate monthly report 
  319. * @param string $type generate daily or monthly report 
  320. * @param int $startTime time in unix timestamp format 
  321. * @param int|bool $endTime time in unix timestamp format, if false than now 
  322. * @return array 
  323. */ 
  324. private function generateReport($type, $startTime, $endTime) 
  325. $postData = array( 
  326. static::APILOGIN => $this->apiLogin,  
  327. static::APIPASS => $this->apiPass,  
  328. ); 
  329. $postData['from'] = date(static::DATE, $startTime); 
  330. if ($endTime !== false) { 
  331. $postData['to'] = date(static::DATE, $endTime); 
  332. } else { 
  333. $postData['to'] = date(static::DATE); 
  334.  
  335. $sha1 = sha1($postData['from'] . $postData['to'] . $this->apiHash); 
  336. $postData['hash'] = $sha1; 
  337.  
  338. return $this->request($type, $postData); 
  339.  
  340. /** 
  341. * Generate daily report 
  342. * @param int $startTime time in unix timestamp format 
  343. * @param int|bool $endTime time in unix timestamp format, if false than now 
  344. * @return array 
  345. */ 
  346. public function dailyReport($startTime, $endTime = false) 
  347. return $this->generateReport('DailyReport', $startTime, $endTime); 
  348.  
  349. /** 
  350. * Handle response from tpay server 
  351. * Check all required fields and sh1 check sum 
  352. * Parse variables to valid types 
  353. * @throws TException 
  354. * @return array 
  355. */ 
  356. public function handleNotification() 
  357. Util::log('szkwal notification', print_r(INPUT_POST, true)); 
  358. $res = Validate::getResponse(Validate::PAYMENT_TYPE_SZKWAL); 
  359.  
  360. echo '<?xml version="1.0" encoding="UTF-8"?> 
  361. <data> 
  362. <result>correct</result> 
  363. </data>'; 
  364.  
  365. return $res; 
  366.  
  367. /** 
  368. * Check md5 sum to confirm tpay response and value of payment amount 
  369. * @param string $sign sha1 checksum 
  370. * @param string $payId unique szkwal payment id 
  371. * @param string $notId unique szkwal notification id 
  372. * @param string $title payment title in agreed format 
  373. * @param string $crc additional client field 
  374. * @param float $amount amount of payment 
  375. * @throws TException 
  376. */ 
  377. public function validateSign($sign, $payId, $notId, $title, $crc, $amount) 
  378. Util::log('Szkwal sign check components', print_r(array( 
  379. 'sign' => $sign,  
  380. 'payId' => $payId,  
  381. 'noti_id' => $notId,  
  382. static::TITLE => $title,  
  383. 'crc' => $crc,  
  384. static::AMOUNT => $amount,  
  385. 'hash' => $this->apiHash,  
  386. ), true)); 
  387.  
  388. $amount = number_format($amount, 2, '.', ''); 
  389.  
  390. if ($sign !== sha1($payId . $notId . $title . $crc . $amount . $this->apiHash)) { 
  391. throw new TException('invalid checksum');