Class: Venice::Receipt

Inherits:
Object
  • Object
show all
Defined in:
lib/venice/receipt.rb

Defined Under Namespace

Classes: VerificationError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Receipt

Returns a new instance of Receipt.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/venice/receipt.rb', line 39

def initialize(attributes = {})
  @bundle_id = attributes['bundle_id']
  @application_version = attributes['application_version']
  @original_application_version = attributes['original_application_version']
  if attributes['original_purchase_date']
    @original_purchase_date = DateTime.parse(attributes['original_purchase_date'])
  end
  if attributes['expiration_date']
    @expires_at = Time.at(attributes['expiration_date'].to_i / 1000).to_datetime
  end

  @receipt_type = attributes['receipt_type']
  @adam_id = attributes['adam_id']
  @download_id = attributes['download_id']
  @requested_at = DateTime.parse(attributes['request_date']) if attributes['request_date']

  init_iap_receipts(attributes)
end

Instance Attribute Details

#adam_idObject (readonly)

Returns the value of attribute adam_id.



28
29
30
# File 'lib/venice/receipt.rb', line 28

def adam_id
  @adam_id
end

#application_versionObject (readonly)

The app’s version number.



12
13
14
# File 'lib/venice/receipt.rb', line 12

def application_version
  @application_version
end

#bundle_idObject (readonly)

The app’s bundle identifier.



9
10
11
# File 'lib/venice/receipt.rb', line 9

def bundle_id
  @bundle_id
end

#download_idObject (readonly)

Returns the value of attribute download_id.



29
30
31
# File 'lib/venice/receipt.rb', line 29

def download_id
  @download_id
end

#expires_atObject (readonly)

The date that the app receipt expires.



24
25
26
# File 'lib/venice/receipt.rb', line 24

def expires_at
  @expires_at
end

#in_appObject (readonly)

The receipt for an in-app purchase.



15
16
17
# File 'lib/venice/receipt.rb', line 15

def in_app
  @in_app
end

#latest_receiptObject

For auto-renewable subscriptions: the ‘receipt-data’ base64 representation of the last receipt if happened after the current receipt



35
36
37
# File 'lib/venice/receipt.rb', line 35

def latest_receipt
  @latest_receipt
end

#latest_receipt_infoObject

the in_app-transactions part of the last receipt



37
38
39
# File 'lib/venice/receipt.rb', line 37

def latest_receipt_info
  @latest_receipt_info
end

#original_application_versionObject (readonly)

The version of the app that was originally purchased.



18
19
20
# File 'lib/venice/receipt.rb', line 18

def original_application_version
  @original_application_version
end

#original_purchase_dateObject (readonly)

The original purchase date



21
22
23
# File 'lib/venice/receipt.rb', line 21

def original_purchase_date
  @original_purchase_date
end

#receipt_typeObject (readonly)

Non-Documented receipt keys/values



27
28
29
# File 'lib/venice/receipt.rb', line 27

def receipt_type
  @receipt_type
end

#requested_atObject (readonly)

Returns the value of attribute requested_at.



30
31
32
# File 'lib/venice/receipt.rb', line 30

def requested_at
  @requested_at
end

Class Method Details

.verify(data, options = {}) ⇒ Object Also known as: validate



98
99
100
# File 'lib/venice/receipt.rb', line 98

def verify(data, options = {})
  verify!(data, options) rescue false
end

.verify!(data, options = {}) ⇒ Object Also known as: validate!



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/venice/receipt.rb', line 102

def verify!(data, options = {})
  client = Client.production

  begin
    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

Instance Method Details

#init_iap_receipts(attributes) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/venice/receipt.rb', line 58

def init_iap_receipts(attributes)
  @in_app = map_iap_receipts(attributes['in_app'] || [])
  # 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

  # @latest_receipt_info = map_iap_receipts(attributes['latest_receipt_info'] || [])
  @latest_receipt_info = InAppReceipt.new (attributes['latest_receipt_info']) rescue []
  @latest_receipt = attributes['latest_receipt']
end

#map_iap_receipts(receipt_hashes) ⇒ Object



91
92
93
94
95
# File 'lib/venice/receipt.rb', line 91

def map_iap_receipts(receipt_hashes)
  receipt_hashes.map do |tx|
    InAppReceipt.new(tx)
  end
end

#to_hashObject Also known as: to_h



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/venice/receipt.rb', line 69

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.map{|iap| iap.to_h },
    :latest_receipt => @latest_receipt,
  }
end

#to_jsonObject



87
88
89
# File 'lib/venice/receipt.rb', line 87

def to_json
  self.to_hash.to_json
end