BxStripeConnectTemplate.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 StripeConnect Stripe Connect
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. require_once('BxStripeConnectApi.php');
  12. class BxStripeConnectTemplate extends BxBaseModGeneralTemplate
  13. {
  14. public function __construct($oConfig, $oDb)
  15. {
  16. $this->MODULE = 'bx_stripe_connect';
  17. parent::__construct($oConfig, $oDb);
  18. }
  19. public function getConnectCode($iVendorId, $aParams = [])
  20. {
  21. $CNF = &$this->_oConfig->CNF;
  22. $sJsObject = $this->_oConfig->getJsObject('main');
  23. $sModeUc = strtoupper($this->_oConfig->getMode());
  24. $sAccIdField = $CNF['FIELD_' . $sModeUc . '_ACCOUNT_ID'];
  25. $sAccDetailsField = $CNF['FIELD_' . $sModeUc . '_DETAILS'];
  26. $aApiButtons = [];
  27. $bShowContinue = false;
  28. $sActionMethod = $sActionTitle = '';
  29. if(($aAccount = $this->_oDb->getAccount(['sample' => 'profile_id', 'profile_id' => $iVendorId])) && is_array($aAccount) && $aAccount[$sAccIdField] != '') {
  30. if(($bShowContinue = (int)$aAccount[$sAccDetailsField] == 0)) {
  31. $oAccount = BxStripeConnectApi::getInstance()->retrieveAccount($aAccount[$sAccIdField]);
  32. if($oAccount->details_submitted) {
  33. $this->_oDb->updateAccount([$sAccDetailsField => 1], [$CNF['FIELD_ID'] => $aAccount[$CNF['FIELD_ID']]]);
  34. $bShowContinue = false;
  35. }
  36. }
  37. $sActionMethod = 'accountDelete';
  38. $sActionTitle = '_bx_stripe_connect_btn_account_delete';
  39. if($this->_bIsApi) {
  40. if($bShowContinue)
  41. $aApiButtons[] = [
  42. 'title' => _t('_bx_stripe_connect_btn_account_continue'),
  43. 'callback' => '/api.php?r=' . $this->MODULE . '/account_continue&params[0]=' . $iVendorId
  44. ];
  45. $aApiButtons[] = [
  46. 'title' => _t($sActionTitle),
  47. 'callback' => '/api.php?r=' . $this->MODULE . '/account_delete&params[0]=' . $iVendorId
  48. ];
  49. }
  50. }
  51. else {
  52. $sActionMethod = 'accountCreate';
  53. $sActionTitle = '_bx_stripe_connect_btn_account_create';
  54. if($this->_bIsApi)
  55. $aApiButtons[] = [
  56. 'title' => _t($sActionTitle),
  57. 'callback' => '/api.php?r=' . $this->MODULE . '/account_create&params[0]=' . $iVendorId
  58. ];
  59. }
  60. if($this->_bIsApi)
  61. return [
  62. 'type' => 'stripe_connect',
  63. 'content' => $aApiButtons
  64. ];
  65. $this->addJs(['main.js']);
  66. return $this->parseHtmlByName('connect_code.html', [
  67. 'js_object' => $sJsObject,
  68. 'js_code' => $this->getJsCode('main'),
  69. 'profile_id' => $iVendorId,
  70. 'action_method' => $sActionMethod,
  71. 'action_title' => _t($sActionTitle),
  72. 'bx_if:show_continue' => [
  73. 'condition' => $bShowContinue,
  74. 'content' => [
  75. 'js_object' => $sJsObject,
  76. 'profile_id' => $iVendorId,
  77. ]
  78. ]
  79. ]);
  80. }
  81. public function getJsCodeEmbeds($iProfileId)
  82. {
  83. $this->addJs([
  84. 'https://connect-js.stripe.com/v1.0/connect.js',
  85. 'embeds.js'
  86. ]);
  87. return $this->getJsCode('embeds', [
  88. 'aHtmlIds' => $this->_oConfig->getHtmlIds(),
  89. 'sPublicKey' => $this->_oConfig->getApiPublicKey(),
  90. 'iProfileId' => $iProfileId
  91. ], [
  92. 'wrap' => true,
  93. 'mask' => "if(window['{object}'] == undefined) {var} {object} = new {class}({params});"
  94. ]);
  95. }
  96. public function getBlockReportingChart()
  97. {
  98. $sEmbed = 'reporting-chart';
  99. return $this->parseHtmlByName('embed_block.html', [
  100. 'js_object' => $this->_oConfig->getJsObject('embeds'),
  101. 'html_id' => $this->_oConfig->getHtmlIds($sEmbed),
  102. 'embed' => $sEmbed
  103. ]);
  104. }
  105. public function getBlockPayments()
  106. {
  107. $sEmbed = 'payments';
  108. return $this->parseHtmlByName('embed_block.html', [
  109. 'js_object' => $this->_oConfig->getJsObject('embeds'),
  110. 'html_id' => $this->_oConfig->getHtmlIds($sEmbed),
  111. 'embed' => $sEmbed
  112. ]);
  113. }
  114. public function getBlockBalances()
  115. {
  116. $sEmbed = 'balances';
  117. return $this->parseHtmlByName('embed_block.html', [
  118. 'js_object' => $this->_oConfig->getJsObject('embeds'),
  119. 'html_id' => $this->_oConfig->getHtmlIds($sEmbed),
  120. 'embed' => $sEmbed
  121. ]);
  122. }
  123. public function getBlockNotifications()
  124. {
  125. $sEmbed = 'notification-banner';
  126. return $this->parseHtmlByName('embed_block.html', [
  127. 'js_object' => $this->_oConfig->getJsObject('embeds'),
  128. 'html_id' => $this->_oConfig->getHtmlIds($sEmbed),
  129. 'embed' => $sEmbed
  130. ]);
  131. }
  132. public function displayProfileLink($mixedProfile)
  133. {
  134. if(!is_array($mixedProfile))
  135. $mixedProfile = BxDolModule::getInstance($this->MODULE)->getProfileInfo((int)$mixedProfile);
  136. return $this->parseHtmlByName('link.html', array(
  137. 'href' => $mixedProfile['link'],
  138. 'title' => bx_html_attribute(!empty($mixedProfile['title']) ? $mixedProfile['title'] : $mixedProfile['name']),
  139. 'content' => $mixedProfile['name']
  140. ));
  141. }
  142. }
  143. /** @} */