Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding invoice UUID to PayReq and PayReqResponse, for uma invoice #58

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions javatest/src/test/java/me/uma/javatest/UmaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public void testGetPayRequest_umaV1() throws Exception {
null,
null,
"comment",
"sample-uuid-string",
"1.0"
);
assertNotNull(request);
Expand Down Expand Up @@ -248,6 +249,7 @@ public void testGetPayRequest_umaV0() throws Exception {
null,
null,
"comment",
"sample-uuid-string",
"0.3"
);
assertNotNull(request);
Expand Down
7 changes: 5 additions & 2 deletions uma-sdk/src/commonMain/kotlin/me/uma/UmaProtocolHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ class UmaProtocolHelper @JvmOverloads constructor(
travelRuleFormat: TravelRuleFormat? = null,
requestedPayeeData: CounterPartyDataOptions? = null,
comment: String? = null,
invoiceUUID: String? = null,
receiverUmaVersion: String = UMA_VERSION_STRING,
): PayRequest {
val compliancePayerData = getSignedCompliancePayerData(
Expand Down Expand Up @@ -386,6 +387,7 @@ class UmaProtocolHelper @JvmOverloads constructor(
amount = amount,
requestedPayeeData = requestedPayeeData,
comment = comment,
invoiceUUID = invoiceUUID
)
}
}
Expand Down Expand Up @@ -668,8 +670,9 @@ class UmaProtocolHelper @JvmOverloads constructor(
successAction: Map<String, String>? = null,
senderUmaVersion: String = UMA_VERSION_STRING,
): PayReqResponse {
val encodedPayerData = query.payerData?.let { serialFormat.encodeToString(query.payerData) } ?: ""
val metadataWithPayerData = "$metadata$encodedPayerData"
val encodedPayerData = query.payerData?.let(serialFormat::encodeToString) ?: ""
val encodedInvoiceUUID = query.invoiceUUID()?.let(serialFormat::encodeToString) ?: ""
val metadataWithPayerData = "$metadata$encodedPayerData$encodedInvoiceUUID"
matthappens marked this conversation as resolved.
Show resolved Hide resolved
if (query.sendingCurrencyCode() != null && query.sendingCurrencyCode() != receivingCurrencyCode) {
throw IllegalArgumentException(
"Currency code in the pay request must match the receiving currency if not null.",
Expand Down
20 changes: 20 additions & 0 deletions uma-sdk/src/commonMain/kotlin/me/uma/protocol/PayRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ sealed interface PayRequest {

fun comment(): String?

fun invoiceUUID(): String?

fun toQueryParamMap(): Map<String, String>

companion object {
Expand All @@ -72,6 +74,7 @@ sealed interface PayRequest {
)
}
val comment = queryMap["comment"]?.firstOrNull()
val invoiceUUID = queryMap["invoiceUUID"]?.firstOrNull()
return PayRequestV1(
sendingCurrencyCode,
receivingCurrencyCode,
Expand Down Expand Up @@ -100,6 +103,11 @@ internal data class PayRequestV1(
* the comment must be less than or equal to the value of `commentAllowed`.
*/
val comment: String? = null,
/**
* InvoiceUUID is the invoice UUID that the sender is paying.
* This only exists in the v1 pay request since the v0 SDK won't support invoices.
*/
val invoiceUUID: String? = null,
) : PayRequest {
override fun receivingCurrencyCode() = receivingCurrencyCode

Expand All @@ -122,6 +130,8 @@ internal data class PayRequestV1(

override fun comment(): String? = comment

override fun invoiceUUID(): String? = invoiceUUID

override fun toQueryParamMap(): Map<String, String> {
val amountStr =
if (sendingCurrencyCode != null) {
Expand All @@ -139,6 +149,7 @@ internal data class PayRequestV1(
map["payeeData"] = serialFormat.encodeToString(it)
}
comment?.let { map["comment"] = it }
invoiceUUID?.let { map["invoiceUUID"] = it }
return map
}
}
Expand All @@ -163,6 +174,8 @@ internal data class PayRequestV0(

override fun comment(): String? = null

override fun invoiceUUID(): String? = null

override fun signablePayload() = payerData.compliance()?.let {
"${payerData.identifier()}|${it.signatureNonce}|${it.signatureTimestamp}".encodeToByteArray()
} ?: payerData.identifier()?.encodeToByteArray()
Expand All @@ -186,6 +199,7 @@ internal object PayRequestV1Serializer : KSerializer<PayRequestV1> {
element<PayerData>("payerData")
element<CounterPartyDataOptions?>("payeeData", isOptional = true)
element<String?>("comment", isOptional = true)
element<String?>("invoiceUUID", isOptional = true)
}

override fun serialize(encoder: Encoder, value: PayRequestV1) {
Expand All @@ -208,6 +222,7 @@ internal object PayRequestV1Serializer : KSerializer<PayRequestV1> {
value.requestedPayeeData,
)
value.comment?.let { encodeStringElement(descriptor, 4, it) }
value.invoiceUUID?.let { encodeStringElement(descriptor, 5, it) }
}
}

Expand All @@ -218,6 +233,7 @@ internal object PayRequestV1Serializer : KSerializer<PayRequestV1> {
var payerData: PayerData? = null
var requestedPayeeData: CounterPartyDataOptions? = null
var comment: String? = null
var invoiceUUID: String? = null

return decoder.decodeStructure(descriptor) {
while (true) {
Expand Down Expand Up @@ -246,6 +262,9 @@ internal object PayRequestV1Serializer : KSerializer<PayRequestV1> {
)

4 -> comment = decodeNullableSerializableElement(descriptor, index, String.serializer().nullable)
5 ->
invoiceUUID =
decodeNullableSerializableElement(descriptor, index, String.serializer().nullable)
}
}

Expand All @@ -267,6 +286,7 @@ internal object PayRequestV1Serializer : KSerializer<PayRequestV1> {
payerData,
requestedPayeeData,
comment,
invoiceUUID
)
}
}
Expand Down
Loading