AbstractPaymentGateway.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ WdpA7GYUyKAazQ+ylMumSrEw2NrSq6I8545WR20o/6uePsPkriFe27vLNLvaIS0hfcJWOFGlmVF6N4FZnp45MA==
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.7
  11. */
  12. /**
  13. * payment gateway model abstract class
  14. *
  15. * IMPORTANT: all methods that extend this class must have the name identical with the
  16. * 'name' field in the payment_gateways table.
  17. */
  18. namespace Ppb\Model\PaymentGateway;
  19. use Ppb\Db\Table\Row\AbstractRow,
  20. Cube\Controller\Request\AbstractRequest,
  21. Cube\Controller\Front,
  22. Cube\View,
  23. Ppb\Service;
  24. abstract class AbstractPaymentGateway extends AbstractRow
  25. {
  26. /**
  27. * constants that define the module/controller and actions for various urls that need to be generated by the
  28. * payment gateways models (ipn/success/failure)
  29. */
  30. const MODULE = 'app';
  31. const CONTROLLER = 'payment';
  32. const ACTION_COMPLETED = 'completed';
  33. const ACTION_FAILED = 'failed';
  34. const ACTION_IPN = 'ipn';
  35. /**
  36. *
  37. * payment gateway success url
  38. *
  39. * @var array
  40. */
  41. public static $successUrl = array(
  42. 'module' => self::MODULE,
  43. 'controller' => self::CONTROLLER,
  44. 'action' => self::ACTION_COMPLETED
  45. );
  46. /**
  47. *
  48. * payment gateway failure url
  49. *
  50. * @var array
  51. */
  52. public static $failureUrl = array(
  53. 'module' => self::MODULE,
  54. 'controller' => self::CONTROLLER,
  55. 'action' => self::ACTION_FAILED,
  56. );
  57. /**
  58. *
  59. * payment gateway ipn url
  60. *
  61. * @var array
  62. */
  63. public static $ipnUrl = array(
  64. 'module' => self::MODULE,
  65. 'controller' => self::CONTROLLER,
  66. 'action' => self::ACTION_IPN,
  67. 'gateway' => null,
  68. );
  69. /**
  70. *
  71. * the name of the payment gateway
  72. *
  73. * @var string
  74. */
  75. protected $_gatewayName;
  76. /**
  77. *
  78. * transaction id, required by the payment form
  79. *
  80. * @var int
  81. */
  82. protected $_transactionId;
  83. /**
  84. *
  85. * name (description) of the payment
  86. * can be an array with text and args (for translation purposes)
  87. *
  88. * @var string|array
  89. */
  90. protected $_name;
  91. /**
  92. *
  93. * payment amount, required by the payment form
  94. *
  95. * @var float
  96. */
  97. protected $_amount;
  98. /**
  99. *
  100. * payment currency, required by the payment form
  101. *
  102. * @var string
  103. */
  104. protected $_currency;
  105. /**
  106. *
  107. * settings array
  108. *
  109. * @var array
  110. */
  111. protected $_settings;
  112. /**
  113. *
  114. * payment gateway description
  115. *
  116. * @var string
  117. */
  118. protected $_description;
  119. /**
  120. *
  121. * gateway payment status response, provided by the ipn
  122. *
  123. * @var string
  124. */
  125. protected $_gatewayPaymentStatus;
  126. /**
  127. *
  128. * gateway transaction code, provided by the ipn
  129. *
  130. * @var string
  131. */
  132. protected $_gatewayTransactionCode;
  133. /**
  134. *
  135. * view object
  136. *
  137. * @var \Cube\View
  138. */
  139. protected $_view;
  140. /**
  141. *
  142. * class constructor
  143. *
  144. * @param int $gatewayName gateway name
  145. * @param int $userId user id, if we have a direct payment
  146. *
  147. * @throws \RuntimeException
  148. */
  149. public function __construct($gatewayName, $userId = null)
  150. {
  151. $gatewaysService = new Service\Table\PaymentGateways();
  152. $gateway = $gatewaysService->findBy('name', $gatewayName);
  153. if (!$gateway['id']) {
  154. $translate = $this->getTranslate();
  155. throw new \RuntimeException(
  156. sprintf($translate->_("The gateway you are trying to use, '%s', does not exist."), $gatewayName));
  157. }
  158. $this->setGatewayName($gatewayName);
  159. $data = array(
  160. 'table' => $gatewaysService->getTable(),
  161. 'data' => $gatewaysService->getData($userId, $gateway['id'], false, true),
  162. );
  163. parent::__construct($data);
  164. $this->_settings = Front::getInstance()->getBootstrap()->getResource('settings');
  165. }
  166. /**
  167. *
  168. * set gateway name
  169. *
  170. * @param string $gatewayName
  171. *
  172. * @return $this
  173. */
  174. public function setGatewayName($gatewayName)
  175. {
  176. $this->_gatewayName = $gatewayName;
  177. return $this;
  178. }
  179. /**
  180. *
  181. * get gateway name
  182. *
  183. * @return string
  184. */
  185. public function getGatewayName()
  186. {
  187. return $this->_gatewayName;
  188. }
  189. /**
  190. *
  191. * get transaction id
  192. *
  193. * @return int
  194. */
  195. public function getTransactionId()
  196. {
  197. return intval($this->_transactionId);
  198. }
  199. /**
  200. *
  201. * set transaction id
  202. *
  203. * @param int $transactionId
  204. *
  205. * @return \Ppb\Model\PaymentGateway\AbstractPaymentGateway
  206. */
  207. public function setTransactionId($transactionId)
  208. {
  209. $this->_transactionId = $transactionId;
  210. return $this;
  211. }
  212. /**
  213. *
  214. * get name of transaction
  215. *
  216. * @return string
  217. */
  218. public function getName()
  219. {
  220. return $this->_name;
  221. }
  222. /**
  223. *
  224. * set name of transaction
  225. * can be a string or an array with "string" and "args" keys - used for translations
  226. *
  227. * @param string|array $name
  228. *
  229. * @throws \InvalidArgumentException
  230. * @return \Ppb\Model\PaymentGateway\AbstractPaymentGateway
  231. */
  232. public function setName($name)
  233. {
  234. $translate = $this->getTranslate();
  235. if (is_array($name)) {
  236. if (!array_key_exists('string', $name) && !array_key_exists('args', $name)) {
  237. throw new \InvalidArgumentException("The 'name' array must contain the 'string' and 'args' keys.");
  238. }
  239. $string = (null !== $translate) ? $translate->_($name['string']) : $name['string'];
  240. $name = vsprintf($string, $name['args']);
  241. }
  242. if (null !== $translate) {
  243. $name = $translate->_($name);
  244. }
  245. $this->_name = $name;
  246. return $this;
  247. }
  248. /**
  249. *
  250. * get transaction amount
  251. *
  252. * @return float
  253. */
  254. public function getAmount()
  255. {
  256. return $this->_amount;
  257. }
  258. /**
  259. *
  260. * set transaction amount
  261. * convert all amounts to a standard format (eg: 12000.00)
  262. *
  263. * @param string $amount
  264. *
  265. * @return \Ppb\Model\PaymentGateway\AbstractPaymentGateway
  266. */
  267. public function setAmount($amount)
  268. {
  269. $this->_amount = number_format($amount, 2, '.', '');
  270. return $this;
  271. }
  272. /**
  273. *
  274. * get transaction currency
  275. *
  276. * @return string
  277. */
  278. public function getCurrency()
  279. {
  280. return $this->_currency;
  281. }
  282. /**
  283. *
  284. * set transaction currency
  285. *
  286. * @param string $currency
  287. *
  288. * @return \Ppb\Model\PaymentGateway\AbstractPaymentGateway
  289. */
  290. public function setCurrency($currency)
  291. {
  292. $this->_currency = $currency;
  293. return $this;
  294. }
  295. /**
  296. *
  297. * get gateway description string
  298. *
  299. * @return string
  300. */
  301. public function getDescription()
  302. {
  303. $translate = $this->getTranslate();
  304. if (null !== $translate) {
  305. return $translate->_($this->_description);
  306. }
  307. return $this->_description;
  308. }
  309. /**
  310. *
  311. * set gateway description string
  312. *
  313. * @param string $description
  314. *
  315. * @return \Ppb\Model\PaymentGateway\AbstractPaymentGateway
  316. */
  317. public function setDescription($description)
  318. {
  319. $this->_description = (string)$description;
  320. return $this;
  321. }
  322. /**
  323. *
  324. * get gateway payment status response
  325. *
  326. * @return string
  327. */
  328. public function getGatewayPaymentStatus()
  329. {
  330. return $this->_gatewayPaymentStatus;
  331. }
  332. /**
  333. *
  334. * set gateway payment status response
  335. *
  336. * @param string $gatewayPaymentStatus
  337. *
  338. * @return \Ppb\Model\PaymentGateway\AbstractPaymentGateway
  339. */
  340. public function setGatewayPaymentStatus($gatewayPaymentStatus)
  341. {
  342. $this->_gatewayPaymentStatus = (string)$gatewayPaymentStatus;
  343. return $this;
  344. }
  345. /**
  346. *
  347. * get gateway transaction code (id), provided by the ipn
  348. *
  349. * @return string
  350. */
  351. public function getGatewayTransactionCode()
  352. {
  353. return $this->_gatewayTransactionCode;
  354. }
  355. /**
  356. *
  357. * set gateway transaction code (id)
  358. *
  359. * @param string $gatewayTransactionCode
  360. *
  361. * @return \Ppb\Model\PaymentGateway\AbstractPaymentGateway
  362. */
  363. public function setGatewayTransactionCode($gatewayTransactionCode)
  364. {
  365. $this->_gatewayTransactionCode = (string)$gatewayTransactionCode;
  366. return $this;
  367. }
  368. /**
  369. *
  370. * get view object
  371. *
  372. * @return \Cube\View
  373. */
  374. public function getView()
  375. {
  376. if (!$this->_view instanceof View) {
  377. $this->setView(
  378. Front::getInstance()->getBootstrap()->getResource('view'));
  379. }
  380. return $this->_view;
  381. }
  382. /**
  383. *
  384. * set view object
  385. *
  386. * @param \Cube\View $view
  387. *
  388. * @return $this
  389. */
  390. public function setView(View $view)
  391. {
  392. $this->_view = $view;
  393. return $this;
  394. }
  395. /**
  396. *
  397. * get form elements, used to create the form needed to add the gateway settings
  398. *
  399. * @return array
  400. */
  401. public function getElements()
  402. {
  403. return array();
  404. }
  405. /**
  406. *
  407. * prepare and return ipn url
  408. *
  409. * @return string
  410. */
  411. public function getIpnUrl()
  412. {
  413. $params = self::$ipnUrl;
  414. $params['gateway'] = strtolower($this->getGatewayName());
  415. return $this->getView()->url($params);
  416. }
  417. /**
  418. *
  419. * prepare and return payment success url
  420. *
  421. * @return string
  422. */
  423. public function getSuccessUrl()
  424. {
  425. $params = self::$successUrl;
  426. if ($this->_transactionId) {
  427. $params['transaction_id'] = $this->_transactionId;
  428. }
  429. return $this->getView()->url($params);
  430. }
  431. /**
  432. *
  433. * prepare and return payment failed url
  434. *
  435. * @return string
  436. */
  437. public function getFailureUrl()
  438. {
  439. $params = self::$failureUrl;
  440. if ($this->_transactionId) {
  441. $params['transaction_id'] = $this->_transactionId;
  442. }
  443. return $this->getView()->url($params);
  444. }
  445. /**
  446. *
  447. * method that checks if the amount and currency submitted through an ipn is the
  448. * coincides with the row in the transactions table
  449. *
  450. * @param float $amount
  451. * @param string $currency
  452. *
  453. * @return bool
  454. */
  455. public function checkIpnAmount($amount, $currency)
  456. {
  457. if ($this->_amount == $amount && $this->_currency == $currency) {
  458. return true;
  459. }
  460. return false;
  461. }
  462. /**
  463. *
  464. * shorten a string that exceeds the maximum allowed number of chars
  465. *
  466. * @param string $string
  467. * @param int $maxChars
  468. * @param string $replace
  469. *
  470. * @return string
  471. */
  472. protected function _shortenString($string, $maxChars, $replace = '...')
  473. {
  474. if (strlen($string) > $maxChars) {
  475. $maxChars -= strlen($replace);
  476. $string = substr($string, 0, $maxChars) . $replace;
  477. }
  478. return $string;
  479. }
  480. /**
  481. *
  482. * dummy function used as a placeholder for translatable sentences
  483. *
  484. * @param $string
  485. *
  486. * @return string
  487. */
  488. protected function _($string)
  489. {
  490. return $string;
  491. }
  492. /**
  493. *
  494. * check if the gateway is enabled
  495. */
  496. abstract public function enabled();
  497. /**
  498. *
  499. * form elements needed to create the payment form
  500. */
  501. abstract public function formElements();
  502. /**
  503. *
  504. * get the post url of the gateway form
  505. */
  506. abstract public function getPostUrl();
  507. /**
  508. *
  509. * @param \Cube\Controller\Request\AbstractRequest $request
  510. */
  511. abstract public function processIpn(AbstractRequest $request);
  512. }