@@ -9,7 +9,6 @@ import io.ktor.client.call.body
9
9
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
10
10
import io.ktor.client.request.parameter
11
11
import io.ktor.client.request.post
12
- import io.ktor.client.request.request
13
12
import io.ktor.client.request.setBody
14
13
import io.ktor.http.ContentType
15
14
import io.ktor.http.HttpStatusCode
@@ -72,54 +71,13 @@ class ReceivingVasp(
72
71
}
73
72
74
73
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)
97
80
}
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())
123
81
return " OK"
124
82
}
125
83
@@ -128,27 +86,48 @@ class ReceivingVasp(
128
86
call.respond(HttpStatusCode .BadRequest , " SenderUma not provided." )
129
87
return " SenderUma not provided."
130
88
}
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 > {
131
114
val amount = try {
132
115
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."
135
117
}
136
118
} 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."
139
120
}
140
121
141
122
val currency = call.parameters[" currencyCode" ]?.let { currencyCode ->
142
123
// check if we support this currency code.
143
124
getReceivingCurrencies(UMA_VERSION_STRING ).firstOrNull {
144
125
it.code == currencyCode
145
126
} ? : run {
146
- call.respond(HttpStatusCode .BadRequest , " Unsupported CurrencyCode $currencyCode ." )
147
- return " Unsupported CurrencyCode $currencyCode ."
127
+ return HttpStatusCode .BadRequest to " Unsupported CurrencyCode $currencyCode ."
148
128
}
149
129
} ? : run {
150
- call.respond(HttpStatusCode .BadRequest , " CurrencyCode not provided." )
151
- return " CurrencyCode not provided."
130
+ return HttpStatusCode .BadRequest to " CurrencyCode not provided."
152
131
}
153
132
154
133
val expiresIn2Days = Clock .System .now().plus(2 , DateTimeUnit .HOUR * 24 ) // ?
@@ -175,17 +154,7 @@ class ReceivingVasp(
175
154
privateSigningKey = config.umaSigningPrivKey,
176
155
senderUma = senderUma
177
156
)
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()
189
158
}
190
159
191
160
suspend fun handleLnurlp (call : ApplicationCall ): String {
0 commit comments