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 checkout(): form = OrderForm(request.form) if request.method == 'POST' and form.validate(): tran = Transaction() form.populate_obj(tran) tran.type = 'order' tran.total_amount = session['all_total_price'] tran.payment_status = 'unpaid' tran.save() tran.generate_txn_number() if 'discount_id' in session: tran.discount_id = session['discount_id'] tran.discounted_amount = session['discounted_price'] tran.save() # save order items for key, product in session['cart_item'].items(): # check if there is an existing order order = Order.query.filter_by( transaction_id=tran.id, registry_id=product['registry_id'], registry_type=product['registry_type']).first() if not order: order = Order() order.registry_id = product['registry_id'] order.registry_type = product['registry_type'] order.transaction_id = tran.id order.status = 'pending' order.save() order.generate_order_number() order.save() item = OrderItem(order_id=order.id, reg_product_id=key, quantity=product['quantity'], unit_price=product['unit_price'], total_price=product['total_price']) item.save() # initialize payments paystack = PaystackPay() amount = tran.discounted_amount if tran.discounted_amount else tran.total_amount response = paystack.fetch_authorization_url(email=tran.email, amount=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 redirect(url_for('.checkout')) products = session['cart_item'] return render_template('frontend/checkout.html', form=form, products=products)