private void stubConfigurationFromGateway(final String responseString) { BraintreeHttpClient fakeClient = new BraintreeHttpClient(mBraintreeFragment.getAuthorization()) { @Override public void get(String path, HttpResponseCallback callback) { if (path.contains(mBraintreeFragment.getAuthorization().getConfigUrl())) { callback.success(responseString); } } }; when(mBraintreeFragment.getHttpClient()).thenReturn(fakeClient); } }
@Test public void requestOneTimePayment_postParamsIncludeNoShipping() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").shippingAddressRequired(false)); ArgumentCaptor<String> pathCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).post(pathCaptor.capture(), dataCaptor.capture(), any(HttpResponseCallback.class)); assertTrue(pathCaptor.getValue().contains("/paypal_hermes/create_payment_resource")); JSONObject json = new JSONObject(dataCaptor.getValue()); assertEquals("1", json.get("amount")); assertEquals(true, json.getJSONObject("experience_profile").get("no_shipping")); assertEquals(false, json.getJSONObject("experience_profile").get("address_override")); }
@Test public void requestBillingAgreement_whenMerchantAccountIdNotPresent_postsDoNotIncludeMerchantAccountId() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPalRequest request = new PayPalRequest(); PayPal.requestBillingAgreement(fragment, request); ArgumentCaptor<String> pathCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).post(pathCaptor.capture(), dataCaptor.capture(), any(HttpResponseCallback.class)); assertTrue(pathCaptor.getValue().contains("/paypal_hermes/setup_billing_agreement")); JSONObject json = new JSONObject(dataCaptor.getValue()); assertFalse(json.has("merchant_account_id")); }
@Test public void getHttpClient_returnsHttpClient() throws InvalidArgumentException { BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY); assertNotNull(fragment.getHttpClient()); }
@Test public void requestOneTimePayment_whenMerchantAccountIdNotPresent_postDoesNotIncludeMerchantAccountId() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPalRequest request = new PayPalRequest("3.43"); PayPal.requestOneTimePayment(fragment, request); ArgumentCaptor<String> pathCaptor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).post(pathCaptor.capture(), dataCaptor.capture(), any(HttpResponseCallback.class)); assertTrue(pathCaptor.getValue().contains("/paypal_hermes/create_payment_resource")); JSONObject json = new JSONObject(dataCaptor.getValue()); assertFalse(json.has("merchant_account_id")); }
@Test public void requestOneTimePayment_landingPageType_canBeSetToLogin() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").landingPageType(PayPalRequest.LANDING_PAGE_TYPE_LOGIN)); ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).post(contains("/paypal_hermes/create_payment_resource"), dataCaptor.capture(), any(HttpResponseCallback.class)); JSONObject json = new JSONObject(dataCaptor.getValue()); assertEquals("1", json.get("amount")); assertEquals(true, json.getJSONObject("experience_profile").get("no_shipping")); assertEquals(false, json.getJSONObject("experience_profile").get("address_override")); assertEquals(PayPalRequest.LANDING_PAGE_TYPE_LOGIN, json.getJSONObject("experience_profile").get("landing_page_type")); }
@Test public void requestOneTimePayment_intent_canBeSetToSale() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").intent(PayPalRequest.INTENT_SALE)); ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).post(contains("/paypal_hermes/create_payment_resource"), dataCaptor.capture(), any(HttpResponseCallback.class)); JSONObject json = new JSONObject(dataCaptor.getValue()); assertEquals("1", json.get("amount")); assertEquals(true, json.getJSONObject("experience_profile").get("no_shipping")); assertEquals(false, json.getJSONObject("experience_profile").get("address_override")); assertEquals(PayPalRequest.INTENT_SALE, json.get("intent")); }
@Test public void requestOneTimePayment_landingPageType_canBeSetToBilling() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").landingPageType(PayPalRequest.LANDING_PAGE_TYPE_BILLING)); ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).post(contains("/paypal_hermes/create_payment_resource"), dataCaptor.capture(), any(HttpResponseCallback.class)); JSONObject json = new JSONObject(dataCaptor.getValue()); assertEquals("1", json.get("amount")); assertEquals(true, json.getJSONObject("experience_profile").get("no_shipping")); assertEquals(false, json.getJSONObject("experience_profile").get("address_override")); assertEquals(PayPalRequest.LANDING_PAGE_TYPE_BILLING, json.getJSONObject("experience_profile").get("landing_page_type")); }
@Test public void requestOneTimePayment_intent_canBeSetToOrder() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").intent(PayPalRequest.INTENT_ORDER)); ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).post(contains("/paypal_hermes/create_payment_resource"), dataCaptor.capture(), any(HttpResponseCallback.class)); JSONObject json = new JSONObject(dataCaptor.getValue()); assertEquals("1", json.get("amount")); assertEquals(true, json.getJSONObject("experience_profile").get("no_shipping")); assertEquals(false, json.getJSONObject("experience_profile").get("address_override")); assertEquals(PayPalRequest.INTENT_ORDER, json.get("intent")); }
@Test public void tokenize_includesSessionIdInRequest() throws JSONException { BraintreeFragment fragment = new MockFragmentBuilder().build(); when(fragment.getSessionId()).thenReturn("session-id"); TokenizationClient.tokenize(fragment, new CardBuilder(), null); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).post(anyString(), captor.capture(), any(HttpResponseCallback.class)); JSONObject data = new JSONObject(captor.getValue()).getJSONObject("_meta"); assertEquals("session-id", data.getString("sessionId")); }
@Test public void getPaymentMethodNonces_includesDefaultFirstParamAndSessionIdInRequestPath() { BraintreeFragment fragment = new MockFragmentBuilder().build(); when(fragment.getSessionId()).thenReturn("session-id"); PaymentMethod.getPaymentMethodNonces(fragment, true); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).get(captor.capture(), any(HttpResponseCallback.class)); String requestUri = captor.getValue(); assertTrue(requestUri.contains("default_first=true")); assertTrue(requestUri.contains("session_id=" + fragment.getSessionId())); }
@Before public void setup() throws InvalidArgumentException { clearSharedPreferences(RuntimeEnvironment.application); ConfigurationManager.sFetchingConfiguration = false; mTokenizationKey = Authorization.fromString(TestTokenizationKey.TOKENIZATION_KEY); mCountDownLatch = new CountDownLatch(1); mBraintreeFragment = mock(BraintreeFragment.class); when(mBraintreeFragment.getAuthorization()).thenReturn(mTokenizationKey); when(mBraintreeFragment.getApplicationContext()).thenReturn(RuntimeEnvironment.application); when(mBraintreeFragment.getHttpClient()).thenReturn(new BraintreeHttpClient(mTokenizationKey)); }
@Test public void requestOneTimePayment_containsOfferPayPalCreditParam() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").offerCredit(true)); ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).post(contains("/paypal_hermes/create_payment_resource"), dataCaptor.capture(), any(HttpResponseCallback.class)); JSONObject json = new JSONObject(dataCaptor.getValue()); assertTrue(json.getBoolean("offer_paypal_credit")); }
@Test public void requestOneTimePayment_displayName_canBeSet() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").displayName("Test Name")); ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).post(contains("/paypal_hermes/create_payment_resource"), dataCaptor.capture(), any(HttpResponseCallback.class)); JSONObject json = new JSONObject(dataCaptor.getValue()); assertEquals("Test Name", json.getJSONObject("experience_profile").get("brand_name")); }
@Test public void requestBillingAgreement_containsOfferPayPalCreditParam() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.requestBillingAgreement(fragment, new PayPalRequest().offerCredit(true)); ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class); verify(fragment.getHttpClient()).post(contains("/paypal_hermes/setup_billing_agreement"), dataCaptor.capture(), any(HttpResponseCallback.class)); JSONObject json = new JSONObject(dataCaptor.getValue()); assertTrue(json.getBoolean("offer_paypal_credit")); }
@Test public void tokenize_tokenizesCardsWithRestWhenGraphQLIsDisabled() { BraintreeFragment fragment = new MockFragmentBuilder().build(); CardBuilder cardBuilder = new CardBuilder(); TokenizationClient.tokenize(fragment, cardBuilder, null); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verifyZeroInteractions(fragment.getGraphQLHttpClient()); verify(fragment.getHttpClient()).post(anyString(), captor.capture(), any(HttpResponseCallback.class)); assertEquals(cardBuilder.build(), captor.getValue()); }
private void flushAnalyticsEvents() { if (getConfiguration() != null && getConfiguration().toJson() != null && getConfiguration().getAnalytics().isEnabled()) { Intent intent = new Intent(mContext, AnalyticsIntentService.class) .putExtra(AnalyticsIntentService.EXTRA_AUTHORIZATION, getAuthorization().toString()) .putExtra(AnalyticsIntentService.EXTRA_CONFIGURATION, getConfiguration().toJson()); try { getApplicationContext().startService(intent); } catch (RuntimeException e) { AnalyticsSender.send(getApplicationContext(), mAuthorization, getHttpClient(), getConfiguration().getAnalytics().getUrl(), false); } } }
protected void setConfiguration(Configuration configuration) { mConfiguration = configuration; getHttpClient().setBaseUrl(configuration.getClientApiUrl()); if (configuration.getGraphQL().isEnabled()) { mGraphQLHttpClient = new BraintreeGraphQLHttpClient(configuration.getGraphQL().getUrl(), mAuthorization.getBearer()); } }
@Test public void tokenize_withGraphQLEnabledButApiBelowLollipop_tokenizesCardsWithRest() throws BraintreeException { BraintreeFragment fragment = new MockFragmentBuilder() .configuration(new TestConfigurationBuilder() .graphQL() .build()) .build(); CardBuilder cardBuilder = new CardBuilder(); TokenizationClient.tokenize(fragment, cardBuilder, null); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verifyZeroInteractions(fragment.getGraphQLHttpClient()); verify(fragment.getHttpClient()).post(anyString(), captor.capture(), any(HttpResponseCallback.class)); assertEquals(cardBuilder.build(), captor.getValue()); }
@Before public void setUp() throws InvalidArgumentException { mConfiguration = new TestConfigurationBuilder() .assetsUrl("http://assets.example.com") .paypal(new TestPayPalConfigurationBuilder(true) .environment("offline") .billingAgreementsEnabled(false)) .buildConfiguration(); mBraintreeFragment = getMockFragment(stringFromFixture("client_token.json"), mConfiguration); mMockHttpClient = mock(BraintreeHttpClient.class); when(mBraintreeFragment.getHttpClient()).thenReturn(mMockHttpClient); }