stripe_v3.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 BxPaymentProviderStripeV3(oOptions) {
  11. this.init(oOptions);
  12. }
  13. BxPaymentProviderStripeV3.prototype = new BxPaymentMain();
  14. BxPaymentProviderStripeV3.prototype.init = function(oOptions) {
  15. this._sProvider = oOptions.sProvider;
  16. this._sActionsUrl = oOptions.sActionUrl;
  17. this._sObjName = oOptions.sObjName == undefined ? 'oPaymentProviderStripeV3' : oOptions.sObjName;
  18. this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'fade' : oOptions.sAnimationEffect;
  19. this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
  20. //-- For Stripe Connect payments
  21. this._sConnectedAccountId = oOptions.sConnectedAccountId == undefined ? '' : oOptions.sConnectedAccountId;
  22. //--- For Single payment
  23. this._sObjNameGrid = oOptions.sObjNameGrid;
  24. //--- For Recurring payment
  25. this._sObjNameCart = oOptions.sObjNameCart;
  26. this._iSellerId = oOptions.iSellerId;
  27. this._iModuleId = oOptions.iModuleId;
  28. this._iItemId = oOptions.iItemId;
  29. this._iItemCount = oOptions.iItemCount;
  30. this._sItemAddons = oOptions.sItemAddons == undefined ? '' : oOptions.sItemAddons;
  31. this._sRedirect = oOptions.sRedirect;
  32. this._sCustom = oOptions.sCustom;
  33. var $this = this;
  34. var oConnectedAccount = this._sConnectedAccountId ? {stripeAccount: this._sConnectedAccountId} : {};
  35. if(window.Stripe === undefined) {
  36. $.getScript('https://js.stripe.com/v3/', function() {
  37. $this._rHandler = Stripe(oOptions.sPublicKey, oConnectedAccount);
  38. });
  39. }
  40. else
  41. this._rHandler = Stripe(oOptions.sPublicKey, oConnectedAccount);
  42. };
  43. BxPaymentProviderStripeV3.prototype.onCartCheckout = function(oData) {
  44. this._rHandler.redirectToCheckout({
  45. sessionId: oData.session_id
  46. }).then(function (oResult) {
  47. if(oResult.error)
  48. bx_alert(oResult.error.message);
  49. }).catch(function(oError) {
  50. console.error('Error:', oError);
  51. });
  52. };
  53. BxPaymentProviderStripeV3.prototype.subscribe = function(oLink) {
  54. return this.subscribeWithAddons(oLink);
  55. };
  56. BxPaymentProviderStripeV3.prototype.subscribeWithAddons = function(oLink, mixedAddons) {
  57. var oDate = new Date();
  58. oLink = jQuery(oLink);
  59. if(oLink.hasClass('bx-btn-disabled'))
  60. return;
  61. oLink.addClass('bx-btn-disabled');
  62. this.loadingInPopup(oLink, true);
  63. var aParams = {
  64. seller_id: this._iSellerId,
  65. seller_provider: this._sProvider,
  66. module_id: this._iModuleId,
  67. item_id: this._iItemId,
  68. item_count: this._iItemCount,
  69. item_addons: mixedAddons != undefined ? mixedAddons : this._sItemAddons,
  70. redirect: this._sRedirect,
  71. custom: this._sCustom,
  72. _t: oDate.getTime()
  73. };
  74. $.post(this._sActionsUrl + 'call/' + this._sProvider + '/get_session_recurring/' + this._iSellerId + '/', aParams, function(oData) {
  75. if(oData && oData.code != undefined && parseInt(oData.code) == 0)
  76. oData.params = aParams;
  77. oLink.removeClass('bx-btn-disabled');
  78. processJsonData(oData);
  79. }, 'json');
  80. };
  81. BxPaymentProviderStripeV3.prototype.onSubscribe = function(oData) {
  82. this._rHandler.redirectToCheckout({
  83. sessionId: oData.session_id
  84. }).then(function (oResult) {
  85. if(oResult.error)
  86. bx_alert(oResult.error.message);
  87. }).catch(function(oError) {
  88. console.error('Error:', oError);
  89. });
  90. };
  91. /** @} */