U
U
uzolenta2019-01-05 20:39:11
In-app purchases
uzolenta, 2019-01-05 20:39:11

Apple payment verification on server?

I am writing here already from almost hopelessness, I have tried a lot of different options, so I will not throw the code so as not to confuse. I feel like I'm doing something wrong.
Bottom line:
After an internal purchase in the application, I want to send Receipt for verification to Apple through my server.
1. If I send inside the application, then everything goes well.
2. If I try to send a Receipt already encoded in base64 to my server (in exactly the same way as in step 1), then PHP (Curl or file_get_contents) gives a broken data error. I found the problem: base64 from the application comes with a line break, if I remove the line break using preg_replace('![^\w\d\\\/+={}\"\:-]*!','',$ stroka) , then everything passes.
Actually please, who has a ready-made code for PHP, throw it off, please! And from the application side to send to your server.
I just really don’t understand what the problem is, I’m obviously stupid in some little thing.
Tried:
1) www.brianjcoleman.com/tutorial-receipt-validation-...
2) https://stackoverflow.com/questions/37566579/ios-t...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uzolenta, 2019-01-06
@uzolenta

Here I did, the problem was in base64. After generating Base64, you need to replace the characters: base64encodedReceipt.replacingOccurrences(of: "+", with: "%2B")

let receiptUrl: URL = Bundle.main.appStoreReceiptURL!
                    var receipt: Data?
                    receipt = try! NSData.init(contentsOf: receiptUrl) as Data
                    print(receipt)
                    var base64encodedReceipt: String = receipt!.base64EncodedString()
                        base64encodedReceipt = base64encodedReceipt.replacingOccurrences(of: "+", with: "%2B")
                        let vkUrl = URL(string: "test.php")
                        var request = URLRequest(url: vkUrl!)
                        let postString: String = "receipt-data=" + base64encodedReceipt
                        request.httpBody = postString.data(using: String.Encoding.utf8);
                        request.httpMethod = "POST"
                        request.addValue("\(postString.count)", forHTTPHeaderField: "Content-Length")
                        request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-type")
                        
                        URLSession.shared.dataTask(with: request) {(data, response, error) in
                            guard let data = data else {
                                let error_code = String(error!._code)
                                let error_description = String(error!.localizedDescription)
                                let error_msg = "Ошибка соединения. Пожалуйста, попробуйте позже. \n Если ошибка будет повторяться, то сообщите в службу поддержки приложения. \n Код ошибки: " + error_code + " \n Описание ошибки: \n" + error_description
                                let alertVC = UIAlertController(title: "Ошибка", message: error_msg, preferredStyle: .alert)
                                let actionOK = UIAlertAction(title: "OK", style: .default)
                                alertVC.addAction(actionOK)
                                
                                DispatchQueue.main.async(execute: {
                                    self.present(alertVC, animated: true, completion: nil)
                                })
                                return
                            }
                            do {
                                json = data
                                k = true
                            }
                            }.resume()

Server script.
<?php

$json['receipt-data'] = $_POST['receipt-data'];

$post = json_encode($json);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://buy.itunes.apple.com/verifyReceipt");
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);

curl_close ($ch);
echo $result;
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question