BxBaseModPaymentProvider.php 6.0 KB

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