def create(image_id):
    nonce_from_the_client = request.form["payment_method_nonce"]
    amount = request.form["amount"]

    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:
        payment = Payment(user_id=current_user.id,
                          image_id=image_id,
                          amount=amount)
        payment.save()
        mg_key = os.environ.get('MAILGUN_API_KEY')

        result = requests.post(
            "https://api.mailgun.net/v3/sandboxdb0c2cd1760a44d08240d1f87633eeab.mailgun.org/messages",
            auth=("api", "'{}'.format(mg_key)"),
            data={
                "from":
                "Excited User <*****@*****.**>",
                "to": ["*****@*****.**"],
                "subject": "Hello",
                "text": "Testing some Mailgun awesomeness!"
            })
        return redirect(url_for('images.show', id=image_id))

    else:
        flash("Failed to donate!", "danger")
        return redirect(url_for("payments.new", image_id=image_id))
Ejemplo n.º 2
0
def new():
    student = Student.get_by_id(get_jwt_identity())
    params = request.json

    if student:
        student_tutor_session = Student_tutor_session.get_by_id(params.get("student_tutor_session"))
        tutor_session =  Tutor_session.get_by_id(student_tutor_session.tutor_session_id)
        # duration = tutor_session.duration
        # amount = tutor_session.price * duration # price per hour * hrs
        amount = tutor_session.price

        new_payment = Payment(
            student_tutor_session_id = student_tutor_session.id,
            amount = amount,
            status = 'complete',
            status_timestamp = datetime.now()
        )

    if new_payment.save():
        responseObject = (
            {
                "message" : "Payment completed." ,
                "status" : "success!",
                "payment" : {
                    "id" : new_payment.id,
                    "amount" : new_payment.amount,
                    "status" : new_payment.status,
                    "status_timestamp" : new_payment.status_timestamp,
                    "student_tutor_session" : new_payment.student_tutor_session_id
                }
            }
        )
        return make_response(jsonify(responseObject)), 201
    else:
        return make_response(jsonify([err for err in new_payment.errors])), 400
Ejemplo n.º 3
0
def create():
    result = gateway.transaction.sale({  # this code then sends the nonce to brain tree's server
        # to check out the data you receive use: "request.form"
        "amount": request.form.get("amount"),
        "payment_method_nonce": request.form.get("paymentMethodNonce"),
        "options": {
            "submit_for_settlement": True
        }
    })
    transaction_id = result.transaction.id
    amount = result.transaction.amount
    payment = Payment(
        amount=amount, transaction_id=transaction_id, user=current_user.id)
    payment.save()
    flash('Payment Sent!')
    return "temp"
Ejemplo n.º 4
0
def create(image_id):
    nonce = request.form.get('nonce')
    moneyleft = request.form.get('money-left')
    moneyright = request.form.get('money-right')

    image = Image.get_by_id(image_id)
    recipient = User.get_by_id(image.user_id)

    try:
        if int(moneyleft) > -1 and int(moneyright) > -1 and int(
                moneyright) < 100:
            amount = f'{moneyleft}.{moneyright}'
            result = gateway.transaction.sale({
                "amount": amount,
                "payment_method_nonce": nonce,
                "options": {
                    "submit_for_settlement": True
                }
            })

            if result.is_success:

                payment = Payment(user_id=current_user.id,
                                  image_id=image_id,
                                  amount=amount)
                payment.save()

                message = Mail(
                    from_email='*****@*****.**',
                    to_emails=recipient.email,
                    subject='Donation Received',
                    html_content=f'Donation received for {image.id}')

                sg = SendGridAPIClient(
                    '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'
                )
                sg.send(message)

                flash('Donation Success')
                return redirect(url_for('users.show', username=recipient.name))
    except:
        flash('Payment failed. Try again', "Danger")
        return redirect(url_for('payments.new', image_id=image_id))
    return f'{nonce}'
