chargebee_v3.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup Payment Payment
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. function BxPaymentProviderChargebeeV3(oOptions) {
  11. this.init(oOptions);
  12. }
  13. BxPaymentProviderChargebeeV3.prototype = new BxPaymentMain();
  14. BxPaymentProviderChargebeeV3.prototype.init = function(oOptions) {
  15. this._sProvider = oOptions.sProvider;
  16. this._sActionsUrl = oOptions.sActionUrl;
  17. this._sObjName = oOptions.sObjName == undefined ? 'oPaymentProviderChargebeeV3' : oOptions.sObjName;
  18. this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'fade' : oOptions.sAnimationEffect;
  19. this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
  20. this._sObjNameCart = oOptions.sObjNameCart == undefined ? '' : oOptions.sObjNameCart;
  21. this._iClientId = oOptions.iClientId == undefined ? 0 : oOptions.iClientId;
  22. this._iSellerId = oOptions.iSellerId == undefined ? 0 : oOptions.iSellerId;
  23. //-- For Single payments only.
  24. this._aItems = oOptions.aItems == undefined ? {} : oOptions.aItems;
  25. //-- For Recurring payments only.
  26. this._iModuleId = oOptions.iModuleId == undefined ? 0 : oOptions.iModuleId;
  27. this._iItemId = oOptions.iItemId == undefined ? 0 : oOptions.iItemId;
  28. this._sItemName = oOptions.sItemName == undefined ? '' : oOptions.sItemName;
  29. this._iItemCount = oOptions.iItemCount == undefined ? 0 : oOptions.iItemCount;
  30. this._sItemAddons = oOptions.sItemAddons == undefined ? '' : oOptions.sItemAddons;
  31. this._sRedirect = oOptions.sRedirect == undefined ? '' : oOptions.sRedirect;
  32. this._sCustom = oOptions.sCustom == undefined ? '' : oOptions.sCustom;
  33. this._rHandler = Chargebee.init({
  34. site: oOptions.sSite
  35. });
  36. this._rHandler = Chargebee.getInstance();
  37. };
  38. BxPaymentProviderChargebeeV3.prototype.checkout = function(oLink) {
  39. return this.checkoutWithAddons(oLink);
  40. };
  41. BxPaymentProviderChargebeeV3.prototype.checkoutWithAddons = function(oLink, mixedAddons) {
  42. var $this = this;
  43. var oDate = new Date();
  44. oLink = jQuery(oLink);
  45. if(oLink.hasClass('bx-btn-disabled'))
  46. return;
  47. oLink.addClass('bx-btn-disabled');
  48. this._rHandler.openCheckout({
  49. hostedPage: function() {
  50. var oParams = {
  51. url: $this._sActionsUrl + 'call/' + $this._sProvider + '/get_hosted_page_single/' + $this._iClientId + '/' + $this._iSellerId + '/',
  52. dataType: 'json'
  53. };
  54. if(mixedAddons != undefined)
  55. oParams.data = {
  56. addons: mixedAddons
  57. };
  58. return $.post(oParams);
  59. },
  60. success: function(sHostedPageId) {
  61. $this.loadingInPopup(oLink, true);
  62. var oParams = {
  63. seller_id: $this._iSellerId,
  64. provider: $this._sProvider,
  65. items: $this._aItems,
  66. page_id: sHostedPageId,
  67. _t: oDate.getTime()
  68. };
  69. $.post(
  70. $this._sActionsUrl + 'initialize_checkout_json/',
  71. oParams,
  72. function(oData){
  73. $this.loadingInPopup(oLink, true);
  74. processJsonData(oData);
  75. },
  76. 'json'
  77. );
  78. },
  79. close: function() {
  80. oLink.removeClass('bx-btn-disabled');
  81. }
  82. });
  83. return false;
  84. };
  85. BxPaymentProviderChargebeeV3.prototype.subscribe = function(oLink) {
  86. return this.subscribeWithAddons(oLink);
  87. };
  88. BxPaymentProviderChargebeeV3.prototype.subscribeWithAddons = function(oLink, mixedAddons) {
  89. var $this = this;
  90. var oDate = new Date();
  91. oLink = jQuery(oLink);
  92. if(oLink.hasClass('bx-btn-disabled'))
  93. return;
  94. oLink.addClass('bx-btn-disabled');
  95. this._rHandler.openCheckout({
  96. hostedPage: function() {
  97. return $.post({
  98. url: $this._sActionsUrl + 'call/' + $this._sProvider + '/get_hosted_page_recurring/' + $this._iClientId + '/' + $this._iSellerId + '/' + $this._sItemName + '/',
  99. data: {
  100. addons: mixedAddons != undefined ? mixedAddons : $this._sItemAddons
  101. },
  102. dataType: 'json'
  103. });
  104. },
  105. success: function(sHostedPageId) {
  106. $this.loadingInPopup(oLink, true);
  107. var oParams = {
  108. seller_id: $this._iSellerId,
  109. seller_provider: $this._sProvider,
  110. module_id: $this._iModuleId,
  111. item_id: $this._iItemId,
  112. item_count: $this._iItemCount,
  113. item_addons: $this._sItemAddons,
  114. redirect: $this._sRedirect,
  115. custom: $this._sCustom,
  116. page_id: sHostedPageId,
  117. _t: oDate.getTime()
  118. };
  119. $.post(
  120. $this._sActionsUrl + 'subscribe_json/',
  121. oParams,
  122. function(oData){
  123. $this.loadingInPopup(oLink, true);
  124. processJsonData(oData);
  125. },
  126. 'json'
  127. );
  128. },
  129. close: function() {
  130. oLink.removeClass('bx-btn-disabled');
  131. }
  132. });
  133. return false;
  134. };
  135. BxPaymentProviderChargebeeV3.prototype.manage = function(oLink, iPendingId) {
  136. var $this = this;
  137. var oDate = new Date();
  138. $(".bx-popup-applied:visible").dolPopupHide();
  139. this._rHandler.setPortalSession(function() {
  140. return $.post({
  141. url: $this._sActionsUrl + 'call/' + $this._sProvider + '/get_portal/' + iPendingId + '/',
  142. dataType: 'json'
  143. });
  144. });
  145. var cbPortal = this._rHandler.createChargebeePortal();
  146. cbPortal.open({
  147. close: function() {
  148. $this._rHandler.logout();
  149. }
  150. });
  151. return false;
  152. };
  153. /** @} */