I have some rails code which get's an in-app purchase receipt from Apple and verifies it towards either sandbox or production. This has worked fine, but as of lately I am seeing some 21002 (The data in the receipt-data property was malformed) errors in my logs. I can see these are from apps in production.
Below is my RoR code that verifies the receipt, do you see anything that would cause a 21002?
Thanks so much!
def verifyReceipt (receipt_data)
# Checking the magazine status to get the proper verification url
if Publication.find_by_app_id(params[:app_id]).development_mode
logger.info "Sandbox mode detected"
url = "https://sandbox.itunes.apple.com/verifyReceipt"
else
logger.info "Production mode detected"
url = "https://buy.itunes.apple.com/verifyReceipt"
end
# Get the magazines shared secret
shared_secret = Publication.where("app_id = ?", params[:app_id]).first.itunes_shared_secret
if shared_secret.nil? or shared_secret.blank?
# Invalid magazine
logger.info "Shared secret does not exist or does not match iTunes shared secret!"
result = false
else
logger.info "Verifying receipt from Apple now!"
# Verify receipt with apple based on magazine status, and save results
data = { "receipt-data" => receipt_data, "password" => shared_secret }
request = Typhoeus::Request.new(url, method: :post, body: data.to_json )
request.run
result = JSON.parse(request.response.body).with_indifferent_access
logger.info "Result from verification: #{result[:status]}"
end
return result
end
If it is on production, it is a normal behavior to get 21002 errors some times.
The major cause is someone use JB device or IAP cracker on your native app and try to do a fake purchase. The fake receipt failed in the AppStore validation and a 21002 error is returned.
You may do a formal purchase to make sure everything works well in case.