diff --git a/thepay/gateApi.py b/thepay/gateApi.py new file mode 100644 index 0000000..25020ba --- /dev/null +++ b/thepay/gateApi.py @@ -0,0 +1,48 @@ +from collections import OrderedDict +import hashlib +from datetime import tzinfo + +from suds.client import Client + +from six.moves.urllib.parse import urlencode + +from thepay.utils import SignatureMixin + + +class GateError(Exception): + pass + + +class GateApi(SignatureMixin): + def __init__(self, config): + """ + + :param config: Config + """ + self.config = config + self.client = None + + self.connect() + + def connect(self): + self.client = Client(self.config.webServicesWsdl) + + def _hashParam(self, params): + # this interface is using deprecated md5 hashing + return hashlib.md5(params).hexdigest() + + def _buildQuery(self, params): + # this interface uses different way of encoding + return urlencode(params) + + def cardCreateRecurrentPayment(self, merchantData, newMerchantData, value): + params = self._signParams(OrderedDict(( + ('merchantId', self.config.merchantId), + ('accountId', self.config.accountId), + ('merchantData', merchantData), + ('newMerchantData', newMerchantData), + ('value', value), + )), self.config.password) + response = self.client.service.cardCreateRecurrentPaymentRequest(**params) + if not response.status: + raise GateError(response.errorDescription) diff --git a/thepay/tests.py b/thepay/tests.py index cf27223..094629c 100644 --- a/thepay/tests.py +++ b/thepay/tests.py @@ -1,11 +1,13 @@ from __future__ import print_function import unittest +import uuid import datetime from datetime import tzinfo, timedelta from thepay.config import Config from thepay.dataApi import DataApi +from thepay.gateApi import GateApi, GateError from thepay.payment import Payment, ReturnPayment from six.moves import urllib @@ -145,3 +147,24 @@ def test_data(self): def test_missing_data(self): self.assertRaises(ReturnPayment.MissingParameter, lambda: self.payment.parseData({})) + + +class GateApiTests(unittest.TestCase): + def setUp(self): + self.config = Config() + self.gateApi = GateApi(self.config) + + def test_invalid_cardCreateRecurrentPayment(self): + with self.assertRaises(GateError): + self.gateApi.cardCreateRecurrentPayment( + '4394c54e-27f1-411b-b5e0-3f4e1ecf3e2c', + uuid.uuid4(), + 10 + ) + + def test_cardCreateRecurrentPayment(self): + self.gateApi.cardCreateRecurrentPayment( + 'c53be2ae-0b84-46e2-90f9-03c144a1a328', + uuid.uuid4(), + 10 + )