tpayTransactionAPI

Class TransactionAPI.

Defined (1)

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

/includes/lib/src/_class_tpay/transactionApi.php  
  1. class TransactionAPI 
  2. const RESULT = 'result'; 
  3. const TITLE = 'title'; 
  4. const ERROR_CODE = 'error_code'; 
  5. const REPORT = 'report'; 
  6. const PACK_ID = 'pack_id'; 
  7. const ERR = 'err'; 
  8. const AMOUNT = 'amount'; 
  9. const RESULT_0_1_RESULT = '/<result>([0-1]*)<\/result>/'; 
  10. const PACKID = '/<pack_id>([0-1]*)<\/pack_id>/'; 
  11. const ERR_ERR = '/<err>(.*)<\/err>/'; 
  12. const ERROR_ERROR = '/<error>(.*)<\/error>/'; 
  13. const PACKS = 'packs'; 
  14. const TRANSFERS = 'transfers'; 
  15. /** 
  16. * Api key 
  17. * @var string 
  18. */ 
  19. private $apiKey; 
  20.  
  21. /** 
  22. * Api pass 
  23. * @var string 
  24. */ 
  25. private $apiPass; 
  26.  
  27. /** 
  28. * Merchant id 
  29. * @var int 
  30. */ 
  31. private $merchantId; 
  32.  
  33. /** 
  34. * Merchant secret 
  35. * @var string 
  36. */ 
  37. private $merchantSecret; 
  38.  
  39. /** 
  40. * tpay api url 
  41. * @var string 
  42. */ 
  43. private $apiURL = 'https://secure.tpay.com/api/gw/'; 
  44.  
  45. /** 
  46. * List of errors 
  47. * @var array 
  48. */ 
  49. private $errorCodes = array( 
  50. 'ERR44' => 'Invalid transaction id',  
  51. 'ERR51' => 'Can\'t create transaction for this channel',  
  52. 'ERR52' => 'Error create a transaction, try again later',  
  53. 'ERR53' => 'Invalid input data',  
  54. 'ERR54' => 'Transation with this id not exists',  
  55. 'ERR55' => 'Invalid range or format for dates',  
  56. 'ERR99' => 'General error',  
  57. 'ERR98' => 'Login error, invalid key or password',  
  58. 'ERR97' => 'No metod',  
  59. 'ERR31' => 'Access disabled',  
  60. 'ERR32' => 'Access forbidden',  
  61.  
  62. 'ERR96' => 'Invalid transaction id or can\'t make refund',  
  63.  
  64. 'ERR4' => 'Nie zosta* przes*any plik o rozszerzeniu csv',  
  65. 'ERR6' => 'Niepoprawna suma kontrolna (sign)',  
  66. 'ERR7' => 'Niepoprawny format linii',  
  67. 'ERR8' => 'Niepoprawny format numeru rachunku',  
  68. 'ERR9' => 'Nazwa odbiorcy nie mo*e by* pusta',  
  69. 'ERR10' => 'Nazwa odbiorcy 1 jest za d*uga - maks. 35 znakw',  
  70. 'ERR11' => 'Nazwa odbiorcy 2 jest za d*uga - maks. 35 znakw',  
  71. 'ERR12' => 'Nazwa odbiorcy 3 jest za d*uga - maks. 35 znakw',  
  72. 'ERR13' => 'Nazwa odbiorcy 4 jest za d*uga - maks. 35 znakw',  
  73. 'ERR14' => 'Niepoprawny format kwoty',  
  74. 'ERR15' => 'Pole tytu* 1 nie mo*e by* puste',  
  75. 'ERR16' => 'Pole tytu* 1 jest za d*ugie - maks. 35 znakw',  
  76. 'ERR17' => 'Pole tytu* 2 jest za d*ugie - maks. 35 znakw',  
  77. 'ERR18' => 'B**d wewn*trzny',  
  78. 'ERR19' => 'Nie uda*o si* za*adowa* pliku o rozszerzeniu csv',  
  79. 'ERR20' => 'B**d przetwarzania przeleww',  
  80. 'ERR21' => 'Niepoprawny packId lub nie znaleziono paczki',  
  81. 'ERR22' => 'B**d przy autoryzacji paczki',  
  82. 'ERR23' => 'Za ma*o *rodkw do autoryzacji paczki',  
  83. 'ERR24' => 'Paczka zosta*a ju* autoryzowana',  
  84.  
  85. ); 
  86.  
  87. /** 
  88. * PaymentTransactionAPI class constructor 
  89. * @param string $apiKey api key 
  90. * @param string $apiPass api password 
  91. * @param int $merchantId merchant id 
  92. * @param string $merchantSecret merchant secret 
  93. * @throws TException 
  94. */ 
  95. public function __construct($apiKey, $apiPass, $merchantId, $merchantSecret) 
  96. if (!is_string($apiKey) || strlen($apiKey) === 0) { 
  97. throw new TException('Invalid API key'); 
  98. if (!is_string($apiPass) || strlen($apiPass) === 0) { 
  99. throw new TException('Invalid API pass'); 
  100.  
  101. $this->merchantId = $merchantId; 
  102. $this->merchantSecret = $merchantSecret; 
  103. $this->apiKey = $apiKey; 
  104. $this->apiPass = $apiPass; 
  105.  
  106. require_once(dirname(__FILE__) . '/util.php'); 
  107.  
  108. Util::loadClass('curl'); 
  109. Util::loadClass('exception'); 
  110. Util::loadClass('validate'); 
  111. Util::loadClass('lang'); 
  112. Util::checkVersionPHP(); 
  113.  
  114. Validate::validateMerchantId($this->merchantId); 
  115. Validate::validateMerchantSecret($this->merchantSecret); 
  116.  
  117. /** 
  118. * Create new transaction 
  119. * More information about config fields @see Validate::$panelPaymentRequestField 
  120. * @param array $config transaction config 
  121. * @return array 
  122. * @throws TException 
  123. */ 
  124. public function create($config) 
  125. $url = $this->apiURL . $this->apiKey . '/transaction/create'; 
  126.  
  127. Validate::validateConfig(Validate::PAYMENT_TYPE_BASIC_API, $config); 
  128. $config = $this->prepareConfig($config); 
  129. $res = $this->requests($url, $config); 
  130.  
  131. $response = array( 
  132. static::RESULT => (int)Util::findSubstring(static::RESULT_0_1_RESULT, $res),  
  133. static::TITLE => Util::findSubstring('/<title>(.*)<\/title>/', $res),  
  134. static::AMOUNT => (float)Util::findSubstring('/<amount>([0-9\.]*)<\/amount>/', $res),  
  135. 'account_number' => Util::findSubstring('/<account_number>([0-9]*)<\/account_number>/', $res),  
  136. 'online' => (int)Util::findSubstring('/<online>([0-1]*)<\/online>/', $res),  
  137. 'url' => Util::findSubstring('/<url>(.*)<\/url>/', $res),  
  138. 'desc' => Util::findSubstring('/<desc>(.*)<\/desc>/', $res),  
  139. ); 
  140.  
  141. if ($response[static::RESULT] !== 1) { 
  142. throw new TException(sprintf('Error in %s', $response['desc'])); 
  143. return $response; 
  144.  
  145. /** 
  146. * Prepare and validate passed config 
  147. * @param array $config 
  148. * @return array 
  149. * @throws TException 
  150. */ 
  151. private function prepareConfig($config) 
  152. $ready = Validate::validateConfig(Validate::PAYMENT_TYPE_BASIC, $config); 
  153.  
  154. $ready['md5sum'] = md5($this->merchantId . $ready['kwota'] . $ready['crc'] . $this->merchantSecret); 
  155. $ready['id'] = $this->merchantId; 
  156.  
  157. return $ready; 
  158.  
  159. /** 
  160. * Execute request to tpay transaction API 
  161. * @param string $url url 
  162. * @param array $params post params 
  163. * @return bool|mixed 
  164. */ 
  165. private function requests($url, $params) 
  166. $params['api_password'] = $this->apiPass; 
  167.  
  168. return Curl::doCurlRequest($url, $params); 
  169.  
  170. public function blik($code, $title) 
  171. $config['code'] = $code; 
  172. $config['title'] = $title; 
  173. $url = $this->apiURL . $this->apiKey . '/transaction/blik'; 
  174.  
  175. $res = $this->requests($url, $config); 
  176. return array( 
  177. static::RESULT => (int)Util::findSubstring(static::RESULT_0_1_RESULT, $res),  
  178. ); 
  179.  
  180.  
  181. /** 
  182. * Get information about transaction 
  183. * @param string $transactionId transaction id 
  184. * @return array 
  185. * @throws TException 
  186. */ 
  187. public function get($transactionId) 
  188. $url = $this->apiURL . $this->apiKey . '/transaction/get'; 
  189.  
  190. $res = $this->requests($url, array(static::TITLE => $transactionId)); 
  191.  
  192. $response = array( 
  193. static::RESULT => (int)Util::findSubstring(static::RESULT_0_1_RESULT, $res),  
  194. 'status' => Util::findSubstring('/<status>(.*)<\/status>/', $res),  
  195. static::ERROR_CODE => Util::findSubstring('/<error_code>(.*)<\/error_code>/', $res),  
  196. static::ERR => Util::findSubstring(static::ERR_ERR, $res),  
  197. 'start_time' => Util::findSubstring('/<start_time>(.*)<\/start_time>/', $res),  
  198. 'payment_time' => Util::findSubstring('/<payment_time>(.*)<\/payment_time>/', $res),  
  199. 'chargeback_time' => Util::findSubstring('/<chargeback_time>(.*)<\/chargeback_time>/', $res),  
  200. 'channel' => (int)Util::findSubstring('/<channel>([0-9]*)<\/channel>/', $res),  
  201. 'test_mode' => (int)Util::findSubstring('/<test_mode>([0-1]*)<\/test_mode>/', $res),  
  202. static::AMOUNT => (float)Util::findSubstring('/<amount>([0-9\.]*)<\/amount>/', $res),  
  203. 'amount_paid' => (float)Util::findSubstring('/<amount_paid>([0-9\.]*)<\/amount_paid>/', $res),  
  204. 'name' => Util::findSubstring('/<name>(.*)<\/name>/', $res),  
  205. 'address' => Util::findSubstring('/<address>(.*)<\/address>/', $res),  
  206. 'code' => Util::findSubstring('/<code>(.*)<\/code>/', $res),  
  207. 'city' => Util::findSubstring('/<city>(.*)<\/city>/', $res),  
  208. 'email' => Util::findSubstring('/<email>(.*)<\/email>/', $res),  
  209. 'country' => Util::findSubstring('/<country>(.*)<\/country>/', $res),  
  210. ); 
  211.  
  212. $this->checkError($response); 
  213. return $response; 
  214.  
  215. /** 
  216. * Check api response error 
  217. * @param array $response 
  218. * @throws TException 
  219. */ 
  220. private function checkError($response) 
  221. if ($response[static::RESULT] !== 1) { 
  222. if (isset($response[static::ERR]) && isset($this->errorCodes[$response[static::ERR]])) { 
  223. throw new TException($this->errorCodes[$response[static::ERR]]); 
  224. } elseif (isset($response[static::ERROR_CODE]) && isset($this->errorCodes[$response[static::ERROR_CODE]])) { 
  225. throw new TException($this->errorCodes[$response[static::ERROR_CODE]]); 
  226. } else { 
  227. throw new TException('Unexpected error'); 
  228.  
  229. /** 
  230. * Get transactions report 
  231. * @param string $fromDate start date in format YYYY-MM-DD 
  232. * @param string|bool $toDate end date in format YYYY-MM-DD 
  233. * @return array 
  234. * @throws TException 
  235. */ 
  236. public function report($fromDate, $toDate = false) 
  237. $url = $this->apiURL . $this->apiKey . '/transaction/report'; 
  238. $postData = array( 
  239. 'from_date' => $fromDate 
  240. ); 
  241.  
  242. if ($toDate !== false) { 
  243. $postData['to_date'] = $toDate; 
  244.  
  245. $res = $this->requests($url, $postData); 
  246.  
  247. $response = array( 
  248. static::RESULT => (int)Util::findSubstring(static::RESULT_0_1_RESULT, $res),  
  249. static::REPORT => Util::findSubstring('/<report>(.*)<\/report>/', $res),  
  250. ); 
  251.  
  252. $this->checkError($response); 
  253. $response[static::REPORT] = base64_decode($response[static::REPORT]); 
  254. return $response; 
  255.  
  256.  
  257. /** 
  258. * Refund all amount to customer 
  259. * @param string $transactionId transaction id 
  260. * @return bool 
  261. * @throws TException 
  262. */ 
  263. public function refund($transactionId) 
  264. $url = $this->apiURL . $this->apiKey . '/chargeback/transaction'; 
  265.  
  266. $res = $this->requests($url, array(static::TITLE => $transactionId)); 
  267.  
  268. $response = array( 
  269. static::RESULT => (int)Util::findSubstring(static::RESULT_0_1_RESULT, $res),  
  270. static::ERR => Util::findSubstring(static::ERR_ERR, $res),  
  271. ); 
  272. $this->checkError($response); 
  273.  
  274. return true; 
  275.  
  276. /** 
  277. * Refund custom amount to customer 
  278. * @param string $transactionId transaction id 
  279. * @param float $amount refund amount 
  280. * @return bool 
  281. * @throws TException 
  282. */ 
  283. public function refundAny($transactionId, $amount) 
  284. $url = $this->apiURL . $this->apiKey . '/chargeback/any'; 
  285.  
  286. $postData = array( 
  287. static::TITLE => $transactionId,  
  288. 'chargeback_amount' => $amount,  
  289. ); 
  290. $res = $this->requests($url, $postData); 
  291.  
  292. $response = array( 
  293. static::RESULT => (int)Util::findSubstring(static::RESULT_0_1_RESULT, $res),  
  294. static::ERR => Util::findSubstring(static::ERR_ERR, $res),  
  295. ); 
  296. $this->checkError($response); 
  297.  
  298. return true; 
  299.  
  300. /** 
  301. * Create mass payment 
  302. * @param string $csv content of csv file 
  303. * @return array 
  304. * @throws TException 
  305. */ 
  306. public function masspaymentCreate($csv) 
  307. $url = $this->apiURL . $this->apiKey . '/masspayment/create'; 
  308.  
  309. $csvEncode = base64_encode($csv); 
  310.  
  311. $postData = array( 
  312. 'csv' => $csvEncode,  
  313. 'sign' => sha1($this->merchantId . $csv . $this->merchantSecret),  
  314. ); 
  315. $res = $this->requests($url, $postData); 
  316.  
  317. $response = array( 
  318. static::RESULT => (int)Util::findSubstring(static::RESULT_0_1_RESULT, $res),  
  319. 'count' => (int)Util::findSubstring('/<count>([0-9]*)<\/count>/', $res),  
  320. 'sum' => (float)Util::findSubstring('/<sum>([0-9\.]*)<\/sum>/', $res),  
  321. static::PACK_ID => Util::findSubstring('/<pack_id>(.*)<\/pack_id>/', $res),  
  322. 'referers' => Util::findSubstring('/<referers>(.*)<\/referers>/', $res),  
  323. static::ERR => Util::findSubstring(static::ERR_ERR, $res),  
  324. ); 
  325. $this->checkError($response); 
  326.  
  327. return $response; 
  328.  
  329. /** 
  330. * Authorize mass payment 
  331. * @param string $packId pack id from masspaymentCreate 
  332. * @return array 
  333. * @throws TException 
  334. */ 
  335. public function masspaymentAuthorize($packId) 
  336. $url = $this->apiURL . $this->apiKey . '/masspayment/authorize'; 
  337.  
  338. $postData = array( 
  339. static::PACK_ID => $packId,  
  340. ); 
  341. $res = $this->requests($url, $postData); 
  342.  
  343. $response = array( 
  344. static::RESULT => (int)Util::findSubstring(static::RESULT_0_1_RESULT, $res),  
  345. static::ERR => Util::findSubstring(static::ERROR_ERROR, $res),  
  346. ); 
  347. $this->checkError($response); 
  348.  
  349. return $response; 
  350.  
  351. /** 
  352. * Get information about packs 
  353. * @param string|bool $packId pack id from masspaymentCreate 
  354. * @param string|bool $fromDate start date in format YYYY-MM-DD 
  355. * @param string|bool $toDate end date in format YYYY-MM-DD 
  356. * @return array 
  357. * @throws TException 
  358. */ 
  359. public function masspaymentPacks($packId = false, $fromDate = false, $toDate = false) 
  360. $url = $this->apiURL . $this->apiKey . '/masspayment/packs'; 
  361.  
  362. $postData = array(); 
  363.  
  364. if ($packId !== false) { 
  365. $postData[static::PACK_ID] = $packId; 
  366. if ($fromDate !== false) { 
  367. $postData['fromDate'] = $fromDate; 
  368. if ($toDate !== false) { 
  369. $postData['toDate'] = $toDate; 
  370.  
  371. $res = $this->requests($url, $postData); 
  372.  
  373. $response = array( 
  374. static::RESULT => (int)Util::findSubstring(static::RESULT_0_1_RESULT, $res),  
  375. static::PACKS => sprintf('<packs>%s</packs>', Util::findSubstring('/<packs>(.*)<\/packs>/', $res)),  
  376. static::ERR => Util::findSubstring(static::ERROR_ERROR, $res),  
  377. ); 
  378. $this->checkError($response); 
  379. $xml = simplexml_load_string($response[static::PACKS]); 
  380. $response[static::PACKS] = unserialize(serialize(json_decode(json_encode((array)$xml), 1))); 
  381. return $response; 
  382.  
  383. /** 
  384. * Authorize mass payment 
  385. * @param string $packId pack id from masspaymentCreate 
  386. * @param string $trId transaction id 
  387. * @return array 
  388. * @throws TException 
  389. */ 
  390. public function masspaymentTransfers($packId, $trId) 
  391. $url = $this->apiURL . $this->apiKey . '/masspayment/transfers'; 
  392.  
  393. $postData = array( 
  394. static::PACK_ID => $packId,  
  395. 'trId' => $trId,  
  396. ); 
  397. $res = $this->requests($url, $postData); 
  398.  
  399. $response = array( 
  400. static::RESULT => (int)Util::findSubstring(static::RESULT_0_1_RESULT, $res),  
  401. static::TRANSFERS => sprintf('<transfers>%s</transfers>',  
  402. Util::findSubstring('/<transfers>(.*)<\/transfers>/', $res)),  
  403. static::ERR => Util::findSubstring(static::ERROR_ERROR, $res),  
  404. ); 
  405. $this->checkError($response); 
  406. $xml = simplexml_load_string($response[static::TRANSFERS]); 
  407. $response[static::TRANSFERS] = unserialize(serialize(json_decode(json_encode((array)$xml), 1))); 
  408. return $response;