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}