Created
February 24, 2018 04:43
-
-
Save captaincole/ca7aec13a7e21079187c5cd5f8bf4f61 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
configurePurchasing() { | |
if (!this.platform.is('cordova')) { return; } | |
console.log('Starting Configurations'); | |
let productId; | |
try { | |
if (this.platform.is('ios')) { | |
productId = this.program.appleProductId; | |
} else if (this.platform.is('android')) { | |
productId = this.program.googleProductId; | |
} | |
// Register Product | |
console.log('Registering Product ' + JSON.stringify(productId)); | |
this.store.verbosity = this.store.DEBUG; | |
this.store.register({ | |
id: productId, | |
alias: productId, | |
type: this.store.NON_RENEWING_SUBSCRIPTION | |
}); | |
// Handlers | |
this.store.when(productId).approved( (product: IAPProduct) => { | |
// Purchase was approved | |
this.logger.logEvent('purchase_approved', {programId: this.program._id}); | |
product.finish(); | |
this.loader.dismiss(); | |
this.subscribe(); | |
}); | |
this.store.when(productId).registered( (product: IAPProduct) => { | |
console.log('Registered: ' + JSON.stringify(product)); | |
}); | |
this.store.when(productId).updated( (product: IAPProduct) => { | |
console.log('Loaded' + JSON.stringify(product)); | |
}); | |
this.store.when(productId).cancelled( (product) => { | |
this.logger.logEvent('purchase_cancelled' , {}); | |
if (this.loader.isOverlay) { | |
this.loader.dismiss(); | |
alert('Purchase was Cancelled'); | |
} | |
}); | |
this.store.error( (err) => { | |
this.logger.logEvent('store_error', {}); | |
this.loader.dismiss(); | |
alert('Store Error ' + JSON.stringify(err)); | |
}); | |
this.store.ready().then((status) => { | |
this.logger.logEvent('store_ready', {}); | |
console.log(JSON.stringify(this.store.get(productId))); | |
console.log('Store is Ready: ' + JSON.stringify(status)); | |
console.log('Products: ' + JSON.stringify(this.store.products)); | |
}); | |
// Errors | |
this.store.when(productId).error( (error) => { | |
this.logger.logEvent('store_error', {}); | |
this.loader.dismiss(); | |
alert('An Error Occured' + JSON.stringify(error)); | |
}); | |
// Refresh Always | |
console.log('Refresh Store'); | |
this.store.refresh(); | |
} catch (err) { | |
console.log('Error On Store Issues' + JSON.stringify(err)); | |
} | |
} | |
purchase() { | |
this.loader = this.loadingCtrl.create({ | |
content: 'Completing Transaction' | |
}); | |
this.loader.present(); | |
// Check For Review Status | |
if (!this.program.appleProductId || !this.program.googleProductId) { | |
this.toastCtrl.create({ | |
message: 'This product has not yet been approved for purchase. Please submit it for review.', | |
duration: 2000 | |
}).present(); | |
this.loader.dismiss(); | |
return; | |
} | |
this.configurePurchasing(); | |
if (!this.platform.is('cordova')) { return }; | |
let productId; | |
if (this.platform.is('ios')) { | |
productId = this.program.appleProductId; | |
} else if (this.platform.is('android')) { | |
productId = this.program.googleProductId; | |
} | |
try { | |
let product = this.store.get(productId); | |
console.log('Product Info: ' + JSON.stringify(product)); | |
this.store.order(productId).then( () => { | |
console.log('Purchase Succesfull'); | |
this.loader.dismiss(); | |
}).catch( () => { | |
console.log('Error Ordering From Store'); | |
this.loader.dismiss(); | |
}); | |
} catch (err) { | |
console.log('Error Ordering ' + JSON.stringify(err)); | |
this.loader.dismiss(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment