#NEWLINE# def create_subscription( self , credit_card , amount , start , days = None , months = None , occurrences = None , trial_amount = None , trial_occurrences = None ) : #NEWLINE# #INDENT# '\n        Creates a recurring subscription payment on the CreditCard provided.\n        \n        ``credit_card``\n            The CreditCard instance to create the subscription for.\n            Subscriptions require that you provide a first and last name with\n            the credit card.\n        \n        ``amount``\n            The amount to charge every occurrence, either as an int, float,\n            or Decimal.\n        \n        ``start``\n            The date to start the subscription, as a date object.\n        \n        ``days``\n            Provide either the days or the months argument to indicate the\n            interval at which the subscription should recur.\n\n        ``months``\n            Provide either the days or the months argument to indicate the\n            interval at which the subscription should recur.\n        \n        ``occurrences``\n            If provided, this is the number of times to charge the credit card\n            before ending. If not provided, will last until canceled.\n        \n        ``trial_amount``\n            If you want to have a trial period at a lower amount for this\n            subscription, provide the amount. (Either both trial arguments\n            should be provided, or neither.)\n        \n        ``trial_occurrences``\n            If you want to have a trial period at a lower amount for this\n            subscription, provide the number of occurences the trial period\n            should last for. (Either both trial arguments should be provided,\n            or neither.)\n        ' #NEWLINE# subscription = self . client . factory . create ( 'ARBSubscriptionType' ) #NEWLINE# amount = Decimal ( str ( amount ) ) . quantize ( Decimal ( '0.01' ) ) #NEWLINE# subscription . amount = str ( amount ) #NEWLINE# payment_type = self . client . factory . create ( 'PaymentType' ) #NEWLINE# credit_card_type = self . client . factory . create ( 'CreditCardType' ) #NEWLINE# credit_card_type . cardNumber = credit_card . card_number #NEWLINE# credit_card_type . expirationDate = '{0}-{1:0>2}' . format ( credit_card . exp_year , credit_card . exp_month ) #NEWLINE# credit_card_type . cardCode = credit_card . cvv #NEWLINE# payment_type . creditCard = credit_card_type #NEWLINE# subscription . payment = payment_type #NEWLINE# if ( not ( credit_card . first_name and credit_card . last_name ) ) : #NEWLINE# #INDENT# raise AuthorizeInvalidError ( 'Subscriptions require first name and last name to be provided with the credit card.' ) #NEWLINE# #UNINDENT# subscription . billTo . firstName = credit_card . first_name #NEWLINE# subscription . billTo . lastName = credit_card . last_name #NEWLINE# if ( ( days and months ) or ( not ( days or months ) ) ) : #NEWLINE# #INDENT# raise AuthorizeInvalidError ( 'Please provide either the months or days argument to define the subscription interval.' ) #NEWLINE# #UNINDENT# if days : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# days = int ( days ) #NEWLINE# assert ( ( days >= 7 ) and ( days <= 365 ) ) #NEWLINE# #UNINDENT# except ( AssertionError , ValueError ) : #NEWLINE# #INDENT# raise AuthorizeInvalidError ( 'The interval days must be an integer value between 7 and 365.' ) #NEWLINE# #UNINDENT# subscription . paymentSchedule . interval . unit = self . client . factory . create ( 'ARBSubscriptionUnitEnum' ) . days #NEWLINE# subscription . paymentSchedule . interval . length = days #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if months : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# months = int ( months ) #NEWLINE# assert ( ( months >= 1 ) and ( months <= 12 ) ) #NEWLINE# #UNINDENT# except ( AssertionError , ValueError ) : #NEWLINE# #INDENT# raise AuthorizeInvalidError ( 'The interval months must be an integer value between 1 and 12.' ) #NEWLINE# #UNINDENT# subscription . paymentSchedule . interval . unit = self . client . factory . create ( 'ARBSubscriptionUnitEnum' ) . months #NEWLINE# subscription . paymentSchedule . interval . length = months #NEWLINE# #UNINDENT# #UNINDENT# if ( start < date . today ( ) ) : #NEWLINE# #INDENT# raise AuthorizeInvalidError ( 'The start date for the subscription may not be in the past.' ) #NEWLINE# #UNINDENT# subscription . paymentSchedule . startDate = start . strftime ( '%Y-%m-%d' ) #NEWLINE# if ( occurrences is None ) : #NEWLINE# #INDENT# occurrences = 9999 #NEWLINE# #UNINDENT# subscription . paymentSchedule . totalOccurrences = occurrences #NEWLINE# if ( trial_amount and trial_occurrences ) : #NEWLINE# #INDENT# subscription . paymentSchedule . trialOccurrences = trial_occurrences #NEWLINE# trial_amount = Decimal ( str ( trial_occurrences ) ) #NEWLINE# trial_amount = trial_amount . quantize ( Decimal ( '0.01' ) ) #NEWLINE# subscription . trialAmount = str ( trial_amount ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( trial_amount or trial_occurrences ) : #NEWLINE# #INDENT# raise AuthorizeInvalidError ( 'To indicate a trial period, you must provide both a trial amount and occurrences.' ) #NEWLINE# #UNINDENT# #UNINDENT# response = self . _make_call ( 'ARBCreateSubscription' , subscription ) #NEWLINE# return response . subscriptionId