class LnurlpayController < ApplicationController before_action :find_user_by_address def index render json: { status: "OK", callback: "https://accounts.kosmos.org/lnurlpay/#{@user.address}/invoice", tag: "payRequest", maxSendable: 1000000 * 1000, # msat minSendable: 100 * 1000, # msat metadata: metadata(@user.address), commentAllowed: 0 } end def invoice amount = params[:amount].to_i # msats address = params[:address] validate_amount(amount) payment_request = @user.ln_create_invoice({ amount: amount / 1000, # we create invoices in sats description_hash: Digest::SHA2.hexdigest(metadata(address)) }) render json: { status: "OK", successAction: { tag: "message", message: "Sats received. Thank you!" }, routes: [], pr: payment_request } end private def find_user_by_address address = params[:address].split("@") @user = User.where(cn: address.first, ou: address.last).first http_status :not_found if @user.nil? end def metadata(address) "[[\"text/identifier\", \"#{address}\"], [\"text/plain\", \"Send sats, receive thanks.\"]]" end def validate_amount(amount) if amount > 1000000 || amount < 1000 render json: { status: "ERROR", reason: "Invalid amount" } return end end end