Class: Itunes::Receipt

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

Defined Under Namespace

Classes: ExpiredReceiptReceived, ReceiptServerOffline, SandboxReceiptReceived, VerificationFailed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Receipt

Returns a new instance of Receipt.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/itunes/receipt.rb', line 52

def initialize(attributes = {})
  receipt_attributes = attributes.with_indifferent_access[:receipt]
  @adam_id = receipt_attributes[:adam_id]
  @app_item_id = receipt_attributes[:app_item_id]
  @application_version = receipt_attributes[:application_version]
  @bid = receipt_attributes[:bid]
  @bundle_id = receipt_attributes[:bundle_id]
  @bvrs = receipt_attributes[:bvrs]
  @download_id = receipt_attributes[:download_id]
  @expires_date = if receipt_attributes[:expires_date]
    Time.at(receipt_attributes[:expires_date].to_i / 1000)
  end
  @in_app = if receipt_attributes[:in_app]
    receipt_attributes[:in_app].map { |ia| self.class.new(:receipt => ia) }
  end
  @is_trial_period = if receipt_attributes[:is_trial_period]
    receipt_attributes[:is_trial_period]
  end
  @itunes_env = attributes[:itunes_env] || Itunes.itunes_env
  @latest = if attributes[:latest_receipt_info]
    full_receipt_data = attributes[:latest_receipt]
    self.class.new(
      :receipt        => attributes[:latest_receipt_info],
      :latest_receipt => full_receipt_data,
      :receipt_type   => :latest
    )
  end
  @original = if receipt_attributes[:original_transaction_id] || receipt_attributes[:original_purchase_date]
    self.class.new(:receipt => {
      :transaction_id      => receipt_attributes[:original_transaction_id],
      :purchase_date       => receipt_attributes[:original_purchase_date],
      :purchase_date_ms    => receipt_attributes[:original_purchase_date_ms],
      :purchase_date_pst   => receipt_attributes[:original_purchase_date_pst],
      :application_version => receipt_attributes[:original_application_version]
    })
  end
  @product_id = receipt_attributes[:product_id]
  @purchase_date = if receipt_attributes[:purchase_date]
    Time.parse receipt_attributes[:purchase_date].sub('Etc/GMT', 'GMT')
  end
  @purchase_date_ms = if receipt_attributes[:purchase_date_ms]
    receipt_attributes[:purchase_date_ms].to_i
  end
  @purchase_date_pst = if receipt_attributes[:purchase_date_pst]
    Time.parse receipt_attributes[:purchase_date_pst].sub('America/Los_Angeles', 'PST')
  end
  @quantity = if receipt_attributes[:quantity]
    receipt_attributes[:quantity].to_i
  end
  @receipt_data = if attributes[:receipt_type] == :latest
    attributes[:latest_receipt]
  end
  @request_date = if receipt_attributes[:request_date]
    Time.parse receipt_attributes[:request_date].sub('Etc/', '')
  end
  @request_date_ms = if receipt_attributes[:request_date_ms]
    receipt_attributes[:request_date_ms].to_i
  end
  @request_date_pst = if receipt_attributes[:request_date_pst]
    Time.parse receipt_attributes[:request_date_pst].sub('America/Los_Angeles', 'PST')
  end
  @transaction_id = receipt_attributes[:transaction_id]
  @version_external_identifier = receipt_attributes[:version_external_identifier]
end

Instance Attribute Details

#adam_idObject (readonly)

Returns the value of attribute adam_id.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def adam_id
  @adam_id
end

#app_item_idObject (readonly)

Returns the value of attribute app_item_id.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def app_item_id
  @app_item_id
end

#application_versionObject (readonly)

Returns the value of attribute application_version.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def application_version
  @application_version
end

#bidObject (readonly)

Returns the value of attribute bid.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def bid
  @bid
end

#bundle_idObject (readonly)

Returns the value of attribute bundle_id.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def bundle_id
  @bundle_id
end

#bvrsObject (readonly)

Returns the value of attribute bvrs.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def bvrs
  @bvrs
end

#download_idObject (readonly)

Returns the value of attribute download_id.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def download_id
  @download_id
end

#expires_dateObject (readonly)

Returns the value of attribute expires_date.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def expires_date
  @expires_date
end

#in_appObject (readonly)

Returns the value of attribute in_app.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def in_app
  @in_app
end

#is_trial_periodObject (readonly)

Returns the value of attribute is_trial_period.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def is_trial_period
  @is_trial_period
end

#itunes_envObject (readonly)

Returns the value of attribute itunes_env.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def itunes_env
  @itunes_env
end

#latestObject (readonly)

Returns the value of attribute latest.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def latest
  @latest
end

#originalObject (readonly)

Returns the value of attribute original.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def original
  @original
end

#product_idObject (readonly)

Returns the value of attribute product_id.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def product_id
  @product_id
end

#purchase_dateObject (readonly)

Returns the value of attribute purchase_date.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def purchase_date
  @purchase_date
end

#purchase_date_msObject (readonly)

Returns the value of attribute purchase_date_ms.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def purchase_date_ms
  @purchase_date_ms
end

#purchase_date_pstObject (readonly)

Returns the value of attribute purchase_date_pst.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def purchase_date_pst
  @purchase_date_pst
end

#quantityObject (readonly)

Returns the value of attribute quantity.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def quantity
  @quantity
end

#receipt_dataObject (readonly)

Returns the value of attribute receipt_data.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def receipt_data
  @receipt_data
end

#request_dateObject (readonly)

Returns the value of attribute request_date.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def request_date
  @request_date
end

#request_date_msObject (readonly)

Returns the value of attribute request_date_ms.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def request_date_ms
  @request_date_ms
end

#request_date_pstObject (readonly)

Returns the value of attribute request_date_pst.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def request_date_pst
  @request_date_pst
end

#transaction_idObject (readonly)

Returns the value of attribute transaction_id.



25
26
27
# File 'lib/itunes/receipt.rb', line 25

def transaction_id
  @transaction_id
end

Class Method Details

.verify!(receipt_data, allow_sandbox_receipt = false) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/itunes/receipt.rb', line 140

def self.verify!(receipt_data, allow_sandbox_receipt = false)
  request_data = {:'receipt-data' => receipt_data}
  request_data.merge!(:password => Itunes.shared_secret) if Itunes.shared_secret
  response = post_to_endpoint(request_data)
  begin
    successful_response(response)
  rescue SandboxReceiptReceived => e
    # Retry with sandbox, as per:
    # http://developer.apple.com/library/ios/#technotes/tn2259/_index.html
    #   FAQ#16
    if allow_sandbox_receipt
      sandbox_response = post_to_endpoint(request_data, Itunes::ENDPOINT[:sandbox])
      successful_response(
        sandbox_response.merge(:itunes_env => :sandbox)
      )
    else
      raise e
    end
  end
end

Instance Method Details

#application_receipt?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/itunes/receipt.rb', line 132

def application_receipt?
  !@bundle_id.nil?
end

#as_jsonObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/itunes/receipt.rb', line 117

def as_json
  {
    :quantity                => @quantity,
    :product_id              => @product_id,
    :transaction_id          => @transaction_id,
    :purchase_date           => @purchase_date,
    :original_transaction_id => @original.try(:transaction_id),
    :original_purchase_date  => @original.try(:purchase_date),
    :is_trial_period         => @is_trial_period
  }.tap do |hash|
    hash[:application_version] = @application_version if @application_version.present?
    hash[:bundle_id]           = @bundle_id           if @bundle_id.present?
  end
end

#sandbox?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/itunes/receipt.rb', line 136

def sandbox?
  itunes_env == :sandbox
end