Search in sources :

Example 16 with RequestOptions

use of com.stripe.net.RequestOptions 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 17 with RequestOptions

use of com.stripe.net.RequestOptions in project cryptonomica by Cryptonomica.

the class TestStripe method makeCharge2.

// end makeCharge1
public void makeCharge2() {
    // see:
    // https://github.com/stripe/stripe-java#usage
    RequestOptions requestOptions = (new RequestOptions.RequestOptionsBuilder()).setApiKey("YOUR-SECRET-KEY").build();
    Map<String, Object> chargeMap = new HashMap<String, Object>();
    chargeMap.put("amount", 100);
    chargeMap.put("currency", "usd");
    Map<String, Object> cardMap = new HashMap<String, Object>();
    cardMap.put("number", "4242424242424242");
    cardMap.put("exp_month", 12);
    cardMap.put("exp_year", 2020);
    chargeMap.put("card", cardMap);
    try {
        Charge charge = Charge.create(chargeMap, requestOptions);
        System.out.println(charge);
    } catch (StripeException e) {
        e.printStackTrace();
    }
}
Also used : RequestOptions(com.stripe.net.RequestOptions) HashMap(java.util.HashMap)

Example 18 with RequestOptions

use of com.stripe.net.RequestOptions in project alf.io by alfio-event.

the class BaseStripeManager method charge.

protected Optional<Charge> charge(PaymentSpecification spec, Map<String, Object> chargeParams) throws StripeException {
    Optional<RequestOptions> opt = options(spec.getPurchaseContext(), builder -> builder.setIdempotencyKey(spec.getReservationId()));
    if (opt.isEmpty()) {
        return Optional.empty();
    }
    RequestOptions options = opt.get();
    Charge charge = Charge.create(chargeParams, options);
    if (charge.getBalanceTransactionObject() == null) {
        try {
            charge.setBalanceTransactionObject(retrieveBalanceTransaction(charge.getBalanceTransaction(), options));
        } catch (Exception e) {
            log.warn("can't retrieve balance transaction", e);
        }
    }
    return Optional.of(charge);
}
Also used : RequestOptions(com.stripe.net.RequestOptions) Charge(com.stripe.model.Charge)

Example 19 with RequestOptions

use of com.stripe.net.RequestOptions in project alf.io by alfio-event.

the class BaseStripeManager method refund.

// https://stripe.com/docs/api#create_refund
boolean refund(Transaction transaction, PurchaseContext purchaseContext, Integer amountToRefund) {
    Optional<Integer> amount = Optional.ofNullable(amountToRefund);
    String chargeId = transaction.getTransactionId();
    try {
        String amountOrFull = amount.map(p -> MonetaryUtil.formatCents(p, transaction.getCurrency())).orElse("full");
        log.info("Stripe: trying to do a refund for payment {} with amount: {}", chargeId, amountOrFull);
        Map<String, Object> params = new HashMap<>();
        params.put("charge", chargeId);
        amount.ifPresent(a -> params.put("amount", a));
        if (transaction.getPlatformFee() > 0 && isConnectEnabled(new PaymentContext(purchaseContext))) {
            params.put("refund_application_fee", true);
        }
        Optional<RequestOptions> requestOptionsOptional = options(purchaseContext);
        if (requestOptionsOptional.isPresent()) {
            RequestOptions options = requestOptionsOptional.get();
            Refund r = Refund.create(params, options);
            boolean pending = PENDING.equals(r.getStatus());
            if (SUCCEEDED.equals(r.getStatus()) || pending) {
                log.info("Stripe: refund for payment {} {} for amount: {}", chargeId, pending ? "registered" : "executed with success", amountOrFull);
                return true;
            } else {
                log.warn("Stripe: was not able to refund payment with id {}, returned status is not 'succeded' but {}", chargeId, r.getStatus());
                return false;
            }
        }
        return false;
    } catch (StripeException e) {
        log.warn("Stripe: was not able to refund payment with id " + chargeId, e);
        return false;
    }
}
Also used : Transaction(alfio.model.transaction.Transaction) RequestOptions(com.stripe.net.RequestOptions) java.util(java.util) PaymentProxy(alfio.model.transaction.PaymentProxy) PaymentResult(alfio.manager.support.PaymentResult) PurchaseContext(alfio.model.PurchaseContext) PurchaseContextType(alfio.model.PurchaseContext.PurchaseContextType) UnaryOperator(java.util.function.UnaryOperator) ConfigurationManager(alfio.manager.system.ConfigurationManager) PaymentInformation(alfio.model.PaymentInformation) Charge(com.stripe.model.Charge) Profiles(org.springframework.core.env.Profiles) Configurable(alfio.model.Configurable) Refund(com.stripe.model.Refund) TicketRepository(alfio.repository.TicketRepository) Predicate(java.util.function.Predicate) FeeCalculator(alfio.manager.support.FeeCalculator) PaymentMethod(alfio.model.transaction.PaymentMethod) Stripe(com.stripe.Stripe) ErrorsCode(alfio.util.ErrorsCode) ConfigurationRepository(alfio.repository.system.ConfigurationRepository) PaymentContext(alfio.model.transaction.PaymentContext) MonetaryUtil(alfio.util.MonetaryUtil) Environment(org.springframework.core.env.Environment) UserManager(alfio.manager.user.UserManager) Log4j2(lombok.extern.log4j.Log4j2) ConfigurationPathLevel(alfio.model.system.ConfigurationPathLevel) com.stripe.exception(com.stripe.exception) AllArgsConstructor(lombok.AllArgsConstructor) ConfigurationKeys(alfio.model.system.ConfigurationKeys) BalanceTransaction(com.stripe.model.BalanceTransaction) Webhook(com.stripe.net.Webhook) RequestOptions(com.stripe.net.RequestOptions) PaymentContext(alfio.model.transaction.PaymentContext) Refund(com.stripe.model.Refund)

