1
# Copyright 2013 Canonical Ltd.
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.
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.
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/>.
16
from django.conf import settings
17
from piston_mini_client import auth, failhandlers
19
import u1testutils.pay.environment
22
def get_pay_client(pay_server_url=None):
23
"""Return the Payment Service API client.
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.
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)
39
def _get_pay_server_url(pay_server_url=None):
40
if pay_server_url is None:
42
u1testutils.pay.environment.get_pay_base_url()
44
pay_server_url = pay_server_url.rstrip('/')
48
def add_new_credit_card(shop_id, user, credit_card, billing_address,
50
"""Add a new credit card through the Pay API.
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
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
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
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
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
83
credit_card_request = payclient.CreditCardRequest(**kwargs)
84
pay_client = get_pay_client(pay_server_url)
86
response = pay_client.new_credit_card(data=credit_card_request)
87
return isinstance(response, payclient.CreditCardResponse)
88
except failhandlers.APIError:
93
def _get_country_code(country_name):
95
country_data = {'United States': 'US'}
96
return country_data.get(country_name)