BxBaseModPaymentProvider.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup BasePayment Base classes for Payment like modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. interface iBxBaseModPaymentProvider
  12. {
  13. public function initializeCheckout($iPendingId, $aCartInfo);
  14. public function finalizeCheckout(&$aData);
  15. public function finalizedCheckout();
  16. }
  17. class BxBaseModPaymentProvider extends BxDol
  18. {
  19. protected $MODULE;
  20. protected $_oModule;
  21. protected $_sLangsPrefix;
  22. protected $_iId;
  23. protected $_sName;
  24. protected $_sCaption;
  25. protected $_sPrefix;
  26. protected $_aOptions;
  27. protected $_bUseSsl;
  28. protected $_bRedirectOnResult;
  29. protected $_sLogFile;
  30. function __construct($aConfig)
  31. {
  32. parent::__construct();
  33. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  34. $this->_sLangsPrefix = $this->_oModule->_oConfig->getPrefix('langs');
  35. $this->_bRedirectOnResult = false;
  36. $this->_bUseSsl = false;
  37. $this->_iId = (int)$aConfig['id'];
  38. $this->_sName = $aConfig['name'];
  39. $this->_sCaption = _t($aConfig['caption']);
  40. $this->_sPrefix = $aConfig['option_prefix'];
  41. $this->_sLogFile = BX_DIRECTORY_PATH_LOGS . 'bx_pp_' . $this->_sName . '.log';
  42. $this->_aOptions = array();
  43. if(!empty($aConfig['options']) && is_array($aConfig['options']))
  44. $this->setOptions($aConfig['options']);
  45. }
  46. public function setOptions($aOptions)
  47. {
  48. $this->_aOptions = $aOptions;
  49. /*
  50. * Note: Any settings related to Vendor Options
  51. * should be initialized here.
  52. */
  53. }
  54. public function setOptionsByVendor($iVendorId)
  55. {
  56. $aOptions = $this->_oModule->_oDb->getOptions((int)$iVendorId, $this->_iId);
  57. if(!empty($aOptions) && is_array($aOptions))
  58. $this->setOptions($aOptions);
  59. }
  60. public function isActive()
  61. {
  62. return $this->getOption('active') == 'on';
  63. }
  64. public function getOption($sName)
  65. {
  66. if(substr($sName, 0, strlen($this->_sPrefix)) != $this->_sPrefix)
  67. $sName = $this->_sPrefix . $sName;
  68. return isset($this->_aOptions[$sName]) ? $this->_aOptions[$sName]['value'] : '';
  69. }
  70. public function getReturnUrl($aParams = array())
  71. {
  72. return $this->_oModule->_oConfig->getUrl('URL_RETURN', $aParams, $this->_bUseSsl);
  73. }
  74. public function getReturnDataUrl($iVendorId, $aParams = array())
  75. {
  76. $sUrl = $this->_oModule->_oConfig->getUrl('URL_RETURN_DATA', array(), $this->_bUseSsl) . $this->_sName . '/' . $iVendorId;
  77. return bx_append_url_params($sUrl, $aParams);
  78. }
  79. public function getNotifyUrl($iVendorId, $aParams = array())
  80. {
  81. $sUrl = $this->_oModule->_oConfig->getUrl('URL_NOTIFY', array(), $this->_bUseSsl) . $this->_sName . '/' . $iVendorId;
  82. return bx_append_url_params($sUrl, $aParams);
  83. }
  84. /**
  85. * TODO: Check whether the method is needed or not.
  86. * Is used on success only.
  87. */
  88. public function needRedirect()
  89. {
  90. return $this->_bRedirectOnResult;
  91. }
  92. public function addJsCss() {}
  93. public function finalizedCheckout() {}
  94. protected function getOptionsByPending($iPendingId)
  95. {
  96. $aPending = $this->_oModule->_oDb->getPending(array(
  97. 'type' => 'id',
  98. 'id' => (int)$iPendingId
  99. ));
  100. return $this->_oModule->_oDb->getOptions((int)$aPending['seller_id'], $this->_iId);
  101. }
  102. protected function getOptionsByVendor($iVendorId)
  103. {
  104. return $this->_oModule->_oDb->getOptions((int)$iVendorId, $this->_iId);
  105. }
  106. /**
  107. *
  108. * Writes $contents to a log file specified in the bp_options file or, if missing,
  109. * defaults to a standard filename of 'bplog.txt'.
  110. *
  111. * @param mixed $contents
  112. * @return
  113. * @throws Exception $e
  114. *
  115. */
  116. protected function log($sContents)
  117. {
  118. try {
  119. if($this->_sLogFile != '')
  120. $file = $this->_sLogFile;
  121. else
  122. $file = dirname(__FILE__) . '/bx_pp_' . $this->_sName . '.log';
  123. file_put_contents($file, date('m-d H:i:s').": ", FILE_APPEND);
  124. if (is_array($sContents))
  125. $sContents = var_export($sContents, true);
  126. else if (is_object($sContents))
  127. $sContents = json_encode($sContents);
  128. file_put_contents($file, $sContents."\n", FILE_APPEND);
  129. }
  130. catch (Exception $e) {
  131. echo 'Error: ' . $e->getMessage();
  132. }
  133. }
  134. }
  135. /** @} */