forked from subscriptions-project/swg-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe-response.js
149 lines (141 loc) · 3.78 KB
/
subscribe-response.js
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
/**
* Copyright 2018 The Subscribe with Google Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Entitlements as EntitlementsDef} from './entitlements';
import {UserData as UserDataDef} from './user-data';
/**
*/
export class SubscribeResponse {
/**
* @param {string} raw
* @param {!PurchaseData} purchaseData
* @param {?UserDataDef} userData
* @param {?EntitlementsDef} entitlements
* @param {!string} productType
* @param {function():!Promise} completeHandler
* @param {?string=} oldSku
* @param {?string=} swgUserToken
* @param {?number=} paymentRecurrence
* @param {?Object=} requestMetadata
*/
constructor(
raw,
purchaseData,
userData,
entitlements,
productType,
completeHandler,
oldSku = null,
swgUserToken = null,
paymentRecurrence = null,
requestMetadata = null
) {
/** @const {string} */
this.raw = raw;
/** @const {!PurchaseData} */
this.purchaseData = purchaseData;
/** @const {?UserDataDef} */
this.userData = userData;
/** @const {?EntitlementsDef} */
this.entitlements = entitlements;
/** @const {string} */
this.productType = productType;
/** @private @const {function():!Promise} */
this.completeHandler_ = completeHandler;
/** @const {?string} */
this.oldSku = oldSku;
/** @const {?string} */
this.swgUserToken = swgUserToken;
/** @const {?number} */
this.paymentRecurrence = paymentRecurrence;
/** @const {?Object} */
this.requestMetadata = requestMetadata;
}
/**
* @return {!SubscribeResponse}
*/
clone() {
return new SubscribeResponse(
this.raw,
this.purchaseData,
this.userData,
this.entitlements,
this.productType,
this.completeHandler_,
this.oldSku,
this.swgUserToken
);
}
/**
* @return {!Object}
*/
json() {
return {
'purchaseData': this.purchaseData.json(),
'userData': this.userData ? this.userData.json() : null,
'entitlements': this.entitlements ? this.entitlements.json() : null,
'oldSku': this.oldSku,
'productType': this.productType,
'swgUserToken': this.swgUserToken,
};
}
/**
* Allows the receiving site to complete/acknowledge that it registered
* the subscription purchase. The typical action would be to create an
* account (or match an existing one) and associated the purchase with
* that account.
*
* SwG will display progress indicator until this method is called and
* upon receiving this call will show the confirmation to the user.
* The promise returned by this method will yield once the user closes
* the confirmation.
*
* @return {!Promise}
*/
complete() {
return this.completeHandler_();
}
}
/**
*/
export class PurchaseData {
/**
* @param {string} raw
* @param {string} signature
*/
constructor(raw, signature) {
/** @const {string} */
this.raw = raw;
/** @const {string} */
this.data = raw;
/** @const {string} */
this.signature = signature;
}
/**
* @return {!PurchaseData}
*/
clone() {
return new PurchaseData(this.raw, this.signature);
}
/**
* @return {!Object}
*/
json() {
return {
'data': this.raw,
'signature': this.signature,
};
}
}