Ejemplo n.º 5
0
def create_purchase(picture_id):
    donation_amount = request.form.get('donation-amount')
    donation_message = request.form.get('donation-message')
    nonce = request.form.get('nonce')
    result = gateway.transaction.sale({
        "amount": donation_amount,
        "payment_method_nonce": nonce,
        "options": {
            "submit_for_settlement": True
        }
    })

    if result:
        new_payment_instance = Payment(donor_id=current_user.id,
                                       payment_amount=donation_amount,
                                       message=donation_message,
                                       picture_id=picture_id)
        new_payment_instance.save()
        picture = Picture.get_by_id(picture_id)
        user = User.get_by_id(picture.user_id)

        message = Mail(
            from_email=current_user.email,
            to_emails=user.email,
            subject=
            f"Nextagram: New Donation from {current_user.name.capitalize()}",
            html_content=
            f"<p>Dear {user.name.capitalize()},</p><br><p>{current_user.name.capitalize()} has just donated ${donation_amount} to you!</p><br><p>{current_user.name.capitalize()}\'s message: {donation_message}</p><br><p>With love,</p><br><p>Nextagram</p>"
        )
        try:
            sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
            response = sg.send(message)
            print(response.status_code)
            print(response.body)
            print(response.headers)
            flash('Payment successful')
        except Exception as e:
            print(str(e))
        return redirect(url_for('users.show', username=user.name))
    else:
        flash('Payment unsuccessful. Please try again.')
        return render_template('home.html')
Ejemplo n.º 6
0
def create_purchase(image_id):
    nonce_from_the_client = request.form["payment_nonce"]
    amount = request.form["amount"]
    print(nonce_from_the_client)
    print("0000000000000000000000000")
    print(amount)
    result = gateway.transaction.sale({
        "amount": amount,
        "payment_method_nonce": nonce_from_the_client,
        "options": {
            "submit_for_settlement": True
        }
    })

    if result.is_success:
        payment = Payment(sender=User.get_by_id(current_user.id),
                          image_id=image_id,
                          amount=amount)
        payment.save()
        return redirect(url_for("images.show", id=image_id))
    else:
        flash("Failed to donate! Please try again!")
        return redirect(url_for("payments.new", image_id=image_id))
Ejemplo n.º 7
0
def create_purchase(img_id):
    nonce = request.form.get("nonce")
    amount = request.form.get("dollar")
    message = request.form.get("message")
    result = gateway.transaction.sale({
        "amount": amount,
        "payment_method_nonce": nonce,
        "options": {
            "submit_for_settlement": True
        }
    })
    if result.is_success:
        payment = Payment(payment=amount,
                          donator_id=current_user.id,
                          image_id=img_id,
                          message=message)
        if payment.save():
            image = Image.get_or_none(id=img_id)
            image_owner = User.get_or_none(id=image.user_id)
            message = Mail(
                from_email="*****@*****.**",
                to_emails=image_owner.email,
                subject=f"Donation from {current_user.username}",
                html_content=
                f'<strong>A donation of RM{amount} is made on your image{img_id} from {current_user.username}</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(str(e))

            flash(
                f"Transaction successful! Thanks on the behalf of Nextagram and {image_owner.username}!",
                'success')
            return redirect(url_for('home'))
        else:
            return render_template('payment/new.html')

    else:
        flash('Transaction failed', 'danger')
        return render_template('payment/new.html')
Ejemplo n.º 8
0
def create():
    student_tutor_session = Student_tutor_session.get_by_id(1)
    tutor_session =  Tutor_session.get_by_id(student_tutor_session.tutor_session)
    duration = tutor_session.duration 
    amount = tutor_session.price*duration/60 # hourly rate * hrs

    print(f"the amount is {amount}")

    new_payment = Payment(
        student_tutor_session=student_tutor_session,
        amount=amount,
    )

    if new_payment.save():
        flash("transaction saved")
    else:
        flash("transaction failed")
    return render_template('home.html', errors = new_payment.errors)