Search in sources :

Example 31 with IntegrationException

use of com.salesmanager.core.modules.integration.IntegrationException in project shopizer by shopizer-ecommerce.

the class Stripe3Payment method authorize.

/* -----------------------------------------  */
@Override
public Transaction authorize(MerchantStore store, Customer customer, List<ShoppingCartItem> items, BigDecimal amount, Payment payment, IntegrationConfiguration configuration, IntegrationModule module) throws IntegrationException {
    Transaction transaction = new Transaction();
    try {
        String apiKey = configuration.getIntegrationKeys().get("secretKey");
        if (payment.getPaymentMetaData() == null || StringUtils.isBlank(apiKey)) {
            IntegrationException te = new IntegrationException("Can't process Stripe, missing payment.metaData");
            te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
            te.setMessageCode("message.payment.error");
            te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
            throw te;
        }
        /**
         * This is the PaymentIntent ID created on the Frontend, that we will now store.
         */
        String token = payment.getPaymentMetaData().get("stripe_token");
        if (StringUtils.isBlank(token)) {
            IntegrationException te = new IntegrationException("Can't process Stripe, missing stripe token");
            te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
            te.setMessageCode("message.payment.error");
            te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
            throw te;
        }
        Stripe.apiKey = apiKey;
        PaymentIntent paymentIntent = PaymentIntent.retrieve(token);
        transaction.setAmount(amount);
        // transaction.setOrder(order);
        transaction.setTransactionDate(new Date());
        transaction.setTransactionType(TransactionType.AUTHORIZE);
        transaction.setPaymentType(PaymentType.CREDITCARD);
        transaction.getTransactionDetails().put("TRANSACTIONID", token);
        transaction.getTransactionDetails().put("TRNAPPROVED", paymentIntent.getStatus());
        // <---- We store the PI id here
        transaction.getTransactionDetails().put("TRNORDERNUMBER", paymentIntent.getId());
        transaction.getTransactionDetails().put("MESSAGETEXT", null);
    } catch (Exception e) {
        throw buildException(e);
    }
    return transaction;
}
Also used : Transaction(com.salesmanager.core.model.payments.Transaction) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException) PaymentIntent(com.stripe.model.PaymentIntent) Date(java.util.Date) CardException(com.stripe.exception.CardException) InvalidRequestException(com.stripe.exception.InvalidRequestException) AuthenticationException(com.stripe.exception.AuthenticationException) StripeException(com.stripe.exception.StripeException) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException)

Example 32 with IntegrationException

use of com.salesmanager.core.modules.integration.IntegrationException in project shopizer by shopizer-ecommerce.

the class Stripe3Payment method capture.

