From e79811afdad268909de97c46b3e9b27780079cc5 Mon Sep 17 00:00:00 2001 From: Mo Date: Thu, 21 Oct 2021 12:41:58 -0500 Subject: [PATCH] feat: add purchase url (#695) * feat: add purchase url * chore: bump snjs * fix: reuse existing function --- .env.sample | 1 + app/assets/javascripts/app.ts | 9 ++++++++ .../account/subscription/NoSubscription.tsx | 9 ++------ .../purchaseFlow/PurchaseFlowWrapper.tsx | 22 +++++++++++++------ .../javascripts/services/errorReporting.ts | 7 ------ app/views/application/app.html.erb | 1 + index.html | 2 ++ package.json | 2 +- 8 files changed, 31 insertions(+), 22 deletions(-) diff --git a/.env.sample b/.env.sample index 5033e80ba..8626915d2 100644 --- a/.env.sample +++ b/.env.sample @@ -7,6 +7,7 @@ RAILS_LOG_LEVEL=INFO RAILS_SERVE_STATIC_FILES=true SECRET_KEY_BASE=test APP_HOST=http://localhost:3001 +PURCHASE_URL=https://standardnotes.com/purchase EXTENSIONS_MANAGER_LOCATION=extensions/extensions-manager/dist/index.html SF_DEFAULT_SERVER=http://localhost:3000 diff --git a/app/assets/javascripts/app.ts b/app/assets/javascripts/app.ts index ddd208fab..ee83819e6 100644 --- a/app/assets/javascripts/app.ts +++ b/app/assets/javascripts/app.ts @@ -1,5 +1,14 @@ 'use strict'; +declare global { + interface Window { + // eslint-disable-next-line camelcase + _bugsnag_api_key?: string; + // eslint-disable-next-line camelcase + _purchase_url?: string; + } +} + import { SNLog } from '@standardnotes/snjs'; import angular from 'angular'; import { configRoutes } from './routes'; diff --git a/app/assets/javascripts/preferences/panes/account/subscription/NoSubscription.tsx b/app/assets/javascripts/preferences/panes/account/subscription/NoSubscription.tsx index ca5a1c953..f76e74ebf 100644 --- a/app/assets/javascripts/preferences/panes/account/subscription/NoSubscription.tsx +++ b/app/assets/javascripts/preferences/panes/account/subscription/NoSubscription.tsx @@ -3,7 +3,7 @@ import { LinkButton, Text } from '@/preferences/components'; import { Button } from '@/components/Button'; import { WebApplication } from "@/ui_models/application"; import { useState } from "preact/hooks"; -import { isDesktopApplication } from "@/utils"; +import { loadPurchaseFlowUrl } from "@/purchaseFlow/PurchaseFlowWrapper"; export const NoSubscription: FunctionalComponent<{ application: WebApplication; @@ -15,12 +15,7 @@ export const NoSubscription: FunctionalComponent<{ const errorMessage = 'There was an error when attempting to redirect you to the subscription page.'; setIsLoadingPurchaseFlow(true); try { - const url = await application.getPurchaseFlowUrl(); - if (url) { - const currentUrl = window.location.href; - const successUrl = isDesktopApplication() ? `standardnotes://${currentUrl}` : currentUrl; - window.location.assign(`${url}&success_url=${successUrl}`); - } else { + if (!await loadPurchaseFlowUrl(application)) { setPurchaseFlowError(errorMessage); } } catch (e) { diff --git a/app/assets/javascripts/purchaseFlow/PurchaseFlowWrapper.tsx b/app/assets/javascripts/purchaseFlow/PurchaseFlowWrapper.tsx index e1998f9d5..361e3dc9f 100644 --- a/app/assets/javascripts/purchaseFlow/PurchaseFlowWrapper.tsx +++ b/app/assets/javascripts/purchaseFlow/PurchaseFlowWrapper.tsx @@ -10,17 +10,25 @@ export type PurchaseFlowWrapperProps = { application: WebApplication; }; +export const getPurchaseFlowUrl = async (application: WebApplication): Promise => { + const token = await application.getNewSubscriptionToken(); + if (token) { + const currentUrl = window.location.href; + const successUrl = isDesktopApplication() ? `standardnotes://${currentUrl}` : currentUrl; + return `${window._purchase_url}?subscription_token=${token}&success_url=${successUrl}`; + } + return undefined; +}; + export const loadPurchaseFlowUrl = async ( application: WebApplication -): Promise => { - const url = await application.getPurchaseFlowUrl(); +): Promise => { + const url = await getPurchaseFlowUrl(application); if (url) { - const currentUrl = window.location.href.split('/?')[0]; - const successUrl = isDesktopApplication() - ? `standardnotes://${currentUrl}` - : currentUrl; - window.location.assign(`${url}&success_url=${successUrl}`); + window.location.assign(url); + return true; } + return false; }; export const PurchaseFlowWrapper: FunctionComponent = diff --git a/app/assets/javascripts/services/errorReporting.ts b/app/assets/javascripts/services/errorReporting.ts index 16c70c867..cb748d164 100644 --- a/app/assets/javascripts/services/errorReporting.ts +++ b/app/assets/javascripts/services/errorReporting.ts @@ -5,13 +5,6 @@ import Bugsnag from '@bugsnag/js'; import { WebCrypto } from '../crypto'; import { AppVersion } from '@/version'; -declare global { - interface Window { - // eslint-disable-next-line camelcase - _bugsnag_api_key?: string; - } -} - function redactFilePath(line: string): string { const fileName = line.match(/\w+\.(html|js)/)?.[0]; const redacted = ''; diff --git a/app/views/application/app.html.erb b/app/views/application/app.html.erb index e92ab8c83..c13c3c7d1 100644 --- a/app/views/application/app.html.erb +++ b/app/views/application/app.html.erb @@ -37,6 +37,7 @@ window._bugsnag_api_key = "<%= ENV['BUGSNAG_API_KEY'] %>"; window._enable_unfinished_features = "<%= ENV['ENABLE_UNFINISHED_FEATURES'] %>" === 'true'; window._websocket_url = "<%= ENV['WEBSOCKET_URL'] %>"; + window._purchase_url = "<%= ENV['PURCHASE_URL'] %>"; <% if Rails.env.development? %> diff --git a/index.html b/index.html index 691b2da48..2826c1918 100644 --- a/index.html +++ b/index.html @@ -34,6 +34,7 @@ data-bugsnag-api-key="<%= env.DEV_BUGSNAG_API_KEY %>" data-enable-unfinished-features="<%= env.ENABLE_UNFINISHED_FEATURES %>" data-web-socket-url="<%= env.DEV_WEBSOCKET_URL %>" + data-purchase-url="<%= env.PURCHASE_URL %>" > diff --git a/package.json b/package.json index a8e0daa13..e0b8813b2 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "@reach/listbox": "^0.16.1", "@standardnotes/features": "1.7.2", "@standardnotes/sncrypto-web": "1.5.2", - "@standardnotes/snjs": "2.15.2", + "@standardnotes/snjs": "2.16.0", "mobx": "^6.3.2", "mobx-react-lite": "^3.2.0", "preact": "^10.5.12",