def save_payment_method(): vendor = get_vendor( ) # to-do: make it based on request.json['vendor'] what is currently 'stripe' ?? skey = vendor.get_secret_key() token = request.json['token'] method_name = request.json['name'] method_text = request.json['text'] event = EventType.payment_method_selected if current_user.account.payment_method is None else EventType.payment_method_changed event_text = 'You selected ' if current_user.account.payment_method is None else 'You changed ' payment_method_info = vendor.save_payment_method_info( current_user.email, **{'token': token}) vendor_name = vendor.get_vendor_name( ) # what is primary - method or vendor? Probably method. So method should have this info. current_user.account.vendor_name = vendor_name current_user.account.payment_method = method_name current_user.account.payment_method_info = payment_method_info current_user.account.payment_method_text = method_text event_text = event_text + ' a payment method. ' + method_text current_user.account.create_history_event(event, event_text) db.session.add(current_user) db.session.commit() return jsonify({'result': True, 'info': payment_method_info})
def get_current_plans(): vendor = get_vendor() plans = vendor.get_plans() return jsonify({ 'result': True, 'plans': plans })
def start_subscription(): vendor = get_vendor() skey = vendor.get_secret_key() event = EventType.subscription_paid if current_user.account.subscription_id != None: if request.json[ 'plan_id'] == current_user.account.plan_id and current_user.account.account_status == 'paused': event = EventType.subscription_resumed subscription_data = vendor.create_subscription( request.json['plan_id'], **{ 'payment_method_info': current_user.account.payment_method_info, 'current_plan_id': current_user.account.plan_id, 'current_subscription_id': current_user.account.subscription_id, 'account_status': current_user.account.account_status }) if subscription_data['result']: next_payment = DateTime.fromtimestamp(subscription_data['period_end']) message_event = '{3}: Your payment succeed: {0} {1}.' if event == EventType.subscription_paid else 'Your subscription is resumed.' message_text = message_event + ' Your next payment will be on {2}.' message = message_text.format(subscription_data['amount'], subscription_data['currency'], next_payment.strftime('%d, %b %Y'), DateTime.now().strftime('%d, %b %Y')) current_user.account.create_history_event(event, message) # Update account data current_user.account.plan_id = request.json['plan_id'] current_user.account.plan_name = request.json['plan_name'] current_user.account.subscription_id = subscription_data[ 'subscription_id'] current_user.account.payment_expiration = next_payment current_user.account.account_status = AccountStatus.paid.value current_user.account.valid_status = ValidStatus.valid.value current_user.account.account_status_text = message else: message = 'Your payment failed: ' + subscription_data['message'] current_user.account.create_history_event( EventType.subscription_failed, message) current_user.account.check_account_status( ) # We need to check and change if trial is expired already. db.session.add(current_user.account) db.session.commit() return jsonify({ 'result': subscription_data['result'], 'account_status': current_user.account.account_status, 'account_status_text': message, 'payment_method_text': current_user.account.payment_method_text })
def start_trial(self): vendor = get_vendor() self.plan_id = vendor.get_default_trial_plan_id() trial_days = current_app.config['TRIAL_PERIOD_IN_DAYS'] self.trial_expiration = DateTime.today() + TimeDelta(days = trial_days) self.account_status = AccountStatus.trial.value str_valid_time = self.trial_expiration.strftime('%d, %b %Y %H:%M') self.account_status_text = ('Your trial is activated and valid till ' + str_valid_time) db.session.add(self) db.session.flush() account_event = self.create_history_event(EventType.trial_started, 'Started a trial, expiration date is ' + str_valid_time)
def cancel_subscription(): vendor = get_vendor() skey = vendor.get_secret_key() if current_user.account.plan_id == None or ( current_user.account.account_status != AccountStatus.paid.value and current_user.account.account_status != AccountStatus.paused.value): return jsonify({ 'result': False, 'message': 'You don\'t have any live subscription.' }) cancel_result = vendor.cancel_subscription( current_user.account.subscription_id) pause_request = True if request.json != None and 'pause' in request.json else False if cancel_result['result'] == False: # to-do: log message pass else: if pause_request: current_user.account.account_status = AccountStatus.paused.value current_user.account.account_status_text = 'The subscription was paused.' else: current_user.account.account_status = AccountStatus.cancelled.value current_user.account.account_status_text = 'The subscription was cancelled.' current_user.account.subscription_id = None if current_user.account.payment_expiration < DateTime.now(): current_user.account.payment_expiration = None current_user.account.valid_status = ValidStatus.invalid.value current_user.account.plan_name = '' current_user.account.plan_id = None else: current_user.account.account_status_text += ( ' But you still have an access to the service until ' + current_user.account.payment_expiration.strftime('%d, %b %Y') + '.') event = EventType.subscription_paused if pause_request else EventType.subscription_cancelled current_user.account.create_history_event( event, current_user.account.account_status_text) db.session.add(current_user.account) db.session.commit() return jsonify({ 'result': cancel_result['result'], 'account_status': current_user.account.account_status, 'account_status_text': current_user.account.account_status_text })
def start_trial(self): vendor = get_vendor() if vendor.keys['publishable_key'] is not None and vendor.keys['secret_key'] is not None: self.plan_id = vendor.get_default_trial_plan_id() trial_days = current_app.config['TRIAL_PERIOD_IN_DAYS'] self.trial_expiration = DateTime.today() + TimeDelta(days = trial_days) self.account_status = AccountStatus.trial.value str_valid_time = self.trial_expiration.strftime('%d, %b %Y %H:%M') self.account_status_text = ('Your trial is activated and valid till ' + str_valid_time) else: self.account_status_text = ('Account was not activated because payment vendors keys have not been provided') db.session.add(self) db.session.flush() if self.account_status == AccountStatus.trial.value: account_event = self.create_history_event(EventType.trial_started, 'Started a trial, expiration date is ' + str_valid_time)
def index_page(path): vendor = get_vendor() return render_template('dashboard.html', company_name=current_app.config.get('COMPANY_NAME'), vendor_pkey=vendor.get_public_key())