def get_donation(): if not session.get('current_building_id'): redirect(url_for('donation_address')) form = DonationForm() conn_error = request.args.get('conn_error') validate_campaign_status(current_user.team.campaign) if form.validate_on_submit(): session['current_donation'] = { "amount": form.amount.data, "payment_type": form.payment_type.data, "team_id": current_user.team_id } if form.payment_type.data == 'bit': return redirect(url_for('bit_donation')) elif form.payment_type.data == 'PayPal': # Create a paypal payment and redirect to the authorization process via paypal's api try: payment = pp.create_payment( form.amount.data, f'{HOST_URL}donation_address/donation/paypal/execute_paypal_donation', f'{HOST_URL}donation_address/donation') return redirect(pp.authorize_payment(payment)) except (ConnectionError, RuntimeError): conn_error = True # if there's a connection error / unexpected error, display an error in the donation page generate_automate_report('paypal') if not conn_error: return redirect(url_for('send_invoice')) return render_template('/donation.html', form=form, conn_error=conn_error)
def view_registry(cat, slug): category = REGISTRY_TYPES.get(cat, None) if not category: flash('The registry category does not exist', 'error') return redirect(url_for('.index')) registry = category.get_by_slug(slug) if not registry: flash('The registry does not exist', 'error') return redirect(url_for('.index')) if cat == 'weddings': form = DonationForm(request.form) if form.validate_on_submit(): tran = Transaction() form.populate_obj(tran) tran.total_amount = form.amount.data tran.payment_status = 'unpaid' tran.type = 'donation' tran.save() tran.generate_txn_number() tran.save() donation = Donation() donation.registry_id = registry.id donation.transaction_id = tran.id donation.amount = form.amount.data donation.save() # initialize payments paystack = PaystackPay() response = paystack.fetch_authorization_url( email=tran.email, amount=tran.total_amount) if response.status_code == 200: json_response = response.json() tran.update( payment_txn_number=json_response['data']['reference']) return redirect(json_response['data']['authorization_url']) else: flash('Something went wrong. Please try again', 'error') return render_template('frontend/registry.html', registry=registry, form=form, cat=cat) return render_template('frontend/registry.html', registry=registry, cat=cat)
def view_campaign(id): connection = get_connection() cursor = get_cursor() form = DonationForm() campaign = None amount_donated = None percentage = None donations = None def setup(): nonlocal campaign, amount_donated, percentage, donations try: cursor.execute( """ SELECT c.id AS campaign_id, c.name, c.description, c.image, c.amount_requested, c.date_created, c.last_modified, up.id AS owner_id, up.first_name, up.last_name, up.profile_image, up.description AS owner_description, get_total_donations(c.id) AS amount_donated, ceil((get_total_donations(c.id)/c.amount_requested)*100) AS percentage FROM campaign c INNER JOIN campaign_relation cr on c.id = cr.campaign_id INNER JOIN user_account ua on cr.user_account_id = ua.id INNER JOIN user_profile up on ua.id = up.user_account_id WHERE c.id = %s AND cr.user_role='owner'; """, (id, )) campaign = cursor.fetchone() cursor.execute( """ SELECT up.first_name, up.last_name, up.profile_image, t.amount FROM campaign c INNER JOIN campaign_relation cr ON c.id = cr.campaign_id INNER JOIN transaction t ON t.id = cr.transaction_id INNER JOIN user_profile up ON cr.user_account_id = up.user_account_id WHERE user_role='pledged' AND c.id=%s ORDER BY t.date_created DESC LIMIT 10; """, (id, )) donations = cursor.fetchall() except Exception as e: current_app.logger.error(e) if form.validate_on_submit(): try: connection = get_connection() cursor = get_cursor() with connection: with cursor: cursor.execute( """ SELECT credit_card FROM user_profile WHERE user_account_id=%s """, (session['user_id'], )) cc_number = cursor.fetchone()[0] cursor.execute( """ INSERT INTO transaction(credit_card, amount) VALUES (%s, %s) RETURNING id; """, (cc_number, form.amount.data)) transaction_id = cursor.fetchone()[0] cursor.execute( """ INSERT INTO campaign_relation(user_account_id, campaign_id, transaction_id, user_role) VALUES (%s, %s, %s, %s) """, (session['user_id'], form.campaign_id.data, transaction_id, 'pledged')) flash('Successfully donated!', 'success') setup() return render_template("campaign/campaign.html", form=form, campaign=campaign, donations=donations) except Exception as e: current_app.logger.error(e) flash(e, 'error') flash(form.errors, 'error') setup() return render_template("campaign/campaign.html", form=form, campaign=campaign, donations=donations)