akkounts/app/controllers/lnurlpay_controller.rb

58 lines
1.3 KiB
Ruby

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: 1000 * 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: "Payment received. Thanks!"
},
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\", \"Sats for #{address}\"]]"
end
def validate_amount(amount)
if amount > 1000000 || amount < 1000
render json: { status: "ERROR", reason: "Invalid amount" }
return
end
end
end