Search in sources :

Example 1 with IdealResult

use of com.braintreepayments.api.models.IdealResult in project braintree_android by braintree.

the class IdealUnitTest method onActivityResult_returnsResultToFragment.

@Test
public void onActivityResult_returnsResultToFragment() throws InvalidArgumentException, JSONException, InterruptedException {
    putResultIdInPrefs("ideal_payment_id");
    final CountDownLatch latch = new CountDownLatch(1);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpResponseCallback callback = (HttpResponseCallback) invocation.getArguments()[1];
            callback.success(stringFromFixture("payment_methods/completed_ideal_bank_payment.json"));
            latch.countDown();
            return null;
        }
    }).when(mMockApiClient).get(eq("/ideal-payments/ideal_payment_id/status"), any(HttpResponseCallback.class));
    Ideal.onActivityResult(mBraintreeFragment, Activity.RESULT_OK);
    latch.await();
    ArgumentCaptor<IdealResult> captor = ArgumentCaptor.forClass(IdealResult.class);
    verify(mBraintreeFragment).postCallback(captor.capture());
    IdealResult capturedResult = captor.getValue();
    assertEquals("ideal_payment_id", capturedResult.getId());
    assertEquals("short_id", capturedResult.getShortId());
    assertEquals("COMPLETE", capturedResult.getStatus());
}
Also used : Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JSONObject(org.json.JSONObject) CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) IdealResult(com.braintreepayments.api.models.IdealResult) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with IdealResult

use of com.braintreepayments.api.models.IdealResult in project braintree_android by braintree.

the class IdealUnitTest method startPayment_callsResponseListenerWithPaymentId.

@Test
public void startPayment_callsResponseListenerWithPaymentId() 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/pending_ideal_bank_payment.json"));
            return null;
        }
    }).when(mMockApiClient).post(eq("/ideal-payments"), any(String.class), any(HttpResponseCallback.class));
    List<IdealBank> banks = IdealBank.fromJson(mConfiguration, stringFromFixture("payment_methods/ideal_issuing_banks.json"));
    IdealRequest builder = new IdealRequest().issuerId(banks.get(0).getId()).amount("1.00").currency("EUR").orderId("abc-123");
    Ideal.startPayment(mBraintreeFragment, builder, new BraintreeResponseListener<IdealResult>() {

        @Override
        public void onResponse(IdealResult idealResult) {
            assertEquals("ideal_payment_id", idealResult.getId());
            latch.countDown();
        }
    });
    latch.await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IdealRequest(com.braintreepayments.api.models.IdealRequest) IdealBank(com.braintreepayments.api.models.IdealBank) JSONObject(org.json.JSONObject) IdealResult(com.braintreepayments.api.models.IdealResult) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with IdealResult

use of com.braintreepayments.api.models.IdealResult in project braintree_android by braintree.

the class IdealTest method startPayment_returnsPendingPayment.

@Test(timeout = 10000)
public void startPayment_returnsPendingPayment() throws InterruptedException {
    IdealRequest builder = new IdealRequest().currency("EUR").amount("10").issuerId("INGBNL2A").orderId(UUID.randomUUID().toString().substring(0, 15));
    Ideal.startPayment(mBraintreeFragment, builder, null);
    String savedId;
    do {
        savedId = BraintreeSharedPreferences.getString(mBraintreeFragment.getApplicationContext(), Ideal.IDEAL_RESULT_ID);
        SystemClock.sleep(500);
    } while (savedId.equals(""));
    Ideal.onActivityResult(mBraintreeFragment, Activity.RESULT_OK);
    mBraintreeFragment.addListener(new BraintreeErrorListener() {

        @Override
        public void onError(Exception error) {
            fail(error.getMessage());
        }
    });
    mBraintreeFragment.addListener(new BraintreePaymentResultListener() {

        @Override
        public void onBraintreePaymentResult(BraintreePaymentResult result) {
            assertTrue(result instanceof IdealResult);
            IdealResult idealResult = (IdealResult) result;
            assertFalse(TextUtils.isEmpty(idealResult.getId()));
            assertFalse(TextUtils.isEmpty(idealResult.getShortId()));
            assertFalse(TextUtils.isEmpty(idealResult.getStatus()));
            mCountDownLatch.countDown();
        }
    });
    mCountDownLatch.await();
}
Also used : BraintreePaymentResult(com.braintreepayments.api.models.BraintreePaymentResult) IdealRequest(com.braintreepayments.api.models.IdealRequest) BraintreeErrorListener(com.braintreepayments.api.interfaces.BraintreeErrorListener) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) IdealResult(com.braintreepayments.api.models.IdealResult) BraintreePaymentResultListener(com.braintreepayments.api.interfaces.BraintreePaymentResultListener) Test(org.junit.Test)

Example 4 with IdealResult

use of com.braintreepayments.api.models.IdealResult in project braintree_android by braintree.