@Override
public Transaction capture(MerchantStore store, Customer customer, Order order, Transaction capturableTransaction, IntegrationConfiguration configuration, IntegrationModule module) throws IntegrationException {
    Transaction transaction = new Transaction();
    try {
        String apiKey = configuration.getIntegrationKeys().get("secretKey");
        if (StringUtils.isBlank(apiKey)) {
            IntegrationException te = new IntegrationException("Can't process Stripe, missing payment.metaData");
            te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
            te.setMessageCode("message.payment.error");
            te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
            throw te;
        }
        // <---- We retrieve the PI id here
        String chargeId = capturableTransaction.getTransactionDetails().get("TRNORDERNUMBER");
        if (StringUtils.isBlank(chargeId)) {
            IntegrationException te = new IntegrationException("Can't process Stripe capture, missing TRNORDERNUMBER");
            te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
            te.setMessageCode("message.payment.error");
            te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
            throw te;
        }
        String amnt = productPriceUtils.getAdminFormatedAmount(store, order.getTotal());
        String strAmount = String.valueOf(amnt);
        strAmount = strAmount.replace(".", "");
        Stripe.apiKey = apiKey;
        PaymentIntent paymentIntent = PaymentIntent.retrieve(chargeId);
        PaymentIntentCaptureParams params = PaymentIntentCaptureParams.builder().setAmountToCapture(Long.parseLong(strAmount)).setStatementDescriptor(store.getStorename().length() > 22 ? store.getStorename().substring(0, 22) : store.getStorename()).build();
        paymentIntent = paymentIntent.capture(params);
        transaction.setAmount(order.getTotal());
        transaction.setOrder(order);
        transaction.setTransactionDate(new Date());
        transaction.setTransactionType(TransactionType.CAPTURE);
        transaction.setPaymentType(PaymentType.CREDITCARD);
        transaction.getTransactionDetails().put("TRANSACTIONID", capturableTransaction.getTransactionDetails().get("TRANSACTIONID"));
        transaction.getTransactionDetails().put("TRNAPPROVED", paymentIntent.getStatus());
        transaction.getTransactionDetails().put("TRNORDERNUMBER", paymentIntent.getId());
        transaction.getTransactionDetails().put("MESSAGETEXT", null);
        return transaction;
    } catch (Exception e) {
        throw buildException(e);
    }
}
Also used : Transaction(com.salesmanager.core.model.payments.Transaction) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException) PaymentIntent(com.stripe.model.PaymentIntent) PaymentIntentCaptureParams(com.stripe.param.PaymentIntentCaptureParams) Date(java.util.Date) CardException(com.stripe.exception.CardException) InvalidRequestException(com.stripe.exception.InvalidRequestException) AuthenticationException(com.stripe.exception.AuthenticationException) StripeException(com.stripe.exception.StripeException) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException)

Example 33 with IntegrationException

use of com.salesmanager.core.modules.integration.IntegrationException in project shopizer by shopizer-ecommerce.

the class Stripe3Payment method buildException.

