~vila/u1-test-utils/for-leo

« back to all changes in this revision

Viewing changes to u1testutils/pay/client.py

  • Committer: Tarmac
  • Author(s): leo.arias at canonical
  • Date: 2013-03-26 16:00:53 UTC
  • mfrom: (38.1.8 pay-client2)
  • Revision ID: tarmac-20130326160053-ptekwkaduekchrxp
[r=matiasb] Added helpers for the Pay API client.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2013 Canonical Ltd.
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify it
 
4
# under the terms of the GNU Lesser General Public License version 3, as
 
5
# published by the Free Software Foundation.
 
6
#
 
7
# This program is distributed in the hope that it will be useful, but
 
8
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
9
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
10
# PURPOSE.  See the GNU Lesser General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License along
 
13
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
14
 
 
15
import payclient
 
16
from django.conf import settings
 
17
from piston_mini_client import auth, failhandlers
 
18
 
 
19
import u1testutils.pay.environment
 
20
 
 
21
 
 
22
def get_pay_client(pay_server_url=None):
 
23
    """Return the Payment Service API client.
 
24
 
 
25
    pay_server_url -- The URL of the Pay server that provides the API. Default
 
26
        is None, which means that the URL will be taken from value of the
 
27
        Django settings PAY_SERVER_URL option.
 
28
 
 
29
    """
 
30
    user = settings.PAY_API_USERNAME
 
31
    password = settings.PAY_API_PASSWORD
 
32
    basic_auth = auth.BasicAuthorizer(user, password)
 
33
    pay_server_url = _get_pay_server_url(pay_server_url)
 
34
    pay_api_url = '{0}/api/2.0/'.format(pay_server_url)
 
35
    return payclient.PaymentServiceAPI(
 
36
        auth=basic_auth, service_root=pay_api_url)
 
37
 
 
38
 
 
39
def _get_pay_server_url(pay_server_url=None):
 
40
    if pay_server_url is None:
 
41
        pay_server_url = \
 
42
            u1testutils.pay.environment.get_pay_base_url()
 
43
    else:
 
44
        pay_server_url = pay_server_url.rstrip('/')
 
45
    return pay_server_url
 
46
 
 
47
 
 
48
def add_new_credit_card(shop_id, user, credit_card, billing_address,
 
49
                        pay_server_url=None):
 
50
    """Add a new credit card through the Pay API.
 
51
 
 
52
    Keyword arguments:
 
53
    shop_id -- The identifier of the shop that will add the credit card.
 
54
    user -- The user that will have the new credit card. It must have the
 
55
        openid attribute.
 
56
    credit_card -- The credit card information. It must have the attributes
 
57
        card_type,  name_on_cad, card_number, ccv_number, expiration_month and
 
58
        expiration_year.
 
59
    billing_address -- The billing address for the card. It must have the
 
60
        attributes street_line1, state, country and postal_code. The country
 
61
        is the name of the country in english.
 
62
    pay_server_url -- The URL of the Pay server that provides the API. Default
 
63
        is None.
 
64
 
 
65
    """
 
66
    billing_address_dict = {
 
67
        'street': billing_address.street_line1,
 
68
        'state': billing_address.state,
 
69
        'country': _get_country_code(billing_address.country),
 
70
        'postal': billing_address.postal_code
 
71
    }
 
72
    kwargs = dict(
 
73
        shop_id=shop_id,
 
74
        open_id=user.openid,
 
75
        card_type=credit_card.card_type,
 
76
        card_holder=credit_card.name_on_card,
 
77
        card_number=credit_card.card_number,
 
78
        card_ccv=credit_card.ccv_number,
 
79
        card_expiration_month=credit_card.expiration_month,
 
80
        card_expiration_year=credit_card.expiration_year,
 
81
        billing_address=billing_address_dict
 
82
    )
 
83
    credit_card_request = payclient.CreditCardRequest(**kwargs)
 
84
    pay_client = get_pay_client(pay_server_url)
 
85
    try:
 
86
        response = pay_client.new_credit_card(data=credit_card_request)
 
87
        return isinstance(response, payclient.CreditCardResponse)
 
88
    except failhandlers.APIError:
 
89
        # TODO log the error.
 
90
        return False
 
91
 
 
92
 
 
93
def _get_country_code(country_name):
 
94
    # TODO add more?
 
95
    country_data = {'United States': 'US'}
 
96
    return country_data.get(country_name)