Example #1
0
def initiate_payment(payload):
    # Check the amount is a float
    amount = payload["amount"]
    try:
        amount = float(amount)
    except Exception as e:
        update_status(payment, "Invalid amount.")
        amount = None
        return

    # Label as a donation if the id is missing
    if "id" in payload.keys():
        label = payload["id"]
    else:
        label = "donation"

    # Get payment method, use one specified in query string if provided
    if "method" in payload.keys():
            payment_method = payload['method']
    else:
        payment_method = config.pay_method

    # Initialise the payment invoice
    payment = create_invoice(amount, "USD", label, payment_method)

    # Wait for the amount to be sent to the address
    process_payment(payment)

    # On successful payment
    if payment.paid:
        update_status(payment, "Payment finalised. Thankyou!")

        # If a w_url for woocommerce webhook has been provided, then we need
        # to take some additional steps to confirm the order.
        if "w_url" in payload.keys():
            # Call webhook
            response = woo_webhook.hook(app.config["SECRET_KEY"], payload, payment)

            if response.status_code != 200:
                print(
                    "Failed to confirm order payment via webhook {}, the response is: {}".format(
                        response.status_code, response.text
                    )
                )
                update_status(payment, response.text)
            else:
                print("Successfully confirmed payment via webhook.")
                update_status(payment, "Order confirmed.")

       # Redirect after payment
        # TODO: add a delay here. Test.
        if config.redirect is not None:
            print("Redirecting to {}".format(config.redirect))
            return redirect(config.redirect)
        else:
            print("No redirect, closing.")
            
    return
Example #2
0
    def get(self):
        "Complete Payment"
        """Run post-payment processing such as any webhooks."""
        uuid = request.args.get("uuid")
        order_id = request.args.get("id")

        invoice = database.load_invoice_from_db(uuid)
        status = check_payment_status(uuid)

        if status["time_left"] < 0:
            return {"message": "Expired."}, 400

        if status["payment_complete"] != 1:
            return {"message": "You havent paid you stingy bastard"}

        if (config.liquid_address is not None) and (invoice["method"]
                                                    in ["lnd", "clightning"]):
            weakhands.swap_lnbtc_for_lusdt(lightning_node,
                                           invoice["btc_value"],
                                           config.liquid_address)

        # Call webhook to confirm payment with merchant
        if (invoice["webhook"] is not None) and (invoice["webhook"] != ""):
            logging.info("Calling webhook {}".format(invoice["webhook"]))
            response = woo_webhook.hook(app.config["SECRET_KEY"], invoice,
                                        order_id)

            if response.status_code != 200:
                err = "Failed to confirm order payment via webhook {}, please contact the store to ensure the order has been confirmed, error response is: {}".format(
                    response.status_code, response.text)
                logging.error(err)
                return {"message": err}, 500

            logging.info("Successfully confirmed payment via webhook.")
            return {"message": "Payment confirmed with store."}, 200

        return {"message": "Payment confirmed."}, 200