Search in sources :

Example 1 with StripeException

use of com.stripe.exception.StripeException in project cryptonomica by Cryptonomica.

the class StripeTestServlet method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    /* --- request*/
    String requestDataStr = ServletUtils.getAllRequestData(request);
    String responseStr = "{\"paymentData\":" + requestDataStr + ",";
    // Get the credit card details submitted by the form
    String token = request.getParameter("stripeToken");
    @SuppressWarnings("unused") String cardHolderName = request.getParameter("cardHolderName");
    /* -- see example on: https://github.com/stripe/stripe-java#usage */
    // 
    RequestOptions requestOptions = (new RequestOptions.RequestOptionsBuilder()).setApiKey(STRIPE_SECRET_KEY).build();
    // 
    // -- Card options:
    // ------------ // Map<String, Object> cardMap = new HashMap<>();
    // cardMap.put("name", cardHolderName);// - "Received both card and source parameters. Please pass in only one."
    /*
        cardMap.put("number", "4242424242424242");
        cardMap.put("exp_month", 12);
        cardMap.put("exp_year", 2020);
        */
    // -- Charge options:
    Map<String, Object> chargeMap = new HashMap<>();
    // -- this is ONE dollar
    chargeMap.put("amount", 100);
    chargeMap.put("currency", "usd");
    // see: https://stripe.com/docs/charges (Java)
    // chargeParams.put("source", token);
    // chargeParams.put("description", "Example charge");
    chargeMap.put("source", token);
    chargeMap.put("description", "Example charge: " + token);
    // 
    // ------------ // chargeMap.put("card", cardMap);
    // -- make charge:
    responseStr += "\"charge\":";
    try {
        Charge charge = Charge.create(chargeMap, requestOptions);
        // System.out.println(charge);
        responseStr += GSON.toJson(charge);
        LOG.warning(GSON.toJson(charge));
        ExternalAccount source = charge.getSource();
        LOG.warning("source: " + GSON.toJson(source));
        // -- "succeeded"
        charge.getStatus();
        // - boolean: true or false
        charge.getPaid();
    // 
    // String customerStr = source.getCustomer();
    // LOG.warning("customerStr: " + customerStr); //
    // responseStr += "," + "\"customerStr\":" + GSON.toJson(charge); // - same as source
    } catch (StripeException e) {
        String errorMessage = e.getMessage();
        responseStr += "\"";
        responseStr += errorMessage;
        responseStr += "\"";
        LOG.warning(errorMessage);
    }
    responseStr += "}";
    ServletUtils.sendJsonResponse(response, responseStr);
}
Also used : StripeException(com.stripe.exception.StripeException) RequestOptions(com.stripe.net.RequestOptions) HashMap(java.util.HashMap) Charge(com.stripe.model.Charge) ExternalAccount(com.stripe.model.ExternalAccount)

Example 2 with StripeException

use of com.stripe.exception.StripeException in project stripe-java by stripe.

the class ChargeTest method testPerCallAPIUsage.

@Test
public void testPerCallAPIUsage() throws StripeException {
    Charge createdCharge = Charge.create(defaultChargeParams, Stripe.apiKey);
    assertFalse(createdCharge.getRefunded());
    try {
        Charge.create(defaultChargeParams, "INVALID_KEY_HERE");
        fail();
    } catch (Exception e) {
    // An exception is expected, so do nothing.
    // (This test is pretty bad, but it's going away Soon™.)
    }
}
Also used : Charge(com.stripe.model.Charge) CardException(com.stripe.exception.CardException) InvalidRequestException(com.stripe.exception.InvalidRequestException) StripeException(com.stripe.exception.StripeException) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Aggregations

StripeException (com.stripe.exception.StripeException)2 Charge (com.stripe.model.Charge)2 BaseStripeFunctionalTest (com.stripe.BaseStripeFunctionalTest)1 CardException (com.stripe.exception.CardException)1 InvalidRequestException (com.stripe.exception.InvalidRequestException)1 ExternalAccount (com.stripe.model.ExternalAccount)1 RequestOptions (com.stripe.net.RequestOptions)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1