0

I'm trying to implement ionic cordova plugin InAppPurchase. Plugin function getReceipt() returns long string that I have to decode. I read this article https://scottbolinger.com/cordova-app-purchases-validating-subscriptions/ and did exactly as there shown but still it doesn't work. For example here is my code

import {Http, Headers, RequestOptions} from '@angular/http';
import { InAppPurchase } from '@ionic-native/in-app-purchase';

constructor(
    public pay:InAppPurchase,
    public http: Http
  )
{
Restore()
{
          let response = this.checkStatus().then(response =>{
            console.log(response)
          }).catch(err=>{
           console.log(err)
          });
        }
 }
checkStatus() {
    return new Promise( (resolve, reject) => {

        // first, ask Apple for the receipt. We get back an encrypted string.
        this.pay.getReceipt()
        .then( receipt => {
          return receipt;

        })
        .then( receiptData => {

          // next, validate the encoded receipt with Apple, and get back the human readable format
          this.validateReceipt( receiptData ).then( receipt => {

            console.log(receipt);

          })

        })
        .catch( err => {
          reject( err );

        })

    })

  }
validateReceipt( receiptData ) {

    return new Promise( (resolve ) => {

      let headers = new Headers({ 'Content-Type': 'application/json' });
      let options = new RequestOptions({ headers: headers });

      let data = {
        'password': 'XXXXXXXXXXXXXXXXXXXXX', // the shared secret key for your in app purchase https://stackoverflow.com/questions/5022296/generate-shared-secret-key-in-itunes-connect-for-in-app-purchase
        'receipt-data': receiptData
      }

      this.http.post( 'https://buy.itunes.apple.com/verifyReceipt', data, options )
          .subscribe(response => {

            let receipt = JSON.parse(response['_body']).receipt
            resolve(receipt)
            // purchases can be found at receipt.in_app <Array>
            // will need to loop through them and get most recent, then work with expiration date
          },
          error => {
            // probably a bad url or 404
            console.log(error);
          })

    })

  }

In simple words I just copy pasted the example from article just implementing in it my shared password and it gave me nothing. Who knows how the problem could be solved please help.

2
  • I don't think that article is a great example to follow. Firstly author is not ensuring they call IAP plugin on platform ready etc. Secondly the receipt validation call in this case is initiated by...client and then the client calls developer's server for "fulfillment". This is a bad pattern. Ideally you want client to get receipt, forward it to your backend, which will do service to service call to validate receipt and handle fulfillment, then only get back to client and confirm fulfillment/update state. I would seek for another article to follow;/ Mar 13, 2019 at 15:29
  • Sergey Rudenko thanks for the tip. You're right this example is awful nevertheless I took valuable information from it. Apr 18, 2019 at 7:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.