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

To initialize this class you will be required to pass in the request header and body. The body can be passed in parsed by json or without.


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

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 display an error message if verification fails.


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pay_with_amazon/ipn_handler.rb', line 63

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


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

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

#messageObject


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

def message
  @raw['Message']
end

#message_idObject


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

def message_id
  @raw['MessageId']
end

#message_timestampObject


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

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

#notification_dataObject


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

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

#notification_typeObject


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

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

#parse_from(json) ⇒ Object


137
138
139
# File 'lib/pay_with_amazon/ipn_handler.rb', line 137

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

#seller_idObject


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

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

#signatureObject


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

def signature
  @raw['Signature']
end

#signature_versionObject


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

def signature_version
  @raw['SignatureVersion']
end

#signing_cert_urlObject


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

def signing_cert_url
  @raw['SigningCertURL']
end

#timestampObject


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

def timestamp
  @raw['Timestamp']
end

#topic_arnObject


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

def topic_arn
  @raw['TopicArn']
end

#typeObject


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

def type
  @raw['Type']
end

#unsubscribe_urlObject


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

def unsubscribe_url
  @raw['UnsubscribeURL']
end

#versionObject


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

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