def activate_plan(): """ Function to activate plan """ try: if current_user.user_type != 'admin': flash('You are not allowed to access this page', 'danger') return redirect(url_for('userbp.dashboard')) configure_paypal() pid = request.args.get('id', '') billing_plan = BillingPlan.find(pid) if billing_plan.activate(): subplan = Subscription.by_planid(pid) subplan.status = True db.session.commit() else: errorlog.error('Plan activate Error', details=str(billing_plan.error)) return redirect(url_for('paymentbp.subscriptions')) except Exception as err: errorlog.error('Plan activate Error', details=str(err)) return render_template('error.html', message="Error!")
def subscribe(payment_id, user_id=None, ccform=None): """ Function to create billing agreement using credit card and paypal """ try: configure_paypal() user_detail = current_user if user_id is not None: user_detail = User.by_id(user_id) session['app_user_id'] = user_id if ccform is not None: exp_detail = ccform.expiry_date.data.split('/') exp_month = exp_detail[0] if exp_detail[0] else 0 exp_year = exp_detail[1] if exp_detail[1] else 0 payer_info = { "payer": { "payment_method": "credit_card", "funding_instruments": [{ "credit_card": { "type": str(ccform.type.data), "number": str(ccform.card_number.data), "expire_month": str(exp_month), "expire_year": str(exp_year), "cvv2": str(ccform.cvv.data), "first_name": str(ccform.first_name.data), "last_name": str(ccform.last_name.data), "billing_address": { "line1": str(current_user.address1), "line2": str(current_user.address2), "city": str(current_user.city), "state": str(current_user.state), "postal_code": str(current_user.zipcode), "country_code": "US" } }, }] } } else: payer_info = { "payer": { "payment_method": "paypal", "funding_option_id": user_detail.id, "payer_info": { "email": user_detail.email, "first_name": user_detail.first_name, "last_name": user_detail.last_name, } } } plan = Subscription.by_planid(payment_id) billing_agreement = BillingAgreement({ "name": plan.subscription_name, "description": "Agreement for the recurring payment for the plan " + plan.subscription_name + ". The cost of this plan is $" + str('%.2f' % plan.subscription_price) + " /month.", "start_date": (datetime.now() + timedelta(hours=1)).strftime('%Y-%m-%dT%H:%M:%SZ'), "plan": { "id": str(payment_id) }, "payer": payer_info['payer'], }) if billing_agreement.create(): if ccform is not None: billing_agreement_response = BillingAgreement.execute( billing_agreement.id) return save_credit_card_response(billing_agreement, payment_id) for link in billing_agreement.links: if link.rel == "approval_url": approval_url = link.href return approval_url return False else: errorlog.error('Subscribe Error', details=str(billing_agreement.error)) flash(SUBSCRIPTION_ERROR, 'danger') return False except Exception as err: errorlog.error('Subscribe Error', details=str(err)) flash(SUBSCRIPTION_ERROR, 'danger') return redirect(url_for('paymentbp.billing'))