Example 20 with RequestOptions

use of com.stripe.net.RequestOptions in project stripe-java by stripe.

the class DisputeTest method testSubmitOldStyleEvidence.

@Test
public void testSubmitOldStyleEvidence() throws StripeException, InterruptedException {
    RequestOptions options = RequestOptions.builder().setStripeVersion("2014-11-20").build();
    int chargeValueCents = 100;
    // Stripe.apiVersion = "2014-11-20";
    Charge disputedCharge = createDisputedCharge(chargeValueCents, options);
    String myEvidence = "Here's evidence showing this charge is legitimate.";
    Dispute initialDispute = disputedCharge.getDisputeObject();
    assertNull(initialDispute.getEvidence());
    assertNull(initialDispute.getEvidenceSubObject());
    Map<String, Object> disputeParams = ImmutableMap.<String, Object>of("evidence", myEvidence);
    Dispute updatedDispute = disputedCharge.updateDispute(disputeParams, options);
    assertNotNull(updatedDispute);
    assertEquals(myEvidence, updatedDispute.getEvidence());
    assertNull(updatedDispute.getEvidenceSubObject());
}
Also used : RequestOptions(com.stripe.net.RequestOptions) Charge(com.stripe.model.Charge) Dispute(com.stripe.model.Dispute) EvidenceSubObject(com.stripe.model.EvidenceSubObject) BaseStripeFunctionalTest(com.stripe.BaseStripeFunctionalTest) Test(org.junit.Test)

Aggregations

RequestOptions (com.stripe.net.RequestOptions)29 Test (org.junit.Test)19 BaseStripeFunctionalTest (com.stripe.BaseStripeFunctionalTest)12 Charge (com.stripe.model.Charge)12 HashMap (java.util.HashMap)11 BaseStripeTest (com.stripe.BaseStripeTest)5 RequestOptionsBuilder (com.stripe.net.RequestOptions.RequestOptionsBuilder)4 PaymentInformation (alfio.model.PaymentInformation)3 DeletedProduct (com.stripe.model.DeletedProduct)2 DeletedSKU (com.stripe.model.DeletedSKU)2 Product (com.stripe.model.Product)2 Refund (com.stripe.model.Refund)2 SKU (com.stripe.model.SKU)2 FeeCalculator (alfio.manager.support.FeeCalculator)1 PaymentResult (alfio.manager.support.PaymentResult)1 ConfigurationManager (alfio.manager.system.ConfigurationManager)1 UserManager (alfio.manager.user.UserManager)1 Configurable (alfio.model.Configurable)1 PurchaseContext (alfio.model.PurchaseContext)1 PurchaseContextType (alfio.model.PurchaseContext.PurchaseContextType)1