private IntegrationException buildException(Exception ex) {
    if (ex instanceof CardException) {
        CardException e = (CardException) ex;
        // Since it's a decline, CardException will be caught
        // System.out.println("Status is: " + e.getCode());
        // System.out.println("Message is: " + e.getMessage());
        /**
         *				invalid_number 	The card number is not a valid credit card number.
         *				invalid_expiry_month 	The card's expiration month is invalid.
         *				invalid_expiry_year 	The card's expiration year is invalid.
         *				invalid_cvc 	The card's security code is invalid.
         *				incorrect_number 	The card number is incorrect.
         *				expired_card 	The card has expired.
         *				incorrect_cvc 	The card's security code is incorrect.
         *				incorrect_zip 	The card's zip code failed validation.
         *				card_declined 	The card was declined.
         *				missing 	There is no card on a customer that is being charged.
         *				processing_error 	An error occurred while processing the card.
         *				rate_limit 	An error occurred due to requests hitting the API too quickly. Please let us know if you're consistently running into this error.
         */
        String declineCode = e.getDeclineCode();
        if ("card_declined".equals(declineCode)) {
            IntegrationException te = new IntegrationException("Can't process stripe message " + e.getMessage());
            te.setExceptionType(IntegrationException.EXCEPTION_PAYMENT_DECLINED);
            te.setMessageCode("message.payment.declined");
            te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
            return te;
        }
        if ("invalid_number".equals(declineCode)) {
            IntegrationException te = new IntegrationException("Can't process stripe message " + e.getMessage());
            te.setExceptionType(IntegrationException.EXCEPTION_VALIDATION);
            te.setMessageCode("messages.error.creditcard.number");
            te.setErrorCode(IntegrationException.EXCEPTION_VALIDATION);
            return te;
        }
        if ("invalid_expiry_month".equals(declineCode)) {
            IntegrationException te = new IntegrationException("Can't process stripe message " + e.getMessage());
            te.setExceptionType(IntegrationException.EXCEPTION_VALIDATION);
            te.setMessageCode("messages.error.creditcard.dateformat");
            te.setErrorCode(IntegrationException.EXCEPTION_VALIDATION);
            return te;
        }
        if ("invalid_expiry_year".equals(declineCode)) {
            IntegrationException te = new IntegrationException("Can't process stripe message " + e.getMessage());
            te.setExceptionType(IntegrationException.EXCEPTION_VALIDATION);
            te.setMessageCode("messages.error.creditcard.dateformat");
            te.setErrorCode(IntegrationException.EXCEPTION_VALIDATION);
            return te;
        }
        if ("invalid_cvc".equals(declineCode)) {
            IntegrationException te = new IntegrationException("Can't process stripe message " + e.getMessage());
            te.setExceptionType(IntegrationException.EXCEPTION_VALIDATION);
            te.setMessageCode("messages.error.creditcard.cvc");
            te.setErrorCode(IntegrationException.EXCEPTION_VALIDATION);
            return te;
        }
        if ("incorrect_number".equals(declineCode)) {
            IntegrationException te = new IntegrationException("Can't process stripe message " + e.getMessage());
            te.setExceptionType(IntegrationException.EXCEPTION_VALIDATION);
            te.setMessageCode("messages.error.creditcard.number");
            te.setErrorCode(IntegrationException.EXCEPTION_VALIDATION);
            return te;
        }
        if ("incorrect_cvc".equals(declineCode)) {
            IntegrationException te = new IntegrationException("Can't process stripe message " + e.getMessage());
            te.setExceptionType(IntegrationException.EXCEPTION_VALIDATION);
            te.setMessageCode("messages.error.creditcard.cvc");
            te.setErrorCode(IntegrationException.EXCEPTION_VALIDATION);
            return te;
        }
        // nothing good so create generic error
        IntegrationException te = new IntegrationException("Can't process stripe card  " + e.getMessage());
        te.setExceptionType(IntegrationException.EXCEPTION_VALIDATION);
        te.setMessageCode("messages.error.creditcard.number");
        te.setErrorCode(IntegrationException.EXCEPTION_VALIDATION);
        return te;
    } else if (ex instanceof InvalidRequestException) {
        LOGGER.error("InvalidRequest error with stripe", ex.getMessage());
        InvalidRequestException e = (InvalidRequestException) ex;
        IntegrationException te = new IntegrationException("Can't process Stripe, missing invalid payment parameters");
        te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
        te.setMessageCode("messages.error.creditcard.number");
        te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
        return te;
    } else if (ex instanceof AuthenticationException) {
        LOGGER.error("Authentication error with stripe", ex.getMessage());
        AuthenticationException e = (AuthenticationException) ex;
        // Authentication with Stripe's API failed
        // (maybe you changed API keys recently)
        IntegrationException te = new IntegrationException("Can't process Stripe, missing invalid payment parameters");
        te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
        te.setMessageCode("message.payment.error");
        te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
        return te;
    } else /*else if (ex instanceof APIConnectionException) {
		LOGGER.error("API connection error with stripe", ex.getMessage());
		APIConnectionException e = (APIConnectionException)ex;
		  // Network communication with Stripe failed
		IntegrationException te = new IntegrationException(
				"Can't process Stripe, missing invalid payment parameters");
		te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
		te.setMessageCode("message.payment.error");
		te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
		return te;
	} */
    if (ex instanceof StripeException) {
        LOGGER.error("Error with stripe", ex.getMessage());
        StripeException e = (StripeException) ex;
        // Display a very generic error to the user, and maybe send
        // yourself an email
        IntegrationException te = new IntegrationException("Can't process Stripe authorize, missing invalid payment parameters");
        te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
        te.setMessageCode("message.payment.error");
        te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
        return te;
    } else if (ex instanceof Exception) {
        LOGGER.error("Stripe module error", ex.getMessage());
        if (ex instanceof IntegrationException) {
            return (IntegrationException) ex;
        } else {
            IntegrationException te = new IntegrationException("Can't process Stripe authorize, exception", ex);
            te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
            te.setMessageCode("message.payment.error");
            te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
            return te;
        }
    } else {
        LOGGER.error("Stripe module error", ex.getMessage());
        IntegrationException te = new IntegrationException("Can't process Stripe authorize, exception", ex);
        te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
        te.setMessageCode("message.payment.error");
        te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
        return te;
    }
}
Also used : StripeException(com.stripe.exception.StripeException) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException) AuthenticationException(com.stripe.exception.AuthenticationException) InvalidRequestException(com.stripe.exception.InvalidRequestException) CardException(com.stripe.exception.CardException) CardException(com.stripe.exception.CardException) InvalidRequestException(com.stripe.exception.InvalidRequestException) AuthenticationException(com.stripe.exception.AuthenticationException) StripeException(com.stripe.exception.StripeException) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException)

