Last active
July 1, 2020 06:58
-
-
Save wegeberg/135dcbb488261b339ca749dbb6fd755b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(self.SWG = self.SWG || []).push(function (subscriptions) { | |
// SwG is ready to be called via subscriptions interface. | |
subscriptions.setOnPaymentResponse(function (paymentResponse) { | |
paymentResponse.then(function (response) { | |
// 1. Handle the payment response. | |
// CREATE LINKED ACCOUNT IN BACKEND | |
axios.post(`https://api-a4.blizzload.com/v2/abonnement/create-linked-account/`, response) | |
.then(claResponse => { | |
console.log("create-linked-account claResponse.data", claResponse.data); | |
if (claResponse.data.bruger) { | |
// User object returned | |
response.complete().then(function () { | |
// login using the returned user object | |
// (login(claResponse.data.bruger)); | |
login(claResponse.data.bruger) | |
.then(() => { | |
mutate("bruger", claResponse.data.bruger); | |
}) | |
.catch(err => console.log(err)) | |
; | |
}); | |
} else { | |
console.log("NO USER OBJECT RETURNED"); | |
} | |
}) | |
.catch(err => console.log(err)) | |
; | |
}); | |
}); | |
// https://github.com/subscriptions-project/swg-js/blob/master/docs/entitlements-flow.md | |
subscriptions.setOnEntitlementsResponse(entitlementsPromise => { | |
entitlementsPromise.then(entitlements => { | |
if (entitlements.enablesThis()) { | |
const subscriptionObject = entitlements.entitlements[0].subscriptionToken; | |
// console.log("subscriptionObject", subscriptionObject, typeof subscriptionObject); | |
if (subscriptionObject) { | |
const url = `https://api-a4.blizzload.com/v2/abonnement/entitlements/`; | |
const params = JSON.parse(subscriptionObject); | |
axios.post(url, params) | |
.then(response => { | |
console.log("entitlements-response", response); | |
if (response.data.bruger) { | |
// Subscription found - log user in | |
login(response.data.bruger) | |
.then(() => { | |
mutate("bruger", response.data.bruger); | |
}) | |
.catch(err => console.log(err)) | |
; | |
} else { | |
subscriptions.completeDeferredAccountCreation({ | |
entitlements: entitlements, | |
consent: true | |
}).then(cdacResponse => { | |
// 1. The user has consented to account creation. Create account | |
// based on the response. | |
console.log("completeDeferredAccountCreation RESPONSE", cdacResponse); | |
// CREATE OR UPDATE LINKED ACCOUNT | |
axios.post(`https://api-a4.blizzload.com/v2/abonnement/create-linked-account/`, cdacResponse) | |
.then(response => { | |
console.log("create-linked-account response.data", response.data); | |
if (response.data.bruger) { | |
// User object returned | |
// login using the returned user object | |
subscriptions.showLoginNotification().then(() => { | |
console.info("1. UNLOCK CONTENT"); | |
login(response.data.bruger) | |
.then(() => { | |
mutate("bruger", response.data.bruger); | |
}) | |
.catch(err => console.log(err)) | |
; | |
}); | |
} else { | |
console.log("NO USER OBJECT RETURNED"); | |
} | |
}) | |
.catch(err => console.log(err)) | |
; | |
}); | |
} | |
}) | |
.catch(err => console.log(err)) | |
; | |
} // if(subscriptionObject) | |
} else { // if (entitlements.enablesThis()) | |
console.info("NO ACCESS"); | |
const buttonElement = swgButtonRef.current; | |
subscriptions.attachButton( | |
buttonElement, // HTML element to add the button to | |
function () { | |
// Launches payment screen | |
subscriptions.subscribe('test_sku'); | |
// subscriptions.subscribe('abonnement_12md'); | |
} | |
); | |
} // if (entitlements.enablesThis()) .. else .. | |
}); | |
}); | |
}); | |
} // if (constants.USE_SWG) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment