Class: PayWithAmazon::IpnHandler
- Inherits:
-
Object
- Object
- PayWithAmazon::IpnHandler
- 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
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#proxy_addr ⇒ Object
Returns the value of attribute proxy_addr.
-
#proxy_pass ⇒ Object
Returns the value of attribute proxy_pass.
-
#proxy_port ⇒ Object
Returns the value of attribute proxy_port.
-
#proxy_user ⇒ Object
Returns the value of attribute proxy_user.
Instance Method Summary collapse
-
#authentic? ⇒ Boolean
This method will authenticate the ipn message sent from Amazon.
- #environment ⇒ Object
-
#initialize(headers, body, proxy_addr: :ENV, proxy_port: nil, proxy_user: nil, proxy_pass: nil) ⇒ IpnHandler
constructor
To initialize this class you will be required to pass in the request header and body.
- #message ⇒ Object
- #message_id ⇒ Object
- #message_timestamp ⇒ Object
- #notification_data ⇒ Object
- #notification_type ⇒ Object
- #parse_from(json) ⇒ Object
- #seller_id ⇒ Object
- #signature ⇒ Object
- #signature_version ⇒ Object
- #signing_cert_url ⇒ Object
- #timestamp ⇒ Object
- #topic_arn ⇒ Object
- #type ⇒ Object
- #unsubscribe_url ⇒ Object
- #version ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
31 32 33 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 31 def body @body end |
#headers ⇒ Object (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_addr ⇒ Object
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_pass ⇒ Object
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_port ⇒ Object
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_user ⇒ Object
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. end end |
#environment ⇒ Object
121 122 123 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 121 def environment parse_from(@raw['Message'])["ReleaseEnvironment"] end |
#message ⇒ Object
89 90 91 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 89 def @raw['Message'] end |
#message_id ⇒ Object
81 82 83 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 81 def @raw['MessageId'] end |
#message_timestamp ⇒ Object
133 134 135 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 133 def parse_from(@raw['Message'])["Timestamp"] end |
#notification_data ⇒ Object
129 130 131 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 129 def notification_data parse_from(@raw['Message'])["NotificationData"] end |
#notification_type ⇒ Object
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_id ⇒ Object
117 118 119 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 117 def seller_id parse_from(@raw['Message'])["SellerId"] end |
#signature ⇒ Object
97 98 99 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 97 def signature @raw['Signature'] end |
#signature_version ⇒ Object
101 102 103 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 101 def signature_version @raw['SignatureVersion'] end |
#signing_cert_url ⇒ Object
105 106 107 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 105 def signing_cert_url @raw['SigningCertURL'] end |
#timestamp ⇒ Object
93 94 95 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 93 def @raw['Timestamp'] end |
#topic_arn ⇒ Object
85 86 87 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 85 def topic_arn @raw['TopicArn'] end |
#type ⇒ Object
77 78 79 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 77 def type @raw['Type'] end |
#unsubscribe_url ⇒ Object
109 110 111 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 109 def unsubscribe_url @raw['UnsubscribeURL'] end |
#version ⇒ Object
125 126 127 |
# File 'lib/pay_with_amazon/ipn_handler.rb', line 125 def version parse_from(@raw['Message'])["Version"] end |