Example 34 with IntegrationException

use of com.salesmanager.core.modules.integration.IntegrationException in project shopizer by shopizer-ecommerce.

the class StripePayment method capture.

@Override
public Transaction capture(MerchantStore store, Customer customer, Order order, Transaction capturableTransaction, IntegrationConfiguration configuration, IntegrationModule module) throws IntegrationException {
    Transaction transaction = new Transaction();
    try {
        String apiKey = configuration.getIntegrationKeys().get("secretKey");
        if (StringUtils.isBlank(apiKey)) {
            IntegrationException te = new IntegrationException("Can't process Stripe, missing payment.metaData");
            te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
            te.setMessageCode("message.payment.error");
            te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
            throw te;
        }
        String chargeId = capturableTransaction.getTransactionDetails().get("TRNORDERNUMBER");
        if (StringUtils.isBlank(chargeId)) {
            IntegrationException te = new IntegrationException("Can't process Stripe capture, missing TRNORDERNUMBER");
            te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
            te.setMessageCode("message.payment.error");
            te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
            throw te;
        }
        Stripe.apiKey = apiKey;
        Charge ch = Charge.retrieve(chargeId);
        ch.capture();
        transaction.setAmount(order.getTotal());
        transaction.setOrder(order);
        transaction.setTransactionDate(new Date());
        transaction.setTransactionType(TransactionType.CAPTURE);
        transaction.setPaymentType(PaymentType.CREDITCARD);
        transaction.getTransactionDetails().put("TRANSACTIONID", capturableTransaction.getTransactionDetails().get("TRANSACTIONID"));
        transaction.getTransactionDetails().put("TRNAPPROVED", ch.getStatus());
        transaction.getTransactionDetails().put("TRNORDERNUMBER", ch.getId());
        transaction.getTransactionDetails().put("MESSAGETEXT", null);
        return transaction;
    } catch (Exception e) {
        throw buildException(e);
    }
}
Also used : Transaction(com.salesmanager.core.model.payments.Transaction) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException) Charge(com.stripe.model.Charge) Date(java.util.Date) CardException(com.stripe.exception.CardException) InvalidRequestException(com.stripe.exception.InvalidRequestException) AuthenticationException(com.stripe.exception.AuthenticationException) StripeException(com.stripe.exception.StripeException) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException)

Example 35 with IntegrationException

use of com.salesmanager.core.modules.integration.IntegrationException in project shopizer by shopizer-ecommerce.

the class StripePayment method authorizeAndCapture.

