Last active
June 16, 2018 13:23
-
-
Save RoyiNamir/82d6ad32cca946a4c84bd53a3c51f974 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import {Injectable} from "@angular/core"; | |
import * as applicationSettings from "application-settings"; | |
import * as purchase from "nativescript-purchase"; | |
import {Product} from "nativescript-purchase/product"; | |
import {Transaction, TransactionState} from "nativescript-purchase/transaction"; | |
import {isAndroid, isIOS} from "platform"; | |
import {productId} from "~/modules/core/consts"; | |
@Injectable() | |
export class PurchaseService | |
{ | |
//In order to save the instance , like the api requires , we save it globaly to the service. | |
product: Product; | |
//this is the flag for restore. Restore purchase , when already purchase ,does'nt do anything. So I set a flag if a purchase restore - IS COMMITED. | |
// I fI don't find that flag after 2 seconds , means you already have purchased | |
// this is just an additional check ALSO as checking to the application-settings. | |
restoreFlag: boolean = false; | |
public init0(): Promise<any> | |
{ | |
return purchase.init([ | |
productId | |
]); | |
} | |
public getProduct1(): Promise<void> | |
{ | |
return purchase.getProducts() | |
.then((products: Array<Product>) => | |
{ | |
products.forEach((product: Product) => | |
{ | |
console.log(product.productIdentifier); | |
console.log(product.localizedTitle); | |
console.log(product.priceFormatted); | |
this.product = product; | |
}); | |
}) | |
.catch((e) => console.log("█11111111", e)); | |
} | |
//promisified API including logs for debug. ( so you can consume it and then AWAIT it , or THEN it. | |
public registerAndHandleTransactions2(): Promise<string> | |
{ | |
return new Promise((v, x) => | |
{ | |
purchase.off(purchase.transactionUpdatedEvent); | |
purchase.on(purchase.transactionUpdatedEvent, (transaction: Transaction) => | |
{ | |
console.log("█1", JSON.stringify(transaction)); | |
if (transaction.transactionState == TransactionState.Purchased) | |
{ | |
this.restoreFlag = true; | |
console.log(`█6Congratulations you just bought ${transaction.productIdentifier}!`); | |
if (isIOS) | |
{ | |
applicationSettings.setBoolean(productId, true); | |
v(""); | |
} | |
else | |
{ | |
if (isAndroid) | |
{ | |
purchase.consumePurchase(transaction.transactionReceipt) | |
.then((responseCode) => | |
{ | |
if (responseCode == 0) | |
{ | |
console.log("█7response code" + responseCode); | |
console.log(transaction.transactionDate); | |
console.log(transaction.transactionIdentifier); | |
applicationSettings.setBoolean(productId, true); | |
this.showApplicationSettings(); | |
v(""); | |
} | |
else | |
{ | |
x(responseCode); | |
this.showApplicationSettings(); | |
console.log("█8response code" + responseCode); | |
} | |
}, () => console.log(555555555)) | |
.catch((e) => | |
{ | |
console.log("█8consumePurchase catch" + e); | |
this.showApplicationSettings(); | |
x(e); | |
}); | |
} | |
} | |
} | |
else | |
{ | |
if (transaction.transactionState == TransactionState.Restored) | |
{this.restoreFlag=true; | |
console.log(`█2,Purchase of ${productId} restored.`); | |
applicationSettings.setBoolean(productId, true); | |
this.showApplicationSettings(); | |
v(""); | |
} | |
else | |
{ | |
if (transaction.transactionState == TransactionState.Failed) | |
{ | |
x(""); | |
console.log(`█4Purchase of ${transaction.productIdentifier} failed!`); | |
this.showApplicationSettings(); | |
} | |
else | |
{ | |
if (transaction.transactionState == TransactionState.Purchasing) | |
{ | |
console.log(`█5Purchasing...`); | |
} | |
else | |
{ | |
console.log(`█9 Another...` + transaction.transactionState); | |
} | |
} | |
} | |
} | |
}); | |
}); | |
} | |
// this.restoreFlag - this is the flag for restore. Restore purchase , when already purchase ,does'nt do anything. So I set a flag if a purchase restore - IS COMMITED. | |
// I fI don't find that flag after 2 seconds , means you already have purchased | |
// this is just an additional check ALSO as checking to the application-settings. | |
public restorePurchases10(): Promise<void> | |
{ | |
console.log("restorePurchases10"); | |
purchase.restorePurchases(); | |
return new Promise((v, x) => | |
{ | |
setTimeout(() => | |
{ | |
if (this.restoreFlag) | |
{ | |
v(); | |
} | |
else | |
{ | |
x(); | |
} | |
}, 2000); | |
}); | |
} | |
public buy9(): boolean | |
{ | |
if (purchase.canMakePayments()) | |
{ | |
purchase.buyProduct(this.product); //product ref should be the exact. | |
return true; | |
} | |
else | |
{ | |
console.log("Sorry, your account is not eligible to make payments!"); | |
return false; | |
} | |
} | |
//debug purpose | |
private showApplicationSettings() | |
{ | |
setTimeout(() => | |
{ | |
console.log(`█3applicationSettings.getBoolean(${productId})------>`, applicationSettings.getBoolean(productId)); | |
}, 3000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment