use of com.salesmanager.core.model.payments.Transaction in project shopizer by shopizer-ecommerce.
the class BraintreePayment method authorizeAndCapture.
@Override
public Transaction authorizeAndCapture(MerchantStore store, Customer customer, List<ShoppingCartItem> items, BigDecimal amount, Payment payment, IntegrationConfiguration configuration, IntegrationModule module) throws IntegrationException {
Validate.notNull(configuration, "Configuration cannot be null");
String merchantId = configuration.getIntegrationKeys().get("merchant_id");
String publicKey = configuration.getIntegrationKeys().get("public_key");
String privateKey = configuration.getIntegrationKeys().get("private_key");
Validate.notNull(merchantId, "merchant_id cannot be null");
Validate.notNull(publicKey, "public_key cannot be null");
Validate.notNull(privateKey, "private_key cannot be null");
// paymentToken
String nonce = payment.getPaymentMetaData().get("paymentToken");
if (StringUtils.isBlank(nonce)) {
IntegrationException te = new IntegrationException("Can't process Braintree, missing authorization nounce");
te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
te.setMessageCode("message.payment.error");
te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
throw te;
}
Environment environment = Environment.PRODUCTION;
if (configuration.getEnvironment().equals("TEST")) {
// sandbox
environment = Environment.SANDBOX;
}
BraintreeGateway gateway = new BraintreeGateway(environment, merchantId, publicKey, privateKey);
TransactionRequest request = new TransactionRequest().amount(amount).paymentMethodNonce(nonce);
Result<com.braintreegateway.Transaction> result = gateway.transaction().sale(request);
String trxId = null;
if (result.isSuccess()) {
com.braintreegateway.Transaction transaction = result.getTarget();
trxId = transaction.getId();
} else {
String errorString = "";
for (ValidationError error : result.getErrors().getAllDeepValidationErrors()) {
errorString += "Error: " + error.getCode() + ": " + error.getMessage() + "\n";
}
IntegrationException te = new IntegrationException("Can't process Braintree auth + capture " + errorString);
te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
te.setMessageCode("message.payment.error");
te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
throw te;
}
if (StringUtils.isBlank(trxId)) {
IntegrationException te = new IntegrationException("Can't process Braintree, missing trxId");
te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
te.setMessageCode("message.payment.error");
te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
throw te;
}
Transaction trx = new Transaction();
trx.setAmount(amount);
trx.setTransactionDate(new Date());
trx.setTransactionType(TransactionType.AUTHORIZECAPTURE);
trx.setPaymentType(PaymentType.CREDITCARD);
trx.getTransactionDetails().put("TRANSACTIONID", trxId);
trx.getTransactionDetails().put("TRNAPPROVED", null);
trx.getTransactionDetails().put("TRNORDERNUMBER", trxId);
trx.getTransactionDetails().put("MESSAGETEXT", null);
return trx;
}
use of com.salesmanager.core.model.payments.Transaction in project shopizer by shopizer-ecommerce.
the class BraintreePayment method capture.
@Override
public Transaction capture(MerchantStore store, Customer customer, Order order, Transaction capturableTransaction, IntegrationConfiguration configuration, IntegrationModule module) throws IntegrationException {
Validate.notNull(configuration, "Configuration cannot be null");
String merchantId = configuration.getIntegrationKeys().get("merchant_id");
String publicKey = configuration.getIntegrationKeys().get("public_key");
String privateKey = configuration.getIntegrationKeys().get("private_key");
Validate.notNull(merchantId, "merchant_id cannot be null");
Validate.notNull(publicKey, "public_key cannot be null");
Validate.notNull(privateKey, "private_key cannot be null");
String auth = capturableTransaction.getTransactionDetails().get("TRANSACTIONID");
if (StringUtils.isBlank(auth)) {
IntegrationException te = new IntegrationException("Can't process Braintree, missing authorization id");
te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
te.setMessageCode("message.payment.error");
te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
throw te;
}
Environment environment = Environment.PRODUCTION;
if (configuration.getEnvironment().equals("TEST")) {
// sandbox
environment = Environment.SANDBOX;
}
BraintreeGateway gateway = new BraintreeGateway(environment, merchantId, publicKey, privateKey);
BigDecimal amount = order.getTotal();
Result<com.braintreegateway.Transaction> result = gateway.transaction().submitForSettlement(auth, amount);
String trxId = null;
if (result.isSuccess()) {
com.braintreegateway.Transaction settledTransaction = result.getTarget();
trxId = settledTransaction.getId();
} else {
String errorString = "";
for (ValidationError error : result.getErrors().getAllDeepValidationErrors()) {
errorString += "Error: " + error.getCode() + ": " + error.getMessage() + "\n";
}
IntegrationException te = new IntegrationException("Can't process Braintree refund " + errorString);
te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
te.setMessageCode("message.payment.error");
te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
throw te;
}
if (StringUtils.isBlank(trxId)) {
IntegrationException te = new IntegrationException("Can't process Braintree, missing original transaction");
te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
te.setMessageCode("message.payment.error");
te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
throw te;
}
Transaction trx = new Transaction();
trx.setAmount(amount);
trx.setTransactionDate(new Date());
trx.setTransactionType(TransactionType.CAPTURE);
trx.setPaymentType(PaymentType.CREDITCARD);
trx.getTransactionDetails().put("TRANSACTIONID", trxId);
trx.getTransactionDetails().put("TRNAPPROVED", null);
trx.getTransactionDetails().put("TRNORDERNUMBER", trxId);
trx.getTransactionDetails().put("MESSAGETEXT", null);
return trx;
}
use of com.salesmanager.core.model.payments.Transaction in project shopizer by shopizer-ecommerce.
the class BraintreePayment method authorize.
@Override
public Transaction authorize(MerchantStore store, Customer customer, List<ShoppingCartItem> items, BigDecimal amount, Payment payment, IntegrationConfiguration configuration, IntegrationModule module) throws IntegrationException {
Validate.notNull(configuration, "Configuration cannot be null");
String merchantId = configuration.getIntegrationKeys().get("merchant_id");
String publicKey = configuration.getIntegrationKeys().get("public_key");
String privateKey = configuration.getIntegrationKeys().get("private_key");
Validate.notNull(merchantId, "merchant_id cannot be null");
Validate.notNull(publicKey, "public_key cannot be null");
Validate.notNull(privateKey, "private_key cannot be null");
String nonce = payment.getPaymentMetaData().get("paymentToken");
if (StringUtils.isBlank(nonce)) {
IntegrationException te = new IntegrationException("Can't process Braintree, missing authorization nounce");
te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
te.setMessageCode("message.payment.error");
te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
throw te;
}
Environment environment = Environment.PRODUCTION;
if (configuration.getEnvironment().equals("TEST")) {
// sandbox
environment = Environment.SANDBOX;
}
BraintreeGateway gateway = new BraintreeGateway(environment, merchantId, publicKey, privateKey);
TransactionRequest request = new TransactionRequest().amount(amount).paymentMethodNonce(nonce);
Result<com.braintreegateway.Transaction> result = gateway.transaction().sale(request);
String authorizationId = null;
if (result.isSuccess()) {
com.braintreegateway.Transaction transaction = result.getTarget();
authorizationId = transaction.getId();
} else if (result.getTransaction() != null) {
com.braintreegateway.Transaction transaction = result.getTransaction();
authorizationId = transaction.getAuthorizedTransactionId();
} else {
String errorString = "";
for (ValidationError error : result.getErrors().getAllDeepValidationErrors()) {
errorString += "Error: " + error.getCode() + ": " + error.getMessage() + "\n";
}
IntegrationException te = new IntegrationException("Can't process Braintree authorization " + errorString);
te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
te.setMessageCode("message.payment.error");
te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
throw te;
}
if (StringUtils.isBlank(authorizationId)) {
IntegrationException te = new IntegrationException("Can't process Braintree, missing authorizationId");
te.setExceptionType(IntegrationException.TRANSACTION_EXCEPTION);
te.setMessageCode("message.payment.error");
te.setErrorCode(IntegrationException.TRANSACTION_EXCEPTION);
throw te;
}
Transaction trx = new Transaction();
trx.setAmount(amount);
trx.setTransactionDate(new Date());
trx.setTransactionType(TransactionType.AUTHORIZE);
trx.setPaymentType(PaymentType.CREDITCARD);
trx.getTransactionDetails().put("TRANSACTIONID", authorizationId);
trx.getTransactionDetails().put("TRNAPPROVED", null);
trx.getTransactionDetails().put("TRNORDERNUMBER", authorizationId);
trx.getTransactionDetails().put("MESSAGETEXT", null);
return trx;
}
use of com.salesmanager.core.model.payments.Transaction in project shopizer by shopizer-ecommerce.
the class PaymentServiceImpl method processRefund.
@Override
public Transaction processRefund(Order order, Customer customer, MerchantStore store, BigDecimal amount) throws ServiceException {
Validate.notNull(customer);
Validate.notNull(store);
Validate.notNull(amount);
Validate.notNull(order);
Validate.notNull(order.getOrderTotal());
BigDecimal orderTotal = order.getTotal();
if (amount.doubleValue() > orderTotal.doubleValue()) {
throw new ServiceException("Invalid amount, the refunded amount is greater than the total allowed");
}
String module = order.getPaymentModuleCode();
Map<String, IntegrationConfiguration> modules = this.getPaymentModulesConfigured(store);
if (modules == null) {
throw new ServiceException("No payment module configured");
}
IntegrationConfiguration configuration = modules.get(module);
if (configuration == null) {
throw new ServiceException("Payment module " + module + " is not configured");
}
PaymentModule paymentModule = this.paymentModules.get(module);
if (paymentModule == null) {
throw new ServiceException("Payment module " + paymentModule + " does not exist");
}
boolean partial = false;
if (amount.doubleValue() != order.getTotal().doubleValue()) {
partial = true;
}
IntegrationModule integrationModule = getPaymentMethodByCode(store, module);
// get the associated transaction
Transaction refundable = transactionService.getRefundableTransaction(order);
if (refundable == null) {
throw new ServiceException("No refundable transaction for this order");
}
Transaction transaction = paymentModule.refund(partial, store, refundable, order, amount, configuration, integrationModule);
transaction.setOrder(order);
transactionService.create(transaction);
OrderTotal refund = new OrderTotal();
refund.setModule(Constants.OT_REFUND_MODULE_CODE);
refund.setText(Constants.OT_REFUND_MODULE_CODE);
refund.setTitle(Constants.OT_REFUND_MODULE_CODE);
refund.setOrderTotalCode(Constants.OT_REFUND_MODULE_CODE);
refund.setOrderTotalType(OrderTotalType.REFUND);
refund.setValue(amount);
refund.setSortOrder(100);
refund.setOrder(order);
order.getOrderTotal().add(refund);
// update order total
orderTotal = orderTotal.subtract(amount);
// update ordertotal refund
Set<OrderTotal> totals = order.getOrderTotal();
for (OrderTotal total : totals) {
if (total.getModule().equals(Constants.OT_TOTAL_MODULE_CODE)) {
total.setValue(orderTotal);
}
}
order.setTotal(orderTotal);
order.setStatus(OrderStatus.REFUNDED);
OrderStatusHistory orderHistory = new OrderStatusHistory();
orderHistory.setOrder(order);
orderHistory.setStatus(OrderStatus.REFUNDED);
orderHistory.setDateAdded(new Date());
order.getOrderHistory().add(orderHistory);
orderService.saveOrUpdate(order);
return transaction;
}
use of com.salesmanager.core.model.payments.Transaction in project shopizer by shopizer-ecommerce.
the class TransactionServiceImpl method getCapturableTransaction.
@Override
public Transaction getCapturableTransaction(Order order) throws ServiceException {
List<Transaction> transactions = transactionRepository.findByOrder(order.getId());
ObjectMapper mapper = new ObjectMapper();
Transaction capturable = null;
for (Transaction transaction : transactions) {
if (transaction.getTransactionType().name().equals(TransactionType.AUTHORIZE.name())) {
if (!StringUtils.isBlank(transaction.getDetails())) {
try {
@SuppressWarnings("unchecked") Map<String, String> objects = mapper.readValue(transaction.getDetails(), Map.class);
transaction.setTransactionDetails(objects);
capturable = transaction;
} catch (Exception e) {
throw new ServiceException(e);
}
}
}
if (transaction.getTransactionType().name().equals(TransactionType.CAPTURE.name())) {
break;
}
if (transaction.getTransactionType().name().equals(TransactionType.REFUND.name())) {
break;
}
}
return capturable;
}
Aggregations