Class: Venice::InAppReceipt

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ InAppReceipt

Returns a new instance of InAppReceipt.



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
# File 'lib/venice/in_app_receipt.rb', line 60

def initialize(attributes = {})
  @original_json_data = attributes
  @quantity = Integer(attributes['quantity']) if attributes['quantity']
  @product_id = attributes['product_id']
  @transaction_id = attributes['transaction_id']
  @web_order_line_item_id = attributes['web_order_line_item_id']
  @purchased_at = DateTime.parse(attributes['purchase_date']) if attributes['purchase_date']
  @app_item_id = attributes['app_item_id']
  @version_external_identifier = attributes['version_external_identifier']
  @is_trial_period = attributes['is_trial_period'].to_s == 'true' if attributes['is_trial_period']
  @is_in_intro_offer_period = attributes['is_in_intro_offer_period'] == 'true' if attributes['is_in_intro_offer_period']

  # expires_date is in ms since the Epoch, Time.at expects seconds
  if attributes['expires_date_ms']
    @expires_at = Time.at(attributes['expires_date_ms'].to_i / 1000)
  elsif attributes['expires_date'] && is_number?(attributes['expires_date'])
    @expires_at = Time.at(attributes['expires_date'].to_i / 1000)
  end

  # cancellation_date is in ms since the Epoch, Time.at expects seconds
  @cancellation_at = Time.at(attributes['cancellation_date_ms'].to_i / 1000) if attributes['cancellation_date_ms']

  if attributes['original_transaction_id'] || attributes['original_purchase_date']
    original_attributes = {
      'transaction_id' => attributes['original_transaction_id'],
      'purchase_date' => attributes['original_purchase_date']
    }

    self.original = InAppReceipt.new(original_attributes)
  end
end

Instance Attribute Details

#app_item_idObject (readonly)

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.



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

def app_item_id
  @app_item_id
end

#cancellation_atObject (readonly)

For a transaction that was canceled by Apple customer support, the time and date of the cancellation. For an auto-renewable subscription plan that was upgraded, the time and date of the upgrade transaction.



51
52
53
# File 'lib/venice/in_app_receipt.rb', line 51

def cancellation_at
  @cancellation_at
end

#expires_atObject (readonly)

For auto-renewable subscriptions, returns the date the subscription will expire



47
48
49
# File 'lib/venice/in_app_receipt.rb', line 47

def expires_at
  @expires_at
end

#is_in_intro_offer_periodObject (readonly)

Only present for auto-renewable subscription receipts. Value is true if the customer’s subscription is currently in an introductory price period, false if not, nil if key is not present on receipt.



58
59
60
# File 'lib/venice/in_app_receipt.rb', line 58

def is_in_intro_offer_period
  @is_in_intro_offer_period
end

#is_trial_periodObject (readonly)

Only present for auto-renewable subscription receipts. Value is true if the customer’s subscription is currently in the free trial period, false if not, nil if key is not present on receipt.



55
56
57
# File 'lib/venice/in_app_receipt.rb', line 55

def is_trial_period
  @is_trial_period
end

#originalObject

For a transaction that restores a previous transaction, this is the original receipt



44
45
46
# File 'lib/venice/in_app_receipt.rb', line 44

def original
  @original
end

#original_json_dataObject (readonly)

Original JSON data returned from Apple for an InAppReceipt object.



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

def original_json_data
  @original_json_data
end

#product_idObject (readonly)

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.



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

def product_id
  @product_id
end

#purchased_atObject (readonly)

The date and time this transaction occurred. This value corresponds to the transaction’s transactionDate property.



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

def purchased_at
  @purchased_at
end

#quantityObject (readonly)

The number of items purchased. This value corresponds to the quantity property of the SKPayment object stored in the transaction’s payment property.



13
14
15
# File 'lib/venice/in_app_receipt.rb', line 13

def quantity
  @quantity
end

#transaction_idObject (readonly)

The transaction identifier of the item that was purchased. This value corresponds to the transaction’s transactionIdentifier property.



22
23
24
# File 'lib/venice/in_app_receipt.rb', line 22

def transaction_id
  @transaction_id
end

#version_external_identifierObject (readonly)

An arbitrary number that uniquely identifies a revision of your application. This key is missing in receipts created by the sandbox.



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

def version_external_identifier
  @version_external_identifier
end

#web_order_line_item_idObject (readonly)

The primary key for identifying subscription purchases. This value is a unique ID that identifies purchase events across devices, including subscription renewal purchase events. When restoring purchase, transaction_id could change



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

def web_order_line_item_id
  @web_order_line_item_id
end

Instance Method Details

#to_hashObject Also known as: to_h



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/venice/in_app_receipt.rb', line 92

def to_hash
  {
    quantity: @quantity,
    product_id: @product_id,
    transaction_id: @transaction_id,
    web_order_line_item_id: @web_order_line_item_id,
    purchase_date: (@purchased_at.httpdate rescue nil),
    original_transaction_id: (@original.transaction_id rescue nil),
    original_purchase_date: (@original.purchased_at.httpdate rescue nil),
    app_item_id: @app_item_id,
    version_external_identifier: @version_external_identifier,
    is_trial_period: @is_trial_period,
    is_in_intro_offer_period: @is_in_intro_offer_period,
    expires_at: (@expires_at.httpdate rescue nil),
    cancellation_at: (@cancellation_at.httpdate rescue nil)
  }
end

#to_jsonObject



111
112
113
# File 'lib/venice/in_app_receipt.rb', line 111

def to_json
  to_hash.to_json
end