@Test
    public void testTokenPayment() {

        Gateway beanstream = new Gateway("v1", 300200578,
                "4BaD82D9197b4cc4b70a221911eE9f70");
        HttpsConnector connector = new HttpsConnector(300200578,
                "4BaD82D9197b4cc4b70a221911eE9f70");

        /* Test Token Payment */
		// The first step is to call the Legato service to get a token.
        // This is normally performed on the client machine, and not on the
        // server.
        // The goal with tokens is to not have credit card information move
        // through your server,
        // thus lowering your scope for PCI compliance
        LegatoTokenRequest legatoTokenRequest = new LegatoTokenRequest();
        legatoTokenRequest.number = "5100000010001004";
        legatoTokenRequest.expiryMonth = 12;
        legatoTokenRequest.expiryYear = 18;
        legatoTokenRequest.cvd = "123";

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

		// 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);

        System.out.println("token: " + output);

        TokenPaymentRequest tokenReq = new TokenPaymentRequest();
        tokenReq.setAmount(100.00);
        tokenReq.setOrderNumber(getRandomOrderId("token"));
        tokenReq.getToken()
                .setName("John Doe")
                .setCode(tokenResponse.getToken());

        try {
            PaymentResponse response = beanstream.payments().makePayment(tokenReq);
            System.out.println("Token Payment Approved? "+ response.isApproved());
            
        } catch (BeanstreamApiException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,"An error occurred", ex);
            Assert.fail(ex.getMessage());
        }
        
        // Pre-Auth and Complete
        
        try {
            output = connector.ProcessTransaction(HttpMethod.post, url,legatoTokenRequest);
        } catch (BeanstreamApiException ex) {
            Logger.getLogger(SampleTransactions.class.getName()).log(Level.SEVERE, null, ex);
            Assert.fail(ex.getMessage());
        }
        tokenResponse = gson.fromJson(output,LegatoTokenResponse.class);

        System.out.println("Token pre-auth: "+tokenResponse.getToken());
        
        TokenPaymentRequest req = new TokenPaymentRequest();
        req.setAmount(80.00);
        req.setOrderNumber(getRandomOrderId("token"));
        req.getToken()
            .setName("John Doe")
            .setCode(tokenResponse.getToken());

        try {
            PaymentResponse response = beanstream.payments().preAuth(req);
            System.out.println("Token Payment Approved? "+ response.isApproved());
            response = beanstream.payments().preAuthCompletion(response.id, 55.30);
            Assert.assertTrue(response.isApproved());
            Assert.assertEquals("PAC", response.type);
        } catch (BeanstreamApiException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,"An error occurred", ex);
            Assert.fail(ex.getMessage());
        }
    }