Skip to content

Commit

Permalink
Start with signature code
Browse files Browse the repository at this point in the history
  • Loading branch information
dokterbob committed Jul 6, 2011
1 parent b16b3e9 commit 7dcadd4
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions adyen/interface.py
@@ -1,3 +1,81 @@
import logging
logger = logging.getLogger(__name__)

from adyen import settings


class PaymentInterface(object):
"""
Wrapper around Adyen API calls.
This object is stateless and does not use any settings, hence it can be
used easily in non-Django applications.
"""

# URL's for single- and multi-page checkouts (test and production)
TEST_URL_BASE = 'https://test.adyen.com/hpp/'
TEST_URL_SINGLE = TEST_URL_BASE + 'pay.shtml'
TEST_URL_MULTIPLE = TEST_URL_BASE + 'select.shtml'

PROD_URL_BASE = 'https://live.adyen.com/hpp/'
PROD_URL_SINGLE = TEST_URL_BASE + 'pay.shtml'
PROD_URL_MULTIPLE = TEST_URL_BASE + 'select.shtml'

# Fields used for signing payment requests
REQUEST_SIGNATURE_FIELDS = \
('paymentAmount', 'currencyCode', 'shipBeforeDate',
'merchantReference', 'skinCode', 'merchantAccount',
'sessionValidity', 'shopperEmail', 'shopperReference',
'allowedMethods', 'blockedMethods', 'shopperStatement',
'billingAddressType')

# Fields used for signing billing addresses
ADDRESS_SIGNATURE_FIELDS = \
('billingAddress.street', 'billingAddress.houseNumberOrName',
'billingAddress.city', 'billingAddress.postalCode',
'billingAddress.stateOrProvince', 'billingAddress.country')

# Fields used for verification of result signatures
RESULT_SIGNATURE_FIELDS = \
('authResult', 'pspReference', 'merchantReference', 'skinCode')

def __init__(self, params=None, request=None):
"""
Initialize the interface.
"""
assert request or params, \
'Please specify either a request or a set of parameters'

if not params:
params = request.REQUEST

self.params = params

# We haven't parsed anything yet
self.parsed = False

@staticmethod
def _sign_plaintext(plaintext, secret):
"""
Sign the string `plaintext` with a given `secret` using HMAC and
encode the result as a base64 encoded string.
Source: Adyen Python signature implementation example.
"""
import base64
import hmac
import hashlib

hm = hmac.new(secret, data, hashlib.sha1)
return base64.encodestring(hm.digest()).strip()

@staticmethod
def _data_to_plaintext(data, fields):
"""
Concatenate the specified `fields` from the `data` dictionary.
"""
plaintext = ''
for field in fields:
plaintext += data.get(field, '')

return plaintext

0 comments on commit 7dcadd4

Please sign in to comment.