summaryrefslogtreecommitdiff
path: root/tools/adyen/fake_notification.py
blob: ba54b298fff5b15f22f860b1d42d116b396b3259 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
#!/usr/bin/env python
#
# This script fakes an Adyen notification of different kinds, including
# sending it with correct passwords and such things.
# Clearly - use with care!
#
#
# Copyright (C) 2013, PostgreSQL Europe
#

import os
import sys
import base64
import urllib
import urllib2

# Set up for accessing django
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), '../../'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "postgresqleu.settings")
import django
django.setup()

from django.conf import settings


if __name__ == "__main__":
    body = ""
    if len(sys.argv) == 3 and sys.argv[1] == "raw":
        body = raw_input('Enter the http POST data as a single string: ')
    elif len(sys.argv) == 3 and sys.argv[1] == "prompt":
        fields = ('merchantAccountCode', 'pspReference', 'merchantReference', 'originalReference', 'eventDate', 'eventCode', 'paymentMethod', 'live', 'success', 'value', 'currency', 'reason', )
        vals = {}
        for f in fields:
            vals[f] = raw_input('%s:' % f)
        body = urllib.urlencode(vals)
    else:
        print "Usage: fake_notification.py <raw|prompt> <baseurl>"
        sys.exit(1)

    print "'%s'\n" % body
    base = sys.argv[2]
    url = "%s/p/adyen_notify/" % base
    while True:
        r = raw_input("Are you sure you want to send this notification to %s ?" % url)
        if r.lower().startswith('y'):
            break
        if r.lower().startswith('n'):
            sys.exit(0)

    req = urllib2.Request(url, body)
    req.add_header("Authorization", "Basic %s" % (
        base64.encodestring("%s:%s" % (
            settings.ADYEN_NOTIFY_USER,
            settings.ADYEN_NOTIFY_PASSWORD,
        )).replace('\n', '')))
    try:
        resp = urllib2.urlopen(req)
        print resp.read()
    except urllib2.HTTPError, e:
        print "Error %s from server" % e.code
        with open('/tmp/fake_notify_error.txt', 'w') as f:
            f.write(e.read())
        print "Content written to /tmp/fake_notify_error.txt"