#NEWLINE# def cron_rebill( request = None ) : #NEWLINE# #INDENT# 'Rebill customers with expiring recurring subscription products\n    This can either be run via a url with GET key authentication or\n    directly from a shell script.\n    ' #NEWLINE# if ( request is not None ) : #NEWLINE# #INDENT# if ( not config_value ( 'PAYMENT' , 'ALLOW_URL_REBILL' ) ) : #NEWLINE# #INDENT# return bad_or_missing ( request , _ ( 'Feature is not enabled.' ) ) #NEWLINE# #UNINDENT# if ( ( 'key' not in request . GET ) or ( request . GET [ 'key' ] != config_value ( 'PAYMENT' , 'CRON_KEY' ) ) ) : #NEWLINE# #INDENT# return HttpResponse ( 'Authentication Key Required' ) #NEWLINE# #UNINDENT# #UNINDENT# expiring_subscriptions = OrderItem . objects . filter ( expire_date__gte = datetime . now ( ) ) . order_by ( 'order' , 'id' , 'expire_date' ) #NEWLINE# for item in expiring_subscriptions : #NEWLINE# #INDENT# if item . product . is_subscription : #NEWLINE# #INDENT# if ( item . product . subscriptionproduct . recurring_times and ( ( item . product . subscriptionproduct . recurring_times + item . product . subscriptionproduct . get_trial_terms ( ) . count ( ) ) == OrderItem . objects . filter ( order = item . order , product = item . product ) . count ( ) ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( ( item . expire_date == datetime . date ( datetime . now ( ) ) ) and item . completed ) : #NEWLINE# #INDENT# if ( item . id == OrderItem . objects . filter ( product = item . product , order = item . order ) . order_by ( '-id' ) [ 0 ] . id ) : #NEWLINE# #INDENT# new_order_item = OrderItem ( order = item . order , product = item . product , quantity = item . quantity , unit_price = item . unit_price , line_item_price = item . line_item_price ) #NEWLINE# if item . product . subscriptionproduct . recurring : #NEWLINE# #INDENT# new_order_item . expire_date = item . product . subscriptionproduct . calc_expire_date ( ) #NEWLINE# #UNINDENT# ordercount = item . order . orderitem_set . all ( ) . count ( ) #NEWLINE# if ( ( item . product . subscriptionproduct . get_trial_terms ( ) . count ( ) > 1 ) and ( item . unit_price == item . product . subscriptionproduct . get_trial_terms ( ( ordercount - 1 ) ) . price ) ) : #NEWLINE# #INDENT# new_order_item . unit_price = item . product . subscriptionproduct . get_trial . terms ( ordercount ) . price #NEWLINE# new_order_item . line_item_price = ( new_order_item . quantity * new_order_item . unit_price ) #NEWLINE# new_order_item . expire_date = item . product . subscriptionproduct . get_trial_terms ( ordercount ) . calc_expire_date ( ) #NEWLINE# #UNINDENT# new_order_item . save ( ) #NEWLINE# item . order . recalculate_total ( ) #NEWLINE# payments = item . order . payments . all ( ) [ 0 ] #NEWLINE# ipn_based = [ 'PAYPAL' ] #NEWLINE# if ( ( not ( payments . payment in ipn_based ) ) and ( item . order . balance > 0 ) ) : #NEWLINE# #INDENT# from livesettings import config_get_group #NEWLINE# payment_module = config_get_group ( ( 'PAYMENT_%s' % payments . payment ) ) #NEWLINE# credit_processor = payment_module . MODULE . load_module ( 'processor' ) #NEWLINE# processor = credit_processor . PaymentProcessor ( payment_module ) #NEWLINE# processor . prepare_data ( item . order ) #NEWLINE# result = processor . process ( ) #NEWLINE# if result . payment : #NEWLINE# #INDENT# reason_code = result . payment . reason_code #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# reason_code = 'unknown' #NEWLINE# #UNINDENT# log . info ( 'Processing %s recurring transaction with %s\n                            Order #%i\n                            Results=%s\n                            Response=%s\n                            Reason=%s' , payment_module . LABEL . value , payment_module . KEY . value , item . order . id , result . success , reason_code , result . message ) #NEWLINE# if result . success : #NEWLINE# #INDENT# item . order . add_status ( status = 'New' , notes = ugettext ( 'Subscription Renewal Order successfully submitted' ) ) #NEWLINE# new_order_item . completed = True #NEWLINE# new_order_item . save ( ) #NEWLINE# orderpayment = OrderPayment ( order = item . order , amount = item . order . balance , payment = unicode ( payment_module . KEY . value ) ) #NEWLINE# orderpayment . save ( ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return HttpResponse ( )