PeterStaev / nativescript-purchase

:moneybag: A NativeScript plugin for making in-app purchases!
Apache License 2.0
83 stars 28 forks source link

Problems with the purchase in Android #85

Closed susanagonzavi closed 4 years ago

susanagonzavi commented 5 years ago

Hi! I'm new in nativeScript with Angular and I 'm using this plugin to do in-app auto-renewable subscription purchases with IOS and Android. I don't have problems with IOS platform, but with Android yes. I have this:

//If there is a purchase
    purchase.on(purchase.transactionUpdatedEvent, (transaction: Transaction) => {
      if (transaction.transactionState === TransactionState.Restored) {
        applicationSettings.setBoolean(transaction.productIdentifier, true); 
     }
      if (transaction.transactionState === TransactionState.Purchased) {
        if (transaction.productIdentifier.indexOf(".premium") >= 0) { 
          purchase.consumePurchase(transaction.transactionReceipt) 
            .then((responseCode) => {
              //if everything it's OK
              if (responseCode === 0) {
                let receipt = new Receipt();
                let user = new User();
                let paymentMethod = new PaymentMethod();
                //Fill object receipt that we'll register in my database with AppleStoreConnect and PlayStore response 
                receipt.transactionNumber = +transaction.transactionIdentifier;
                receipt.service = this.productToBuy.localizedTitle;
                receipt.price = this.productToBuy.priceAmount;
                user.id = +localStorage.getItem('idBabesearch');
                paymentMethod.name = isIOS ? "App Store" : "Play Store";
                receipt.moneyType = this.productToBuy.priceCurrencyCode;
                receipt.user = user;
                receipt.paymentMethod = paymentMethod;
                receipt.packageName = this.productToBuy.productIdentifier;
                receipt.productId = this.productToBuy.localizedTitle;
                receipt.token = JSON.stringify({
                  "estadoTransaccion": transaction.transactionState,
                  "identificadorProducto": this.productToBuy.productIdentifier,
                  "identificadorTransaccion": transaction.transactionIdentifier,
                  "fechaTransaccion": transaction.transactionDate,
                  "reciboTransaccion": transaction.transactionReceipt,
                  "reciboTransaccionOriginal": transaction.originalTransaction,
                  "datos": transaction.developerPayload,
                  "firmaTransaccion": transaction.dataSignature
                });            
                console.log("== TRANSACTION ATRIBUTES ==\n" +
                  "transactionState -> " + transaction.transactionState + "\n" +
                  "productIdentifier -> " + this.productToBuy.productIdentifier + "\n" +
                  "transactionIdentifier -> " + transaction.transactionIdentifier + "\n" +
                  "transactionDate -> " + transaction.transactionDate + "\n" +
                  "transactionReceipt -> " + transaction.transactionReceipt + "\n" +
                  "developerPayload -> " + transaction.originalTransaction + "\n" +
                  "DeveloperPayload -> " + transaction.developerPayload + "\n" +
                  "dataSignature -> " + transaction.dataSignature);
                //Send the receipt to my sevice to complete its register
                this.membershipService.registerRecepit(receipt).subscribe((response: any) => {
                  console.log("RECEIPT ->" + JSON.stringify(receipt));
                  console.log("RESPONSE->" + JSON.stringify(response));
                  //if all is OK I change the users permissions
                  if (!response.error) {
                    localStorage.removeItem('membership');
                    localStorage.removeItem('membershipID');
                    localStorage.setItem('membershipID', response.response.membership[0].id);
                    localStorage.setItem('membership', response.response.membership[0].authority);
                    localStorage.setItem('membershipExpiration', response.response.expirate);
                    let parts: any = response.response.expirate.split(" ");
                    let parts2: any = parts[0].split("-");
                    let year = parts2[0];
                    let month = parts2[1] - 1;
                    let day = parts2[2];
                    let expirationDate = new Date(year, month, day).toLocaleDateString();
                    let options = {
                      title: "Success payment",
                      message: "Thanks for buy our " + response.response.membership[0].authority + " membership! \nNow you have new functions until " + expirationDate + ". Check them!",
                      okButtonText: "Ok"
                    };
                    alert(options).then(() => {
                      this.routerExtensions.navigate(["/index"], { clearHistory: true });
                    });
                  } else {
                    let options = {
                      title: "We're sorry",
                      message: "Something is wrong with our app, please, try again later and if you want, cancel the suscription",
                      okButtonText: "Ok"
                    };
                    alert(options);
                });
              } else {
                confirm({
                  title: "Upps! RESPONSE CODE ---> "+responseCode,
                  message: "Payment not realized. Do you want to try again?",
                  okButtonText: "Yeah",
                  cancelButtonText: "No, thanks"
                }).then((result) => {
                  if (!result) {
                    this.routerExtensions.navigate(["/index"]);
                  }
                });
              }
            })
            .catch((e) => console.log(e));
        }
        else {
          applicationSettings.setBoolean(transaction.productIdentifier, true);
        }
      }
    });

And when I try to purchase with an Android cellphone, I just can see an Alert from Play Store saying "Se realizó con éxito el pago" but I can't see my messages in console and the app doesn't change the user permissions and doesn't do the change screen that I indicate with "this.routerExtensions.navigate(["/index"]);" in the code

Does someone have an idea about what is my error or what am I doing wrong? Thanks for your help!

PeterStaev commented 5 years ago

Hey @susanagonzavi , your code looks ok, but may be your purchases are not set up correctly in the play store? For example if your product id doesn't have .premium in its name, the code wont be executed?

I suggest you add a console log to start of the event handler and move it down so you can figure out what is going on.

PeterStaev commented 4 years ago

No further response so closing this one for now. In case you still have problems, please provide more details.