-
Notifications
You must be signed in to change notification settings - Fork 0
/
paying.bak.ts
190 lines (146 loc) · 4.43 KB
/
paying.bak.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
export interface PayingServicePrepareSubscriptionOptions {}
export interface PayingServicePrepareSubscriptionResult {}
export interface IPayingService {
prepareSubscription(
options: PayingServicePrepareSubscriptionOptions,
): Promise<PayingServicePrepareSubscriptionResult>;
submitSubscription(options: unknown): unknown;
handleCallback(data: unknown): {
type: 'renew';
expiresAt: Date;
};
handleReceipt(data: unknown):
| {
type: 'subscription';
prepare: PayingPrepareSubscriptionOptions;
submit: PayingSubmitSubscriptionOptions;
}
| {
type: 'payment';
prepare: PayingPreparePaymentOptions;
submit: PayingSubmitPaymentOptions;
};
}
export class PayingUser {
constructor(readonly id: string, readonly paying: Paying) {}
prepareSubscription(
serviceName: string,
options: PayingPrepareSubscriptionOptions,
): Promise<PreparingSubscription> {
return this.paying.prepareSubscription(this.id, serviceName, options);
}
handleReceipt(
userId: string,
serviceName: string,
data: unknown,
): Promise<void> {
return this.paying.handleReceipt(this.id, serviceName, data);
}
}
export interface SubscriptionDocument {
service: string;
}
export interface IPayingDB {
findOneSubscription(
query: unknown,
): Promise<SubscriptionDocument | undefined>;
findSubscriptions(query: unknown): Promise<SubscriptionDocument[]>;
}
export interface PayingOptions {}
export class Paying {
constructor(
readonly services: Record<string, IPayingService>,
readonly db: IPayingDB, // readonly options: PayingOptions,
) {}
user(id: string): PayingUser {
return new PayingUser(id, this);
}
async handleReceipt(
userId: string,
serviceName: string,
data: unknown,
): Promise<void> {
const service = this.requireService(serviceName);
const {prepare: prepareOptions, submit: submitOptions} =
service.handleReceipt(data);
await this.prepareSubscription(userId, serviceName, prepareOptions);
await this.submitSubscription(userId, serviceName, submitOptions);
}
async handleCallback(serviceName: string, data: unknown): Promise<void> {
const service = this.requireService(serviceName);
const result = service.handleCallback(data);
switch (result.type) {
case 'renew':
break;
default:
break;
}
}
async prepareSubscription(
userId: string,
serviceName: string,
{name}: PayingPrepareSubscriptionOptions,
): Promise<PreparingSubscription> {
const service = this.requireService(serviceName);
const subscription = new PreparingSubscription(userId, {}, service);
let subscriptionDoc = await this.db.findOneSubscription({
user: userId,
name,
});
if (subscriptionDoc) {
const service = this.requireService(subscriptionDoc.service);
// apple -> apple
}
let result = await service.prepareSubscription({});
return subscription;
}
async submitSubscription(
userId: string,
serviceName: string,
options: PayingSubmitSubscriptionOptions,
): Promise<void> {}
private requireService(serviceName: string): IPayingService {
const service = this.services[serviceName];
if (!service) {
throw new Error(`Unknown PayingService ${JSON.stringify(serviceName)}`);
}
return service;
}
}
export interface PayingPrepareSubscriptionOptions {
/**
* Subscription name, e.g.: "premium-membership".
*/
name: string;
}
export interface PayingSubmitSubscriptionOptions {}
export interface PayingPreparePaymentOptions {}
export interface PayingSubmitPaymentOptions {}
export class PreparingSubscription {
constructor(
readonly userId: string,
readonly options: PreparingSubscriptionOptions,
readonly service: IPayingService,
) {}
async submit(): Promise<void> {
await this.service.submitSubscription({});
}
}
export interface PreparingSubscriptionOptions {}
function getExpiration(transactions: any[], now = Date.now()): number {
let startsAt = 0;
let expiresAt = 0;
transactions.sort((x, y) => x.startsAt - y.startsAt);
for (let transaction of transactions) {
if (transaction.startsAt <= expiresAt) {
expiresAt += transaction.expiresAt - transaction.startsAt;
} else {
if (transaction.startsAt > now) {
break;
}
startsAt = transaction.startsAt;
expiresAt = transaction.expiresAt;
}
}
return expiresAt;
}