Created
August 28, 2015 05:10
-
-
Save koriaf/bed8e905fa46ff69e801 to your computer and use it in GitHub Desktop.
Small library to receive advcash.com SCI payments on your website.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdvcashProcessor(object): | |
'''Advcash SCI processing class - accept payments, check checksums and more | |
Developer documentation: | |
http://advcash.com/files/documents/sci_russian_2.pdf | |
Usage: | |
processor = MyWebsiteAdvcashProcessor(request_data, logger=transactions_logger) | |
verification_success = processor.validate() | |
if verification_success: | |
if processor.enroll(): | |
# havent found required responses, looks like even 5xx is fine for them and don't affect transaction status on remote end | |
return HttpResponse('ok') | |
else: | |
return HttpResponse('failed') | |
else: | |
transactions_logger.error('Wow. Wrong data from advcash. Such suspicious. Much strange. Wow.') | |
return HttpResponse('verification failed') | |
''' | |
def __init__(self, request_data, logger): | |
self._request_data = {} | |
for key in request_data.keys(): | |
self._request_data[key] = request_data.get(key) | |
self.logger = logger | |
self._validated = False | |
return | |
def validate(self): | |
'return True if calculated hash sighature equal to provided in request' | |
their_hash = self._request_data.get('ac_hash') | |
if not their_hash: | |
return False | |
self._request_data['ADVCASH_SCI_PASSWORD'] = settings.ADVCASH_SCI_PASSWORD | |
signature_string = ( | |
'{ac_transfer}:{ac_start_date}:{ac_sci_name}:{ac_src_wallet}:{ac_dest_wallet' | |
'}:{ac_order_id}:{ac_amount}:{ac_merchant_currency}:{ADVCASH_SCI_PASSWORD}' | |
).format(**self._request_data) | |
my_hash = hashlib.sha256(signature_string.encode('utf-8')).hexdigest() | |
self.__verification_result = my_hash == their_hash | |
return self.__verification_result | |
def enroll(self): | |
if not self.__verification_result: | |
raise Exception("Trying to enroll while verifications is failed or ommited!") | |
return self._do_enroll() | |
@transaction.atomic | |
def _do_enroll(self): | |
# here you can do whatever you want with self._request_data, it's validated. | |
# don't forget to check amounts (income, client outcome with tax, etc) and currency, | |
# about possibility of duplicated requests with same data | |
raise NotImplementedError('Please subclass AdvcashProcessor and implement _do_enroll') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment