All Files
(81.25%
covered at
3.01
hits/line)
4 files in total.
160 relevant lines.
130 lines covered and
30 lines missed
-
1
require 'venice/version'
-
1
require 'venice/client'
-
1
require 'venice/in_app_receipt'
-
1
require 'venice/receipt'
-
1
require 'json'
-
1
require 'net/https'
-
1
require 'uri'
-
-
1
module Venice
-
1
ITUNES_PRODUCTION_RECEIPT_VERIFICATION_ENDPOINT = "https://buy.itunes.apple.com/verifyReceipt"
-
1
ITUNES_DEVELOPMENT_RECEIPT_VERIFICATION_ENDPOINT = "https://sandbox.itunes.apple.com/verifyReceipt"
-
-
1
class Client
-
1
attr_accessor :verification_url
-
1
attr_writer :shared_secret
-
-
1
class << self
-
1
def development
-
client = self.new
-
client.verification_url = ITUNES_DEVELOPMENT_RECEIPT_VERIFICATION_ENDPOINT
-
client
-
end
-
-
1
def production
-
1
client = self.new
-
1
client.verification_url = ITUNES_PRODUCTION_RECEIPT_VERIFICATION_ENDPOINT
-
1
client
-
end
-
end
-
-
1
def initialize
-
5
@verification_url = ENV['IAP_VERIFICATION_ENDPOINT']
-
end
-
-
1
def verify!(data, options = {})
-
5
@verification_url ||= ITUNES_DEVELOPMENT_RECEIPT_VERIFICATION_ENDPOINT
-
5
@shared_secret = options[:shared_secret] if options[:shared_secret]
-
-
5
json = json_response_from_verifying_data(data)
-
5
status, receipt_attributes = json['status'].to_i, json['receipt']
-
5
receipt_attributes['original_json_response'] = json if receipt_attributes
-
-
5
case status
-
when 0, 21006
-
5
receipt = Receipt.new(receipt_attributes)
-
-
# From Apple docs:
-
# > Only returned for iOS 6 style transaction receipts for auto-renewable subscriptions.
-
# > The JSON representation of the receipt for the most recent renewal
-
5
if latest_receipt_info_attributes = json['latest_receipt_info']
-
# AppStore returns 'latest_receipt_info' even if we use over iOS 6. Besides, its format is an Array.
-
1
receipt.latest_receipt_info = []
-
1
latest_receipt_info_attributes.each do |latest_receipt_info_attribute|
-
# latest_receipt_info format is identical with in_app
-
1
receipt.latest_receipt_info << InAppReceipt.new(latest_receipt_info_attribute)
-
end
-
end
-
-
5
return receipt
-
else
-
raise Receipt::VerificationError.new(status, receipt)
-
end
-
end
-
-
1
private
-
-
1
def json_response_from_verifying_data(data)
-
3
parameters = {
-
'receipt-data' => data
-
}
-
-
3
parameters['password'] = @shared_secret if @shared_secret
-
-
3
uri = URI(@verification_url)
-
3
http = Net::HTTP.new(uri.host, uri.port)
-
3
http.use_ssl = true
-
3
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
-
-
3
request = Net::HTTP::Post.new(uri.request_uri)
-
3
request['Accept'] = "application/json"
-
3
request['Content-Type'] = "application/json"
-
3
request.body = parameters.to_json
-
-
3
response = http.request(request)
-
-
3
JSON.parse(response.body)
-
end
-
end
-
end
-
1
require 'time'
-
-
1
module Venice
-
1
class InAppReceipt
-
# For detailed explanations on these keys/values, see
-
# https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html#//apple_ref/doc/uid/TP40010573-CH106-SW12
-
-
# The number of items purchased. This value corresponds to the quantity property of
-
# the SKPayment object stored in the transaction’s payment property.
-
1
attr_reader :quantity
-
-
# The product identifier of the item that was purchased. This value corresponds to
-
# the productIdentifier property of the SKPayment object stored in the transaction’s
-
# payment property.
-
1
attr_reader :product_id
-
-
# The transaction identifier of the item that was purchased. This value corresponds
-
# to the transaction’s transactionIdentifier property.
-
1
attr_reader :transaction_id
-
-
# The date and time this transaction occurred. This value corresponds to the
-
# transaction’s transactionDate property.
-
1
attr_reader :purchased_at
-
-
# A string that the App Store uses to uniquely identify the application that created
-
# the payment transaction. If your server supports multiple applications, you can use
-
# this value to differentiate between them. Applications that are executing in the
-
# sandbox do not yet have an app-item-id assigned to them, so this key is missing from
-
# receipts created by the sandbox.
-
1
attr_reader :app_item_id
-
-
# An arbitrary number that uniquely identifies a revision of your application. This key
-
# is missing in receipts created by the sandbox.
-
1
attr_reader :version_external_identifier
-
-
# For a transaction that restores a previous transaction, this is the original receipt
-
1
attr_accessor :original
-
-
# For auto-renewable subscriptions, returns the date the subscription will expire
-
1
attr_reader :expires_at
-
-
# For a transaction that was canceled by Apple customer support, the time and date of the cancellation.
-
1
attr_reader :cancellation_at
-
-
-
1
def initialize(attributes = {})
-
22
@quantity = Integer(attributes['quantity']) if attributes['quantity']
-
22
@product_id = attributes['product_id']
-
22
@transaction_id = attributes['transaction_id']
-
22
@purchased_at = DateTime.parse(attributes['purchase_date']) if attributes['purchase_date']
-
22
@app_item_id = attributes['app_item_id']
-
22
@version_external_identifier = attributes['version_external_identifier']
-
-
# expires_date is in ms since the Epoch, Time.at expects seconds
-
22
@expires_at = Time.at(attributes['expires_date_ms'].to_i / 1000) if attributes['expires_date_ms']
-
-
# cancellation_date is in ms since the Epoch, Time.at expects seconds
-
22
@cancellation_date = Time.at(attributes['cancellation_date'].to_i / 1000) if attributes['cancellation_date']
-
-
22
if attributes['original_transaction_id'] || attributes['original_purchase_date']
-
11
original_attributes = {
-
'transaction_id' => attributes['original_transaction_id'],
-
'purchase_date' => attributes['original_purchase_date']
-
}
-
-
11
self.original = InAppReceipt.new(original_attributes)
-
end
-
-
end
-
-
1
def to_hash
-
{
-
:quantity => @quantity,
-
:product_id => @product_id,
-
:transaction_id => @transaction_id,
-
1
:purchase_date => (@purchased_at.httpdate rescue nil),
-
1
:original_transaction_id => (@original.transaction_id rescue nil),
-
1
:original_purchase_date => (@original.purchased_at.httpdate rescue nil),
-
:app_item_id => @app_item_id,
-
:version_external_identifier => @version_external_identifier,
-
1
:expires_at => (@expires_at.httpdate rescue nil),
-
1
:cancellation_at => (@cancellation_at.httpdate rescue nil)
-
1
}
-
end
-
1
alias_method :to_h, :to_hash
-
-
1
def to_json
-
self.to_hash.to_json
-
end
-
-
end
-
end
-
1
require 'time'
-
-
1
module Venice
-
1
class Receipt
-
# For detailed explanations on these keys/values, see
-
# https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html#//apple_ref/doc/uid/TP40010573-CH106-SW1
-
-
# The app’s bundle identifier.
-
1
attr_reader :bundle_id
-
-
# The app’s version number.
-
1
attr_reader :application_version
-
-
# The receipt for an in-app purchase.
-
1
attr_reader :in_app
-
-
# The version of the app that was originally purchased.
-
1
attr_reader :original_application_version
-
-
# The original purchase date
-
1
attr_reader :original_purchase_date
-
-
# The date that the app receipt expires.
-
1
attr_reader :expires_at
-
-
# Non-Documented receipt keys/values
-
1
attr_reader :receipt_type
-
1
attr_reader :adam_id
-
1
attr_reader :download_id
-
1
attr_reader :requested_at
-
-
# Original json response from AppStore
-
1
attr_reader :original_json_response
-
-
-
1
attr_accessor :latest_receipt_info
-
-
1
def initialize(attributes = {})
-
6
@original_json_response = attributes['original_json_response']
-
-
6
@bundle_id = attributes['bundle_id']
-
6
@application_version = attributes['application_version']
-
6
@original_application_version = attributes['original_application_version']
-
6
if attributes['original_purchase_date']
-
5
@original_purchase_date = DateTime.parse(attributes['original_purchase_date'])
-
end
-
6
if attributes['expiration_date']
-
5
@expires_at = Time.at(attributes['expiration_date'].to_i / 1000).to_datetime
-
end
-
-
6
@receipt_type = attributes['receipt_type']
-
6
@adam_id = attributes['adam_id']
-
6
@download_id = attributes['download_id']
-
6
@requested_at = DateTime.parse(attributes['request_date']) if attributes['request_date']
-
-
6
@in_app = []
-
6
if attributes['in_app']
-
5
attributes['in_app'].each do |in_app_purchase_attributes|
-
5
@in_app << InAppReceipt.new(in_app_purchase_attributes)
-
end
-
end
-
-
end
-
-
1
def to_hash
-
{
-
:bundle_id => @bundle_id,
-
:application_version => @application_version,
-
:original_application_version => @original_application_version,
-
:original_purchase_date => (@original_purchase_date.httpdate rescue nil),
-
:expires_at => (@expires_at.httpdate rescue nil),
-
:receipt_type => @receipt_type,
-
:adam_id => @adam_id,
-
:download_id => @download_id,
-
:requested_at => (@requested_at.httpdate rescue nil),
-
:in_app => @in_app.map{|iap| iap.to_h },
-
:latest_receipt_info => @latest_receipt_info
-
}
-
end
-
1
alias_method :to_h, :to_hash
-
-
1
def to_json
-
self.to_hash.to_json
-
end
-
-
1
class << self
-
1
def verify(data, options = {})
-
1
verify!(data, options) rescue false
-
end
-
-
1
def verify!(data, options = {})
-
1
client = Client.production
-
-
1
begin
-
1
client.verify!(data, options)
-
rescue VerificationError => error
-
case error.code
-
when 21007
-
client = Client.development
-
retry
-
when 21008
-
client = Client.production
-
retry
-
else
-
raise error
-
end
-
end
-
end
-
-
1
alias :validate :verify
-
1
alias :validate! :verify!
-
end
-
-
1
class VerificationError < StandardError
-
1
attr_accessor :code
-
1
attr_accessor :receipt
-
-
1
def initialize(code, receipt)
-
@code = Integer(code)
-
@receipt = receipt
-
end
-
-
1
def message
-
case @code
-
when 21000
-
"The App Store could not read the JSON object you provided."
-
when 21002
-
"The data in the receipt-data property was malformed."
-
when 21003
-
"The receipt could not be authenticated."
-
when 21004
-
"The shared secret you provided does not match the shared secret on file for your account."
-
when 21005
-
"The receipt server is not currently available."
-
when 21006
-
"This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response."
-
when 21007
-
"This receipt is a sandbox receipt, but it was sent to the production service for verification."
-
when 21008
-
"This receipt is a production receipt, but it was sent to the sandbox service for verification."
-
else
-
"Unknown Error: #{@code}"
-
end
-
end
-
end
-
end
-
end