Skip to content

Instantly share code, notes, and snippets.

@manuthu
Created February 1, 2018 20:23
Show Gist options
  • Save manuthu/fa083350086871a0e6f26906a2d07649 to your computer and use it in GitHub Desktop.
Save manuthu/fa083350086871a0e6f26906a2d07649 to your computer and use it in GitHub Desktop.
How to receive a HTTP message from AWS SNS - https://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html
import logging
import json
from flask import Flask, current_app, jsonify
from flask import request
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def index():
current_app.logger.info('Request Data %s', request.data)
json_data = json.loads(request.data)
c2b_alert = json.loads(json_data.get('Message'))
current_app.logger.info('c2b_alert Data %s', c2b_alert)
return jsonify(dict(success=True))
@manuthu
Copy link
Author

manuthu commented Feb 1, 2018

A sample response:

--------------------------------------------------------------------------------
INFO in server [server.py:13]:
Request Data {
  "Type" : "Notification",
  "MessageId" : "0e52f621-0fec-56a8-8771-a4cc7a80a802",
  "TopicArn" : "arn:aws:sns:eu-central-1:619031000055:mpesa-c2b-alerts-integ",
  "Subject" : "C2BAlerts",
  "Message" : "{\"@xmlns:ns1\": \"http://cps.huawei.com/cpsinterface/c2bpayment\", \"TransType\": \"Pay Bill\", \"TransID\": \"MB18IOQVY0\", \"TransTime\": \"20180201230603\", \"TransAmount\": \"10.00\", \"BusinessShortCode\": \"291222\", \"BillRefNumber\": \"test3\", \"OrgAccountBalance\": \"8830.00\", \"MSISDN\": \"254705000001\", \"KYCInfo\": [{\"KYCName\": \"[Personal Details][First Name]\", \"KYCValue\": \"NAME\"}, {\"KYCName\": \"[Personal Details][Middle Name]\", \"KYCValue\": \"TEST\"}, {\"KYCName\": \"[Personal Details][Last Name]\", \"KYCValue\": \"TEST\"}]}",
  "Timestamp" : "2018-02-01T20:06:04.892Z",
  "SignatureVersion" : "1",
  "Signature" : "RiSa0Ue/POP/GrOtGYQHmRSnxqNDthGWDgeekA8hAciBnqt5NiORtkoUHNCc38mbCWxfD5Y/0YRZuA6hfC1p40wujXRMGsnGwFcs562e0qNB2od6Y6fflptV9jXOyzUncU/V8jeD1GoEXqAX/m32NWcZhnqD+UG9ZqrHrGj8lSUR3fwS+TVlFEJteYE5T3RedMmej40jGPb4UoVpjy4LQSmbqGvU/WcPuF7e/B4M6gZZsnSQfV6c5w5jZ4bRT/v6D+L+cyHYn6nxwa1ZRe9r+SX7fD+LeCC/cSgHgVeCucNiJup2XJHmdNho3S8uLfh+diQl7jwyTtvAlxqPe3Yr9w==",
  "SigningCertURL" : "https://sns.eu-central-1.amazonaws.com/SimpleNotificationService-433026a4050d206028891664da859041.pem",
  "UnsubscribeURL" : "https://sns.eu-central-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-central-1:619031000055:mpesa-c2b-alerts-integ:7068cb0a-1ca0-440c-bf18-72d7647f0481"
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
INFO in server [server.py:15]:
c2b_alert {u'TransTime': u'20180201230603', u'BillRefNumber': u'test3', u'BusinessShortCode': u'291222', u'KYCInfo': [{u'KYCName': u'[Personal Details][First Name]', u'KYCValue': NAME'}, {u'KYCName': u'[Personal Details][Middle Name]', u'KYCValue': u'TEST'}, {u'KYCName': u'[Personal Details][Last Name]', u'KYCValue': u'TEST'}], u'@xmlns:ns1': u'http://cps.huawei.com/cpsinterface/c2bpayment', u'OrgAccountBalance': u'8830.00', u'MSISDN': u'254705000001', u'TransType': u'Pay Bill', u'TransAmount': u'10.00', u'TransID': u'MB18IOQVY0'}

@manuthu
Copy link
Author

manuthu commented Feb 1, 2018

The POST HEADERS:

POST / HTTP/1.1
x-amz-sns-message-type: Notification
x-amz-sns-message-id: 22b80b92-fdea-4c2c-8f9d-bdfb0c7bf324
x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic
x-amz-sns-subscription-arn: arn:aws:sns:us-west-2:123456789012:MyTopic:c9135db0-26c4-47ec-8998-413945fb5a96
Content-Length: 773
Content-Type: text/plain; charset=UTF-8
Host: example.com
Connection: Keep-Alive
User-Agent: Amazon Simple Notification Service Agent

@manuthu
Copy link
Author

manuthu commented Feb 1, 2018

THE PAYLOAD

{
  "Type" : "Notification",
  "MessageId" : "7136bb1e-7f42-596c-9f04-e9f31159294b",
  "TopicArn" : "arn:aws:sns:eu-central-1:619031000055:<>",
  "Subject" : "C2BAlerts",
  "Message" : "{\"@xmlns:ns1\": \"http://cps.huawei.com/cpsinterface/c2bpayment\", \"TransType\": \"Pay Bill\", \"TransID\": \"MB13IOIDE1\", \"TransTime\": \"20180201223450\", \"TransAmount\": \"10.00\", \"BusinessShortCode\": \"291222\", \"BillRefNumber\": \"testmike\", \"OrgAccountBalance\": \"8810.00\", \"MSISDN\": \"254705000001\", \"KYCInfo\": [{\"KYCName\": \"[Personal Details][First Name]\", \"KYCValue\": \"NAME\"}, {\"KYCName\": \"[Personal Details][Middle Name]\", \"KYCValue\": \"TEST\"}, {\"KYCName\": \"[Personal Details][Last Name]\", \"KYCValue\": \"TEST\"}]}",
  "Timestamp" : "2018-02-01T19:34:51.738Z",
  "SignatureVersion" : "1",
  "Signature" : "E9FSoL7HM+yRkALKx4CJ61C9IiMEPX6GgCtCGqSKdVRBLt2HipouGr7Zs9kIHKCdth/jY1AtXEVi0g0i0/pityh204yHO+/tVrsmJCnJA38tO899cvucQbOVk/N5NcFsBIhprp/zXxgzgd2bUsnrIq8H8t4gwvLKRk3Ejx5yh7Q+MA3PW/YqkuPkFpFwp1h564NyatTn1MVPTmJ75bkP9ZbW3MLkIGFp5CGc7EZCp/veNwQRJkoc5nqqUXX9wVhZ1psIvrHvbmyJ1vwJsOid+2/o2eSI4YlcnyBW1QvBC5jTR4HsDaf3IO1ZF94FcnSxBpRD2pUrLFCFp3sRZ7b3ow==",
  "SigningCertURL" : "https://sns.eu-central-1.amazonaws.com/SimpleNotificationService-433026a4050d206028891664da859041.pem",
  "UnsubscribeURL" : "https://sns.eu-central-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-central-1:619031000055:mpesa-c2b-alerts-integ:7068cb0a-1ca0-440c-bf18-72d7647f0481"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment