def api_add_rp(data): """Create a recurrring payment account. Accepted format: json Input fields: email - required description - required payment_amount - required billing_period - optional, default to 'month' billing_frequency - optional, default to 1 billing_start_dt - optional, default to today num_days - optional, default to 0 has_trial_period - optional, default to False trial_period_start_dt - optional, default to today trial_period_end_dt - optional, default to today trial_amount - optional, default to 0 Output: rp_id - a recurring payment id result_code """ ALLOWED_FIELES = ('email', 'description', 'payment_amount', 'billing_period', 'billing_frequency', 'billing_start_dt', 'num_days', 'has_trial_period', 'trial_period_start_dt', 'trial_period_end_dt', 'trial_amount', ) from tendenci.apps.base.utils import validate_email import dateutil.parser as dparser from tendenci.apps.imports.utils import get_unique_username email = data.get('email', '') payment_amount = data.get('payment_amount', '') try: payment_amount = Decimal(payment_amount) except: payment_amount = 0 if not all([validate_email(email), 'description' in data, payment_amount>0]): return False, {} rp = RecurringPayment() for key, value in data.items(): if key in ALLOWED_FIELES: if hasattr(rp, key): setattr(rp, key, value) if rp.billing_start_dt: try: rp.billing_start_dt = dparser.parse(rp.billing_start_dt) except: rp.billing_start_dt = datetime.now() else: rp.billing_start_dt = datetime.now() if rp.trial_period_start_dt: try: rp.trial_period_start_dt = dparser.parse(rp.trial_period_start_dt) except: rp.trial_period_start_dt = datetime.now() if rp.trial_period_end_dt: try: rp.trial_period_end_dt = dparser.parse(rp.trial_period_end_dt) except: rp.trial_period_end_dt = datetime.now() rp.payment_amount = Decimal(rp.payment_amount) try: rp.billing_frequency = int(rp.billing_frequency) except: rp.billing_frequency = 1 try: rp.num_days = int(rp.num_days) except: rp.num_days = 1 if rp.has_trial_period in ['True', '1', True, 1] and all([rp.trial_period_start_dt, rp.trial_period_end_dt]): rp.has_trial_period = True else: rp.has_trial_period = False # start the real work # # get or create a user account with this email # users = User.objects.filter(email=email) # if users: # u = users[0] # else: # always create a new user account - This is very important! # it is to prevent hacker from trying to use somebody else's account. u = User() u.email=email u.username = data.get('username', '') if not u.username: u.username = email.split('@')[0] u.username = get_unique_username(u) raw_password = data.get('password', '') if not raw_password: raw_password = User.objects.make_random_password(length=8) u.set_password(raw_password) u.first_name = data.get('first_name', '') u.last_name = data.get('last_name', '') u.is_staff = False u.is_superuser = False u.save() # profile = Profile.objects.create( # user=u, # creator=u, # creator_username=u.username, # owner=u, # owner_username=u.username, # email=u.email # ) # add a recurring payment entry for this user rp.user = u # activate it when payment info is received rp.status_detail = 'inactive' rp.save() return True, {'rp_id': rp.id}
def api_add_rp(data): """Create a recurrring payment account. Accepted format: json Input fields: email - required description - required payment_amount - required billing_period - optional, default to 'month' billing_frequency - optional, default to 1 billing_start_dt - optional, default to today num_days - optional, default to 0 has_trial_period - optional, default to False trial_period_start_dt - optional, default to today trial_period_end_dt - optional, default to today trial_amount - optional, default to 0 Output: rp_id - a recurring payment id result_code """ ALLOWED_FIELES = ( "email", "description", "payment_amount", "billing_period", "billing_frequency", "billing_start_dt", "num_days", "has_trial_period", "trial_period_start_dt", "trial_period_end_dt", "trial_amount", ) from decimal import Decimal from tendenci.apps.base.utils import validate_email import dateutil.parser as dparser from tendenci.apps.imports.utils import get_unique_username email = data.get("email", "") payment_amount = data.get("payment_amount", "") try: payment_amount = Decimal(payment_amount) except: payment_amount = 0 if not all([validate_email(email), data.has_key("description"), payment_amount > 0]): return False, {} rp = RecurringPayment() for key, value in data.items(): if key in ALLOWED_FIELES: if hasattr(rp, key): setattr(rp, key, value) if rp.billing_start_dt: try: rp.billing_start_dt = dparser.parse(rp.billing_start_dt) except: rp.billing_start_dt = datetime.now() else: rp.billing_start_dt = datetime.now() if rp.trial_period_start_dt: try: rp.trial_period_start_dt = dparser.parse(rp.trial_period_start_dt) except: rp.trial_period_start_dt = datetime.now() if rp.trial_period_end_dt: try: rp.trial_period_end_dt = dparser.parse(rp.trial_period_end_dt) except: rp.trial_period_end_dt = datetime.now() rp.payment_amount = Decimal(rp.payment_amount) try: rp.billing_frequency = int(rp.billing_frequency) except: rp.billing_frequency = 1 try: rp.num_days = int(rp.num_days) except: rp.num_days = 1 if rp.has_trial_period in ["True", "1", True, 1] and all([rp.trial_period_start_dt, rp.trial_period_end_dt]): rp.has_trial_period = True else: rp.has_trial_period = False # start the real work # # get or create a user account with this email # users = User.objects.filter(email=email) # if users: # u = users[0] # else: # always create a new user account - This is very important! # it is to prevent hacker from trying to use somebody else's account. u = User() u.email = email u.username = data.get("username", "") if not u.username: u.username = email.split("@")[0] u.username = get_unique_username(u) raw_password = data.get("password", "") if not raw_password: raw_password = User.objects.make_random_password(length=8) u.set_password(raw_password) u.first_name = data.get("first_name", "") u.last_name = data.get("last_name", "") u.is_staff = False u.is_superuser = False u.save() # profile = Profile.objects.create( # user=u, # creator=u, # creator_username=u.username, # owner=u, # owner_username=u.username, # email=u.email # ) # add a recurring payment entry for this user rp.user = u # activate it when payment info is received rp.status_detail = "inactive" rp.save() return True, {"rp_id": rp.id}