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

serialize payment account payload to comma delimited string #1553

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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(e -> e.code) // 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
Loading