Skip to content

Commit

Permalink
serialize missing payment account related input fields to comma delim…
Browse files Browse the repository at this point in the history
…ited string
  • Loading branch information
abhishek818 committed Jan 19, 2025
1 parent 130a45c commit 1af5cef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
7 changes: 1 addition & 6 deletions core/src/main/java/haveno/core/payment/PaymentAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,7 @@ public PaymentAccountForm toForm() {
PaymentAccountForm form = new PaymentAccountForm(PaymentAccountForm.FormId.valueOf(paymentMethod.getId()));
for (PaymentAccountFormField.FieldId fieldId : getInputFieldIds()) {
PaymentAccountFormField field = getEmptyFormField(fieldId);
Object value = jsonMap.get(HavenoUtils.toCamelCase(field.getId().toString()));
if (value instanceof List) { // TODO: list should already be serialized to comma delimited string in PaymentAccount.toJson() (PaymentAccountTypeAdapter?)
field.setValue(String.join(",", (List<String>) value));
} else {
field.setValue((String) value);
}
field.setValue((String) jsonMap.get(HavenoUtils.toCamelCase(field.getId().toString())));
form.getFields().add(field);
}
return form;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ private void writeComments(JsonWriter out, PaymentAccount account) throws IOExce


private void writeInnerMutableFields(JsonWriter out, PaymentAccount account) {
if (account instanceof CountryBasedPaymentAccount) {
writeAcceptedCountryCodesField(out, account);
}

if (account.hasMultipleCurrencies()) {
writeTradeCurrenciesField(out, account);
writeSelectedTradeCurrencyField(out, account);
Expand Down Expand Up @@ -176,7 +180,16 @@ private void writeTradeCurrenciesField(JsonWriter out, PaymentAccount account) {
String fieldName = "tradeCurrencies";
log.debug("Append form with non-settable field: {}", fieldName);
out.name(fieldName);
out.value("comma delimited currency code list, e.g., gbp,eur,jpy,usd");
List<TradeCurrency> tradeCurrencies = account.getTradeCurrencies();
if (tradeCurrencies != null && !tradeCurrencies.isEmpty()) {
String tradeCurrenciesValue = tradeCurrencies.stream()
.map(TradeCurrency::getCode) // convert each currency to its code.
.reduce((c1, c2) -> c1 + "," + c2) // create a comma-delimited string.
.orElse("");
out.value(tradeCurrenciesValue);
} else {
out.value(""); // if no currencies exist, write an empty string.
}
} catch (Exception ex) {
String errMsg = format("cannot create a new %s json form",
account.getClass().getSimpleName());
Expand All @@ -202,6 +215,34 @@ private void writeSelectedTradeCurrencyField(JsonWriter out, PaymentAccount acco
}
}

private void writeAcceptedCountryCodesField(JsonWriter out, PaymentAccount account) {
try {
String fieldName = "acceptedCountryCodes";
log.debug("Append form with non-settable field: {}", fieldName);
out.name(fieldName);

if (account instanceof CountryBasedPaymentAccount) {
List<Country> acceptedCountries = ((CountryBasedPaymentAccount) account).getAcceptedCountries();
if (acceptedCountries != null && !acceptedCountries.isEmpty()) {
String countryCodesValue = acceptedCountries.stream()
.map(Country::getCode) // convert each country to its code.
.reduce((c1, c2) -> c1 + "," + c2) // create a comma-delimited string.
.orElse("");
out.value(countryCodesValue);
} else {
out.value(""); // if no countries exist, write an empty string.
}
} else {
out.value(""); // default empty value for non-country-based accounts.
}
} catch (Exception ex) {
String errMsg = format("cannot create a new %s json form",
account.getClass().getSimpleName());
log.error(capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg);
}
}

@Override
public PaymentAccount read(JsonReader in) throws IOException {
PaymentAccount account = initNewPaymentAccount();
Expand Down

0 comments on commit 1af5cef

Please sign in to comment.