2

For the last 7 days I have been trying to create the Auto-Renewable Subscription.I have done it without the Shared Secret,i was following a tutorial,and the working code was with Storing Information on cloud.I wanted to use Reciept Validation but the code in the tutorial was full of bugs,and also the PHP code was full of errors.I have found another free tutorial, but the same thing,only bugs,no way of making it run.Till now I have searched the whole GitHub,i have payed the Lynda tutorial,and found lots of other tutorials but non of them is working.Is there any tutorial that you might suggest me,or can you help me out with the code.

Here is the swift code which has 2 bugs: Bugs in code func validateReceipt(){

var receiptUrl = NSBundle.mainBundle().appStoreReceiptURL
var receipt: NSData = NSData.dataWithContentsOfURL(receiptUrl!, options: nil, error: nil)

var response: NSURLResponse?
var error: NSError?

var receiptdata:NSString = receipt.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

var request = NSMutableURLRequest(URL: NSURL(string: "http://mm214.com/write.php")!)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"

var err: NSError?

request.HTTPBody = receiptdata.dataUsingEncoding(NSASCIIStringEncoding)

var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
    var err: NSError?
    var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary

    if err != nil  {
        print(err!.localizedDescription)
        let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("Error could not parse JSON: '\(jsonStr)'")
    }
    else {
        if let parseJSON = json {
            println("Receipt \(parseJSON)")
        }
        else {
            let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("Receipt Error: \(jsonStr)")
        }
    }
})

task.resume()

Here is the php code:

<?php
        function getReceiptData($receipt)
        {
            $fh = fopen('showme.txt',w);
            fwrite($fh,$receipt);
            fclose($fh);
            $endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';

            $ch = curl_init($endpoint);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $receipt);
            $response = curl_exec($ch);
            $errno = curl_errno($ch);
            $errmsg = curl_error($ch);
            curl_close($ch);
            $msg = $response.' - '.$errno.' - '.$errmsg;
            echo $response;
        }

    foreach ($_POST as $key=>$value){
        $newcontent .= $key.' '.$value;
    }

    $new = trim($newcontent);
    $new = trim($newcontent);
    $new = str_replace('_','+',$new);
    $new = str_replace(' =','==',$new);

    if (substr_count($new,'=') == 0){
    if (strpos('=',$new) === false){
            $new .= '=';
    }
    }

    $new = '{"receipt-data":"'.$new.'","password":"shared secreet"}';
    $info = getReceiptData($new);
    ?>

I'm getting the error on 31 line :

Warning: strpos(): Empty needle in /home2/arslan/public_html/verifyReciep.php on line 31 {"status":21002}

Avi
  • 7,469
  • 2
  • 21
  • 22
  • So you are sending receipt data to Php server and Php server using receipt data to communicate with iTunes server for receipt validation. Make sure you are getting proper receipt. – technerd Jan 21 '16 at 12:41
  • Yes thats correct couse it can be manipulated if i talk directly to the itunes server.The problem is in my code if you know how to solve it would be great. –  Jan 21 '16 at 12:43

1 Answers1

0

21002 - The data in the receipt-data property was malformed or missing.

Here receipt malformed when you convert it to NSData.

In the iOS 7 , Apple introduced new base64 methods on NSData that make it unnecessary to use a 3rd party base 64 decoding library , but i still suggest you to try to use Base64 encoding for receipt encoding. I hope this will solve your problem. As i also face same issue of 21002 when I verify receipt from server side and this encoding library works in this case. Don't know How , but it solved my issue at server side for receipt validation call. Hope it will work for you also.

Follow given steps :

  1. Download Base64 from here.
  2. Add Base64.h and Base64.m in your project. enter image description here
  3. Add bridging header to use Objective C file in Swift class. enter image description here

To create Bridging Header.

  1. Now use base64EncodedString() of added class.

Follow my answer for more detail. In-app purchase receipt verification for auto renewal using AFNetworking objective-c

Community
  • 1
  • 1
technerd
  • 14,144
  • 10
  • 61
  • 92
  • Can you help me a bit with it. –  Jan 21 '16 at 13:02
  • So im getting the error on let encodeddata = data!.base64EncodedString(); NSHasNomember of base64Endocing and expirationDateFromResponse has no member. –  Jan 21 '16 at 13:03
  • Just download Base 64 encoding decoding files from this link and add it to project and use it. https://github.com/nicklockwood/Base64 – technerd Jan 21 '16 at 13:06
  • Answer updated for step wise integration of Base64 class. This will surely work for you. Thanks – technerd Jan 21 '16 at 13:11
  • Thanks what do i place here? requestContents.setObject(encodeddata, forKey: "receipt-data"); requestContents.setObject("xxxxxxxxxxxxxxxxxxxxxxx", forKey: "password"); –  Jan 21 '16 at 13:19
  • You have to send your sharedSecret as password to server. – technerd Jan 21 '16 at 13:21
  • So i place my shared secret pass there? –  Jan 21 '16 at 13:23
  • Yes, server needs shared secret as password to communicate with iTunes server. You can find shared secret from iTunes store . – technerd Jan 21 '16 at 13:25
  • Sorry can you help me out I'm getting the json.But I'm unable to validate it in the correct way.Can you help me i will send you my code ? –  Jan 21 '16 at 13:46
  • So, now what error code you getting in receipt validation . Are you getting 21004 ? – technerd Jan 21 '16 at 13:49
  • It's not an error.I get the json output,but when i follow the tutoral from lynda with the non working validation.I have placed this validation,but its not calling it.Here is my code https://gist.github.com/ArslanHaj/679d4a1df236bacdb9c8 –  Jan 21 '16 at 13:52
  • Ok, i will check it. – technerd Jan 21 '16 at 13:54
  • Thanks a lot sorry for wasting your time –  Jan 21 '16 at 13:55
  • Please answer my comment on gist. – technerd Jan 21 '16 at 14:02
  • I have 2min ago sorry i havet seen it at the beggining. –  Jan 21 '16 at 14:03