Skip to content

Commit e6e92e8

Browse files
committed
remove duplicate code for create invoices
1 parent adbbca9 commit e6e92e8

File tree

1 file changed

+36
-67
lines changed

1 file changed

+36
-67
lines changed

Diff for: umaserverdemo/src/main/kotlin/com/lightspark/ReceivingVasp.kt

+36-67
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import io.ktor.client.call.body
99
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
1010
import io.ktor.client.request.parameter
1111
import io.ktor.client.request.post
12-
import io.ktor.client.request.request
1312
import io.ktor.client.request.setBody
1413
import io.ktor.http.ContentType
1514
import io.ktor.http.HttpStatusCode
@@ -72,54 +71,13 @@ class ReceivingVasp(
7271
}
7372

7473
suspend fun createInvoice(call: ApplicationCall): String {
75-
// get currencyCode, Amount, verify user
76-
val amount = try {
77-
call.parameters["amount"]?.toLong() ?: run {
78-
call.respond(HttpStatusCode.BadRequest, "Amount not provided.")
79-
return "Amount not provided."
80-
}
81-
} catch (e: NumberFormatException) {
82-
call.respond(HttpStatusCode.BadRequest, "Amount not parsable as number.")
83-
return "Amount not parsable as number."
84-
}
85-
86-
val currency = call.parameters["currencyCode"]?.let { currencyCode ->
87-
// check if we support this currency code.
88-
getReceivingCurrencies(UMA_VERSION_STRING).firstOrNull {
89-
it.code == currencyCode
90-
} ?: run {
91-
call.respond(HttpStatusCode.BadRequest, "Unsupported CurrencyCode $currencyCode.")
92-
return "Unsupported CurrencyCode $currencyCode."
93-
}
94-
} ?: run {
95-
call.respond(HttpStatusCode.BadRequest, "CurrencyCode not provided.")
96-
return "CurrencyCode not provided."
74+
val (status, data) = createUmaInvoice(call)
75+
if (status != HttpStatusCode.OK) {
76+
call.respond(status, data)
77+
return data
78+
} else {
79+
call.respond(data)
9780
}
98-
99-
val expiresIn2Days = Clock.System.now().plus(2, DateTimeUnit.HOUR*24) //?
100-
101-
val receiverUma = "${config.username}:${getReceivingVaspDomain(call)}"
102-
println(config.vaspDomain)
103-
104-
val response = uma.getInvoice(
105-
receiverUma = receiverUma,
106-
invoiceUUID = UUID.randomUUID().toString(),
107-
amount = amount,
108-
receivingCurrency = InvoiceCurrency(
109-
currency.code, currency.name, currency.symbol, currency.decimals
110-
),
111-
expiration = expiresIn2Days.toEpochMilliseconds(),
112-
isSubjectToTravelRule = true,
113-
requiredPayerData = createCounterPartyDataOptions(
114-
"name" to false,
115-
"email" to false,
116-
"compliance" to true,
117-
"identifier" to true,
118-
),
119-
callback = getLnurlpCallback(call), // structured the same, going to /api/uma/payreq/{user_id}
120-
privateSigningKey = config.umaSigningPrivKey
121-
)
122-
call.respond(response.toBech32())
12381
return "OK"
12482
}
12583

@@ -128,27 +86,48 @@ class ReceivingVasp(
12886
call.respond(HttpStatusCode.BadRequest, "SenderUma not provided.")
12987
return "SenderUma not provided."
13088
}
89+
val (status, data) = createUmaInvoice(call, senderUma)
90+
if (status != HttpStatusCode.OK) {
91+
call.respond(status, data)
92+
return data
93+
}
94+
val response = try {
95+
httpClient.post("/api/uma/payreq/${config.userID}") {
96+
contentType(ContentType.Application.Json)
97+
setBody(parameter("invoice", data))
98+
}
99+
} catch (e: Exception) {
100+
call.respond(HttpStatusCode.FailedDependency, "failed to fetch /api/uma/payreq/${config.userID}")
101+
return "failed to fetch /api/uma/payreq/${config.userID}"
102+
}
103+
if (response.status != HttpStatusCode.OK) {
104+
call.respond(HttpStatusCode.InternalServerError, "Payreq to Sending Vasp: ${response.status}")
105+
return "Payreq to sending vasp failed: ${response.status}"
106+
}
107+
call.respond(response.body())
108+
return "OK"
109+
}
110+
111+
private fun createUmaInvoice(
112+
call: ApplicationCall, senderUma: String? = null
113+
): Pair<HttpStatusCode, String> {
131114
val amount = try {
132115
call.parameters["amount"]?.toLong() ?: run {
133-
call.respond(HttpStatusCode.BadRequest, "Amount not provided.")
134-
return "Amount not provided."
116+
return HttpStatusCode.BadRequest to "Amount not provided."
135117
}
136118
} catch (e: NumberFormatException) {
137-
call.respond(HttpStatusCode.BadRequest, "Amount not parsable as number.")
138-
return "Amount not parsable as number."
119+
return HttpStatusCode.BadRequest to "Amount not parsable as number."
139120
}
140121

141122
val currency = call.parameters["currencyCode"]?.let { currencyCode ->
142123
// check if we support this currency code.
143124
getReceivingCurrencies(UMA_VERSION_STRING).firstOrNull {
144125
it.code == currencyCode
145126
} ?: run {
146-
call.respond(HttpStatusCode.BadRequest, "Unsupported CurrencyCode $currencyCode.")
147-
return "Unsupported CurrencyCode $currencyCode."
127+
return HttpStatusCode.BadRequest to "Unsupported CurrencyCode $currencyCode."
148128
}
149129
} ?: run {
150-
call.respond(HttpStatusCode.BadRequest, "CurrencyCode not provided.")
151-
return "CurrencyCode not provided."
130+
return HttpStatusCode.BadRequest to "CurrencyCode not provided."
152131
}
153132

154133
val expiresIn2Days = Clock.System.now().plus(2, DateTimeUnit.HOUR*24) //?
@@ -175,17 +154,7 @@ class ReceivingVasp(
175154
privateSigningKey = config.umaSigningPrivKey,
176155
senderUma = senderUma
177156
)
178-
val encodedInvoice = invoice.toBech32()
179-
val response = httpClient.post("/api/uma/payreq/${config.userID}") {
180-
contentType(ContentType.Application.Json)
181-
setBody(parameter("invoice", encodedInvoice))
182-
}
183-
if (response.status != HttpStatusCode.OK) {
184-
call.respond(HttpStatusCode.InternalServerError, "Payreq to Sending Vasp: ${response.status}")
185-
return "Payreq to vasp2 failed: ${response.status}"
186-
}
187-
call.respond(response.body())
188-
return "OK"
157+
return HttpStatusCode.OK to invoice.toBech32()
189158
}
190159

191160
suspend fun handleLnurlp(call: ApplicationCall): String {

0 commit comments

Comments
 (0)