Skip to content

Commit 6733ab0

Browse files
vshcherbyna-epamfmattos-hw
authored andcommitted
HW-51321: Get paypal account (#3)
* HW-51321: Add support to retrieve PayPal accounts
1 parent 296bb3b commit 6733ab0

File tree

2 files changed

+189
-15
lines changed

2 files changed

+189
-15
lines changed

core/src/main/java/com/hyperwallet/android/Hyperwallet.java

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static Hyperwallet getDefault() {
121121
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
122122
* processing the request.</p>
123123
*
124-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
124+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
125125
* if the current one is expired or about to expire.</p>
126126
*
127127
* @param bankAccount the {@code HyperwalletBankAccount} to be created; must not be null
@@ -161,7 +161,7 @@ public void createBankAccount(@NonNull final HyperwalletBankAccount bankAccount,
161161
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
162162
* * processing the request.</p>
163163
*
164-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
164+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
165165
* if the current one is expired or about to expire.</p>
166166
*
167167
* @param bankAccountPagination the ordering and filtering criteria
@@ -186,7 +186,7 @@ public void listBankAccounts(@Nullable final HyperwalletBankAccountPagination ba
186186
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
187187
* * processing the request.</p>
188188
*
189-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
189+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
190190
* if the current one is expired or about to expire.</p>
191191
*
192192
* @param bankCard the {@code HyperwalletBankCard} to be created; must not be null
@@ -233,14 +233,14 @@ public void createPayPalAccount(@NonNull final PayPalAccount payPalAccount,
233233
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
234234
* processing the request.</p>
235235
*
236-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
236+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
237237
* if the current one is expired or about to expire.</p>
238238
*
239239
* @param transferMethodToken the Hyperwallet specific unique identifier for the {@code HyperwalletBankAccount}
240240
* being requested; must not be null
241241
* @param listener the callback handler of responses from the Hyperwallet platform; must not be null
242242
*/
243-
public void getBankAccount(@NonNull String transferMethodToken,
243+
public void getBankAccount(@NonNull final String transferMethodToken,
244244
@NonNull final HyperwalletListener<HyperwalletBankAccount> listener) {
245245
PathFormatter pathFormatter = new PathFormatter("users/{0}/bank-accounts/{1}", transferMethodToken);
246246

@@ -257,7 +257,7 @@ public void getBankAccount(@NonNull String transferMethodToken,
257257
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
258258
* processing the request.</p>
259259
*
260-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
260+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
261261
* if the current one is expired or about to expire.</p>
262262
*
263263
* @param transferMethodToken the Hyperwallet specific unique identifier for the {@code HyperwalletBankCard}
@@ -284,7 +284,7 @@ public void getBankCard(@NonNull final String transferMethodToken,
284284
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
285285
* processing the request.</p>
286286
*
287-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
287+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
288288
* if the current one is expired or about to expire.</p>
289289
*
290290
* @param bankAccount the {@code HyperwalletBankAccount} to be created; must not be null
@@ -313,7 +313,7 @@ public void updateBankAccount(@NonNull final HyperwalletBankAccount bankAccount,
313313
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
314314
* processing the request.</p>
315315
*
316-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
316+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
317317
* if the current one is expired or about to expire.</p>
318318
*
319319
* @param bankCard the {@code HyperwalletBankCard} to be created; must not be null
@@ -368,7 +368,7 @@ public void updatePayPalAccount(@NonNull final PayPalAccount payPalAccount,
368368
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
369369
* processing the request.</p>
370370
*
371-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
371+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
372372
* if the current one is expired or about to expire.</p>
373373
*
374374
* @param transferMethodToken the Hyperwallet specific unique identifier for the {@code HyperwalletBankAccount}
@@ -400,7 +400,7 @@ public void deactivateBankAccount(@NonNull final String transferMethodToken, @Nu
400400
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
401401
* processing the request.</p>
402402
*
403-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
403+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
404404
* if the current one is expired or about to expire.</p>
405405
*
406406
* @param transferMethodToken the Hyperwallet specific unique identifier for the {@code HyperwalletBankCard} being
@@ -446,7 +446,7 @@ public void deactivateBankCard(@NonNull final String transferMethodToken, @Nulla
446446
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
447447
* processing the request.</p>
448448
*
449-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
449+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
450450
* if the current one is expired or about to expire.</p>
451451
*
452452
* @param transferMethodPagination the ordering and filtering criteria
@@ -486,7 +486,7 @@ public void listTransferMethods(@Nullable final HyperwalletTransferMethodPaginat
486486
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
487487
* processing the request.</p>
488488
*
489-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
489+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
490490
* if the current one is expired or about to expire.</p>
491491
*
492492
* @param bankCardPagination the ordering and filtering criteria
@@ -525,7 +525,7 @@ public void listBankCards(@Nullable final HyperwalletBankCardPagination bankCard
525525
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
526526
* processing the request.</p>
527527
*
528-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
528+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
529529
* if the current one is expired or about to expire.</p>
530530
*
531531
* @param bankCardPagination the ordering and filtering criteria
@@ -542,6 +542,30 @@ public void listPayPalAccounts(@Nullable final PayPalAccountPagination bankCardP
542542
performRestTransaction(builder, listener);
543543
}
544544

545+
/**
546+
* Returns the {@link PayPalAccount} linked to the transfer method token specified, or null if none exists.
547+
*
548+
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
549+
* processing the request.</p>
550+
*
551+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
552+
* if the current one is expired or about to expire.</p>
553+
*
554+
* @param transferMethodToken the Hyperwallet specific unique identifier for the {@code PayPalAccount}
555+
* being requested; must not be null
556+
* @param listener the callback handler of responses from the Hyperwallet platform; must not be null
557+
*/
558+
public void getPayPalAccount(@NonNull final String transferMethodToken,
559+
@NonNull final HyperwalletListener<PayPalAccount> listener) {
560+
PathFormatter pathFormatter = new PathFormatter("users/{0}/paypal-accounts/{1}", transferMethodToken);
561+
562+
RestTransaction.Builder builder = new RestTransaction.Builder<>(GET, pathFormatter,
563+
new TypeReference<PayPalAccount>() {
564+
}, listener);
565+
566+
performRestTransaction(builder, listener);
567+
}
568+
545569
/**
546570
* Returns the transfer method configuration key set, processing times, and fees for the User that is associated
547571
* with the authentication token returned from
@@ -550,7 +574,7 @@ public void listPayPalAccounts(@Nullable final PayPalAccountPagination bankCardP
550574
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
551575
* processing the request.</p>
552576
*
553-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
577+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
554578
* if the current one is expired or about to expire.</p>
555579
*
556580
* @param transferMethodConfigurationKeysQuery containing the transfer method configuration key query,
@@ -577,7 +601,7 @@ public void retrieveTransferMethodConfigurationKeys(
577601
* <p>The {@link HyperwalletListener} that is passed in to this method invocation will receive the responses from
578602
* processing the request.</p>
579603
*
580-
* <p>This function will requests a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
604+
* <p>This function will request a new authentication token via {@link HyperwalletAuthenticationTokenProvider}
581605
* if the current one is expired or about to expire.</p>
582606
*
583607
* @param transferMethodConfigurationFieldQuery containing a transfer method configuration key tuple of
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package com.hyperwallet.android.transfermethod;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.hamcrest.CoreMatchers.notNullValue;
5+
import static org.hamcrest.CoreMatchers.nullValue;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.Matchers.hasSize;
8+
import static org.mockito.ArgumentMatchers.any;
9+
import static org.mockito.Mockito.never;
10+
import static org.mockito.Mockito.verify;
11+
12+
import static com.hyperwallet.android.model.HyperwalletStatusTransition.StatusDefinition.ACTIVATED;
13+
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.CREATED_ON;
14+
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.EMAIL;
15+
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.STATUS;
16+
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.TOKEN;
17+
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.TRANSFER_METHOD_COUNTRY;
18+
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.TRANSFER_METHOD_CURRENCY;
19+
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodFields.TYPE;
20+
import static com.hyperwallet.android.model.HyperwalletTransferMethod.TransferMethodTypes.PAYPAL_ACCOUNT;
21+
22+
import com.hyperwallet.android.Hyperwallet;
23+
import com.hyperwallet.android.exception.HyperwalletException;
24+
import com.hyperwallet.android.listener.HyperwalletListener;
25+
import com.hyperwallet.android.model.HyperwalletError;
26+
import com.hyperwallet.android.model.HyperwalletErrors;
27+
import com.hyperwallet.android.model.PayPalAccount;
28+
import com.hyperwallet.android.rule.HyperwalletExternalResourceManager;
29+
import com.hyperwallet.android.rule.HyperwalletMockWebServer;
30+
import com.hyperwallet.android.rule.HyperwalletSdkMock;
31+
32+
import org.junit.Rule;
33+
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.mockito.ArgumentCaptor;
36+
import org.mockito.Captor;
37+
import org.mockito.Mock;
38+
import org.mockito.junit.MockitoJUnit;
39+
import org.mockito.junit.MockitoRule;
40+
import org.robolectric.RobolectricTestRunner;
41+
42+
import java.net.HttpURLConnection;
43+
import java.util.concurrent.CountDownLatch;
44+
import java.util.concurrent.TimeUnit;
45+
46+
import okhttp3.mockwebserver.RecordedRequest;
47+
48+
@RunWith(RobolectricTestRunner.class)
49+
public class HyperwalletGetPayPalTest {
50+
@Rule
51+
public HyperwalletMockWebServer mServer = new HyperwalletMockWebServer();
52+
@Rule
53+
public HyperwalletSdkMock mSdkMock = new HyperwalletSdkMock(mServer);
54+
@Rule
55+
public HyperwalletExternalResourceManager mExternalResourceManager = new HyperwalletExternalResourceManager();
56+
@Rule
57+
public MockitoRule mMockito = MockitoJUnit.rule();
58+
59+
@Mock
60+
private HyperwalletListener<PayPalAccount> mListener;
61+
@Captor
62+
private ArgumentCaptor<PayPalAccount> mPayPalAccountArgumentCaptor;
63+
@Captor
64+
private ArgumentCaptor<HyperwalletException> mExceptionCaptor;
65+
66+
private final CountDownLatch mAwait = new CountDownLatch(1);
67+
private static final long COUNTDOWN_TIMEOUT_MILLIS = 100L;
68+
69+
@Test
70+
public void testGetGetPayPalAccount_returnsAccount() throws InterruptedException {
71+
72+
String responseBody = mExternalResourceManager.getResourceContent("paypal_account_response.json");
73+
mServer.mockResponse().withHttpResponseCode(HttpURLConnection.HTTP_OK).withBody(responseBody).mock();
74+
75+
Hyperwallet.getDefault().getPayPalAccount("trm-ac5727ac-8fe7-42fb-b69d-977ebdd7b48b", mListener);
76+
mAwait.await(COUNTDOWN_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
77+
78+
RecordedRequest recordedRequest = mServer.getRequest();
79+
assertThat(recordedRequest.getPath(),
80+
is("/rest/v3/users/usr-fbfd5848-60d0-43c5-8462-099c959b49c7/paypal-accounts/trm-ac5727ac-8fe7-42fb"
81+
+ "-b69d-977ebdd7b48b"));
82+
83+
verify(mListener).onSuccess(mPayPalAccountArgumentCaptor.capture());
84+
verify(mListener, never()).onFailure(any(HyperwalletException.class));
85+
86+
PayPalAccount payPalAccountResponse = mPayPalAccountArgumentCaptor.getValue();
87+
assertThat(payPalAccountResponse, is(notNullValue()));
88+
assertThat(payPalAccountResponse.getField(TYPE), is(PAYPAL_ACCOUNT));
89+
assertThat(payPalAccountResponse.getField(TOKEN), is("trm-ac5727ac-8fe7-42fb-b69d-977ebdd7b48b"));
90+
assertThat(payPalAccountResponse.getField(STATUS), is(ACTIVATED));
91+
92+
assertThat(payPalAccountResponse.getField(CREATED_ON), is("2019-01-09T22:50:14"));
93+
assertThat(payPalAccountResponse.getField(TRANSFER_METHOD_COUNTRY), is("US"));
94+
assertThat(payPalAccountResponse.getField(TRANSFER_METHOD_CURRENCY), is("USD"));
95+
assertThat(payPalAccountResponse.getField(EMAIL), is("jsmith@paypal.com"));
96+
97+
}
98+
99+
@Test
100+
public void testGetGetPayPalAccount_returnsNoAccount() throws InterruptedException {
101+
102+
String responseBody = "";
103+
mServer.mockResponse().withHttpResponseCode(HttpURLConnection.HTTP_NO_CONTENT).withBody(responseBody).mock();
104+
105+
Hyperwallet.getDefault().getPayPalAccount("trm-ac5727ac-8fe7-42fb-b69d-977ebdd7b48b", mListener);
106+
mAwait.await(COUNTDOWN_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
107+
108+
RecordedRequest recordedRequest = mServer.getRequest();
109+
assertThat(recordedRequest.getPath(),
110+
is("/rest/v3/users/usr-fbfd5848-60d0-43c5-8462-099c959b49c7/paypal-accounts/trm-ac5727ac-8fe7-42fb"
111+
+ "-b69d-977ebdd7b48b"));
112+
113+
verify(mListener).onSuccess(mPayPalAccountArgumentCaptor.capture());
114+
verify(mListener, never()).onFailure(any(HyperwalletException.class));
115+
116+
PayPalAccount payPalAccount = mPayPalAccountArgumentCaptor.getValue();
117+
assertThat(payPalAccount, is(nullValue()));
118+
}
119+
120+
@Test
121+
public void testGetGetPayPalAccount_returnsError() throws InterruptedException {
122+
123+
String responseBody = mExternalResourceManager.getResourceContentError("system_error_response.json");
124+
mServer.mockResponse().withHttpResponseCode(HttpURLConnection.HTTP_BAD_REQUEST).withBody(responseBody).mock();
125+
126+
Hyperwallet.getDefault().getPayPalAccount("trm-854c4ec1-9161-49d6-92e2-b8d15aa4bf56", mListener);
127+
mAwait.await(COUNTDOWN_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
128+
129+
RecordedRequest recordedRequest = mServer.getRequest();
130+
assertThat(recordedRequest.getPath(),
131+
is("/rest/v3/users/usr-fbfd5848-60d0-43c5-8462-099c959b49c7/paypal-accounts/trm-854c4ec1-9161-49d6-92e2"
132+
+ "-b8d15aa4bf56"));
133+
134+
verify(mListener, never()).onSuccess(any(PayPalAccount.class));
135+
verify(mListener).onFailure(mExceptionCaptor.capture());
136+
137+
HyperwalletException exception = mExceptionCaptor.getValue();
138+
assertThat(exception, is(notNullValue()));
139+
HyperwalletErrors errors = exception.getHyperwalletErrors();
140+
assertThat(errors, is(notNullValue()));
141+
assertThat(errors.getErrors(), hasSize(1));
142+
143+
HyperwalletError error = errors.getErrors().get(0);
144+
assertThat(error.getCode(), is("SYSTEM_ERROR"));
145+
assertThat(error.getMessage(),
146+
is("A system error has occurred. Please try again. If you continue to receive this error, please "
147+
+ "contact customer support for assistance (Ref ID: 99b4ad5c-4aac-4cc2-aa9b-4b4f4844ac9b)."));
148+
149+
}
150+
}

0 commit comments

Comments
 (0)