Files
breez-sdk-liquid/lib/bindings/langs/swift/Sources/BreezSDKLiquid/Task/InvoiceRequest.swift
Ross Savage 5b69c7beb2 BOLT12 receive (#882)
* Add BOLT12 receive payment handling

* Handle BOLT12 invoice requests via WS

* Fix invoice request subscription on stream initialisation

* Store the BOLT12 offer used to receive a payment

* Address review feedback

* Separate into create BOLT12 invoice fn

* Update all BOLT12 offers when webhook URL changes

* Deprecate Lightning for Bolt11Invoice
2025-04-29 11:43:45 +00:00

46 lines
2.3 KiB
Swift

import UserNotifications
import Foundation
struct InvoiceRequestRequest: Codable {
let offer: String
let invoice_request: String
let reply_url: String
}
struct InvoiceRequestResponse: Decodable, Encodable {
let invoice: String
init(invoice: String) {
self.invoice = invoice
}
}
class InvoiceRequestTask : ReplyableTask {
fileprivate let TAG = "InvoiceRequestTask"
init(payload: String, logger: ServiceLogger, contentHandler: ((UNNotificationContent) -> Void)? = nil, bestAttemptContent: UNMutableNotificationContent? = nil) {
let successNotificationTitle = ResourceHelper.shared.getString(key: Constants.INVOICE_REQUEST_NOTIFICATION_TITLE, fallback: Constants.DEFAULT_INVOICE_REQUEST_NOTIFICATION_TITLE)
let failNotificationTitle = ResourceHelper.shared.getString(key: Constants.INVOICE_REQUEST_NOTIFICATION_FAILURE_TITLE, fallback: Constants.DEFAULT_INVOICE_REQUEST_NOTIFICATION_FAILURE_TITLE)
super.init(payload: payload, logger: logger, contentHandler: contentHandler, bestAttemptContent: bestAttemptContent, successNotificationTitle: successNotificationTitle, failNotificationTitle: failNotificationTitle)
}
override func start(liquidSDK: BindingLiquidSdk) throws {
var request: InvoiceRequestRequest? = nil
do {
request = try JSONDecoder().decode(InvoiceRequestRequest.self, from: self.payload.data(using: .utf8)!)
} catch let e {
self.logger.log(tag: TAG, line: "failed to decode payload: \(e)", level: "ERROR")
self.displayPushNotification(title: self.failNotificationTitle, logger: self.logger, threadIdentifier: Constants.NOTIFICATION_THREAD_REPLACEABLE)
throw e
}
do {
let createBolt12InvoiceRes = try liquidSDK.createBolt12Invoice(req: CreateBolt12InvoiceRequest(offer: request!.offer, invoiceRequest: request!.invoice_request))
self.replyServer(encodable: InvoiceRequestResponse(invoice: createBolt12InvoiceRes.invoice), replyURL: request!.reply_url)
} catch let e {
self.logger.log(tag: TAG, line: "failed to process invoice request: \(e)", level: "ERROR")
self.displayPushNotification(title: self.failNotificationTitle, logger: self.logger, threadIdentifier: Constants.NOTIFICATION_THREAD_REPLACEABLE)
}
}
}