Skip to content

Commit d669bb9

Browse files
committed
Bindings for new metadata API
1 parent ab0f60d commit d669bb9

File tree

6 files changed

+151
-4
lines changed

6 files changed

+151
-4
lines changed

src/main/java/com/stripe/model/Charge.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.stripe.exception.InvalidRequestException;
1111
import com.stripe.net.APIResource;
1212

13-
public class Charge extends APIResource {
13+
public class Charge extends APIResource implements MetadataStore<Charge> {
1414
Integer amount;
1515
Long created;
1616
String currency;
@@ -30,6 +30,7 @@ public class Charge extends APIResource {
3030
Card card;
3131
Dispute dispute;
3232
String balanceTransaction;
33+
Map<String, String> metadata;
3334

3435
public String getId() {
3536
return id;
@@ -183,6 +184,14 @@ public void setBalanceTransaction(String balanceTransaction) {
183184
this.balanceTransaction = balanceTransaction;
184185
}
185186

187+
public Map<String, String> getMetadata() {
188+
return metadata;
189+
}
190+
191+
public void setMetadata(Map<String, String> metadata) {
192+
this.metadata = metadata;
193+
}
194+
186195
public static Charge create(Map<String, Object> params)
187196
throws AuthenticationException, InvalidRequestException,
188197
APIConnectionException, CardException, APIException {
@@ -195,6 +204,12 @@ public static Charge retrieve(String id) throws AuthenticationException,
195204
return retrieve(id, null);
196205
}
197206

207+
public Charge update(Map<String, Object> params)
208+
throws AuthenticationException, InvalidRequestException,
209+
APIConnectionException, CardException, APIException {
210+
return update(params, null);
211+
}
212+
198213
public static ChargeCollection all(Map<String, Object> params)
199214
throws AuthenticationException, InvalidRequestException,
200215
APIConnectionException, CardException, APIException {
@@ -251,6 +266,13 @@ public static Charge retrieve(String id, String apiKey)
251266
Charge.class, apiKey);
252267
}
253268

269+
public Charge update(Map<String, Object> params, String apiKey)
270+
throws AuthenticationException, InvalidRequestException,
271+
APIConnectionException, CardException, APIException {
272+
return request(RequestMethod.POST, instanceURL(Charge.class, id), params,
273+
Charge.class, apiKey);
274+
}
275+
254276
public static ChargeCollection all(Map<String, Object> params, String apiKey)
255277
throws AuthenticationException, InvalidRequestException,
256278
APIConnectionException, CardException, APIException {

src/main/java/com/stripe/model/Customer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.stripe.exception.InvalidRequestException;
1111
import com.stripe.net.APIResource;
1212

13-
public class Customer extends APIResource {
13+
public class Customer extends APIResource implements MetadataStore<Customer> {
1414
Long created;
1515
String id;
1616
Boolean livemode;
@@ -26,6 +26,7 @@ public class Customer extends APIResource {
2626
Boolean delinquent;
2727
Integer accountBalance;
2828
CustomerCardCollection cards;
29+
Map<String, String> metadata;
2930

3031
public Long getCreated() {
3132
return created;
@@ -139,6 +140,14 @@ public void setAccountBalance(Integer accountBalance) {
139140
this.accountBalance = accountBalance;
140141
}
141142

143+
public Map<String, String> getMetadata() {
144+
return metadata;
145+
}
146+
147+
public void setMetadata(Map<String, String> metadata) {
148+
this.metadata = metadata;
149+
}
150+
142151
public static Customer create(Map<String, Object> params)
143152
throws AuthenticationException, InvalidRequestException,
144153
APIConnectionException, CardException, APIException {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.stripe.model;
2+
3+
import java.util.Map;
4+
5+
import com.stripe.exception.APIConnectionException;
6+
import com.stripe.exception.APIException;
7+
import com.stripe.exception.AuthenticationException;
8+
import com.stripe.exception.CardException;
9+
import com.stripe.exception.InvalidRequestException;
10+
11+
/**
12+
* Common interface for Stripe objects that can store metadata.
13+
*/
14+
public interface MetadataStore<T> {
15+
Map<String, String> getMetadata();
16+
void setMetadata(Map<String, String> metadata);
17+
18+
MetadataStore<T> update(Map<String, Object> params) throws AuthenticationException, InvalidRequestException,
19+
APIConnectionException, CardException, APIException;
20+
MetadataStore<T> update(Map<String, Object> params, String apiKey) throws AuthenticationException, InvalidRequestException,
21+
APIConnectionException, CardException, APIException;
22+
}

src/main/java/com/stripe/model/Recipient.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import com.stripe.exception.InvalidRequestException;
1010
import com.stripe.net.APIResource;
1111

12-
public class Recipient extends APIResource {
12+
public class Recipient extends APIResource implements MetadataStore<Recipient> {
1313
Long created;
1414
String id;
1515
String type;
@@ -20,6 +20,7 @@ public class Recipient extends APIResource {
2020
BankAccount activeAccount;
2121
String email;
2222
Boolean verified;
23+
Map<String, String> metadata;
2324

2425
public Long getCreated() {
2526
return created;
@@ -97,6 +98,14 @@ public Boolean getDeleted() {
9798
return deleted;
9899
}
99100

101+
public Map<String, String> getMetadata() {
102+
return metadata;
103+
}
104+
105+
public void setMetadata(Map<String, String> metadata) {
106+
this.metadata = metadata;
107+
}
108+
100109
public static Recipient create(Map<String, Object> params)
101110
throws AuthenticationException, InvalidRequestException,
102111
APIConnectionException, CardException, APIException {

src/main/java/com/stripe/model/Transfer.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.stripe.exception.InvalidRequestException;
1111
import com.stripe.net.APIResource;
1212

13-
public class Transfer extends APIResource {
13+
public class Transfer extends APIResource implements MetadataStore<Transfer> {
1414
String id;
1515
String status;
1616
Long date;
@@ -24,6 +24,7 @@ public class Transfer extends APIResource {
2424
String recipient;
2525
BankAccount account;
2626
String balanceTransaction;
27+
Map<String, String> metadata;
2728

2829
public String getId() {
2930
return id;
@@ -129,6 +130,14 @@ public void setBalanceTransaction(String balanceTransaction) {
129130
this.balanceTransaction = balanceTransaction;
130131
}
131132

133+
public Map<String, String> getMetadata() {
134+
return metadata;
135+
}
136+
137+
public void setMetadata(Map<String, String> metadata) {
138+
this.metadata = metadata;
139+
}
140+
132141
public static Transfer create(Map<String, Object> params)
133142
throws AuthenticationException, InvalidRequestException,
134143
APIConnectionException, CardException, APIException {
@@ -141,6 +150,12 @@ public static Transfer retrieve(String id) throws AuthenticationException,
141150
return retrieve(id, null);
142151
}
143152

153+
public Transfer update(Map<String, Object> params)
154+
throws AuthenticationException, InvalidRequestException,
155+
APIConnectionException, CardException, APIException {
156+
return update(params, null);
157+
}
158+
144159
public static TransferCollection all(Map<String, Object> params)
145160
throws AuthenticationException, InvalidRequestException,
146161
APIConnectionException, CardException, APIException {
@@ -160,6 +175,14 @@ public static Transfer create(Map<String, Object> params, String apiKey)
160175
Transfer.class, apiKey);
161176
}
162177

178+
public Transfer update(Map<String, Object> params, String apiKey)
179+
throws AuthenticationException, InvalidRequestException,
180+
APIConnectionException, CardException, APIException {
181+
return request(RequestMethod.POST,
182+
instanceURL(Transfer.class, this.id), params, Transfer.class,
183+
apiKey);
184+
}
185+
163186
public static Transfer retrieve(String id, String apiKey)
164187
throws AuthenticationException, InvalidRequestException,
165188
APIConnectionException, CardException, APIException {

src/test/java/com/stripe/StripeTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.stripe.model.Invoice;
3737
import com.stripe.model.InvoiceItem;
3838
import com.stripe.model.InvoiceLineItemCollection;
39+
import com.stripe.model.MetadataStore;
3940
import com.stripe.model.Plan;
4041
import com.stripe.model.Subscription;
4142
import com.stripe.model.Token;
@@ -1115,4 +1116,65 @@ public void testEventListPerCallAPIKey() throws StripeException {
11151116
assertEquals(events.size(), 1);
11161117
}
11171118

1119+
private void testMetadata(MetadataStore<?> object) throws StripeException {
1120+
assertTrue(object.getMetadata().isEmpty());
1121+
1122+
Map<String, String> initialMetadata = new HashMap<String, String>();
1123+
initialMetadata.put("foo", "bar");
1124+
initialMetadata.put("baz", "qux");
1125+
Map<String, Object> updateParams = new HashMap<String, Object>();
1126+
updateParams.put("metadata", initialMetadata);
1127+
object = object.update(updateParams);
1128+
assertFalse(object.getMetadata().isEmpty());
1129+
assertEquals("bar", object.getMetadata().get("foo"));
1130+
assertEquals("qux", object.getMetadata().get("baz"));
1131+
1132+
// Test update single key
1133+
Map<String, String> metadataUpdate = new HashMap<String, String>();
1134+
metadataUpdate.put("foo", "new-bar");
1135+
updateParams = new HashMap<String, Object>();
1136+
updateParams.put("metadata", metadataUpdate);
1137+
object = object.update(updateParams);
1138+
assertFalse(object.getMetadata().isEmpty());
1139+
assertEquals("new-bar", object.getMetadata().get("foo"));
1140+
assertEquals("qux", object.getMetadata().get("baz"));
1141+
1142+
// Test delete single key
1143+
metadataUpdate = new HashMap<String, String>();
1144+
metadataUpdate.put("foo", null);
1145+
updateParams = new HashMap<String, Object>();
1146+
updateParams.put("metadata", metadataUpdate);
1147+
object = object.update(updateParams);
1148+
assertFalse(object.getMetadata().isEmpty());
1149+
assertFalse(object.getMetadata().containsKey("foo"));
1150+
assertEquals("qux", object.getMetadata().get("baz"));
1151+
1152+
// Test delete all keys
1153+
updateParams = new HashMap<String, Object>();
1154+
updateParams.put("metadata", null);
1155+
object = object.update(updateParams);
1156+
assertTrue(object.getMetadata().isEmpty());
1157+
}
1158+
1159+
@Test
1160+
public void testChargeMetadata() throws StripeException {
1161+
testMetadata(Charge.create(defaultChargeParams));
1162+
}
1163+
1164+
@Test
1165+
public void testCustomerMetadata() throws StripeException {
1166+
testMetadata(Customer.create(defaultCustomerParams));
1167+
}
1168+
1169+
@Test
1170+
public void testTransferMetadata() throws StripeException {
1171+
Recipient recipient = Recipient.create(defaultRecipientParams);
1172+
defaultTransferParams.put("recipient", recipient.getId());
1173+
testMetadata(Transfer.create(defaultTransferParams));
1174+
}
1175+
1176+
@Test
1177+
public void testRecipientMetadata() throws StripeException {
1178+
testMetadata(Recipient.create(defaultRecipientParams));
1179+
}
11181180
}

0 commit comments

Comments
 (0)