1
0

BxBaseModPaymentProvider.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 isHidden()
  65. {
  66. return $this->getOption('hidden') == 'on';
  67. }
  68. public function getOption($sName)
  69. {
  70. if(substr($sName, 0, strlen($this->_sPrefix)) != $this->_sPrefix)
  71. $sName = $this->_sPrefix . $sName;
  72. return isset($this->_aOptions[$sName]) ? $this->_aOptions[$sName]['value'] : '';
  73. }
  74. public function getReturnUrl($aParams = array())
  75. {
  76. return $this->_oModule->_oConfig->getUrl('URL_RETURN', $aParams, $this->_bUseSsl);
  77. }
  78. public function getReturnDataUrl($iVendorId, $aParams = array())
  79. {
  80. $sUrl = $this->_oModule->_oConfig->getUrl('URL_RETURN_DATA', array(), $this->_bUseSsl) . $this->_sName . '/' . $iVendorId;
  81. return bx_append_url_params($sUrl, $aParams);
  82. }
  83. public function getNotifyUrl($iVendorId, $aParams = array())
  84. {
  85. $sUrl = $this->_oModule->_oConfig->getUrl('URL_NOTIFY', array(), $this->_bUseSsl) . $this->_sName . '/' . $iVendorId;
  86. return bx_append_url_params($sUrl, $aParams);
  87. }
  88. /**
  89. * TODO: Check whether the method is needed or not.
  90. * Is used on success only.
  91. */
  92. public function needRedirect()
  93. {
  94. return $this->_bRedirectOnResult;
  95. }
  96. public function addJsCss() {}
  97. public function finalizedCheckout() {}
  98. protected function getOptionsByPending($iPendingId)
  99. {
  100. $aPending = $this->_oModule->_oDb->getPending(array(
  101. 'type' => 'id',
  102. 'id' => (int)$iPendingId
  103. ));
  104. return $this->_oModule->_oDb->getOptions((int)$aPending['seller_id'], $this->_iId);
  105. }
  106. protected function getOptionsByVendor($iVendorId)
  107. {
  108. return $this->_oModule->_oDb->getOptions((int)$iVendorId, $this->_iId);
  109. }
  110. /**
  111. *
  112. * Writes $contents to a log file specified in the bp_options file or, if missing,
  113. * defaults to a standard filename of 'bplog.txt'.
  114. *
  115. * @param mixed $contents
  116. * @return
  117. * @throws Exception $e
  118. *
  119. */
  120. protected function log($sContents)
  121. {
  122. try {
  123. if($this->_sLogFile != '')
  124. $file = $this->_sLogFile;
  125. else
  126. $file = dirname(__FILE__) . '/bx_pp_' . $this->_sName . '.log';
  127. file_put_contents($file, date('m-d H:i:s').": ", FILE_APPEND);
  128. if (is_array($sContents))
  129. $sContents = var_export($sContents, true);
  130. else if (is_object($sContents))
  131. $sContents = json_encode($sContents);
  132. file_put_contents($file, $sContents."\n", FILE_APPEND);
  133. }
  134. catch (Exception $e) {
  135. echo 'Error: ' . $e->getMessage();
  136. }
  137. }
  138. }
  139. /** @} */