def create(id): if current_user.is_authenticated: amount = int(request.form.get('amount')) result = transact({ 'amount': amount, 'payment_method_nonce': request.form['payment_method_nonce'], 'options': { "submit_for_settlement": True } }) if result.is_success or result.transaction: image = Image.get_or_none(Image.id==id) receiver = image.user donor = User.get_by_id(current_user.id) amount = amount*100 donation = Donation(receiver=receiver, donor_id=donor.id, image=image, amount=amount) if donation.save(): # print(f"donor: {donation.donor.username}") # print(f"receiver: {donation.receiver.username}") # print(f"amount: {donation.amount}") flash('Donation successful', 'alert alert-success') return redirect(url_for('home')) else: flash('Donation unsuccessful', 'alert alert-danger') return redirect(url_for('home')) else: for x in result.errors.deep_errors: flash('Error: %s: %s' % (x.code, x.message)) return redirect(url_for('payment.new', id=id)) else: flash('Log in required', 'alert alert-danger') return redirect(url_for('home'))
def create_purchase(): nonce_from_the_client = request.form["payment_method_nonce"] amount_paid = request.form['amount_paid'] s = Donation(amount=amount_paid, user=current_user.id, image=request.form["image_id"]) s.save() result = gateway.transaction.sale({ "amount": amount_paid, "payment_method_nonce": nonce_from_the_client, "options": { "submit_for_settlement": True } }) message = Mail( from_email='*****@*****.**', to_emails='*****@*****.**', subject='Sending with SendGrid is Fun', html_content= f'<strong>you donate{amount_paid}to nextagram.Thank you !</strong>') try: sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) response = sg.send(message) print(response.status_code) print(response.body) print(response.headers) except Exception as e: print(e.message) return redirect(url_for('users.index'))
def create(image_id): nonce = request.form["nonce"] amount = request.form["amount"] result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce, "options": { "submit_for_settlement": True } }) if result.is_success: image = Image.get_by_id(image_id) donation = Donation(image=image, amount=Decimal(amount)) donation.save() requests.post( f"https://api.mailgun.net/v3/{os.environ['MAILGUN_DOMAIN']}/messages", auth=("api", os.environ["MAILGUN_API"]), data={ "from": f"Me <mailgun@{os.environ['MAILGUN_DOMAIN']}>", "to": ["*****@*****.**"], "subject": "Donated", "text": f"You have donated ${amount}!" } ) flash("Payment successful") else: flash("Payment not successful") return redirect(url_for("users.show", username=current_user.username))
def payment(image_id): donation_amount = (request.form.get('donation_amount')) payment_nonce = request.form.get('payment_method_nonce') image = Image.get_or_none(Image.id == image_id) if not donation_amount or int(donation_amount) == 0: flash('Please insert whole number') return redirect(url_for('donations.new', image_id=image.id)) if not payment_nonce: flash('System Error') return redirect(url_for('donations.new', image_id=image.id)) result = gateway.transaction.sale({ "amount": donation_amount, "payment_method_nonce": payment_nonce, "options": { "submit_for_settlement": True } }) if result.is_success or result.transaction: donation = Donation(user=current_user.id, amount=result.transaction.amount, image_id=image.id) donation.save() flash('Success') return redirect(url_for('donations.new', image_id=image.id))
def create(image_id): image = Image.get_or_none(Image.id == image_id) nonce = request.form.get("payment_method_nonce") amount = request.form.get("amount") result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce, "options": { "submit_for_settlement": True } }) if result.is_success: donation = Donation(amount=amount, image_id=image.id, user_id=current_user.id) donation.save() sender = current_user.username receiver = User.get_or_none(User.id == image.user_id) amount = float(amount) amount = "{:.2f}".format(amount) receiver_email = receiver.email image_url = image.user_images_url send_email(sender, receiver_email, amount, image_url) flash(u"Thank you for your donation", 'success') flash(u"The owner of the photo has been notified", 'success') return redirect(url_for('home')) else: flash(u"An error has occurred", 'warning') return redirect(url_for('home'))
def create_checkout(id): # receiver = Image.get(Image.id==id).user_id # image_name = Image.get(Image.id==id).image_name image = Image.get_by_id(id) receiver = User.get_by_id(image.user_id) amount = request.form.get('amount') nonce_from_the_client = request.form["payment_method_nonce"] result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce_from_the_client, "options": { "submit_for_settlement": True } }) if result.is_success and result.transaction: new_donation = Donation(amount=amount, image_id=id, donor_user_id=current_user.id) new_donation.save() email_notification(amount=amount, receiver=receiver, image_name=image.image_name) return redirect( url_for('donations.show_checkout', transaction_id=result.transaction.id)) else: for x in result.errors.deep_errors: flash('Error: %s: %s' % (x.code, x.message)) return redirect(url_for('users.show', username=receiver.username))
def payment(): image = Image.get_by_id(request.form.get('image')) user = User.get_by_id(image.user_id) amount = request.form.get('amount') result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": request.form.get('payment_method_nonce'), "options":{ "submit_for_settlement": True } }) message = Mail( from_email='*****@*****.**', to_emails=f'{user.email}', subject=f'{current_user.username} has donated to your photo', html_content=f'<strong>Congratulations!</strong><br>{current_user.username} has donated ${amount} to your photo. />') if result.is_success: donation = Donation(amount=amount, user_id=current_user.id, image_id=image.id) donation.save() try: sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) sg.send(message) flash(f"Thank you for your donation", 'success') except Exception as e: flash(f"Thank you for your donation - email failure", "info") flash(e, "danger") return redirect(url_for('images.show', id=image.id)) else: flash("Payment fail", "danger") return redirect(url_for('payments.new', id=image.id))
def complete_donation(id): amount = request.form['amount'] payment_method_nonce = request.form['payment_method_nonce'] image = Image.get_by_id(id) user = current_user result = transact({ 'amount': amount, 'payment_method_nonce': payment_method_nonce, 'options': { 'submit_for_settlement': True } }) if result.is_success or result.transaction: donation = Donation(amount=int(amount), image_id=image.id, user_id=user.id, payment_status=True) donation.save() flash('Donation Successfully created!') image_user = User.get_by_id(image.user_id) send_email() return redirect(url_for('users.show', username_id=image_user.id)) else: for x in result.errors.deep_errors: flash('Error: %s: %s' % (x.code, x.message)) return render_template('donations/new.html', img_id=image.id, client_token=client_token)
def summary(id): user = User.get_by_id(id) if current_user.id == user.id: images = Image.select().where(Image.user==id) donations = Donation.select().where(Donation.image.in_(images)) ttl = Donation.select(fn.SUM(Donation.amount).alias('total')).where(Donation.image.in_(images)) return render_template('checkouts/summary.html',donations=donations,ttl=ttl) return render_template('401.html'), 401
def create_purchase(): nonce_from_the_client = request.form["payment_method_nonce"] amount = request.form["amount"] donor = current_user.id image = request.form['image'] i = Image.get(id=image) m = Money(amount, Currency.USD) result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce_from_the_client, "options": { "submit_for_settlement": True } }) if result.is_success or result.transaction: d = Donation(amount=amount, txs_id=result.transaction.id, image=image, donor=donor) if d.save(): flash( f"Thanks {current_user.username}! Your donation of {m} has been successfully sent to {i.user.username}! \nTransaction_ID: {result.transaction.id}", 'success') message = Mail( from_email='*****@*****.**', to_emails=i.user.email, subject="Donation received for your Image", html_content= f"Hi, {i.user.username}! <br /><br /> Your image as shown below has received a donation of {m} from {current_user.username}. <br /><br /> <img src={os.environ.get('S3_LOCATION')}user_images/{i.image_name} />" ) try: sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) response = sg.send(message) print(response.status_code) print(response.body) print(response.headers) except Exception as e: print(str(e)) return redirect(url_for('users.show', username=i.user.username)) else: flash("Something went wrong, it's not saved to the database", 'danger') return render_template('donations/new.html') else: for x in result.errors.deep_errors: flash(f"Erorr: {x.code}: {x.message} ") return redirect(url_for('users.show', username=i.user.username))
def create(image_id): # Get form info payment_method_nonce = request.form['payment_method_nonce'] amount = request.form['amount'] donor_id = request.form['donor_id'] receiver_username = request.form['receiver_username'] image_id = image_id # Get donor_id details donor = User.get_by_id(donor_id) sender = donor.email # Create a transactionr result = gateway.transaction.sale({ # Need to amend amount 'amount': amount, 'payment_method_nonce': payment_method_nonce, 'options': { "submit_for_settlement": True } }) if result.is_success: transaction_id = result.transaction.id # Add row in Donation model donation = Donation(image=image_id, amount=amount, donor=donor_id, currency="USD", transaction_id=result.transaction.id) if donation.save(): flash( f'Your {result.transaction.currency_iso_code}{amount} donation to {receiver_username} was successful. The receipt number is {result.transaction.id}' ) email = send_donation_email(receiver_email="*****@*****.**", receiver_username=receiver_username, amount=amount, sender_email=sender) return redirect( url_for('donations.show', transaction_id=transaction_id)) else: flash( 'Your donation could not save to the database. Data validation failed?' ) return redirect( url_for('users.show', username=current_user.username)) else: flash(f'Donation failed: {result.transaction.status}') return redirect(url_for('users.show', username=current_user.username))
def create(image_id): nonce = request.form.get('payment_method_nonce') if not nonce: flash("invalid cred card deets") return redirect(url_for('users.index')) image = Image.get_or_none(Image.id == image_id) # image could have been deleted, so double check first if not image: flash("no image found with ID provided") return redirect(url_for('users.index')) amount = request.form.get('amount') if not amount: flash("no amount provided") return redirect(url_for('users.index')) # make sure sale goes through result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce, "options": { "submit_for_settlement": True } }) # amounts more than 1000 WONT BE ACCEPTED # can check result # result.is_success # should return True if not result.is_success: flash("unable to complete transaction") return redirect(request.referrer) # redirect to where they came from donation = Donation(amount=amount, image_id=image.id, user_id=current_user.id) if not donation.save(): flash('donation successful but error creating record') return redirect(url_for('users.index')) flash(f"successfully donated RM{amount} thankiu C:") user = User.select().join(Image).where(Image.id == image_id) send_simple_message(user[0].email) return redirect(url_for('users.index'))
def create(photo_id): nonce = request.form.get('payment_method_nonce') if not nonce: flash(f"Error with payment method nonce", 'warning') return redirect(url_for('users.show')) image = Photos.get_or_none(Photos.id == photo_id) if not image: flash(f"Could not find image with provided ID", 'warning') return redirect(url_for('users.show')) amount = request.form.get("amount") if not amount: flash(f"No donation provided", 'warning') return redirect(url_for('users.show')) result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce, "options": { "submit_for_settlement": True } }) if not result.is_success: flash('Unable to complete transaction', 'warning') return redirect(request.referrer) donation = Donation(amount=amount, photo_id=photo_id, user_id=current_user.id) if not donation.save(): flash('Dono successful but error recording', 'warning') return redirect(url_for('users.show')) # breakpoint() receiving_name = donation.photo.user.name name = User.get_or_none(User.id == current_user.id).name flash(f'{name} successfully donated ${amount} to {receiving_name} ') send_simple_message(amount=amount, receiving_name=receiving_name, name=name) return redirect(url_for('users.show'))
def create_checkout(img_id): result = transact({ 'amount': request.form['amount'], 'payment_method_nonce': request.form['payment_method_nonce'], 'options': { "submit_for_settlement": True } }) if result.is_success or result.transaction: d = Donation(value=request.form['amount'],img_id=img_id) d.save() return redirect(url_for('users_api.send_email' ,img_id=img_id)) else: return 'failed'
def create(image_id): payment_nonce = request.form.get("payment_nonce") amount = request.form.get("donation_amount") image = Image.get_or_none(Image.id == image_id) email = image.user.email if not image: flash("Unable to find image. Please try again.") return redirect(url_for("users.index")) if not amount or round(int(amount), 2) == 0: flash("That is not a proper amount. Please insert a valid amount.") return redirect(url_for("donations.new", image_id=image.id)) if not payment_nonce: flash("Error with payment system. Please try again.") return redirect(url_for("donations.new", image_id=image.id)) if not complete_transaction(payment_nonce, amount): flash("Something went wrong. Please check your details or the amount and try again") return redirect(url_for("donations.new", image_id=image.id)) # result = gateway.transaction.sale({ # "amount": amount, # "payment_method_nonce": nonce, # "options": { # "submit_for_settlement": True # } # }) # if not result.is_success: # flash("Donation was unsuccessful!") # return redirect(request.referrer) new_donation = Donation( user_id=current_user.id, amount=amount, image_id=image.id ) if not new_donation.save(): flash("Unable to complete the transaction. Please try again.") return redirect(url_for("donations.new", image_id=image.id)) send_message(amount, image.image, current_user.username) flash("Donation successful!") return redirect(url_for("users.show", username=image.user.username))
def user_ballance_changes(): # TODO check if an ammount can be substracted """ :type notification_data: NotificationUrl :rtype: str """ print("received notification2") print(request.data[19:-1]) data = json.loads(request.data[19:-1]) # print(d) # transaction = json.loads(request.data[19:-1]) # print(transaction.object_.Payment.amount.value) amount = decimal.Decimal(data['object']['Payment']['amount']['value']) print(amount) receiver = data['object']['Payment']['counterparty_alias']['iban'] print(receiver) if amount < 0 and receiver \ not in charities: donation = (5 + ((amount * 100) % 5)) / 100 print(donation) if donation > 0 and donation < 0.05: configuration_json = configurationRepository.read() configuration = Configuration.build_from_json(configuration_json) payment = bunqLib.make_payment( str(donation), 'Donation', charities_name[configuration.active_charity]) print(json.dumps(dict(payment.headers))) donationsRepository.create( Donation(value=float(donation), charity=receiver)) return ""
def total_donations(self): from models.donation import Donation total = 0 for donation in Donation.select().where(Donation.image_id == self.id): total = total + donation.amount return round(total)
def create(image_id): amount = request.form['amount'] nonce_from_the_client = request.form['payment_method_nonce'] result = gateway.transaction.sale({ 'amount': amount, 'payment_method_nonce': nonce_from_the_client, 'options': { "submit_for_settlement": True } }) image_owner_id = Image.get_by_id(image_id) if result.is_success: new_donation = Donation.create(sender_id=current_user.id, receiver_id=image_owner_id, image=image_id, amount=amount) # need to link to user not image new_donation.save() flash('Donation received successfully', 'success') return redirect(url_for('users.show', username=current_user.name)) else: flash(result.transaction.status) # flash(f'{result.transaction.processor_response_code}': {result.transaction.processor_response_code}) return redirect(url_for('users.show', username=current_user.name))
def create_checkout(id): image = Image.get_by_id(id) donation_amount = request.form.get('amount') result = transact({ 'amount': request.form['amount'], 'payment_method_nonce': request.form['payment_method_nonce'], 'options': { "submit_for_settlement": True } }) if result.is_success or result.transaction: new_donation = Donation(image_id=image.id, donor=current_user.id, donation_amount=donation_amount).save() return redirect( url_for('donations.show_checkout', transaction_id=result.transaction.id, id=image)) else: for x in result.errors.deep_errors: flash('Error: %s: %s' % (x.code, x.message)) return redirect(url_for('users.show', username=image.user.username))
def create_purchase(id): nonce_from_the_client = request.form["this-input"] amount = request.form.get('this-amount') gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce_from_the_client, "options": { "submit_for_settlement": True } }) Donation(amount=amount, image_id=id, donor_id=current_user.id).save() pic = Pictures.get_by_id(id) user = User.get_or_none(User.id == pic.user_id) username = user.username message = Mail( from_email='*****@*****.**', to_emails=user.email, subject='Donation Notification', html_content='@' + current_user.username + ' donated ' + amount + f'$ to the following image below!<br><img src="{ pic.post_image }" />') try: sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) response = sg.send(message) print(response.status_code) print(response.body) print(response.headers) except Exception as e: print(str(e)) return redirect(url_for('users.show', username=username))
def total_donations(self): from models.donation import Donation total = 0 for donation in Donation.select().where(Donation.image_id == self.id): # where user_id == self.id TO SEE TOTAL DONATIONS A USER HAS RECEIVED total = total + donation.amount return round(total)
def create(image_id): nonce = request.form.get('payment_method_nonce') if not nonce: flash(f"Error with payment method nonce", 'warning') return redirect(url_for('users.index')) image = UserImage.get_or_none(UserImage.id == image_id) if not image: flash(f"Could not find image with provided ID", 'warning') return redirect(url_for('users.index')) amount = request.form.get("amount") if not amount: flash(f"No donation provided", "warning") return redirect(url_for('users.index')) result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce, "options": { "submit_for_settlement": True } }) if not result.is_success: flash(f"Error in gateway transaction", "warning") return redirect(request.referrer) donation = Donation(amount=amount, image_id=image.id, user_id=current_user.id) # ---- GET DONATOR NAME AND PASS TO EMAIL------ donator = User.get_or_none(User.id == current_user.id) if not donation.save(): flash(f"Donated succesfully but error creating a record in database", 'warning') return redirect(url_for('users.index')) flash(f"Donated: ${amount}", 'success') send_simple_message(amount=amount, name=donator.name) return redirect(url_for('users.index'))
def create(image_id): nonce = request.form["nonce"] result = gateway.transaction.sale({ "amount": "10.00", "payment_method_nonce": nonce, "options": { "submit_for_settlement": True } }) if result.is_success: image = Image.get_by_id(image_id) donation = Donation(image=image, amount=Decimal(10)) donation.save() flash("Payment Successful") else: flash("payment not successful") return redirect(url_for("users.show", username=current_user.username))
def create(image_id): payload_nonce = request.form.get("payload_nonce") payment_amount = request.form.get("payment_amount") print( "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ) print(payload_nonce) image = Image.get_by_id(int(image_id)) user = User.get_by_id(current_user.id) result = gateway.transaction.sale({ "amount": payment_amount, "payment_method_nonce": payload_nonce, "device_data": None, "options": { "submit_for_settlement": True } }) if result.is_success: donation = Donation(amount=int(payment_amount), donor=user, image=image) donation.save() flash( f"Thanks you for the RM{payment_amount} donation {current_user.username}", "success") def send_simple_message(): return requests.post( "https://api.mailgun.net/v3/sandboxeaa30d1922044857bee3122cfe3ac0f9.mailgun.org/messages", auth=("api", os.environ.get("MAILGUN_PRIVATE_KEY")), data={ "from": "Excited User <*****@*****.**>", "to": ["*****@*****.**"], "subject": "Hello hahaha", "text": "changing the textailgun awesomness!", "html": f"<img src={image.full_image_url}> " }) print(send_simple_message()) return redirect(url_for('images.show', image_id=image.id))
def pay(id): nonce = request.form["nonce"] print(id) print("ITSSSSSS HEREEEEEEEEEEEEEE ====> " + nonce) result = gateway.transaction.sale({ "amount": "100.00", "payment_method_nonce": nonce, "options": { "submit_for_settlement": True } }) if result.is_success: donation = Donation(sender=User.get_by_id(current_user.id), image_id=id, amount=Decimal(100.00)) donation.save() return "Payment Received" else: return "Payment Failed"
def checkout(): print(request.form.get('paymentMethodNonce')) donation_amount = request.form.get('donation_amount') Donation.create(user=current_user.id, amount=donation_amount) print(donation_amount) result = gateway.transaction.sale({ "amount": donation_amount, "payment_method_nonce": request.form.get('paymentMethodNonce'), "options": { "submit_for_settlement": True } }) print(result) print(request.form) send_message() return redirect(url_for('users.show', username=current_user.name))
def show_image(username, image_id): image = Image.get_by_id(image_id) donations = Donation.select().join(Image).join(User).where( Image.id == image_id) total_donation = 0 for donation in donations: total_donation += donation.amount return render_template('users/image.html', image=image, donations=donations, total_donation=total_donation)
def donation_received(self): from models.donation import Donation from models.image import Image donation_r = Donation.select().join( Image, on=(Donation.image_id == Image.id)).where(Image.user_id == self.id) donation_received = 0 for d in donation_r: donation_received = donation_received + d.amount donation_received = round(donation_received, 2) return donation_received
def create(): nonce_from_the_client = request.form["payment_method_nonce"] sender_id = current_user.id or None receiver_id = request.form['receiver_id'] amount = request.form["amount"] result = gateway.transaction.sale({ "amount": request.form["amount"], "payment_method_nonce": nonce_from_the_client, "options": { "submit_for_settlement": True } }) if type(result) == SuccessfulResult: new_donation = Donation(receiver_id=receiver_id, sender_id=sender_id, amount=amount) new_donation.save() return redirect(url_for('index')) flash('Invalid amount entered!') return render_template('donate/new.html', client_token=client_token)
def create(image_id): nonce = request.form.get('payment_method_nonce') if not nonce: flash('Invalid credit card details') return redirect(url_for('users.index')) image = Image.get_or_none(Image.id == image_id) if not image: flash('No Image Found with provided id', 'warning') return redirect(url_for('users.index')) amount = request.form.get('amount') if not amount: flash('No donation amount provided', 'warning') return redirect(url_for('users.index')) result = gateway.transaction.sale({ "amount": amount, "payment_method_nonce": nonce, "options": { "submit_for_settlement": True } }) if not result.is_success: flash('Unable to complete transaction', 'warning') return redirect(request.referrer) donation = Donation(amount=amount, image_id=image.id, user_id=current_user.id) if not donation.save(): flash('Donation successful but error creating record', 'warning') return redirect(url_for('users.index')) flash(f'Successfully donated RM{amount}', 'success') return redirect(url_for('users.index'))