Skip to content

Commit 040fc3f

Browse files
authored
tas passkey support (#189)
* tas passkey support * add test for example class * fix sonar warnings * resolve sonar warning: duplicate literal * Log authentication wire values
1 parent 3322c79 commit 040fc3f

14 files changed

Lines changed: 588 additions & 79 deletions

File tree

src/main/java/co/omise/Example.java

Lines changed: 138 additions & 76 deletions
Large diffs are not rendered by default.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package co.omise.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
public enum AuthenticationType {
6+
@JsonProperty("3DS")
7+
THREE_DS("3DS"),
8+
@JsonProperty("PASSKEY")
9+
PASSKEY("PASSKEY");
10+
11+
private final String wireValue;
12+
13+
AuthenticationType(String wireValue) {
14+
this.wireValue = wireValue;
15+
}
16+
17+
public String getWireValue() {
18+
return wireValue;
19+
}
20+
}

src/main/java/co/omise/models/Charge.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public class Charge extends Model {
2424
@JsonProperty("authorize_uri")
2525
private String authorizeUri;
2626
private boolean authorized;
27+
@JsonProperty("authentication")
28+
private AuthenticationType authentication;
29+
@JsonProperty("authenticated_by")
30+
private String authenticatedBy;
2731
private String branch;
2832
private boolean capturable;
2933
private boolean capture;
@@ -122,6 +126,22 @@ public void setAuthorized(boolean authorized) {
122126
this.authorized = authorized;
123127
}
124128

129+
public AuthenticationType getAuthentication() {
130+
return authentication;
131+
}
132+
133+
public void setAuthentication(AuthenticationType authentication) {
134+
this.authentication = authentication;
135+
}
136+
137+
public String getAuthenticatedBy() {
138+
return authenticatedBy;
139+
}
140+
141+
public void setAuthenticatedBy(String authenticatedBy) {
142+
this.authenticatedBy = authenticatedBy;
143+
}
144+
125145
public String getBranch() {
126146
return this.branch;
127147
}
@@ -579,6 +599,8 @@ public static class CreateRequestBuilder extends RequestBuilder<Charge> {
579599
private String returnUri;
580600
@JsonProperty
581601
private String source;
602+
@JsonProperty("authentication")
603+
private AuthenticationType authentication;
582604
@JsonProperty("authorization_type")
583605
private AuthorizationType authorizationType;
584606
@JsonProperty("webhook_endpoints")
@@ -677,6 +699,11 @@ public CreateRequestBuilder zeroInterestInstallments(boolean zeroInterestInstall
677699
return this;
678700
}
679701

702+
public CreateRequestBuilder authentication(AuthenticationType authentication) {
703+
this.authentication = authentication;
704+
return this;
705+
}
706+
680707
public CreateRequestBuilder authorizationType(AuthorizationType authorizationType) {
681708
this.authorizationType = authorizationType;
682709
return this;
@@ -971,4 +998,4 @@ protected ResponseType<Charge> type() {
971998
return new ResponseType<>(Charge.class);
972999
}
9731000
}
974-
}
1001+
}
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
package co.omise;
2+
3+
import co.omise.models.*;
4+
import co.omise.requests.Request;
5+
import co.omise.requests.Requester;
6+
import co.omise.requests.ResponseType;
7+
import co.omise.models.schedules.Schedule;
8+
import co.omise.models.schedules.Occurrence;
9+
import okhttp3.OkHttpClient;
10+
import org.junit.Test;
11+
12+
import java.io.ByteArrayOutputStream;
13+
import java.io.PrintStream;
14+
import java.lang.reflect.Method;
15+
import java.lang.reflect.ParameterizedType;
16+
import java.lang.reflect.Type;
17+
import java.util.Collections;
18+
19+
import static org.junit.Assert.assertNotNull;
20+
21+
public class ExampleTest {
22+
23+
@Test
24+
public void coversAllExampleScenarios() throws Exception {
25+
Client stubClient = new Client(new MockRequester());
26+
Example example = new Example(stubClient);
27+
28+
PrintStream originalOut = System.out;
29+
ByteArrayOutputStream sink = new ByteArrayOutputStream();
30+
System.setOut(new PrintStream(sink));
31+
try {
32+
example.retrieveAccount();
33+
example.retrieveBalance();
34+
example.destroyCard();
35+
example.listCards();
36+
example.retrieveCard();
37+
example.updateCard();
38+
example.captureCharge();
39+
example.partialCaptureCharge();
40+
example.chargeWithCard();
41+
example.chargeWithAuthentication(AuthenticationType.THREE_DS);
42+
example.chargeWithCustomer();
43+
example.chargeWithToken();
44+
example.createPartialCaptureCharge();
45+
example.listCharges();
46+
example.retrieveCharge();
47+
example.reverseCharge();
48+
example.updateCharge();
49+
example.attachCardToCustomer();
50+
example.createCustomerSimple();
51+
example.updateCustomer();
52+
example.destroyCustomer();
53+
example.listAllDisputes();
54+
example.listClosedDiputes();
55+
example.listOpenDiputes();
56+
example.listPendingDiputes();
57+
example.retrieveDispute();
58+
example.updateDispute();
59+
example.listEvents();
60+
example.retrieveEvent();
61+
example.retrieveCustomer();
62+
example.listCustomers();
63+
example.createTransfer();
64+
example.createTransferWithRecipient();
65+
example.destroyTransfer();
66+
example.listTransfers();
67+
example.retrieveTransfer();
68+
example.updateTransfer();
69+
example.createRecipient();
70+
example.destroyRecipient();
71+
example.listRecipients();
72+
example.retrieveRecipient();
73+
example.updateRecipient();
74+
example.createRefund();
75+
example.listRefunds();
76+
example.retrieveRefund();
77+
example.createToken();
78+
example.retrieveToken();
79+
example.listTransactions();
80+
example.retrieveTransaction();
81+
example.createLink();
82+
example.retrieveLink();
83+
example.listLinks();
84+
example.createSource();
85+
example.createSourceInstallment();
86+
example.retrieveSearch();
87+
example.retrieveSchedule();
88+
example.listSchedule();
89+
example.listChargeSchedule();
90+
example.listCustomerSchedule();
91+
example.listTransferSchedule();
92+
example.listRecipientSchedule();
93+
example.createSchedule();
94+
example.destroySchedule();
95+
example.retrieveOccurrence();
96+
example.listOccurrence();
97+
example.retrieveReceipt();
98+
example.listReceipt();
99+
example.getForex();
100+
example.getCapapabilities();
101+
} finally {
102+
System.setOut(originalOut);
103+
}
104+
105+
Example realExample = new Example();
106+
Method clientMethod = Example.class.getDeclaredMethod("client");
107+
clientMethod.setAccessible(true);
108+
Client actualClient = (Client) clientMethod.invoke(realExample);
109+
assertNotNull(actualClient);
110+
}
111+
112+
private static final class MockRequester implements Requester {
113+
private final OkHttpClient httpClient = new OkHttpClient();
114+
115+
@Override
116+
public <T extends OmiseObjectBase, R extends Request<T>> T sendRequest(R request) {
117+
ResponseType<T> responseType = request.getType();
118+
119+
if (responseType.isClassType()) {
120+
return responseType.getClassType().cast(createStubForClass(responseType.getClassType()));
121+
}
122+
123+
if (responseType.isTypeReference()) {
124+
return castFromType(responseType.getTypeReference().getType());
125+
}
126+
127+
throw new UnsupportedOperationException("Unhandled response type: " + responseType);
128+
}
129+
130+
@SuppressWarnings("unchecked")
131+
private <T extends OmiseObjectBase> T castFromType(Type type) {
132+
Object stub = createStubForType(type);
133+
return (T) stub;
134+
}
135+
136+
private OmiseObjectBase createStubForClass(Class<?> clazz) {
137+
if (clazz.equals(Account.class)) {
138+
return withId(new Account(), "acct_test");
139+
}
140+
if (clazz.equals(Balance.class)) {
141+
Balance balance = withId(new Balance(), "bal_test");
142+
balance.setTransferable(12345L);
143+
return balance;
144+
}
145+
if (clazz.equals(Card.class)) {
146+
Card card = withId(new Card(), "card_test");
147+
card.setLastDigits("4242");
148+
return card;
149+
}
150+
if (clazz.equals(Charge.class)) {
151+
Charge charge = withId(new Charge(), "chrg_test");
152+
charge.setAmount(1000L);
153+
charge.setDescription("desc");
154+
charge.setReversed(true);
155+
charge.setAuthorizeUri("https://example.com/auth");
156+
return charge;
157+
}
158+
if (clazz.equals(Token.class)) {
159+
Token token = withId(new Token(), "tokn_test");
160+
Card card = withId(new Card(), "card_token");
161+
card.setLastDigits("1111");
162+
token.setCard(card);
163+
return token;
164+
}
165+
if (clazz.equals(Customer.class)) {
166+
Customer customer = withId(new Customer(), "cust_test");
167+
customer.setEmail("user@example.com");
168+
return customer;
169+
}
170+
if (clazz.equals(Dispute.class)) {
171+
Dispute dispute = withId(new Dispute(), "dspt_test");
172+
dispute.setAmount(5000L);
173+
dispute.setMessage("message");
174+
return dispute;
175+
}
176+
if (clazz.equals(Event.class)) {
177+
Event event = withId(new Event(), "evnt_test");
178+
event.setKey("charge.completed");
179+
return event;
180+
}
181+
if (clazz.equals(Transfer.class)) {
182+
Transfer transfer = withId(new Transfer(), "trsf_test");
183+
transfer.setAmount(2000L);
184+
return transfer;
185+
}
186+
if (clazz.equals(Recipient.class)) {
187+
Recipient recipient = withId(new Recipient(), "recp_test");
188+
recipient.setEmail("recipient@example.com");
189+
return recipient;
190+
}
191+
if (clazz.equals(Refund.class)) {
192+
Refund refund = withId(new Refund(), "rfnd_test");
193+
refund.setAmount(3000L);
194+
return refund;
195+
}
196+
if (clazz.equals(Transaction.class)) {
197+
Transaction transaction = withId(new Transaction(), "trxn_test");
198+
transaction.setAmount(4000L);
199+
return transaction;
200+
}
201+
if (clazz.equals(Link.class)) {
202+
return withId(new Link(), "link_test");
203+
}
204+
if (clazz.equals(Source.class)) {
205+
return withId(new Source(), "src_test");
206+
}
207+
if (clazz.equals(Schedule.class)) {
208+
return withId(new Schedule(), "schd_test");
209+
}
210+
if (clazz.equals(Occurrence.class)) {
211+
return withId(new Occurrence(), "occu_test");
212+
}
213+
if (clazz.equals(Receipt.class)) {
214+
return withId(new Receipt(), "rcpt_test");
215+
}
216+
if (clazz.equals(Forex.class)) {
217+
Forex forex = withId(new Forex(), "forx_test");
218+
forex.setRate(1.23d);
219+
return forex;
220+
}
221+
if (clazz.equals(Capability.class)) {
222+
Capability capability = withId(new Capability(), "cap_test");
223+
capability.setZeroInterestInstallments(true);
224+
return capability;
225+
}
226+
227+
throw new UnsupportedOperationException("Unhandled class: " + clazz.getName());
228+
}
229+
230+
private OmiseObjectBase createStubForType(Type type) {
231+
if (type instanceof ParameterizedType) {
232+
ParameterizedType parameterizedType = (ParameterizedType) type;
233+
Type rawType = parameterizedType.getRawType();
234+
235+
if (rawType == ScopedList.class) {
236+
Type elementType = parameterizedType.getActualTypeArguments()[0];
237+
Class<?> element = elementType instanceof Class
238+
? (Class<?>) elementType
239+
: Charge.class;
240+
ScopedList<Model> scopedList = new ScopedList<>();
241+
scopedList.setTotal(1);
242+
scopedList.setData(Collections.singletonList((Model) createStubForClass(element)));
243+
return scopedList;
244+
}
245+
246+
if (rawType == SearchResult.class) {
247+
SearchResult<Model> searchResult = new SearchResult<>();
248+
searchResult.setTotal(1);
249+
searchResult.setData(Collections.singletonList((Model) createStubForClass(Charge.class)));
250+
return searchResult;
251+
}
252+
}
253+
254+
throw new UnsupportedOperationException("Unhandled type: " + type.getTypeName());
255+
}
256+
257+
private <T extends Model> T withId(T model, String id) {
258+
model.setId(id);
259+
return model;
260+
}
261+
262+
@Override
263+
public OkHttpClient getHttpClient() {
264+
return httpClient;
265+
}
266+
}
267+
}

0 commit comments

Comments
 (0)