8000 Used zeep library by matllubos · Pull Request #2 · druids/thepay · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Used zeep library #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
language: python
python:
- "2.7"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
# does not have headers provided, please ask https://launchpad.ne 8000 t/~pypy/+archive/ppa
# maintainers to fix their pypy-dev package.
# command to install dependencies
install:
- pip install six
- pip install pytz
- pip install suds
- pip install zeep
- pip install coverage
- pip install coveralls
# command to run tests
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[metadata]
description-file = README.md
description_file = README.md
2 changes: 1 addition & 1 deletion thepay/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Config(object): 10000
class Config:

# ThePay API
gate_url = 'https://www.thepay.cz/demo-gate/'
Expand Down
17 changes: 11 additions & 6 deletions thepay/data_api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from collections import OrderedDict
import suds.client
from zeep import Client, Transport

from thepay.utils import SignatureMixin


class DataApi(SignatureMixin):

def __init__(self, config):
"""

Expand All @@ -16,21 +17,24 @@ def __init__(self, config):
self.connect()

def connect(self):
self.client = suds.client.Client(self.config.data_web_services_wsdl)
self.client = Client(self.config.data_web_services_wsdl, transport=self._get_transport())

def _get_transport(self):
return Transport()

def get_payment_methods(self):
params = self._sign_params(OrderedDict((
('merchantId', self.config.merchant_id),
('accountId', self.config.account_id),
)), self.config.data_api_password)
return self.client.service.getPaymentMethods(**params).methods[0]
return self.client.service.getPaymentMethods(**params).methods.method

def get_payment_state(self, payment_id):
params = self._sign_params(OrderedDict((
('merchantId', self.config.merchant_id),
('paymentId', payment_id),
)), self.config.data_api_password)
return int(self.client.service.getPaymentState(**params).state)
return self.client.service.getPaymentState(**params).state

def get_payment(self, payment_id):
params = self._sign_params(OrderedDict((
Expand Down Expand Up @@ -88,9 +92,10 @@ def get_payments(self, account_ids=None, value_from=None, value_to=None, created

signed_params = self._sign_params(params, self.config.data_api_password)

factory = self.client.type_factory('ns0')

if params.get('searchParams', {}).get('accountId'):
account_id_array = self.client.factory.create('idArray')
account_id_array.id = params['searchParams']['accountId']
account_id_array = factory.idArray(id=params['searchParams']['accountId'])
params['searchParams']['accountId'] = account_id_array

return self.client.service.getPayments(**signed_params)
5 changes: 3 additions & 2 deletions thepay/payment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict
import hashlib
from six.moves import urllib
from urllib.parse import urlencode

from decimal import Decimal
from thepay.utils import SignatureMixin
from thepay.exceptions import InvalidSignature, MissingParameter
Expand Down Expand Up @@ -82,7 +83,7 @@ def get_create_url(self):
:return: url-encoded string
"""
params = self._sign_params(self.get_params(), self.config.password)
return "{}?{}".format(self.config.gate_url, urllib.parse.urlencode(params))
return "{}?{}".format(self.config.gate_url, urlencode(params))


class ReturnPayment(SignatureMixin):
Expand Down
24 changes: 16 additions & 8 deletions thepay/tests.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
from __future__ import print_function

from datetime import datetime, date

from pytz import timezone

from urllib.parse import parse_qs

import unittest

from thepay.config import Config
from thepay.data_api import DataApi
from thepay.payment import Payment, ReturnPayment
from six.moves import urllib
from decimal import Decimal


class DataApiTests(unittest.TestCase):

def setUp(self):
super(DataApiTests, self).setUp()
self.config = Config()
self.data_api = DataApi(self.config)

def test_methods(self):
self.assertEqual(self.data_api.get_payment_methods()[0].name, 'Platba kartou')
self.assertEqual(self.data_api.get_payment_methods()[0].name, 'Platba24')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this changed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because there was changed data in testing api :(


def test_payment_statue(self):
self.assertEqual(self.data_api.get_payment_state(1), 2)
id = self.data_api.get_payments().payments.payment[0].id
self.assertEqual(self.data_api.get_payment_state(id), 1)

def test_payment(self):
self.assertEqual(self.data_api.get_payment(1).id, '1')
id = self.data_api.get_payments().payments.payment[0].id
self.assertEqual(
self.data_api.get_payment(id).id,
id
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be on one line


def test_payment_info(self):
self.data_api.get_payment_instructions(1)
id = self.data_api.get_payments().payments.payment[0].id
self.data_api.get_payment_instructions(id)

def test_credentials(self):
self.config.set_credentials(42, 43, 'test', 'test2')
Expand All @@ -52,6 +58,7 @@ def test_payments(self):


class PaymentTests(unittest.TestCase):

def setUp(self):
self.config = Config()
self.payment = Payment(self.config)
Expand Down Expand Up @@ -85,12 +92,13 @@ def test_url(self):


class ReturnPaymentTests(unittest.TestCase):

def setUp(self):
self.config = Config()

def test_data(self):
params_str = 'merchantId=1&accountId=1&value=123.00&currency=CZK&methodId=1&description=Order+123+payment&merchantData=Order+123&status=2&paymentId=34886&ipRating=&isOffline=0&needConfirm=0&signature=f38ff15cc17752a6d4035044a93deb06'
params = urllib.parse.parse_qs(params_str, keep_blank_values=True)
params = parse_qs(params_str, keep_blank_values=True)
params = {key: value[0] for key, value in params.items()}

payment = ReturnPayment(self.config, params)
Expand Down
9 changes: 4 additions & 5 deletions thepay/utils.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import hashlib
import six
from collections import OrderedDict


class SignatureMixin(object):
class SignatureMixin:

def _build_query(self, params):
results = []
for key, val in params.items():
if isinstance(val, dict):
val = self._hash_param(self._build_query(val).encode('utf-8'))
elif isinstance(val, (list, tuple)):
val = '|'.join(map(six.text_type, val))
val = '|'.join(map(str, val))
else:
val = six.text_type(val)
results.append('='.join((six.text_type(key), val)))
val = str(val)
results.append('='.join((str(key), val)))
return '&'.join(results)

def _sign_params(self, params, password):
Expand Down
0