tevrocsdk 0.1.33 → 0.1.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/tevroc-sdk.umd.js CHANGED
@@ -5039,6 +5039,12 @@ var Tevroc = (() => {
5039
5039
  });
5040
5040
 
5041
5041
  // src/services/useGoogleAuth.js
5042
+ var useGoogleAuth_exports = {};
5043
+ __export(useGoogleAuth_exports, {
5044
+ createGoogleLogin: () => createGoogleLogin,
5045
+ initializeGoogleLogin: () => initializeGoogleLogin,
5046
+ subscribeGoogleLogin: () => subscribeGoogleLogin
5047
+ });
5042
5048
  function emitGoogleLogin(user, err = null) {
5043
5049
  googleListeners.forEach((cb) => {
5044
5050
  try {
@@ -5055,7 +5061,7 @@ var Tevroc = (() => {
5055
5061
  if (i !== -1) googleListeners.splice(i, 1);
5056
5062
  };
5057
5063
  }
5058
- var googleInitialized, googleListeners, authWindowRef, handleCallback, initializeGoogleLogin, createGoogleLogin;
5064
+ var googleInitialized, googleListeners, authWindowRef, authCleanupTimer, authMessageListener, cleanupAuthWindow, handleCallback, initializeGoogleLogin, createGoogleLogin;
5059
5065
  var init_useGoogleAuth = __esm({
5060
5066
  "src/services/useGoogleAuth.js"() {
5061
5067
  init_esm2();
@@ -5065,8 +5071,58 @@ var Tevroc = (() => {
5065
5071
  googleInitialized = false;
5066
5072
  googleListeners = [];
5067
5073
  authWindowRef = null;
5074
+ authCleanupTimer = null;
5075
+ authMessageListener = null;
5076
+ cleanupAuthWindow = async () => {
5077
+ if (authCleanupTimer) {
5078
+ clearInterval(authCleanupTimer);
5079
+ authCleanupTimer = null;
5080
+ }
5081
+ if (authMessageListener && typeof window !== "undefined") {
5082
+ window.removeEventListener("message", authMessageListener);
5083
+ authMessageListener = null;
5084
+ }
5085
+ if (authWindowRef) {
5086
+ const currentAuthWindow = authWindowRef;
5087
+ authWindowRef = null;
5088
+ try {
5089
+ if (typeof currentAuthWindow?.close === "function") {
5090
+ await currentAuthWindow.close();
5091
+ }
5092
+ } catch (e) {
5093
+ console.warn("Error closing auth window:", e);
5094
+ }
5095
+ }
5096
+ if (isTauri) {
5097
+ try {
5098
+ const tauriAuthWindow = await WebviewWindow.getByLabel("auth");
5099
+ if (tauriAuthWindow && tauriAuthWindow !== authWindowRef) {
5100
+ await tauriAuthWindow.close();
5101
+ }
5102
+ } catch (e) {
5103
+ console.warn("Error closing Tauri auth window by label:", e);
5104
+ }
5105
+ }
5106
+ if (isCapacitor) {
5107
+ try {
5108
+ await Browser2.close();
5109
+ } catch (e) {
5110
+ console.warn("Error closing Capacitor browser:", e);
5111
+ }
5112
+ }
5113
+ if (typeof window !== "undefined" && window !== window.parent) {
5114
+ try {
5115
+ if (authWindowRef && typeof authWindowRef.close === "function") {
5116
+ await authWindowRef.close();
5117
+ }
5118
+ } catch (e) {
5119
+ console.warn("Error closing popup fallback:", e);
5120
+ }
5121
+ }
5122
+ };
5068
5123
  handleCallback = async (url) => {
5069
5124
  try {
5125
+ await cleanupAuthWindow();
5070
5126
  if (!url?.includes("token=")) return;
5071
5127
  const params = new URLSearchParams(url.split("?")[1]);
5072
5128
  const token = params.get("token");
@@ -5112,14 +5168,7 @@ var Tevroc = (() => {
5112
5168
  url = url?.url || url?.payload || null;
5113
5169
  }
5114
5170
  if (!url) return;
5115
- if (authWindowRef) {
5116
- try {
5117
- await authWindowRef.close();
5118
- authWindowRef = null;
5119
- } catch (e) {
5120
- }
5121
- }
5122
- handleCallback(url);
5171
+ await handleCallback(url);
5123
5172
  });
5124
5173
  }
5125
5174
  };
@@ -5171,21 +5220,21 @@ var Tevroc = (() => {
5171
5220
  console.error("Popup blocked");
5172
5221
  return;
5173
5222
  }
5174
- const listener = (event) => {
5175
- if (event.origin !== serviceUrl) return;
5223
+ authWindowRef = popup;
5224
+ authMessageListener = async (event) => {
5225
+ const serviceOrigin = new URL(serviceUrl).origin;
5226
+ if (event.origin !== serviceOrigin) return;
5176
5227
  if (event.data?.type === "google-auth") {
5177
5228
  const token = event.data?.token || null;
5178
5229
  const url = token ? `${window.location.href}${window.location.href.includes("?") ? "&" : "?"}token=${token}` : window.location.href;
5179
- popup.close();
5180
- window.removeEventListener("message", listener);
5181
- handleCallback(url);
5230
+ await handleCallback(url);
5182
5231
  }
5183
5232
  };
5184
- window.addEventListener("message", listener);
5185
- const timer = setInterval(() => {
5233
+ window.addEventListener("message", authMessageListener);
5234
+ authCleanupTimer = setInterval(() => {
5186
5235
  if (popup.closed) {
5187
- clearInterval(timer);
5188
- window.removeEventListener("message", listener);
5236
+ cleanupAuthWindow().catch(() => {
5237
+ });
5189
5238
  }
5190
5239
  }, 500);
5191
5240
  return;
@@ -5236,17 +5285,17 @@ var Tevroc = (() => {
5236
5285
  }) => {
5237
5286
  try {
5238
5287
  addLog("PAYMENT CALLBACK:", url);
5239
- if (!url?.includes("state=")) return;
5288
+ if (!url?.includes("status=")) return;
5240
5289
  const params = new URLSearchParams(url.split("?")[1]);
5241
- const state = params.get("state");
5242
- addLog("PAYMENT RESULT:", state);
5243
- if (state == "success") {
5290
+ const status = params.get("status");
5291
+ addLog("PAYMENT RESULT:", status);
5292
+ if (status == "success") {
5244
5293
  addLog("PAYMENT SUCCESS");
5245
5294
  } else {
5246
5295
  addLog("PAYMENT FAILED");
5247
5296
  }
5248
- externalCallback(state, null);
5249
- emitPaymentStatus(state, null);
5297
+ externalCallback(status, null);
5298
+ emitPaymentStatus(status, null);
5250
5299
  } catch (err) {
5251
5300
  addLog("PAYMENT CALLBACK ERROR:", err.message);
5252
5301
  externalCallback(null, err);
@@ -5290,13 +5339,15 @@ var Tevroc = (() => {
5290
5339
  createPayment = async (amountZAR, {
5291
5340
  appId = "tevrocsoft",
5292
5341
  serviceUrl = "https://tevrocsoftapi.netlify.app",
5342
+ payAccount = "default",
5293
5343
  externalCallback = () => {
5294
5344
  }
5295
5345
  } = {}) => {
5296
5346
  await initializePayment(externalCallback);
5297
5347
  const cents = Math.round(Number(amountZAR) * 100);
5298
- const basePaymentUrl = `${serviceUrl}/pay`;
5299
- const paymentUrl = `${basePaymentUrl}?mode=mobile&amount=${cents}&app=${appId}`;
5348
+ const basePaymentUrl = `${serviceUrl.replace(/\/$/, "")}/yoco-pay`;
5349
+ const paymentUrl = `${basePaymentUrl}?mode=mobile&amount=${cents}&app=${appId}&account=${payAccount}`;
5350
+ console.log(basePaymentUrl, paymentUrl);
5300
5351
  if (isCapacitor) {
5301
5352
  await Browser2.open({
5302
5353
  url: paymentUrl
@@ -5304,7 +5355,7 @@ var Tevroc = (() => {
5304
5355
  return;
5305
5356
  }
5306
5357
  if (isTauri) {
5307
- const url = `${basePaymentUrl}?mode=desktop&amount=${cents}&app=${appId}`;
5358
+ const url = `${basePaymentUrl}?mode=desktop&amount=${cents}&app=${appId}&account=${payAccount}`;
5308
5359
  const win = new WebviewWindow("payment", {
5309
5360
  url,
5310
5361
  title: "Complete Payment",
@@ -5324,7 +5375,55 @@ var Tevroc = (() => {
5324
5375
  }
5325
5376
  const isLocalhost = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1";
5326
5377
  const mode = isLocalhost ? "localhost" : "netlify";
5327
- window.location.href = `${basePaymentUrl}?mode=${mode}&amount=${cents}&app=${appId}`;
5378
+ const finalUrl = `${basePaymentUrl}?mode=${mode}&amount=${cents}&app=${appId}`;
5379
+ try {
5380
+ const popup = window.open(finalUrl, "yocoPayment", "width=500,height=650");
5381
+ if (!popup) {
5382
+ window.location.href = finalUrl;
5383
+ return;
5384
+ }
5385
+ let listenerAttached = true;
5386
+ let timerRunning = true;
5387
+ const listener = (event) => {
5388
+ try {
5389
+ const serviceOrigin = new URL(serviceUrl).origin;
5390
+ if (event.origin !== serviceOrigin) return;
5391
+ if (event.data?.type === "payment-result") {
5392
+ const status = event.data?.status || null;
5393
+ listenerAttached = false;
5394
+ timerRunning = false;
5395
+ window.removeEventListener("message", listener);
5396
+ try {
5397
+ popup.close();
5398
+ } catch (e) {
5399
+ }
5400
+ const url = status ? `${window.location.href}${window.location.href.includes("?") ? "&" : "?"}status=${status}` : window.location.href;
5401
+ handlePaymentCallback(url, externalCallback);
5402
+ }
5403
+ } catch (err) {
5404
+ console.error("Payment listener error", err);
5405
+ }
5406
+ };
5407
+ window.addEventListener("message", listener);
5408
+ const timer = setInterval(() => {
5409
+ if (!timerRunning) {
5410
+ clearInterval(timer);
5411
+ return;
5412
+ }
5413
+ if (popup.closed) {
5414
+ clearInterval(timer);
5415
+ timerRunning = false;
5416
+ if (listenerAttached) {
5417
+ listenerAttached = false;
5418
+ window.removeEventListener("message", listener);
5419
+ }
5420
+ }
5421
+ }, 500);
5422
+ return;
5423
+ } catch (e) {
5424
+ window.location.href = finalUrl;
5425
+ return;
5426
+ }
5328
5427
  };
5329
5428
  }
5330
5429
  });
@@ -5879,6 +5978,13 @@ var Tevroc = (() => {
5879
5978
  }
5880
5979
  return platformModule;
5881
5980
  }
5981
+ var googleModule = null;
5982
+ async function getGoogleModule() {
5983
+ if (!googleModule) {
5984
+ googleModule = await Promise.resolve().then(() => (init_useGoogleAuth(), useGoogleAuth_exports));
5985
+ }
5986
+ return googleModule;
5987
+ }
5882
5988
  var TevrocError = class extends Error {
5883
5989
  constructor(message, options = {}) {
5884
5990
  super(message);
@@ -5901,6 +6007,7 @@ var Tevroc = (() => {
5901
6007
  this.serviceUrl = normalizeBaseUrl(serviceUrl);
5902
6008
  this.appId = options.appId;
5903
6009
  this.appName = options.appName;
6010
+ this.payAccount = options.payAccount ?? "default";
5904
6011
  this.isNative = options.isNative ?? false;
5905
6012
  this.fetch = (options.fetch ?? globalThis.fetch).bind(globalThis);
5906
6013
  if (typeof this.fetch !== "function") {
@@ -5950,6 +6057,7 @@ var Tevroc = (() => {
5950
6057
  const controller = new AbortController();
5951
6058
  const timeout = setTimeout(() => controller.abort(), options.timeoutMs ?? this.timeoutMs);
5952
6059
  const token = this.token || getStoredToken();
6060
+ console.log(token, options);
5953
6061
  if (token && options.auth !== false) {
5954
6062
  headers.set("authorization", `Bearer ${token}`);
5955
6063
  }
@@ -5966,7 +6074,7 @@ var Tevroc = (() => {
5966
6074
  if (value !== void 0 && value !== null) headers.set(key, String(value));
5967
6075
  }
5968
6076
  }
5969
- console.log(headers, url);
6077
+ console.error(headers, url);
5970
6078
  try {
5971
6079
  const response = await this.fetch(url, {
5972
6080
  method,
@@ -5999,13 +6107,18 @@ var Tevroc = (() => {
5999
6107
  }
6000
6108
  function createPaymentApi(client) {
6001
6109
  return {
6002
- create: async ({ amountZAR, appId = "tevrocsoft", mode = "web" }) => {
6110
+ create: async ({ amountZAR, appId = "tevrocsoft", mode = "web", account = "default", externalCallback = void 0 } = {}) => {
6003
6111
  return await createPayment(amountZAR, {
6004
6112
  appId,
6005
6113
  serviceUrl: client.serviceUrl,
6006
- mode
6114
+ mode,
6115
+ payAccount: account,
6116
+ externalCallback
6007
6117
  });
6008
6118
  },
6119
+ createCheckout: async (payload = {}) => {
6120
+ return await client.request("/create-checkout", { method: "POST", body: payload, auth: false });
6121
+ },
6009
6122
  onstatus: (callback) => {
6010
6123
  if (typeof callback !== "function") {
6011
6124
  throw new Error("onstatus requires a function");
@@ -6017,31 +6130,83 @@ var Tevroc = (() => {
6017
6130
  function createAuthApi(client) {
6018
6131
  return {
6019
6132
  async register(input) {
6020
- const result = await client.request("/auth/register", { method: "POST", body: input, auth: false });
6133
+ const body = buildAuthPayload(input, client);
6134
+ const result = await client.request("/auth/register", { method: "POST", body, auth: false });
6021
6135
  if (result.token) client.setToken(result.token);
6022
6136
  return result;
6023
6137
  },
6138
+ // base44
6139
+ loginWithProvider: async (provider, redirectTo, setToken = async () => {
6140
+ }) => {
6141
+ if (provider === "google") {
6142
+ const { createGoogleLogin: createGoogleLogin2, subscribeGoogleLogin: subscribeGoogleLogin2 } = await getGoogleModule();
6143
+ console.log({
6144
+ serviceUrl: client.serviceUrl,
6145
+ appId: client.appId,
6146
+ appName: client.appName
6147
+ });
6148
+ createGoogleLogin2({
6149
+ serviceUrl: client.serviceUrl,
6150
+ appId: client.appId,
6151
+ appName: client.appName
6152
+ });
6153
+ if (redirectTo) {
6154
+ const unsub = subscribeGoogleLogin2(async (user, err) => {
6155
+ console.log("Google callback:", user, err);
6156
+ unsub();
6157
+ if (!user) return;
6158
+ const result = await client.request("/auth/google", {
6159
+ method: "POST",
6160
+ body: buildAuthPayload({ accessToken: user.accessToken }, client),
6161
+ auth: false
6162
+ });
6163
+ console.log(result);
6164
+ await setToken(result.token, result.user);
6165
+ console.log("Redirecting to:", redirectTo);
6166
+ window.location.href = redirectTo;
6167
+ });
6168
+ }
6169
+ }
6170
+ },
6171
+ // extra
6024
6172
  googleLogin: (params) => createGoogleLogin(params),
6025
6173
  onGoogleLogin: (callback) => {
6026
6174
  return subscribeGoogleLogin(callback);
6027
6175
  },
6176
+ //
6028
6177
  async login(input) {
6029
- const result = await client.request("/auth/login", { method: "POST", body: input, auth: false });
6178
+ const body = buildAuthPayload(input, client);
6179
+ const result = await client.request("/auth/login", { method: "POST", body, auth: false });
6030
6180
  if (result.token) client.setToken(result.token);
6031
6181
  return result;
6032
6182
  },
6033
6183
  async logout() {
6034
- const result = await client.request("/auth/logout", { method: "POST" });
6184
+ const result = await client.request("/auth/logout", { method: "POST", body: buildAuthPayload({}, client) });
6035
6185
  client.clearToken();
6036
6186
  return result;
6037
6187
  },
6038
- me: () => client.request("/auth/me"),
6039
- verifyOtp: (input) => client.request("/auth/verify-otp", { method: "POST", body: input, auth: false }),
6040
- changePassword: (input) => client.request("/auth/change-password", { method: "POST", body: input }),
6041
- resetPassword: (input) => client.request("/auth/reset-password", { method: "POST", body: input, auth: false }),
6042
- loginUrl: (redirectTo) => client.request("/auth/login-url", { query: redirectTo ? { redirectTo } : void 0, auth: false })
6188
+ inviteUser: (input) => client.request("/users/invite", { method: "POST", body: buildAuthPayload(input, client) }),
6189
+ acceptInvite: (input) => client.request("/auth/accept-invite", { method: "POST", body: buildAuthPayload(input, client), auth: false }),
6190
+ me: () => client.request("/auth/me", { headers: buildAuthHeaders(client) }),
6191
+ verifyOtp: (input) => client.request("/auth/verify-otp", { method: "POST", body: buildAuthPayload(input, client), auth: false }),
6192
+ changePassword: (input) => client.request("/auth/change-password", { method: "POST", body: buildAuthPayload(input, client) }),
6193
+ resetPassword: (input) => client.request("/auth/reset-password", { method: "POST", body: buildAuthPayload(input, client), auth: false }),
6194
+ loginUrl: (redirectTo) => client.request("/auth/login-url", {
6195
+ query: redirectTo ? { redirectTo, appId: client.appId || void 0 } : { appId: client.appId || void 0 },
6196
+ auth: false
6197
+ })
6043
6198
  };
6044
6199
  }
6200
+ function buildAuthPayload(input = {}, client) {
6201
+ const payload = { ...input || {} };
6202
+ if (client?.appId && payload.appId == null) {
6203
+ payload.appId = client.appId;
6204
+ }
6205
+ return payload;
6206
+ }
6207
+ function buildAuthHeaders(client) {
6208
+ return client?.appId ? { "x-app-id": client.appId } : {};
6209
+ }
6045
6210
  function createEntityRegistry(client) {
6046
6211
  return new Proxy(
6047
6212
  {},