BxBaseModPaymentProvider.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /**
  31. * Is needed to translate status received from Payment Provider
  32. * to internal one.
  33. */
  34. protected $_aSbsStatuses;
  35. function __construct($aConfig)
  36. {
  37. parent::__construct();
  38. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  39. $this->_sLangsPrefix = $this->_oModule->_oConfig->getPrefix('langs');
  40. $this->_bRedirectOnResult = false;
  41. $this->_bUseSsl = false;
  42. $this->_iId = (int)$aConfig['id'];
  43. $this->_sName = $aConfig['name'];
  44. $this->_sCaption = _t($aConfig['caption']);
  45. $this->_sPrefix = $aConfig['option_prefix'];
  46. $this->_sLogFile = BX_DIRECTORY_PATH_LOGS . 'bx_pp_' . $this->_sName . '.log';
  47. $this->_aSbsStatuses = array();
  48. $this->_aOptions = array();
  49. if(!empty($aConfig['options']) && is_array($aConfig['options']))
  50. $this->setOptions($aConfig['options']);
  51. }
  52. public function setOptions($aOptions)
  53. {
  54. $this->_aOptions = $aOptions;
  55. /*
  56. * Note: Any settings related to Vendor Options
  57. * should be initialized here.
  58. */
  59. }
  60. public function setOptionsByVendor($iVendorId)
  61. {
  62. $aOptions = $this->_oModule->_oDb->getOptions((int)$iVendorId, $this->_iId);
  63. if(!empty($aOptions) && is_array($aOptions))
  64. $this->setOptions($aOptions);
  65. }
  66. public function isActive()
  67. {
  68. return $this->getOption('active') == 'on';
  69. }
  70. public function isHidden()
  71. {
  72. return $this->getOption('hidden') == 'on';
  73. }
  74. public function getOption($sName)
  75. {
  76. if(substr($sName, 0, strlen($this->_sPrefix)) != $this->_sPrefix)
  77. $sName = $this->_sPrefix . $sName;
  78. return isset($this->_aOptions[$sName]) ? $this->_aOptions[$sName]['value'] : '';
  79. }
  80. public function getReturnUrl($aParams = array())
  81. {
  82. return $this->_oModule->_oConfig->getUrl('URL_RETURN', $aParams, $this->_bUseSsl);
  83. }
  84. public function getReturnDataUrl($iVendorId, $aParams = array())
  85. {
  86. $sUrl = $this->_oModule->_oConfig->getUrl('URL_RETURN_DATA', array(), $this->_bUseSsl) . $this->_sName . '/' . $iVendorId;
  87. return bx_append_url_params($sUrl, $aParams);
  88. }
  89. public function getNotifyUrl($iVendorId, $aParams = array())
  90. {
  91. $sUrl = $this->_oModule->_oConfig->getUrl('URL_NOTIFY', array(), $this->_bUseSsl) . $this->_sName . '/' . $iVendorId;
  92. return bx_append_url_params($sUrl, $aParams);
  93. }
  94. /**
  95. * TODO: Check whether the method is needed or not.
  96. * Is used on success only.
  97. */
  98. public function needRedirect()
  99. {
  100. return $this->_bRedirectOnResult;
  101. }
  102. public function addJsCss() {}
  103. public function finalizedCheckout() {}
  104. public function getSubscription($iPendingId, $sCustomerId, $sSubscriptionId)
  105. {
  106. return array();
  107. }
  108. protected function getOptionsByPending($iPendingId)
  109. {
  110. $aPending = $this->_oModule->_oDb->getPending(array(
  111. 'type' => 'id',
  112. 'id' => (int)$iPendingId
  113. ));
  114. return $this->_oModule->_oDb->getOptions((int)$aPending['seller_id'], $this->_iId);
  115. }
  116. protected function getOptionsByVendor($iVendorId)
  117. {
  118. return $this->_oModule->_oDb->getOptions((int)$iVendorId, $this->_iId);
  119. }
  120. /**
  121. *
  122. * Writes $contents to a log file specified in the bp_options file or, if missing,
  123. * defaults to a standard filename of 'bplog.txt'.
  124. *
  125. * @param mixed $contents
  126. * @return
  127. * @throws Exception $e
  128. *
  129. */
  130. protected function log($sContents)
  131. {
  132. try {
  133. if($this->_sLogFile != '')
  134. $file = $this->_sLogFile;
  135. else
  136. $file = dirname(__FILE__) . '/bx_pp_' . $this->_sName . '.log';
  137. file_put_contents($file, date('m-d H:i:s').": ", FILE_APPEND);
  138. if (is_array($sContents))
  139. $sContents = var_export($sContents, true);
  140. else if (is_object($sContents))
  141. $sContents = json_encode($sContents);
  142. file_put_contents($file, $sContents."\n", FILE_APPEND);
  143. }
  144. catch (Exception $e) {
  145. echo 'Error: ' . $e->getMessage();
  146. }
  147. }
  148. }
  149. /** @} */