the class Ideal method startPayment.

/**
 * Initiates the payment flow by opening a browser where the customer can authenticate with their bank.
 *
 * @param fragment {@link BraintreeFragment}
 * @param builder {@link IdealRequest} with the payment details.
 * @param listener {@link BraintreeResponseListener} the callback to which the {@link IdealResult} will be sent
 * with a status of `PENDING` before the flow starts. This result contains the iDEAL payment ID.
 */
public static void startPayment(final BraintreeFragment fragment, final IdealRequest builder, final BraintreeResponseListener<IdealResult> listener) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            fragment.sendAnalyticsEvent("ideal.start-payment.selected");
            Exception configException = checkIdealEnabled(configuration);
            if (configException != null) {
                fragment.postCallback(configException);
                fragment.sendAnalyticsEvent("ideal.start-payment.invalid-configuration");
                return;
            }
            String redirectUrl = URI.create(configuration.getIdealConfiguration().getAssetsUrl() + ASSET_SERVER_REDIRECT_PATH + fragment.getReturnUrlScheme() + "://").toString();
            fragment.getBraintreeApiHttpClient().post("/ideal-payments", builder.build(redirectUrl, configuration.getIdealConfiguration().getRouteId()), new HttpResponseCallback() {

                @Override
                public void success(String responseBody) {
                    try {
                        IdealResult idealResult = IdealResult.fromJson(responseBody);
                        BraintreeSharedPreferences.putString(fragment.getApplicationContext(), IDEAL_RESULT_ID, idealResult.getId());
                        if (listener != null) {
                            listener.onResponse(idealResult);
                        }
                        JSONObject responseJson = new JSONObject(responseBody);
                        String approvalUrl = responseJson.getJSONObject("data").getString("approval_url");
                        fragment.browserSwitch(BraintreeRequestCodes.IDEAL, approvalUrl);
                        fragment.sendAnalyticsEvent("ideal.webswitch.initiate.succeeded");
                    } catch (JSONException jsonException) {
                        failure(jsonException);
                    }
                }

                @Override
                public void failure(Exception exception) {
                    fragment.sendAnalyticsEvent("ideal.webswitch.initiate.failed");
                    fragment.postCallback(exception);
                }
            });
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException) IdealResult(com.braintreepayments.api.models.IdealResult)

Example 5 with IdealResult

use of com.braintreepayments.api.models.IdealResult in project braintree_android by braintree.

the class IdealActivity method launchIdeal.

public void launchIdeal(View v) {
    final ProgressDialog dialog = new ProgressDialog(this);
    dialog.setTitle("Fetching issuing banks");
    Ideal.fetchIssuingBanks(mBraintreeFragment, new BraintreeResponseListener<List<IdealBank>>() {

        @Override
        public void onResponse(final List<IdealBank> idealBanks) {
            dialog.dismiss();
            new AlertDialog.Builder(IdealActivity.this).setTitle("Issuing Banks").setAdapter(new BankListAdapter(IdealActivity.this, idealBanks), new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    IdealBank bank = idealBanks.get(i);
                    IdealRequest builder = new IdealRequest().issuerId(bank.getId()).amount("10").orderId(UUID.randomUUID().toString().substring(0, 15)).currency("EUR");
                    Ideal.startPayment(mBraintreeFragment, builder, new BraintreeResponseListener<IdealResult>() {

                        @Override
                        public void onResponse(IdealResult idealResult) {
                            String idealId = idealResult.getId();
                        }
                    });
                }
            }).create().show();
        }
    });
    dialog.show();
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) ProgressDialog(android.app.ProgressDialog) IdealRequest(com.braintreepayments.api.models.IdealRequest) IdealBank(com.braintreepayments.api.models.IdealBank) OnClickListener(android.content.DialogInterface.OnClickListener) List(java.util.List) IdealResult(com.braintreepayments.api.models.IdealResult)

Aggregations

IdealResult (com.braintreepayments.api.models.IdealResult)11 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)6 JSONObject (org.json.JSONObject)6 Test (org.junit.Test)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 Answer (org.mockito.stubbing.Answer)5 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 IdealRequest (com.braintreepayments.api.models.IdealRequest)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)3 Configuration (com.braintreepayments.api.models.Configuration)3 IdealBank (com.braintreepayments.api.models.IdealBank)3 BraintreeApiHttpClient (com.braintreepayments.api.internal.BraintreeApiHttpClient)2 TestConfigurationBuilder (com.braintreepayments.testutils.TestConfigurationBuilder)2 TestBraintreeApiConfigurationBuilder (com.braintreepayments.testutils.TestConfigurationBuilder.TestBraintreeApiConfigurationBuilder)2 TestIdealConfigurationBuilder (com.braintreepayments.testutils.TestConfigurationBuilder.TestIdealConfigurationBuilder)2 AlertDialog (android.app.AlertDialog)1 ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1