BxBaseModPaymentProvider.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 $_iVendor;
  27. protected $_aOptions;
  28. protected $_bUseSsl;
  29. protected $_bRedirectOnResult;
  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->_aSbsStatuses = array();
  47. $this->_iVendor = 0;
  48. if(!empty($aConfig['vendor']))
  49. $this->_iVendor = (int)$aConfig['vendor'];
  50. $this->_aOptions = array();
  51. if(!empty($aConfig['options']) && is_array($aConfig['options']))
  52. $this->initOptions($aConfig['options']);
  53. }
  54. public function initOptions($aOptions)
  55. {
  56. $this->_aOptions = $aOptions;
  57. /*
  58. * Note: Any settings related to Vendor Options
  59. * should be initialized here.
  60. */
  61. }
  62. public function initOptionsByVendor($iVendorId)
  63. {
  64. $this->_iVendor = (int)$iVendorId;
  65. $aOptions = $this->_oModule->_oDb->getOptions($this->_iVendor, $this->_iId);
  66. if(!empty($aOptions) && is_array($aOptions))
  67. $this->initOptions($aOptions);
  68. }
  69. public function isActive()
  70. {
  71. return $this->getOption('active') == 'on';
  72. }
  73. public function isHidden()
  74. {
  75. return $this->getOption('hidden') == 'on';
  76. }
  77. public function getOption($sName)
  78. {
  79. if(substr($sName, 0, strlen($this->_sPrefix)) != $this->_sPrefix)
  80. $sName = $this->_sPrefix . $sName;
  81. return isset($this->_aOptions[$sName]) ? $this->_aOptions[$sName]['value'] : '';
  82. }
  83. public function setOption($sName, $mixedValue, $bSave = false)
  84. {
  85. if(substr($sName, 0, strlen($this->_sPrefix)) != $this->_sPrefix)
  86. $sName = $this->_sPrefix . $sName;
  87. if(empty($this->_aOptions[$sName]) || !is_array($this->_aOptions[$sName]))
  88. $this->_aOptions[$sName] = array('name' => $sName, 'value' => '');
  89. $this->_aOptions[$sName]['value'] = $mixedValue;
  90. if($bSave && !empty($this->_iVendor)) {
  91. $aOption = $this->_oModule->_oDb->getOption(array('type' => 'by_pid_and_name', 'provider_id' => $this->_iId, 'name' => $sName));
  92. if(!empty($aOption) && is_array($aOption))
  93. $this->_oModule->_oDb->updateOption($this->_iVendor, $aOption['id'], $mixedValue);
  94. }
  95. return true;
  96. }
  97. public function getReturnUrl($aParams = array())
  98. {
  99. return $this->_oModule->_oConfig->getUrl('URL_RETURN', $aParams, $this->_bUseSsl);
  100. }
  101. public function getReturnDataUrl($iVendorId, $aParams = array())
  102. {
  103. $sUrl = $this->_oModule->_oConfig->getUrl('URL_RETURN_DATA', array(), $this->_bUseSsl) . $this->_sName . '/' . $iVendorId;
  104. return bx_append_url_params($sUrl, $aParams);
  105. }
  106. public function getNotifyUrl($iVendorId, $aParams = array())
  107. {
  108. $sUrl = $this->_oModule->_oConfig->getUrl('URL_NOTIFY', array(), $this->_bUseSsl) . $this->_sName . '/' . $iVendorId;
  109. return bx_append_url_params($sUrl, $aParams);
  110. }
  111. /**
  112. * TODO: Check whether the method is needed or not.
  113. * Is used on success only.
  114. */
  115. public function needRedirect()
  116. {
  117. return $this->_bRedirectOnResult;
  118. }
  119. public function addJsCss() {}
  120. public function getJsObject()
  121. {
  122. return $this->_oModule->_oConfig->getJsObject($this->_sName);
  123. }
  124. public function finalizedCheckout() {}
  125. public function isSubscriptionStatus($sStatus, $aSubscription, $sStatusKey = 'status')
  126. {
  127. return isset($this->_aSbsStatuses[$aSubscription[$sStatusKey]]) && $this->_aSbsStatuses[$aSubscription[$sStatusKey]] == $sStatus;
  128. }
  129. public function getSubscription($iPendingId, $sCustomerId, $sSubscriptionId)
  130. {
  131. return array();
  132. }
  133. protected function getOptionsByPending($iPendingId)
  134. {
  135. $aPending = $this->_oModule->_oDb->getPending(array(
  136. 'type' => 'id',
  137. 'id' => (int)$iPendingId
  138. ));
  139. return $this->_oModule->_oDb->getOptions((int)$aPending['seller_id'], $this->_iId);
  140. }
  141. protected function getOptionsByVendor($iVendorId)
  142. {
  143. return $this->_oModule->_oDb->getOptions((int)$iVendorId, $this->_iId);
  144. }
  145. /**
  146. *
  147. * Writes $contents to a log file specified in the bp_options file or, if missing,
  148. * defaults to a standard filename of 'bplog.txt'.
  149. *
  150. * @param mixed $contents
  151. * @return
  152. * @throws Exception $e
  153. *
  154. */
  155. protected function log($mixedContents, $sTitle = '')
  156. {
  157. $this->_oModule->log($mixedContents, $this->_sName, $sTitle);
  158. }
  159. }
  160. /** @} */