iOS/Swift
[Swift] Check Appstore receipts validation
min_ee
2021. 2. 19. 16:01
영수증 유효성 체크
간혹가다 영수증 유효성 체크를 해야하는 경우가 있다.
유효성 체크는 로컬과 서버를 통해서 체크하는 방법이 있다.
이 글은 서버를 통해 유효성 체크를 하는 방법이다.
func checkReceiptValidation(with receipt: String, isProduction: Bool = true) {
var urlString: String = ""
if isProduction {
urlString = "https://buy.itunes.apple.com/verifyReceipt"
} else {
urlString = "https://sandbox.itunes.apple.com/verifyReceipt"
}
let url = URL(string: urlString)!
let dic: [String: Any] = [
"password": "공유 암호",
"receipt-data": receipt
]
var request = URLRequest(url: url)
request.httpBody = try? JSONSerialization.data(withJSONObject: dic, options: [])
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data,
let object = try? JSONSerialization.jsonObject(with: data, options: []),
let json = object as? [String: Any] else {
return
}
print(json)
if let statusCode = json["status"] as? Int,
statusCode == 21007 {
self.checkReceiptValidation(with: receipt, isProduction: false)
}
}
task.resume()
}
production과 test를 어떻게 구분할까 하고 찾다가
status 21007 - This receipt is from the test environment, but it was sent to the production environment for verification.
을 보고 sandbox인데 production을 호출하면 그냥 sandbox링크로 재호출하자는 생각으로 저렇게 짰다.
아래 참고의 status를 통해 다른 code들도 확인할 수 있으니 본인에게 알맞게 에러코드를 짜면 될 것 같다.
공유 암호 찾는 법
App Store Connect - 앱 내 구입 - 관리 - 앱 공유 암호
혹시 다른 방법이 있다면 댓글 남겨주세용
참고
developer.apple.com/documentation/appstorereceipts/verifyreceipt
Apple Developer Documentation
developer.apple.com
developer.apple.com/documentation/appstorereceipts/status
Apple Developer Documentation
developer.apple.com