fix payment restore
This commit is contained in:
parent
cbcac82541
commit
ed3442eb03
2 changed files with 24 additions and 26 deletions
|
|
@ -357,7 +357,7 @@ export default class {
|
||||||
if (useProvider) {
|
if (useProvider) {
|
||||||
const res = await this.liquidProvider.PayInvoice(invoice, decodedAmount, from)
|
const res = await this.liquidProvider.PayInvoice(invoice, decodedAmount, from)
|
||||||
const providerDst = this.liquidProvider.GetProviderDestination()
|
const providerDst = this.liquidProvider.GetProviderDestination()
|
||||||
return { feeSat: res.network_fee + res.service_fee, valueSat: res.amount_paid, paymentPreimage: res.preimage, providerDst }
|
return { feeSat: res.service_fee, valueSat: res.amount_paid, paymentPreimage: res.preimage, providerDst }
|
||||||
}
|
}
|
||||||
await this.Health()
|
await this.Health()
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,7 @@ export default class {
|
||||||
checkPendingProviderPayment = async (log: PubLogger, p: UserInvoicePayment) => {
|
checkPendingProviderPayment = async (log: PubLogger, p: UserInvoicePayment) => {
|
||||||
const state = await this.lnd.liquidProvider.GetPaymentState(p.invoice)
|
const state = await this.lnd.liquidProvider.GetPaymentState(p.invoice)
|
||||||
if (state.paid_at_unix < 0) {
|
if (state.paid_at_unix < 0) {
|
||||||
const fullAmount = p.paid_amount + p.service_fees + p.routing_fees
|
const fullAmount = p.paid_amount + p.service_fees
|
||||||
log("found a failed provider payment, refunding", fullAmount, "sats to user", p.user.user_id)
|
log("found a failed provider payment, refunding", fullAmount, "sats to user", p.user.user_id)
|
||||||
await this.storage.StartTransaction(async tx => {
|
await this.storage.StartTransaction(async tx => {
|
||||||
await this.storage.userStorage.IncrementUserBalance(p.user.user_id, fullAmount, "payment_refund:" + p.invoice, tx)
|
await this.storage.userStorage.IncrementUserBalance(p.user.user_id, fullAmount, "payment_refund:" + p.invoice, tx)
|
||||||
|
|
@ -96,18 +96,16 @@ export default class {
|
||||||
return
|
return
|
||||||
} else if (state.paid_at_unix > 0) {
|
} else if (state.paid_at_unix > 0) {
|
||||||
log("provider payment succeeded", p.serial_id, "updating payment info")
|
log("provider payment succeeded", p.serial_id, "updating payment info")
|
||||||
const routingFeeLimit = p.routing_fees
|
|
||||||
const serviceFee = p.service_fees
|
const serviceFee = p.service_fees
|
||||||
const actualFee = state.network_fee + state.service_fee
|
const networkFee = state.service_fee
|
||||||
await this.storage.StartTransaction(async tx => {
|
await this.storage.paymentStorage.UpdateExternalPayment(p.serial_id, networkFee, serviceFee, true)
|
||||||
if (routingFeeLimit - actualFee > 0) {
|
const remainingFee = serviceFee - networkFee
|
||||||
this.log("refund pending provider payment routing fee", routingFeeLimit, actualFee, "sats")
|
if (remainingFee < 0) {
|
||||||
await this.storage.userStorage.IncrementUserBalance(p.user.user_id, routingFeeLimit - actualFee, "routing_fee_refund:" + p.invoice, tx)
|
this.log("WARNING: provider fee was higher than expected,", remainingFee, "were lost")
|
||||||
}
|
}
|
||||||
await this.storage.paymentStorage.UpdateExternalPayment(p.serial_id, actualFee, p.service_fees, true, undefined, tx)
|
|
||||||
}, "pending provider payment success after restart")
|
if (p.linkedApplication && p.user.user_id !== p.linkedApplication.owner.user_id && remainingFee > 0) {
|
||||||
if (p.linkedApplication && p.user.user_id !== p.linkedApplication.owner.user_id && serviceFee > 0) {
|
await this.storage.userStorage.IncrementUserBalance(p.linkedApplication.owner.user_id, remainingFee, "fees")
|
||||||
await this.storage.userStorage.IncrementUserBalance(p.linkedApplication.owner.user_id, serviceFee, "fees")
|
|
||||||
}
|
}
|
||||||
const user = await this.storage.userStorage.GetUser(p.user.user_id)
|
const user = await this.storage.userStorage.GetUser(p.user.user_id)
|
||||||
this.storage.eventsLog.LogEvent({ type: 'invoice_payment', userId: p.user.user_id, appId: p.linkedApplication?.app_id || "", appUserId: "", balance: user.balance_sats, data: p.invoice, amount: p.paid_amount })
|
this.storage.eventsLog.LogEvent({ type: 'invoice_payment', userId: p.user.user_id, appId: p.linkedApplication?.app_id || "", appUserId: "", balance: user.balance_sats, data: p.invoice, amount: p.paid_amount })
|
||||||
|
|
@ -123,7 +121,6 @@ export default class {
|
||||||
log(ERROR, "lnd payment not found for pending payment hash ", decoded.paymentHash)
|
log(ERROR, "lnd payment not found for pending payment hash ", decoded.paymentHash)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (payment.status) {
|
switch (payment.status) {
|
||||||
case Payment_PaymentStatus.UNKNOWN:
|
case Payment_PaymentStatus.UNKNOWN:
|
||||||
log("pending payment in unknown state", p.serial_id, "no action will be performed")
|
log("pending payment in unknown state", p.serial_id, "no action will be performed")
|
||||||
|
|
@ -133,24 +130,22 @@ export default class {
|
||||||
return
|
return
|
||||||
case Payment_PaymentStatus.SUCCEEDED:
|
case Payment_PaymentStatus.SUCCEEDED:
|
||||||
log("pending payment succeeded", p.serial_id, "updating payment info")
|
log("pending payment succeeded", p.serial_id, "updating payment info")
|
||||||
const routingFeeLimit = p.routing_fees
|
|
||||||
const serviceFee = p.service_fees
|
const serviceFee = p.service_fees
|
||||||
const actualFee = Number(payment.feeSat)
|
const networkFee = Number(payment.feeSat)
|
||||||
await this.storage.StartTransaction(async tx => {
|
|
||||||
if (routingFeeLimit - actualFee > 0) {
|
await this.storage.paymentStorage.UpdateExternalPayment(p.serial_id, networkFee, p.service_fees, true, undefined)
|
||||||
this.log("refund pending payment routing fee", routingFeeLimit, actualFee, "sats")
|
const remainingFee = serviceFee - networkFee
|
||||||
await this.storage.userStorage.IncrementUserBalance(p.user.user_id, routingFeeLimit - actualFee, "routing_fee_refund:" + p.invoice, tx)
|
if (remainingFee < 0) { // should not be possible beacuse of the fee limit
|
||||||
|
this.log("WARNING: lnd fee was higher than expected,", remainingFee, "were lost")
|
||||||
}
|
}
|
||||||
await this.storage.paymentStorage.UpdateExternalPayment(p.serial_id, actualFee, p.service_fees, true, undefined, tx)
|
if (p.linkedApplication && p.user.user_id !== p.linkedApplication.owner.user_id && remainingFee > 0) {
|
||||||
}, "pending payment success after restart")
|
await this.storage.userStorage.IncrementUserBalance(p.linkedApplication.owner.user_id, remainingFee, "fees")
|
||||||
if (p.linkedApplication && p.user.user_id !== p.linkedApplication.owner.user_id && serviceFee > 0) {
|
|
||||||
await this.storage.userStorage.IncrementUserBalance(p.linkedApplication.owner.user_id, serviceFee, "fees")
|
|
||||||
}
|
}
|
||||||
const user = await this.storage.userStorage.GetUser(p.user.user_id)
|
const user = await this.storage.userStorage.GetUser(p.user.user_id)
|
||||||
this.storage.eventsLog.LogEvent({ type: 'invoice_payment', userId: p.user.user_id, appId: p.linkedApplication?.app_id || "", appUserId: "", balance: user.balance_sats, data: p.invoice, amount: p.paid_amount })
|
this.storage.eventsLog.LogEvent({ type: 'invoice_payment', userId: p.user.user_id, appId: p.linkedApplication?.app_id || "", appUserId: "", balance: user.balance_sats, data: p.invoice, amount: p.paid_amount })
|
||||||
return
|
return
|
||||||
case Payment_PaymentStatus.FAILED:
|
case Payment_PaymentStatus.FAILED:
|
||||||
const fullAmount = p.paid_amount + p.service_fees + p.routing_fees
|
const fullAmount = p.paid_amount + p.service_fees
|
||||||
log("found a failed pending payment, refunding", fullAmount, "sats to user", p.user.user_id)
|
log("found a failed pending payment, refunding", fullAmount, "sats to user", p.user.user_id)
|
||||||
await this.storage.StartTransaction(async tx => {
|
await this.storage.StartTransaction(async tx => {
|
||||||
await this.storage.userStorage.IncrementUserBalance(p.user.user_id, fullAmount, "payment_refund:" + p.invoice, tx)
|
await this.storage.userStorage.IncrementUserBalance(p.user.user_id, fullAmount, "payment_refund:" + p.invoice, tx)
|
||||||
|
|
@ -303,6 +298,9 @@ export default class {
|
||||||
paymentInfo = await this.PayExternalInvoice(userId, req.invoice, { payAmount, serviceFee, amountForLnd: req.amount }, linkedApplication, req.debit_npub, ack)
|
paymentInfo = await this.PayExternalInvoice(userId, req.invoice, { payAmount, serviceFee, amountForLnd: req.amount }, linkedApplication, req.debit_npub, ack)
|
||||||
}
|
}
|
||||||
const feeDiff = serviceFee - paymentInfo.networkFee
|
const feeDiff = serviceFee - paymentInfo.networkFee
|
||||||
|
if (feeDiff < 0) {
|
||||||
|
this.log("WARNING: provider fee was higher than expected,", feeDiff, "were lost")
|
||||||
|
}
|
||||||
if (isAppUserPayment && feeDiff > 0) {
|
if (isAppUserPayment && feeDiff > 0) {
|
||||||
await this.storage.userStorage.IncrementUserBalance(linkedApplication.owner.user_id, feeDiff, "fees")
|
await this.storage.userStorage.IncrementUserBalance(linkedApplication.owner.user_id, feeDiff, "fees")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue