Class: Kakeibo::Client::Receipt::Iab

Inherits:
Object
  • Object
show all
Defined in:
lib/kakeibo/client/receipt/iab.rb

Constant Summary collapse

VALID_RECEIPT =
0
INVALID_JSON =
1
SIGNATURE_IS_NIL =
2
BASE64_ENCODED_PUBILC_KEY_IS_NIL =
3
INVALID_SIGNATURE =
4
INVALID_DEVELOPER_PAYLOAD =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_receipt_data, option = {}) ⇒ Iab

Returns a new instance of Iab.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kakeibo/client/receipt/iab.rb', line 19

def initialize(raw_receipt_data, option={})
  @raw_receipt_data = raw_receipt_data
  @receipt_data     = nil
  @signature                 = option[:signature]
  @base64_encoded_public_key = option[:base64_encoded_public_key]
  @developer_payload         = option[:developer_payload]

  @order_id = nil
  @status = nil
  valid?
end

Instance Attribute Details

#order_idObject (readonly)

Returns the value of attribute order_id.



16
17
18
# File 'lib/kakeibo/client/receipt/iab.rb', line 16

def order_id
  @order_id
end

#receipt_dataObject (readonly)

Returns the value of attribute receipt_data.



17
18
19
# File 'lib/kakeibo/client/receipt/iab.rb', line 17

def receipt_data
  @receipt_data
end

#statusObject (readonly)

Returns the value of attribute status.



15
16
17
# File 'lib/kakeibo/client/receipt/iab.rb', line 15

def status
  @status
end

Instance Method Details

#errorObject



65
66
67
68
# File 'lib/kakeibo/client/receipt/iab.rb', line 65

def error
  return nil if valid?
  Kakeibo::Error.new(@status, message)
end

#valid?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kakeibo/client/receipt/iab.rb', line 31

def valid?
  begin
    @receipt_data ||= JSON.parse(@raw_receipt_data, symbolize_names: true)
  rescue JSON::ParserError => e
    @status = INVALID_JSON
    return false
  end

  @order_id = @receipt_data[:orderId]

  if @signature.nil?
    @status = SIGNATURE_IS_NIL
    return false
  end
  if @base64_encoded_public_key.nil?
    @status = BASE64_ENCODED_PUBILC_KEY_IS_NIL
    return false
  end

  public_key = OpenSSL::PKey::RSA.new(Base64.decode64(@base64_encoded_public_key))
  unless public_key.verify(OpenSSL::Digest::SHA1.new, Base64.decode64(@signature), @raw_receipt_data)
    @status = INVALID_SIGNATURE
    return false
  end

  if !@receipt_data[:developerPayload].nil? && (@receipt_data[:developerPayload] != @developer_payload)
    @status = INVALID_DEVELOPER_PAYLOAD
    return false
  end

  @status = VALID_RECEIPT
  return true
end