BxBaseModPaymentProvider.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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->_iId = (int)$aConfig['id'];
  36. $this->_sName = $aConfig['name'];
  37. $this->_sCaption = _t($aConfig['caption']);
  38. $this->_sPrefix = $aConfig['option_prefix'];
  39. $this->_aOptions = !empty($aConfig['options']) ? $aConfig['options'] : array();
  40. $this->_bUseSsl = false;
  41. $this->_bRedirectOnResult = false;
  42. }
  43. public function setOptionsByVendor($iVendorId)
  44. {
  45. $this->_aOptions = $this->_oModule->_oDb->getOptions((int)$iVendorId, $this->_iId);
  46. }
  47. public function isActive()
  48. {
  49. return $this->getOption('active') == 'on';
  50. }
  51. public function getOption($sName)
  52. {
  53. if(substr($sName, 0, strlen($this->_sPrefix)) != $this->_sPrefix)
  54. $sName = $this->_sPrefix . $sName;
  55. return isset($this->_aOptions[$sName]) ? $this->_aOptions[$sName]['value'] : '';
  56. }
  57. public function getReturnUrl($aParams = array())
  58. {
  59. return $this->_oModule->_oConfig->getUrl('URL_RETURN', $aParams, $this->_bUseSsl);
  60. }
  61. public function getReturnDataUrl($iVendorId, $aParams = array())
  62. {
  63. $sUrl = $this->_oModule->_oConfig->getUrl('URL_RETURN_DATA', array(), $this->_bUseSsl) . $this->_sName . '/' . $iVendorId;
  64. return bx_append_url_params($sUrl, $aParams);
  65. }
  66. public function getNotifyUrl($iVendorId, $aParams = array())
  67. {
  68. $sUrl = $this->_oModule->_oConfig->getUrl('URL_NOTIFY', array(), $this->_bUseSsl) . $this->_sName . '/' . $iVendorId;
  69. return bx_append_url_params($sUrl, $aParams);
  70. }
  71. /**
  72. * TODO: Check whether the method is needed or not.
  73. * Is used on success only.
  74. */
  75. public function needRedirect()
  76. {
  77. return $this->_bRedirectOnResult;
  78. }
  79. public function addJsCss() {}
  80. public function finalizedCheckout() {}
  81. protected function getOptionsByPending($iPendingId)
  82. {
  83. $aPending = $this->_oModule->_oDb->getPending(array(
  84. 'type' => 'id',
  85. 'id' => (int)$iPendingId
  86. ));
  87. return $this->_oModule->_oDb->getOptions((int)$aPending['seller_id'], $this->_iId);
  88. }
  89. protected function getOptionsByVendor($iVendorId)
  90. {
  91. return $this->_oModule->_oDb->getOptions((int)$iVendorId, $this->_iId);
  92. }
  93. /**
  94. *
  95. * Writes $contents to a log file specified in the bp_options file or, if missing,
  96. * defaults to a standard filename of 'bplog.txt'.
  97. *
  98. * @param mixed $contents
  99. * @return
  100. * @throws Exception $e
  101. *
  102. */
  103. protected function log($sContents)
  104. {
  105. try {
  106. if($this->_sLogFile != '')
  107. $file = $this->_sLogFile;
  108. else
  109. $file = dirname(__FILE__) . '/bx_pp_' . $this->_sName . '.log';
  110. file_put_contents($file, date('m-d H:i:s').": ", FILE_APPEND);
  111. if (is_array($sContents))
  112. $sContents = var_export($sContents, true);
  113. else if (is_object($sContents))
  114. $sContents = json_encode($sContents);
  115. file_put_contents($file, $sContents."\n", FILE_APPEND);
  116. }
  117. catch (Exception $e) {
  118. echo 'Error: ' . $e->getMessage();
  119. }
  120. }
  121. }
  122. /** @} */