def free_subscription(uid): """ Function to make free sunscription for user""" user_detail = User.by_id(uid) dash_redirect = url_for('userbp.dashboard') if int(uid) != int(current_user.id): dash_redirect = url_for('userbp.customers') if user_detail.payment_status == 1: configure_paypal() pay_info = Payment.byuser_id(uid) billing_agreement_detail = BillingAgreement.find( pay_info.PaymentDetail.billing_aggrement_id) cancel_note = {"note": "Canceling the agreement"} cancel_states = ['Active', 'Suspended'] if billing_agreement_detail.state in cancel_states: if billing_agreement_detail.cancel(cancel_note): user_detail.payment_status = 2 flash(MAKE_FREE_USER, 'success') flash(CANCEL_PLAN, 'success') else: errorlog.error('Cancel Current Plan Error', details=str(billing_agreement_detail.error)) flash(CANCEL_PLAN_ERROR, 'danger') else: errorlog.error('Cancel Current Plan Error', details=str(billing_agreement_detail.error)) flash(PLAN_NOT_ACTIVE, 'danger') else: user_detail.payment_status = 2 flash(MAKE_FREE_USER, 'success') db.session.commit() return redirect(dash_redirect)
def my_account(): """ Function to view my account having info abount subscription and payment history """ try: get_uid = request.args.get('userid', default=None, type=int) uid = current_user.id if get_uid is not None and current_user.user_type == 'admin': uid = get_uid userinfo = User.by_id(uid) trans_list = None billing_agreement = None account_detail = None if userinfo.payment_status == 1: account_detail = Payment.byuser_id(userinfo.id) get_date = datetime.strptime(str( userinfo.created_at), '%Y-%m-%d %H:%M:%S') - timedelta(days=1) start_date, end_date = get_date.strftime('%Y-%m-%d'), \ datetime.now().strftime("%Y-%m-%d") account_detail = Payment.byuser_id(userinfo.id) configure_paypal() billing_agreement = BillingAgreement.find( account_detail.PaymentDetail.billing_aggrement_id) transactions = billing_agreement.search_transactions( start_date, end_date) trans_list = transactions.agreement_transaction_list if trans_list is None: trans_list = [] credit_card_form = payment_form.credit_card() plan = Subscription.get_all(True) credit_card_form.payment_token.data = plan.subscription_id return render_template('payment/my_account.html', account_detail=account_detail, transactions=trans_list, agreement=billing_agreement, userinfo=userinfo, plan=plan, ccform=credit_card_form) except Exception as err: errorlog.error('My Account Error', details=str(err)) return render_template('error.html', message="Error!")
def cancel_current_plan(): """ Function to cancel the current plan """ try: get_uid = request.args.get('userid', default=None, type=int) uid = current_user.id profile_redirect = url_for('userbp.profile') dashboard_redirect = url_for('userbp.dashboard') if get_uid is not None and current_user.user_type == 'admin': uid = get_uid profile_redirect = url_for('userbp.profile', userid=uid) dashboard_redirect = url_for('userbp.dashboard', userid=uid) userinfo = User.by_id(uid) if userinfo.payment_status != 1: flash(PLAN_SUBSCRIPTION_ERROR, 'danger') return redirect(profile_redirect) configure_paypal() pay_info = Payment.byuser_id(uid) billing_agreement_detail = BillingAgreement.find( pay_info.PaymentDetail.billing_aggrement_id) cancel_note = {"note": "Canceling the agreement"} cancel_states = ['Active', 'Suspended'] if billing_agreement_detail.state in cancel_states: if billing_agreement_detail.cancel(cancel_note): userinfo.payment_status = 0 db.session.commit() flash(CANCEL_PLAN, 'success') return redirect(dashboard_redirect) else: errorlog.error('Cancel Current Plan Error', details=str(billing_agreement_detail.error)) flash(CANCEL_PLAN_ERROR, 'danger') else: flash(PLAN_NOT_ACTIVE, 'danger') except Exception as err: errorlog.error('Cancel Current Plan Error', details=str(err)) flash(CANCEL_PLAN_ERROR, 'danger') return redirect(profile_redirect)