<s> from __future__ import unicode_literals <EOL> import binascii <EOL> from cryptography . hazmat . primitives import padding <EOL> from cryptography . hazmat . primitives . ciphers import Cipher , algorithms , modes <EOL> from cryptography . hazmat . backends import default_backend <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . shortcuts import redirect <EOL> from . . core import BasicProvider <EOL> class SagepayProvider ( BasicProvider ) : <EOL> '''<STR_LIT>''' <EOL> _version = '<STR_LIT>' <EOL> _action = '<STR_LIT>' <EOL> def __init__ ( self , vendor , encryption_key , endpoint = _action , ** kwargs ) : <EOL> self . _vendor = vendor <EOL> self . _enckey = encryption_key . encode ( '<STR_LIT:utf-8>' ) <EOL> self . _action = endpoint <EOL> super ( SagepayProvider , self ) . __init__ ( ** kwargs ) <EOL> if not self . _capture : <EOL> raise ImproperlyConfigured ( <EOL> '<STR_LIT>' ) <EOL> def _get_cipher ( self ) : <EOL> backend = default_backend ( ) <EOL> return Cipher ( algorithms . AES ( self . _enckey ) , modes . CBC ( self . _enckey ) , <EOL> backend = backend ) <EOL> def _get_padding ( self ) : <EOL> return padding . PKCS7 ( <NUM_LIT> ) <EOL> def aes_enc ( self , data ) : <EOL> data = data . encode ( '<STR_LIT:utf-8>' ) <EOL> padder = self . _get_padding ( ) . padder ( ) <EOL> data = padder . update ( data ) + padder . finalize ( ) <EOL> encryptor = self . _get_cipher ( ) . encryptor ( ) <EOL> enc = encryptor . update ( data ) + encryptor . finalize ( ) <EOL> return b"<STR_LIT:@>" + binascii . hexlify ( enc ) <EOL> def aes_dec ( self , data ) : <EOL> data = data . lstrip ( b'<STR_LIT:@>' ) <EOL> data = binascii . unhexlify ( data ) <EOL> decryptor = self . _get_cipher ( ) . decryptor ( ) <EOL> data = decryptor . update ( data ) + decryptor . finalize ( ) <EOL> return data . decode ( '<STR_LIT:utf-8>' ) <EOL> def get_hidden_fields ( self , payment ) : <EOL> payment . save ( ) <EOL> return_url = self . get_return_url ( payment ) <EOL> data = { <EOL> '<STR_LIT>' : payment . pk , <EOL> '<STR_LIT>' : "<STR_LIT>" % ( payment . total , ) , <EOL> '<STR_LIT>' : payment . currency , <EOL> '<STR_LIT>' : "<STR_LIT>" % ( payment . pk , ) , <EOL> '<STR_LIT>' : return_url , <EOL> '<STR_LIT>' : return_url , <EOL> '<STR_LIT>' : payment . billing_last_name , <EOL> '<STR_LIT>' : payment . billing_first_name , <EOL> '<STR_LIT>' : payment . billing_address_1 , <EOL> '<STR_LIT>' : payment . billing_address_2 , <EOL> '<STR_LIT>' : payment . billing_city , <EOL> '<STR_LIT>' : payment . billing_postcode , <EOL> '<STR_LIT>' : payment . billing_country_code , <EOL> '<STR_LIT>' : payment . billing_last_name , <EOL> '<STR_LIT>' : payment . billing_first_name , <EOL> '<STR_LIT>' : payment . billing_address_1 , <EOL> '<STR_LIT>' : payment . billing_address_2 , <EOL> '<STR_LIT>' : payment . billing_city , <EOL> '<STR_LIT>' : payment . billing_postcode , <EOL> '<STR_LIT>' : payment . billing_country_code } <EOL> if payment . billing_country_code == '<STR_LIT>' : <EOL> data [ '<STR_LIT>' ] = payment . billing_country_area <EOL> data [ '<STR_LIT>' ] = payment . billing_country_area <EOL> udata = "<STR_LIT:&>" . join ( "<STR_LIT>" % kv for kv in data . items ( ) ) <EOL> crypt = self . aes_enc ( udata ) <EOL> return { '<STR_LIT>' : self . _version , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . _vendor , '<STR_LIT>' : crypt } <EOL> def process_data ( self , payment , request ) : <EOL> udata = self . aes_dec ( request . GET [ '<STR_LIT>' ] ) <EOL> data = { } <EOL> for kv in udata . split ( '<STR_LIT:&>' ) : <EOL> k , v = kv . split ( '<STR_LIT:=>' ) <EOL> data [ k ] = v <EOL> success_url = payment . get_success_url ( ) <EOL> if payment . status == '<STR_LIT>' : <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT:OK>' : <EOL> payment . captured_amount = payment . total <EOL> payment . change_status ( '<STR_LIT>' ) <EOL> return redirect ( success_url ) <EOL> else : <EOL> payment . change_status ( '<STR_LIT>' ) <EOL> return redirect ( payment . get_failure_url ( ) ) <EOL> return redirect ( success_url ) </s>
