Class: Venice::Receipt

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

Defined Under Namespace

Classes: VerificationError

Constant Summary collapse

MAX_RE_VERIFY_COUNT =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Receipt

Returns a new instance of Receipt.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/venice/receipt.rb', line 43

def initialize(attributes = {})
  @original_json_response = attributes['original_json_response']

  @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']
  @receipt_created_at = DateTime.parse(attributes['receipt_creation_date']) if attributes['receipt_creation_date']

  @in_app = []
  if attributes['in_app']
    attributes['in_app'].each do |in_app_purchase_attributes|
      @in_app << InAppReceipt.new(in_app_purchase_attributes)
    end
  end

  @pending_renewal_info = []
  if original_json_response && original_json_response['pending_renewal_info']
    original_json_response['pending_renewal_info'].each do |pending_renewal_attributes|
      @pending_renewal_info << PendingRenewalInfo.new(pending_renewal_attributes)
    end
  end
end

Instance Attribute Details

#adam_idObject (readonly)

Returns the value of attribute adam_id.



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

def adam_id
  @adam_id
end

#application_versionObject (readonly)

The app’s version number.



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

def application_version
  @application_version
end

#bundle_idObject (readonly)

The app’s bundle identifier.



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

def bundle_id
  @bundle_id
end

#download_idObject (readonly)

Returns the value of attribute download_id.



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

def download_id
  @download_id
end

#expires_atObject (readonly)

The date that the app receipt expires.



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

def expires_at
  @expires_at
end

#in_appObject (readonly)

The receipt for an in-app purchase.



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

def in_app
  @in_app
end

#latest_receipt_infoObject

Returns the value of attribute latest_receipt_info.



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

def latest_receipt_info
  @latest_receipt_info
end

#original_application_versionObject (readonly)

The version of the app that was originally purchased.



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

def original_application_version
  @original_application_version
end

#original_json_responseObject (readonly)

Original json response from AppStore



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

def original_json_response
  @original_json_response
end

#original_purchase_dateObject (readonly)

The original purchase date



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

def original_purchase_date
  @original_purchase_date
end

#pending_renewal_infoObject (readonly)

Information about the status of the customer’s auto-renewable subscriptions



41
42
43
# File 'lib/venice/receipt.rb', line 41

def pending_renewal_info
  @pending_renewal_info
end

#receipt_created_atObject (readonly)

Returns the value of attribute receipt_created_at.



33
34
35
# File 'lib/venice/receipt.rb', line 33

def receipt_created_at
  @receipt_created_at
end

#receipt_typeObject (readonly)

Non-Documented receipt keys/values



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

def receipt_type
  @receipt_type
end

#requested_atObject (readonly)

Returns the value of attribute requested_at.



32
33
34
# File 'lib/venice/receipt.rb', line 32

def requested_at
  @requested_at
end

Class Method Details

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



101
102
103
104
105
# File 'lib/venice/receipt.rb', line 101

def verify(data, options = {})
  verify!(data, options)
rescue VerificationError, Client::TimeoutError
  false
end

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/venice/receipt.rb', line 107

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

  retry_count = 0
  begin
    client.verify!(data, options)
  rescue VerificationError => error
    case error.code
    when 21007
      client = Client.development
      retry
    when 21008
      client = Client.production
      retry
    else
      retry_count += 1
      if error.retryable? && retry_count <= MAX_RE_VERIFY_COUNT
        retry
      end

      raise error
    end
  rescue Net::ReadTimeout, Timeout::Error, OpenSSL::SSL::SSLError,
         Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE
    # verifyReceipt is idempotent so we can retry it.
    # Net::Http has retry logic for some idempotent http methods but it verifyReceipt is POST.
    retry_count += 1
    retry if retry_count <= MAX_RE_VERIFY_COUNT
    raise
  end
end

Instance Method Details

#to_hashObject Also known as: to_h



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/venice/receipt.rb', line 77

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),
    receipt_created_at: (@receipt_created_at.httpdate rescue nil),
    in_app: @in_app.map(&:to_h),
    pending_renewal_info: @pending_renewal_info.map(&:to_h),
    latest_receipt_info: @latest_receipt_info
  }
end

#to_jsonObject



96
97
98
# File 'lib/venice/receipt.rb', line 96

def to_json
  to_hash.to_json
end