11import braintree
2+ import warnings
3+ from braintree .util .deprecation_util import DeprecationUtil
24from braintree .util .http import Http
35from braintree .successful_result import SuccessfulResult
46from braintree .error_result import ErrorResult
57from braintree .resource import Resource
68from braintree .address import Address
79from braintree .exceptions .not_found_error import NotFoundError
810from braintree .configuration import Configuration
11+ from braintree .ids_search import IdsSearch
12+ from braintree .resource_collection import ResourceCollection
913from braintree .transparent_redirect import TransparentRedirect
1014
1115class CreditCard (Resource ):
@@ -96,8 +100,9 @@ def confirm_transparent_redirect(query_string):
96100 result = braintree.CreditCard.confirm_transparent_redirect_request("foo=bar&id=12345")
97101 """
98102
99- id = TransparentRedirect .parse_and_validate_query_string (query_string )
100- return CreditCard .__post ("/payment_methods/all/confirm_transparent_redirect_request" , {"id" : id })
103+ warnings .warn ("Please use TransparentRedirect.confirm instead" , DeprecationWarning )
104+ id = TransparentRedirect .parse_and_validate_query_string (query_string )["id" ][0 ]
105+ return CreditCard ._post ("/payment_methods/all/confirm_transparent_redirect_request" , {"id" : id })
101106
102107 @staticmethod
103108 def create (params = {}):
@@ -111,7 +116,7 @@ def create(params={}):
111116 """
112117
113118 Resource .verify_keys (params , CreditCard .create_signature ())
114- return CreditCard .__post ("/payment_methods" , {"credit_card" : params })
119+ return CreditCard ._post ("/payment_methods" , {"credit_card" : params })
115120
116121 @staticmethod
117122 def update (credit_card_token , params = {}):
@@ -141,6 +146,35 @@ def delete(credit_card_token):
141146 Http ().delete ("/payment_methods/" + credit_card_token )
142147 return SuccessfulResult ()
143148
149+ @staticmethod
150+ def expired ():
151+ """ Return a collection of expired credit cards. """
152+ response = Http ().post ("/payment_methods/all/expired_ids" )
153+ return ResourceCollection (None , response , CreditCard .__fetch_expired )
154+
155+ @staticmethod
156+ def expiring_between (start_date , end_date ):
157+ """ Return a collection of credit cards expiring between the given dates. """
158+ formatted_start_date = start_date .strftime ("%m%Y" )
159+ formatted_end_date = end_date .strftime ("%m%Y" )
160+ query = "start=%s&end=%s" % (formatted_start_date , formatted_end_date )
161+ response = Http ().post ("/payment_methods/all/expiring_ids?" + query )
162+ return ResourceCollection (query , response , CreditCard .__fetch_existing_between )
163+
164+ @staticmethod
165+ def __fetch_expired (query , ids ):
166+ criteria = {}
167+ criteria ["ids" ] = IdsSearch .ids .in_list (ids ).to_param ()
168+ response = Http ().post ("/payment_methods/all/expired" , {"search" : criteria })
169+ return [CreditCard (item ) for item in ResourceCollection ._extract_as_array (response ["payment_methods" ], "credit_card" )]
170+
171+ @staticmethod
172+ def __fetch_existing_between (query , ids ):
173+ criteria = {}
174+ criteria ["ids" ] = IdsSearch .ids .in_list (ids ).to_param ()
175+ response = Http ().post ("/payment_methods/all/expiring?" + query , {"search" : criteria })
176+ return [CreditCard (item ) for item in ResourceCollection ._extract_as_array (response ["payment_methods" ], "credit_card" )]
177+
144178 @staticmethod
145179 def find (credit_card_token ):
146180 """
@@ -168,16 +202,21 @@ def update_signature():
168202 @staticmethod
169203 def signature (type ):
170204 billing_address_params = ["company" , "country_name" , "extended_address" , "first_name" , "last_name" , "locality" , "postal_code" , "region" , "street_address" ]
205+ options = ["make_default" , "verification_merchant_account_id" , "verify_card" ]
206+
171207 signature = [
172208 "cardholder_name" , "cvv" , "expiration_date" , "expiration_month" , "expiration_year" , "number" , "token" ,
173209 {"billing_address" : billing_address_params },
174- {"options" : [ "make_default" , "verify_card" ] }
210+ {"options" : options }
175211 ]
176212
177213 if type == "create" :
178214 signature .append ("customer_id" )
179215 elif type == "update" :
180216 billing_address_params .append ({"options" : ["update_existing" ]})
217+ elif type == "update_via_customer" :
218+ options .append ("update_existing_token" )
219+ billing_address_params .append ({"options" : ["update_existing" ]})
181220 else :
182221 raise AttributeError
183222
@@ -188,6 +227,7 @@ def transparent_redirect_create_url():
188227 """
189228 Returns the url to use for creating CreditCards through transparent redirect.
190229 """
230+ warnings .warn ("Please use TransparentRedirect.url instead" , DeprecationWarning )
191231 return Configuration .base_merchant_url () + "/payment_methods/all/create_via_transparent_redirect_request"
192232
193233 @staticmethod
@@ -197,25 +237,28 @@ def tr_data_for_create(tr_data, redirect_url):
197237 """
198238
199239 Resource .verify_keys (tr_data , [{"credit_card" : CreditCard .create_signature ()}])
240+ tr_data ["kind" ] = TransparentRedirect .Kind .CreatePaymentMethod
200241 return TransparentRedirect .tr_data (tr_data , redirect_url )
201242
202243 @staticmethod
203244 def tr_data_for_update (tr_data , redirect_url ):
204245 """
205246 Builds tr_data for CreditCard updating.
206247 """
207- Resource .verify_keys (tr_data , ["payment_method_token" , {"credit_card" : CreditCard .create_signature ()}])
248+ Resource .verify_keys (tr_data , ["payment_method_token" , {"credit_card" : CreditCard .update_signature ()}])
249+ tr_data ["kind" ] = TransparentRedirect .Kind .UpdatePaymentMethod
208250 return TransparentRedirect .tr_data (tr_data , redirect_url )
209251
210252 @staticmethod
211253 def transparent_redirect_update_url ():
212254 """
213255 Returns the url to be used for updating CreditCards through transparent redirect.
214256 """
257+ warnings .warn ("Please use TransparentRedirect.url instead" , DeprecationWarning )
215258 return Configuration .base_merchant_url () + "/payment_methods/all/update_via_transparent_redirect_request"
216259
217260 @staticmethod
218- def __post (url , params ):
261+ def _post (url , params = {} ):
219262 response = Http ().post (url , params )
220263 if "credit_card" in response :
221264 return SuccessfulResult ({"credit_card" : CreditCard (response ["credit_card" ])})
@@ -224,6 +267,7 @@ def __post(url, params):
224267
225268 def __init__ (self , attributes ):
226269 Resource .__init__ (self , attributes )
270+ self .is_expired = self .expired
227271 if "billing_address" in attributes :
228272 self .billing_address = Address (self .billing_address )
229273 if "subscriptions" in attributes :
0 commit comments