@Override
public Transaction authorizeAndCapture(MerchantStore store, Customer customer, List<ShoppingCartItem> items, BigDecimal amount, Payment payment, IntegrationConfiguration configuration, IntegrationModule module) throws IntegrationException {
    String apiKey = configuration.getIntegrationKeys().get("secretKey");
    if (payment.getPaymentMetaData() == null || StringUtils.isBlank(apiKey)) {
        IntegrationException te = new IntegrationException("Can't process Stripe, missing payment.metaData");
        te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
        te.setMessageCode("message.payment.error");
        te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
        throw te;
    }
    String token = payment.getPaymentMetaData().get("stripe_token");
    if (StringUtils.isBlank(token)) {
        // possibly from api
        // from api
        token = payment.getPaymentMetaData().get("paymentToken");
    }
    if (StringUtils.isBlank(token)) {
        IntegrationException te = new IntegrationException("Can't process Stripe, missing stripe token");
        te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
        te.setMessageCode("message.payment.error");
        te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
        throw te;
    }
    Transaction transaction = new Transaction();
    try {
        String amnt = productPriceUtils.getAdminFormatedAmount(store, amount);
        // stripe does not support floating point
        // so amnt * 100 or remove floating point
        // 553.47 = 55347
        String strAmount = String.valueOf(amnt);
        strAmount = strAmount.replace(".", "");
        Map<String, Object> chargeParams = new HashMap<String, Object>();
        chargeParams.put("amount", strAmount);
        chargeParams.put("capture", true);
        chargeParams.put("currency", store.getCurrency().getCode());
        // obtained with Stripe.js
        chargeParams.put("source", token);
        chargeParams.put("description", new StringBuilder().append(TRANSACTION).append(" - ").append(store.getStorename()).toString());
        Stripe.apiKey = apiKey;
        Charge ch = Charge.create(chargeParams);
        // Map<String,String> metadata = ch.getMetadata();
        transaction.setAmount(amount);
        // transaction.setOrder(order);
        transaction.setTransactionDate(new Date());
        transaction.setTransactionType(TransactionType.AUTHORIZECAPTURE);
        transaction.setPaymentType(PaymentType.CREDITCARD);
        transaction.getTransactionDetails().put("TRANSACTIONID", token);
        transaction.getTransactionDetails().put("TRNAPPROVED", ch.getStatus());
        transaction.getTransactionDetails().put("TRNORDERNUMBER", ch.getId());
        transaction.getTransactionDetails().put("MESSAGETEXT", null);
    } catch (Exception e) {
        throw buildException(e);
    }
    return transaction;
}
Also used : IntegrationException(com.salesmanager.core.modules.integration.IntegrationException) Transaction(com.salesmanager.core.model.payments.Transaction) HashMap(java.util.HashMap) Charge(com.stripe.model.Charge) Date(java.util.Date) CardException(com.stripe.exception.CardException) InvalidRequestException(com.stripe.exception.InvalidRequestException) AuthenticationException(com.stripe.exception.AuthenticationException) StripeException(com.stripe.exception.StripeException) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException)

Aggregations

IntegrationException (com.salesmanager.core.modules.integration.IntegrationException)42 Date (java.util.Date)18 Transaction (com.salesmanager.core.model.payments.Transaction)17 AuthenticationException (com.stripe.exception.AuthenticationException)11 CardException (com.stripe.exception.CardException)11 InvalidRequestException (com.stripe.exception.InvalidRequestException)11 StripeException (com.stripe.exception.StripeException)11 HashMap (java.util.HashMap)10 ServiceException (com.salesmanager.core.business.exception.ServiceException)9 ArrayList (java.util.ArrayList)8 ModuleConfig (com.salesmanager.core.model.system.ModuleConfig)5 PaymentIntent (com.stripe.model.PaymentIntent)5 BraintreeGateway (com.braintreegateway.BraintreeGateway)4 Environment (com.braintreegateway.Environment)4 ValidationError (com.braintreegateway.ValidationError)4 ShippingOption (com.salesmanager.core.model.shipping.ShippingOption)4 IntegrationConfiguration (com.salesmanager.core.model.system.IntegrationConfiguration)4 MerchantConfiguration (com.salesmanager.core.model.system.MerchantConfiguration)4 ShippingQuoteModule (com.salesmanager.core.modules.integration.shipping.model.ShippingQuoteModule)4 Charge (com.stripe.model.Charge)4