RealPosClient implements PosClient { @NonNull @Override public Intent createChargeIntent(@NonNull ChargeRequest chargeRequest) { nonNull(chargeRequest, "chargeRequest"); List<ResolveInfo> activities = queryChargeActivities(); PackageInfo pointOfSalePackage = findPointOfSaleWithHighestVersion(activities); return createPinnedChargeIntent(chargeRequest, pointOfSalePackage); } RealPosClient(Context context, String clientId); @NonNull @Override Intent createChargeIntent(@NonNull ChargeRequest chargeRequest); @Override boolean isPointOfSaleInstalled(); @Override void launchPointOfSale(); @Override void openPointOfSalePlayStoreListing(); @NonNull @Override ChargeRequest.Success parseChargeSuccess(@NonNull Intent data); @NonNull @Override ChargeRequest.Error parseChargeError(@NonNull Intent data);  }
@Test public void fieldsSetAsExpectedForDefaultRequest() { ChargeRequest request = new ChargeRequest.Builder(1_00, CurrencyCode.USD).build(); Intent intent = client.createChargeIntent(request); assertThat(intent.hasExtra(EXTRA_POINT_OF_SALE_CLIENT_ID)).isTrue(); assertThat(intent.hasExtra(EXTRA_TOTAL_AMOUNT)).isTrue(); assertThat(intent.hasExtra(EXTRA_API_VERSION)).isTrue(); assertThat(intent.hasExtra(EXTRA_SDK_VERSION)).isTrue(); assertThat(intent.hasExtra(EXTRA_CURRENCY_CODE)).isTrue(); assertThat(intent.hasExtra(EXTRA_REQUEST_METADATA)).isTrue(); assertThat(intent.hasExtra(EXTRA_TENDER_TYPES)).isTrue(); assertThat(intent.hasExtra(EXTRA_CUSTOMER_ID)).isFalse(); assertThat(intent.hasExtra(EXTRA_LOCATION_ID)).isFalse(); assertThat(intent.hasExtra(EXTRA_AUTO_RETURN_TIMEOUT_MS)).isFalse(); }
@Test public void pinsToHighestVersionNumber() { chargeActivities.clear(); installApp("com.squareup", 2, POINT_OF_SALE_SIGNATURE); installApp("com.squareup.beta", 3, POINT_OF_SALE_SIGNATURE); Intent intent = client.createChargeIntent(new ChargeRequest.Builder(1_00, CurrencyCode.USD).build()); assertThat(intent.getPackage()).isEqualTo("com.squareup.beta"); }
@Test(expected = ActivityNotFoundException.class) public void createIntentWithNoPointOfSaleThrows() { chargeActivities.clear(); client.createChargeIntent(new ChargeRequest.Builder(1_00, CurrencyCode.USD).build()); }
@Test(expected = ActivityNotFoundException.class) public void createIntentWithInvalidPointOfSaleThrows() { chargeActivities.clear(); installApp("com.squareup", 2, INVALID_SIGNATURE); client.createChargeIntent(new ChargeRequest.Builder(1_00, CurrencyCode.USD).build()); }
@Test public void intentCreatedWithRequestParams() { ChargeRequest request = new ChargeRequest.Builder(1_00, CurrencyCode.USD).restrictTendersTo( ChargeRequest.TenderType.CARD) .autoReturn(4, SECONDS) .enforceBusinessLocation("location") .customerId("customerId") .requestMetadata("metadata") .note("note") .build(); Intent intent = client.createChargeIntent(request); assertThat(intent.getIntExtra(EXTRA_TOTAL_AMOUNT, -1)).isEqualTo(1_00); assertThat(intent.getStringExtra(EXTRA_CURRENCY_CODE)).isEqualTo("USD"); assertThat(intent.getStringExtra(EXTRA_POINT_OF_SALE_CLIENT_ID)).isEqualTo(CLIENT_ID); assertThat(intent.getStringExtra(EXTRA_NOTE)).isEqualTo("note"); assertThat(intent.getStringExtra(EXTRA_API_VERSION)).isEqualTo("v2.0"); assertThat(intent.getStringExtra(EXTRA_REQUEST_METADATA)).isEqualTo("metadata"); assertThat(intent.getStringExtra(EXTRA_LOCATION_ID)).isEqualTo("location"); assertThat(intent.getStringExtra(EXTRA_CUSTOMER_ID)).isEqualTo("customerId"); assertThat(intent.getStringArrayListExtra(EXTRA_TENDER_TYPES)).containsExactly( EXTRA_TENDER_CARD); assertThat(intent.getLongExtra(EXTRA_AUTO_RETURN_TIMEOUT_MS, -1)).isEqualTo(4_000); assertThat(intent.getPackage()).isEqualTo("com.squareup"); }