Skip to content

Commit 43bef9b

Browse files
committed
pg: redeem microsoft store and xbox, WIP, #315, #5
1 parent f920aa2 commit 43bef9b

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

prime-gaming.js

+31-12
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ try {
155155
// await (await card.$('text=Claim')).click(); // goes to URL of game, no need to wait
156156
external_info.push({ title, url });
157157
}
158+
// external_info = [ { title: 'Fallout 76 (XBOX)', url: 'https://gaming.amazon.com/fallout-76-xbox-fgwp/dp/amzn1.pg.item.9fe17d7b-b6c2-4f58-b494-cc4e79528d0b?ingress=amzn&ref_=SM_Fallout76XBOX_S01_FGWP_CRWN' } ];
158159
for (const { title, url } of external_info) {
159160
console.log('Current free game:', title); // , url);
160161
await page.goto(url, { waitUntil: 'domcontentloaded' });
@@ -185,7 +186,8 @@ try {
185186
const redeem = {
186187
// 'origin': 'https://www.origin.com/redeem', // TODO still needed or now only via account linking?
187188
'gog.com': 'https://www.gog.com/redeem',
188-
'microsoft games': 'https://redeem.microsoft.com',
189+
'microsoft store': 'https://account.microsoft.com/billing/redeem',
190+
xbox: 'https://account.microsoft.com/billing/redeem',
189191
'legacy games': 'https://www.legacygames.com/primedeal',
190192
};
191193
if (store in redeem) { // did not work for linked origin: && !await page.locator('div:has-text("Successfully Claimed")').count()
@@ -240,27 +242,44 @@ try {
240242
console.log(' Unknown Response 2 - please report in https://github.com/vogler/free-games-claimer/issues/5');
241243
}
242244
}
243-
} else if (store == 'microsoft games') {
244-
console.error(` Redeem on ${store} not yet implemented!`);
245+
} else if (store == 'microsoft store' || store == 'xbox') {
246+
console.error(` Redeem on ${store} is experimental!`);
247+
// await page2.pause();
245248
if (page2.url().startsWith('https://login.')) {
246-
console.error(' Not logged in! Use the browser to login manually.');
249+
console.error(' Not logged in! Use the browser to login manually. Waiting for 60s.');
250+
await page2.waitForTimeout(60 * 1000);
247251
redeem_action = 'redeem (login)';
248252
} else {
249-
const r = page2.waitForResponse(r => r.url().startsWith('https://purchase.mp.microsoft.com/'));
250-
await page2.fill('[name=tokenString]', code);
253+
const iframe = page2.frameLocator('#redeem-iframe');
254+
const input = iframe.locator('[name=tokenString]');
255+
await input.waitFor();
256+
await input.fill(code);
257+
const r = page2.waitForResponse(r => r.url().startsWith('https://cart.production.store-web.dynamics.com/v1.0/Redeem/PrepareRedeem'));
251258
// console.log(await page2.locator('.redeem_code_error').innerText());
252259
const rt = await (await r).text();
253-
console.debug(` Response: ${rt}`);
254260
// {"code":"NotFound","data":[],"details":[],"innererror":{"code":"TokenNotFound",...
255-
const reason = JSON.parse(rt).code;
256-
if (reason == 'NotFound') {
261+
const j = JSON.parse(rt);
262+
const reason = j?.events?.cart.length && j.events.cart[0]?.data?.reason;
263+
if (reason == 'TokenNotFound') {
257264
redeem_action = 'redeem (not found)';
258265
console.error(' Code was not found!');
266+
} else if (j?.productInfos?.length && j.productInfos[0]?.redeemable) {
267+
await iframe.locator('button:has-text("Next")').click();
268+
await iframe.locator('button:has-text("Confirm")').click();
269+
const r = page2.waitForResponse(r => r.url().startsWith('https://cart.production.store-web.dynamics.com/v1.0/Redeem/RedeemToken'));
270+
const j = JSON.parse(await (await r).text());
271+
if (j?.events?.cart.length && j.events.cart[0]?.data?.reason == 'UserAlreadyOwnsContent') {
272+
redeem_action = 'already redeemed';
273+
console.error(' error: UserAlreadyOwnsContent');
274+
} else if (true) { // TODO what's returned on success?
275+
redeem_action = 'redeemed';
276+
db.data[user][title].status = 'claimed and redeemed?';
277+
console.log(' Redeemed successfully? Please report if not in https://github.com/vogler/free-games-claimer/issues/5');
278+
}
259279
} else { // TODO find out other responses
260-
await page2.click('#nextButton');
261-
redeem_action = 'redeemed?';
280+
redeem_action = 'unknown';
281+
console.debug(` Response: ${rt}`);
262282
console.log(' Redeemed successfully? Please report your Response from above (if it is new) in https://github.com/vogler/free-games-claimer/issues/5');
263-
db.data[user][title].status = 'claimed and redeemed?';
264283
}
265284
}
266285
} else if (store == 'legacy games') {

0 commit comments

Comments
 (0)