Class: PayWithAmazon::IpnHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/pay_with_amazon/ipn_handler.rb

Overview

Pay with Amazon Ipn Handler

This class authenticates an sns message sent from Amazon. It will validate the header, subject, and certificate. After validation there are many helper methods in place to extract information received from the ipn notification.

Constant Summary collapse

SIGNABLE_KEYS =
[
  'Message',
  'MessageId',
  'Timestamp',
  'TopicArn',
  'Type',
].freeze
COMMON_NAME =
'sns.amazonaws.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, body, proxy_addr: :ENV, proxy_port: nil, proxy_user: nil, proxy_pass: nil) ⇒ IpnHandler

Returns a new instance of IpnHandler.

Parameters:



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

def initialize(
        headers,
        body,
        proxy_addr: :ENV,
        proxy_port: nil,
        proxy_user: nil,
        proxy_pass: nil)

  @body = body
  @raw = parse_from(@body)
  @headers = headers
  @proxy_addr = proxy_addr
  @proxy_port = proxy_port
  @proxy_user = proxy_user
  @proxy_pass = proxy_pass
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



31
32
33
# File 'lib/pay_with_amazon/ipn_handler.rb', line 31

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



31
32
33
# File 'lib/pay_with_amazon/ipn_handler.rb', line 31

def headers
  @headers
end

#proxy_addrObject

Returns the value of attribute proxy_addr.



32
33
34
# File 'lib/pay_with_amazon/ipn_handler.rb', line 32

def proxy_addr
  @proxy_addr
end

#proxy_passObject

Returns the value of attribute proxy_pass.



32
33
34
# File 'lib/pay_with_amazon/ipn_handler.rb', line 32

def proxy_pass
  @proxy_pass
end

#proxy_portObject

Returns the value of attribute proxy_port.



32
33
34
# File 'lib/pay_with_amazon/ipn_handler.rb', line 32

def proxy_port
  @proxy_port
end

#proxy_userObject

Returns the value of attribute proxy_user.



32
33
34
# File 'lib/pay_with_amazon/ipn_handler.rb', line 32

def proxy_user
  @proxy_user
end

Instance Method Details

#authentic?Boolean

This method will authenticate the ipn message sent from Amazon. It will return true if everything is verified. It will raise an error message if verification fails.

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pay_with_amazon/ipn_handler.rb', line 60

def authentic?
  begin
    decoded_from_base64 = Base64.decode64(signature)
    validate_header
    validate_subject(get_certificate.subject)
    public_key = get_public_key_from(get_certificate)
    verify_public_key(public_key, decoded_from_base64, canonical_string)

    return true
  rescue IpnWasNotAuthenticError => e
    raise e.message
  end
end

#environmentObject



118
119
120
# File 'lib/pay_with_amazon/ipn_handler.rb', line 118

def environment
  parse_from(@raw['Message'])["ReleaseEnvironment"]
end

#messageObject



86
87
88
# File 'lib/pay_with_amazon/ipn_handler.rb', line 86

def message
  @raw['Message']
end

#message_idObject



78
79
80
# File 'lib/pay_with_amazon/ipn_handler.rb', line 78

def message_id
  @raw['MessageId']
end

#message_timestampObject



130
131
132
# File 'lib/pay_with_amazon/ipn_handler.rb', line 130

def message_timestamp
  parse_from(@raw['Message'])["Timestamp"]
end

#notification_dataObject



126
127
128
# File 'lib/pay_with_amazon/ipn_handler.rb', line 126

def notification_data
  parse_from(@raw['Message'])["NotificationData"]
end

#notification_typeObject



110
111
112
# File 'lib/pay_with_amazon/ipn_handler.rb', line 110

def notification_type
  parse_from(@raw['Message'])["NotificationType"]
end

#parse_from(json) ⇒ Object



134
135
136
# File 'lib/pay_with_amazon/ipn_handler.rb', line 134

def parse_from(json)
  JSON.parse(json)
end

#seller_idObject



114
115
116
# File 'lib/pay_with_amazon/ipn_handler.rb', line 114

def seller_id
  parse_from(@raw['Message'])["SellerId"]
end

#signatureObject



94
95
96
# File 'lib/pay_with_amazon/ipn_handler.rb', line 94

def signature
  @raw['Signature']
end

#signature_versionObject



98
99
100
# File 'lib/pay_with_amazon/ipn_handler.rb', line 98

def signature_version
  @raw['SignatureVersion']
end

#signing_cert_urlObject



102
103
104
# File 'lib/pay_with_amazon/ipn_handler.rb', line 102

def signing_cert_url
  @raw['SigningCertURL']
end

#timestampObject



90
91
92
# File 'lib/pay_with_amazon/ipn_handler.rb', line 90

def timestamp
  @raw['Timestamp']
end

#topic_arnObject



82
83
84
# File 'lib/pay_with_amazon/ipn_handler.rb', line 82

def topic_arn
  @raw['TopicArn']
end

#typeObject



74
75
76
# File 'lib/pay_with_amazon/ipn_handler.rb', line 74

def type
  @raw['Type']
end

#unsubscribe_urlObject



106
107
108
# File 'lib/pay_with_amazon/ipn_handler.rb', line 106

def unsubscribe_url
  @raw['UnsubscribeURL']
end

#versionObject



122
123
124
# File 'lib/pay_with_amazon/ipn_handler.rb', line 122

def version
  parse_from(@raw['Message'])["Version"]
end