Module: Imobile::AppStoreReceiptValidation
- Defined in:
- lib/imobile/validate_receipt.rb
Overview
Implementation details for validate_receipt.
Class Method Summary collapse
-
.issue_request(http_request, store_uri) ⇒ Object
Issues a HTTP request to Apple’s receipt validating server.
-
.nice_receipt(json_receipt) ⇒ Object
Turns JSON receipt information into a nice Ruby-like receipt.
-
.process_response(http_response) ⇒ Object
Processes a HTTP response into a receipt.
-
.request(receipt_blob, store_uri) ⇒ Object
A Net:HTTP request for validating a receipt.
-
.store_uri(server_type) ⇒ Object
An URI object pointing to the App Store receipt validating server.
-
.validate_receipt(receipt_blob, server_type = :production) ⇒ Object
Real implementation of Imobile.validate_receipt.
Class Method Details
.issue_request(http_request, store_uri) ⇒ Object
Issues a HTTP request to Apple’s receipt validating server.
61 62 63 64 65 66 |
# File 'lib/imobile/validate_receipt.rb', line 61 def self.issue_request(http_request, store_uri) http = Net::HTTP.new store_uri.host, store_uri.port http.use_ssl = (store_uri.scheme == 'https') http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.request http_request end |
.nice_receipt(json_receipt) ⇒ Object
Turns JSON receipt information into a nice Ruby-like receipt.
String keys are mapped to Ruby symbols (e.g. ‘purchase-date’ becomes :purchase_date) and date strings are parsed to Ruby dates.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/imobile/validate_receipt.rb', line 72 def self.nice_receipt(json_receipt) nice = Hash[*json_receipt.map { |key, value| [key.gsub('-', '_').to_sym, value] }.flatten] [:purchase_date, :original_purchase_date].each do |key| nice[key] = DateTime.parse(nice[key]) if nice[key] end nice[:quantity] = nice[:quantity].to_i if nice[:quantity] nice end |
.process_response(http_response) ⇒ Object
Processes a HTTP response into a receipt.
86 87 88 89 90 91 92 93 94 |
# File 'lib/imobile/validate_receipt.rb', line 86 def self.process_response(http_response) unless http_response.kind_of? Net::HTTPSuccess raise "Internal error in Apple's Web service -- #{http_response.inspect}" end response = JSON.parse http_response.body return false unless response['status'] == 0 nice_receipt response['receipt'] end |
.request(receipt_blob, store_uri) ⇒ Object
A Net:HTTP request for validating a receipt.
53 54 55 56 57 58 |
# File 'lib/imobile/validate_receipt.rb', line 53 def self.request(receipt_blob, store_uri) request = Net::HTTP::Post.new store_uri.path request.set_content_type 'application/json' request.body = {'receipt-data' => Base64.encode64(receipt_blob) }.to_json request end |
.store_uri(server_type) ⇒ Object
An URI object pointing to the App Store receipt validating server.
The server type is production or sandbox (strings and symbols work).
43 44 45 46 47 48 49 50 |
# File 'lib/imobile/validate_receipt.rb', line 43 def self.store_uri(server_type) uri_string = { :production => "https://buy.itunes.apple.com/verifyReceipt", :sandbox => "https://sandbox.itunes.apple.com/verifyReceipt" }[server_type.to_sym] uri_string and URI.parse(uri_string) end |
.validate_receipt(receipt_blob, server_type = :production) ⇒ Object
Real implementation of Imobile.validate_receipt
97 98 99 100 101 |
# File 'lib/imobile/validate_receipt.rb', line 97 def self.validate_receipt(receipt_blob, server_type = :production) uri = store_uri server_type process_response issue_request(request(receipt_blob, uri), uri) end |