* @param {!./deps.DepsDef} deps * @param {*} data * @param {function():!Promise} completeHandler * @return {!SubscribeResponse}
(deps, data, completeHandler)
| 6135 | * @return {!SubscribeResponse} |
| 6136 | */ |
| 6137 | function parseSubscriptionResponse(deps, data, completeHandler) { |
| 6138 | let swgData = null; |
| 6139 | let raw = null; |
| 6140 | let productType = ProductType.SUBSCRIPTION; |
| 6141 | let oldSku = null; |
| 6142 | let paymentRecurrence = null; |
| 6143 | let requestMetadata = null; |
| 6144 | |
| 6145 | if (data) { |
| 6146 | if (typeof data == 'string') { |
| 6147 | raw = /** @type {string} */ (data); |
| 6148 | } else { |
| 6149 | // Assume it's a json object in the format: |
| 6150 | // `{integratorClientCallbackData: "..."}` or `{swgCallbackData: "..."}`. |
| 6151 | const json = /** @type {!Object} */ (data); |
| 6152 | if ('swgCallbackData' in json) { |
| 6153 | swgData = /** @type {!Object} */ (json['swgCallbackData']); |
| 6154 | } else if ('integratorClientCallbackData' in json) { |
| 6155 | raw = json['integratorClientCallbackData']; |
| 6156 | } |
| 6157 | if ('paymentRequest' in data) { |
| 6158 | const swgObj = data['paymentRequest']['swg'] || {}; |
| 6159 | oldSku = swgObj['oldSku']; |
| 6160 | paymentRecurrence = swgObj['paymentRecurrence']; |
| 6161 | requestMetadata = swgObj['metadata']; |
| 6162 | productType = |
| 6163 | (data['paymentRequest']['i'] || {})['productType'] || |
| 6164 | ProductType.SUBSCRIPTION; |
| 6165 | } |
| 6166 | // Set productType if paymentRequest is not present, which happens |
| 6167 | // if the pay flow was opened in redirect mode. |
| 6168 | else if ('productType' in data) { |
| 6169 | productType = data['productType']; |
| 6170 | } |
| 6171 | } |
| 6172 | } |
| 6173 | if (raw && !swgData) { |
| 6174 | raw = atob(raw); |
| 6175 | if (raw) { |
| 6176 | const parsed = parseJson(raw); |
| 6177 | swgData = parsed['swgCallbackData']; |
| 6178 | } |
| 6179 | } |
| 6180 | if (!swgData) { |
| 6181 | throw new Error('unexpected payment response'); |
| 6182 | } |
| 6183 | raw = JSON.stringify(/** @type {!JsonObject} */ (swgData)); |
| 6184 | return new SubscribeResponse( |
| 6185 | raw, |
| 6186 | parsePurchaseData(swgData), |
| 6187 | parseUserData(swgData), |
| 6188 | parseEntitlements(deps, swgData), |
| 6189 | productType, |
| 6190 | completeHandler, |
| 6191 | oldSku, |
| 6192 | swgData['swgUserToken'], |
| 6193 | paymentRecurrence, |
| 6194 | requestMetadata |
no test coverage detected