@Test
  public void testMakePaymentsWithProfile() {
    Gateway beanstream =
        new Gateway(
            "v1",
            300200578,
            "4BaD82D9197b4cc4b70a221911eE9f70", // payments API passcode
            "D97D3BE1EE964A6193D17A571D9FBC80", // profiles API passcode
            "4e6Ff318bee64EA391609de89aD4CF5d"); // reporting API passcode

    Address billing = getTestCardValidAddress();
    Card card =
        new Card()
            .setName("JANE DOE")
            .setNumber("5100000010001004")
            .setExpiryMonth("12")
            .setExpiryYear("18")
            .setCvd("123");

    try {
      ProfileResponse profile = beanstream.profiles().createProfile(card, billing);

      ProfilePaymentRequest paymentRequest = new ProfilePaymentRequest();
      paymentRequest.setProfile(
          new ProfilePaymentRequestData().setCardId(1).setCustomerCode(profile.getId()));
      paymentRequest.setAmount(13);
      paymentRequest.setOrderNumber(getRandomOrderId("TEST"));

      // make a regular payment
      PaymentResponse result = beanstream.payments().makePayment(paymentRequest);
      Assert.assertNotNull(result);

      // run a pre-auth
      paymentRequest.setAmount(100).setOrderNumber(getRandomOrderId("TEST"));
      result = beanstream.payments().preAuth(paymentRequest);
      Assert.assertNotNull(result);
      Assert.assertTrue("PA".equals(result.type));

      // complete the pre-auth
      result = beanstream.payments().preAuthCompletion(result.id, 100);
      Assert.assertNotNull(result);
      Assert.assertTrue("PAC".equals(result.type));

    } catch (BeanstreamApiException ex) {
      Logger.getLogger(SampleTransactions.class.getName()).log(Level.SEVERE, null, ex);
      Assert.fail(ex.getMessage());
    }
  }
  @Test
  public void testProfileCrudUsingToken() {
    Gateway beanstream =
        new Gateway(
            "v1",
            300200578,
            "4BaD82D9197b4cc4b70a221911eE9f70", // payments API passcode
            "D97D3BE1EE964A6193D17A571D9FBC80", // profiles API passcode
            "4e6Ff318bee64EA391609de89aD4CF5d"); // reporting API passcode
    HttpsConnector connector = new HttpsConnector(300200578, "4BaD82D9197b4cc4b70a221911eE9f70");

    String profileId = null;
    try {

      Address billing = getTestCardValidAddress();
      LegatoTokenRequest tokenRequest = new LegatoTokenRequest();
      tokenRequest.number = "5100000010001004";
      tokenRequest.expiryMonth = 12;
      tokenRequest.expiryYear = 18;
      tokenRequest.cvd = "123";

      String url = "https://www.beanstream.com/scripts/tokenization/tokens";
      String output = "";
      try {
        output = connector.ProcessTransaction(HttpMethod.post, url, tokenRequest);
      } catch (BeanstreamApiException ex) {
        Logger.getLogger(SampleTransactions.class.getName()).log(Level.SEVERE, null, ex);
        Assert.fail(ex.getMessage());
      }

      System.out.println(output);
      // Parse the output and return a token response to get the token for the payment request
      Gson gson = new Gson();
      LegatoTokenResponse tokenResponse = gson.fromJson(output, LegatoTokenResponse.class);

      // test create profile
      Token token = new Token("John Doe", tokenResponse.getToken());

      ProfileResponse createdProfile = beanstream.profiles().createProfile(token, billing);
      profileId = createdProfile.getId();
      System.out.println(profileId);
      Assert.assertNotNull(
          "Test failed because it should create the profile and return a valid id", profileId);

      // test get profile by id
      PaymentProfile paymentProfile = beanstream.profiles().getProfileById(profileId);
      Assert.assertEquals(
          "billing address assinged does not matches with the one sent at creation time",
          paymentProfile.getBilling(),
          billing);
      Assert.assertNotNull("Credit card was not in the response", paymentProfile.getCard());
      Assert.assertTrue(
          "The default lenguage should be english", "en".equals(paymentProfile.getLanguage()));

      // update the profile to francais
      paymentProfile.setLanguage("fr");
      paymentProfile.setComments("test updating profile sending billing info only");
      // update profile
      beanstream.profiles().updateProfile(paymentProfile);

      // refresh the updated profile
      paymentProfile = beanstream.profiles().getProfileById(profileId);

      Assert.assertEquals("Language was updated to Francais", "fr", paymentProfile.getLanguage());

      // delete the payment profile
      beanstream.profiles().deleteProfileById(profileId);
      try {
        beanstream.profiles().getProfileById(profileId);
      } catch (BeanstreamApiException e) {
        profileId = null;
      }
    } catch (BeanstreamApiException ex) {
      Assert.fail("Test can not continue, " + ex.getMessage());
    } catch (JsonSyntaxException ex) {
      Assert.fail("unexpected exception occur, test can not continue");
    } finally {
      if (profileId != null) {
        try {
          beanstream.profiles().deleteProfileById(profileId);
        } catch (BeanstreamApiException e) {
          Assert.fail("unexpected exception occur, test can not continue. " + e.getMessage());
        }
      }
    }
  }
  @Test
  public void testProfileCrud() {
    Gateway beanstream =
        new Gateway(
            "v1",
            300200578,
            "4BaD82D9197b4cc4b70a221911eE9f70", // payments API passcode
            "D97D3BE1EE964A6193D17A571D9FBC80", // profiles API passcode
            "4e6Ff318bee64EA391609de89aD4CF5d"); // reporting API passcode

    String profileId = null;

    try {
      Address billing = getTestCardValidAddress();
      Card card =
          new Card()
              .setName("John Doe")
              .setNumber("5100000010001004")
              .setExpiryMonth("12")
              .setExpiryYear("18")
              .setCvd("123");

      // test create profile
      ProfileResponse createdProfile = beanstream.profiles().createProfile(card, billing);
      profileId = createdProfile.getId();
      System.out.println(createdProfile);

      // test get profile by id
      PaymentProfile paymentProfile = beanstream.profiles().getProfileById(profileId);
      System.out.println(paymentProfile);
      Assert.assertNotNull(paymentProfile);

      // update the profile to francais
      paymentProfile.setLanguage("fr");
      paymentProfile.setComments("test updating profile sending billing info only");
      // update profile
      beanstream.profiles().updateProfile(paymentProfile);

      // refresh the updated profile
      paymentProfile = beanstream.profiles().getProfileById(profileId);
      System.out.println(paymentProfile);
      Assert.assertNotNull(paymentProfile);
      Assert.assertEquals("fr", paymentProfile.getLanguage());

      // add a new card
      Card newCard =
          new Card()
              .setCvd("123")
              .setName("Tester Doe")
              .setNumber("4030000010001234")
              .setExpiryMonth("01")
              .setExpiryYear("19");
      ProfileResponse newCardResp = beanstream.profiles().addCard(profileId, newCard);
      System.out.println(newCardResp);

      // get all cards
      List<Card> profileCards = beanstream.profiles().getCards(profileId);
      Assert.assertNotNull(profileCards);
      Assert.assertEquals("Number of cards not expected.", 2, profileCards.size());

      // update card
      Card cardUpdated = beanstream.profiles().getCard(profileId, "1");
      cardUpdated.setExpiryMonth("04");
      beanstream.profiles().updateCard(profileId, cardUpdated);

      // delete card
      ProfileResponse result = beanstream.profiles().removeCard(profileId, "2");

      // delete the payment profile
      beanstream.profiles().deleteProfileById(profileId);
      boolean notFound = false;
      try {
        beanstream.profiles().getProfileById(profileId);
        System.out.println("This profile was deleted, therefore should throw an exception");
      } catch (BeanstreamApiException e) {
        profileId = null;
        notFound = true;
      }
      Assert.assertTrue("Profile should not have been found", notFound);

    } catch (Exception ex) {
      System.out.println(
          "unexpected exception occurred, test can not continue : " + ex.getMessage());
      Assert.fail(ex.getMessage());
    } finally {
      if (profileId != null) {
        try {
          beanstream.profiles().deleteProfileById(profileId);
        } catch (BeanstreamApiException e) {
          e.printStackTrace();
          Assert.fail(e.getMessage());
        }
      }
    }
  }