|
24 | 24 |
|
25 | 25 | package com.trustly.api.data.request.requestdata; |
26 | 26 |
|
| 27 | +import java.util.Map; |
| 28 | +import java.util.TreeMap; |
| 29 | +import java.util.stream.Collectors; |
| 30 | + |
27 | 31 | import com.google.gson.annotations.SerializedName; |
28 | 32 |
|
29 | 33 | public class RecipientInformation { |
@@ -64,27 +68,37 @@ public RecipientInformation setDateOfBirth(final String dateOfBirth) { |
64 | 68 | return this; |
65 | 69 | } |
66 | 70 |
|
| 71 | + /** |
| 72 | + * Method for returning a String representation of the |
| 73 | + * RecipientInformation object. Used for serialization. |
| 74 | + * <p> |
| 75 | + * Uses a TreeMap for automatically sorting the object's |
| 76 | + * field values in an alphabetical order, which is required |
| 77 | + * for generating a valid signature from the serialized string. |
| 78 | + */ |
67 | 79 | @Override |
68 | 80 | public String toString() { |
69 | | - final StringBuilder stringBuilder = new StringBuilder(); |
| 81 | + final Map<String, String> attributes = new TreeMap<>(); |
70 | 82 |
|
71 | | - stringBuilder.append("Partytype").append(partyType); |
72 | | - stringBuilder.append("Firstname").append(firstName); |
73 | | - stringBuilder.append("Lastname").append(lastName); |
74 | | - stringBuilder.append("CountryCode").append(countryCode); |
| 83 | + attributes.put("Partytype", partyType); |
| 84 | + attributes.put("Firstname", firstName); |
| 85 | + attributes.put("Lastname", lastName); |
| 86 | + attributes.put("CountryCode", countryCode); |
75 | 87 |
|
76 | 88 | if (customerId != null) { |
77 | | - stringBuilder.append("CustomerID").append(customerId); |
| 89 | + attributes.put("CustomerID", customerId); |
78 | 90 | } |
79 | 91 |
|
80 | 92 | if (address != null) { |
81 | | - stringBuilder.append("Address").append(address); |
| 93 | + attributes.put("Address", address); |
82 | 94 | } |
83 | 95 |
|
84 | 96 | if (dateOfBirth != null) { |
85 | | - stringBuilder.append("DateOfBirth").append(dateOfBirth); |
| 97 | + attributes.put("DateOfBirth", dateOfBirth); |
86 | 98 | } |
87 | 99 |
|
88 | | - return stringBuilder.toString(); |
| 100 | + return attributes.entrySet().stream() |
| 101 | + .map(entry -> entry.getKey() + entry.getValue()) |
| 102 | + .collect(Collectors.joining()); |
89 | 103 | } |
90 | 104 | } |
0 commit comments