-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathsubscriptions.ts
116 lines (96 loc) · 3.09 KB
/
subscriptions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import * as Rewards from "@brave-intl/skus-sdk";
import { isProduction, shouldForcePaymentsStaging } from "./environment";
let sdkref: Rewards.JSSDK | undefined;
const loadRewardsSdk = async (): Promise<Rewards.JSSDK> => {
if (sdkref) {
return sdkref;
}
// this envvar is set by the EnvironmentPlugin in webpack.config.js
const env = process.env.ENVIRONMENT ?? "local";
log(`calling initialize(${env}, false)...`);
const sdk = await Rewards.initialize(
shouldForcePaymentsStaging ? "staging" : env,
false,
isProduction ? "error" : "info",
);
sdkref = sdk;
return sdk;
};
export async function provisionOrder(orderId: string): Promise<void> {
let currentMethod;
try {
const sdk = await loadRewardsSdk();
currentMethod = "refresh_order";
log(`calling refresh_order...`);
const order = await sdk.refresh_order(orderId);
if (order && order.status === "paid") {
currentMethod = "fetch_order_credentials";
log(`calling fetch_order_credentials...`);
await sdk.fetch_order_credentials(orderId);
}
} catch (e) {
error(`${currentMethod} fails`, e);
throw e;
}
}
export async function recoverCredsIfRequired(orderId: string): Promise<void> {
let currentMethod;
try {
const sdk = await loadRewardsSdk();
currentMethod = "refresh_order";
log("calling refresh_order...");
const order = await sdk.refresh_order(orderId);
log("order status is ", order.status);
if (["paid", "canceled"].includes(order.status)) {
// user should have a subscription, re-fetch their credentials if not
const isSubscribed = await checkSubscribedUsingSDK();
if (!isSubscribed) {
currentMethod = "fetch_order_credentials";
log("calling fetch_order_credentials...");
await sdk.fetch_order_credentials(orderId);
}
} else {
throw new Error("Order not paid.");
}
} catch (e) {
error(`${currentMethod} fails`, e);
throw e;
}
}
function getCredentialHostname(): string | undefined {
return shouldForcePaymentsStaging ? "talk.bravesoftware.com" : undefined;
}
export async function checkSubscribedUsingSDK(): Promise<boolean> {
try {
const sdk = await loadRewardsSdk();
log(`calling credential_summary...`);
const result = await sdk.credential_summary(getCredentialHostname());
log("credential_summary returns", result);
if (result && result.active) {
return true;
}
} catch (e) {
error("credential_summary fails", e);
}
return false;
}
export async function setTemporaryCredentialCookie(): Promise<boolean> {
try {
const sdk = await loadRewardsSdk();
log(`calling present_credentials...`);
const result = await sdk.present_credentials(getCredentialHostname());
log("present_credentials returns", result);
if (result) {
return true;
}
} catch (e) {
error("present_credentials fails", e);
}
return false;
}
function log(message: string, ...args: any[]) {
console.log(`skus-sdk: ${message}`, ...args);
}
function error(message: string, ...args: any[]) {
console.error(`!!! skus-sdk: ${message}`, ...args);
}