@Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } });
@Test public void startPayment_callsExceptionListener_localPaymentRequestIsNull() throws InvalidArgumentException, JSONException { LocalPayment.startPayment(mBraintreeFragment, null, new BraintreeResponseListener<LocalPaymentRequest>() { @Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } }); verify(mBraintreeFragment).postCallback(any(BraintreeException.class)); }
@Test public void onActivityResult_tokenize_sendsAnalyticsEvent() throws JSONException, InterruptedException { final CountDownLatch latch = new CountDownLatch(1); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[2]; callback.success(stringFromFixture("error_response.json")); latch.countDown(); return null; } }).when(mMockHttpClient).post(eq("/v1/paypal_hermes/create_payment_resource"), any(String.class), any(HttpResponseCallback.class)); LocalPayment.startPayment(mBraintreeFragment, getIdealLocalPaymentRequest(), new BraintreeResponseListener<LocalPaymentRequest>() { @Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } }); latch.await(); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[2]; callback.success(stringFromFixture("payment_methods/local_payment_response.json")); return null; } }).when(mMockHttpClient).post(eq("/v1/payment_methods/paypal_accounts"), any(String.class), any(HttpResponseCallback.class)); LocalPayment.onActivityResult(mBraintreeFragment, Activity.RESULT_OK, getSuccessResponseIntent()); verify(mBraintreeFragment).sendAnalyticsEvent(eq("ideal.local-payment.tokenize.succeeded")); }
@Test public void onActivityResult_returnsResultToFragment() throws InvalidArgumentException, JSONException, InterruptedException { final CountDownLatch latch = new CountDownLatch(1); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[2]; callback.success(stringFromFixture("payment_methods/local_payment_response.json")); latch.countDown(); return null; } }).when(mMockHttpClient).post(eq("/v1/payment_methods/paypal_accounts"), any(String.class), any(HttpResponseCallback.class)); LocalPayment.onActivityResult(mBraintreeFragment, Activity.RESULT_OK, getSuccessResponseIntent()); latch.await(); ArgumentCaptor<LocalPaymentResult> captor = ArgumentCaptor.forClass(LocalPaymentResult.class); verify(mBraintreeFragment).postCallback(captor.capture()); LocalPaymentResult capturedResult = captor.getValue(); assertEquals("e11c9c39-d6a4-0305-791d-bfe680ef2d5d", capturedResult.getNonce()); }
/** * Initiates the browser switch for a payment flow by opening a browser where the customer can authenticate with their bank. * * @param fragment {@link BraintreeFragment} * @param request {@link LocalPaymentRequest} which has already been sent to {@link #startPayment(BraintreeFragment, LocalPaymentRequest, BraintreeResponseListener)} * and now has an approvalUrl and paymentId. */ public static void approvePayment(BraintreeFragment fragment, LocalPaymentRequest request) { fragment.browserSwitch(BraintreeRequestCodes.LOCAL_PAYMENT, request.getApprovalUrl()); fragment.sendAnalyticsEvent(paymentTypeForAnalytics() + ".local-payment.webswitch.initiate.succeeded"); }
@Test public void startPayment_callsExceptionListener_whenListenerIsNull() throws InvalidArgumentException, JSONException { LocalPayment.startPayment(mBraintreeFragment, getIdealLocalPaymentRequest(),null); verify(mBraintreeFragment).postCallback(any(BraintreeException.class)); }
@Test public void onActivityResult_cancel_sendsAnalyticsEvent() throws JSONException, InterruptedException { final CountDownLatch latch = new CountDownLatch(1); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[2]; callback.success(stringFromFixture("error_response.json")); latch.countDown(); return null; } }).when(mMockHttpClient).post(eq("/v1/paypal_hermes/create_payment_resource"), any(String.class), any(HttpResponseCallback.class)); LocalPayment.startPayment(mBraintreeFragment, getIdealLocalPaymentRequest(), new BraintreeResponseListener<LocalPaymentRequest>() { @Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } }); latch.await(); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[2]; callback.success(stringFromFixture("payment_methods/local_payment_response.json")); return null; } }).when(mMockHttpClient).post(eq("/v1/payment_methods/paypal_accounts"), any(String.class), any(HttpResponseCallback.class)); LocalPayment.onActivityResult(mBraintreeFragment, Activity.RESULT_OK, getCancelResponseIntent()); verify(mBraintreeFragment).sendAnalyticsEvent(eq("ideal.local-payment.webswitch.canceled")); }
break; case BraintreeRequestCodes.LOCAL_PAYMENT: LocalPayment.onActivityResult(this, resultCode, data); break; case BraintreeRequestCodes.GOOGLE_PAYMENT:
fragment.sendAnalyticsEvent(paymentTypeForAnalytics() + ".local-payment.webswitch.canceled"); fragment.postCancelCallback(BraintreeRequestCodes.LOCAL_PAYMENT); return; fragment.sendAnalyticsEvent(paymentTypeForAnalytics() + ".local-payment.webswitch-response.invalid"); fragment.postCallback(new BraintreeException("LocalPayment encountered an error, " + "return URL is invalid."));
@Test public void startPayment_callsExceptionListener_whenApprovalUrlIsSet() throws InvalidArgumentException, JSONException { LocalPaymentRequest request = getIdealLocalPaymentRequest() .approvalUrl("aUrl"); LocalPayment.startPayment(mBraintreeFragment, request, new BraintreeResponseListener<LocalPaymentRequest>() { @Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } }); verify(mBraintreeFragment).postCallback(any(BraintreeException.class)); }
@Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } });
@Test public void startPayment_callsExceptionListener_whenPaymentIdIsSet() throws InvalidArgumentException, JSONException { LocalPaymentRequest request = getIdealLocalPaymentRequest() .paymentId("pid"); LocalPayment.startPayment(mBraintreeFragment, request, new BraintreeResponseListener<LocalPaymentRequest>() { @Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } }); verify(mBraintreeFragment).postCallback(any(BraintreeException.class)); }
@Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } });
@Test public void startPayment_callsExceptionListener_paymentTypeIsNull() throws InvalidArgumentException, JSONException { LocalPaymentRequest request = getIdealLocalPaymentRequest() .paymentType(null); LocalPayment.startPayment(mBraintreeFragment, request, new BraintreeResponseListener<LocalPaymentRequest>() { @Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } }); verify(mBraintreeFragment).postCallback(any(BraintreeException.class)); }
@Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } });
@Test public void startPayment_callsExceptionListener_amountIsNull() throws InvalidArgumentException, JSONException { LocalPaymentRequest request = getIdealLocalPaymentRequest() .amount(null); LocalPayment.startPayment(mBraintreeFragment, request, new BraintreeResponseListener<LocalPaymentRequest>() { @Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } }); verify(mBraintreeFragment).postCallback(any(BraintreeException.class)); }
@Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } });
@Test public void startPayment_failure_sendsAnalyticsEvent() throws JSONException, InvalidArgumentException { final CountDownLatch latch = new CountDownLatch(1); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[2]; callback.success(stringFromFixture("error_response.json")); return null; } }).when(mMockHttpClient).post(eq("/v1/paypal_hermes/create_payment_resource"), any(String.class), any(HttpResponseCallback.class)); LocalPayment.startPayment(mBraintreeFragment, getIdealLocalPaymentRequest(), new BraintreeResponseListener<LocalPaymentRequest>() { @Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } }); verify(mBraintreeFragment).sendAnalyticsEvent(eq("ideal.local-payment.start-payment.selected")); verify(mBraintreeFragment).sendAnalyticsEvent(eq("ideal.local-payment.webswitch.initiate.failed")); latch.countDown(); }
@Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } });
@Test public void startPayment_success_sendsAnalyticsEvent() throws JSONException { final CountDownLatch latch = new CountDownLatch(1); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[2]; callback.success(stringFromFixture("payment_methods/local_payment_create_response.json")); return null; } }).when(mMockHttpClient).post(eq("/v1/paypal_hermes/create_payment_resource"), any(String.class), any(HttpResponseCallback.class)); LocalPayment.startPayment(mBraintreeFragment, getIdealLocalPaymentRequest(), new BraintreeResponseListener<LocalPaymentRequest>() { @Override public void onResponse(LocalPaymentRequest localPaymentRequest) { LocalPayment.approvePayment(mBraintreeFragment, localPaymentRequest); } }); verify(mBraintreeFragment).sendAnalyticsEvent(eq("ideal.local-payment.start-payment.selected")); verify(mBraintreeFragment).sendAnalyticsEvent(eq("ideal.local-payment.webswitch.initiate.succeeded")); latch.countDown(); }