EntityManager em = JpaConnection.getInstance().getCurrentEntityManager();
OrderHeader orderHeader = (OrderHeader) em.find(OrderHeader.class, Format.getLong(form.getOrderHeaderId()));
getMissingFormInformation(form, adminBean.getSiteId());
String invoiceHeaderId = form.getInvoiceHeaderId();
InvoiceHeader invoiceHeader = null;
if (!Format.isNullOrEmpty(invoiceHeaderId)) {
invoiceHeader = (InvoiceHeader) em.find(InvoiceHeader.class, Format.getLong(form.getInvoiceHeaderId()));
}
InvoiceEngine invoiceEngine = null;
if (invoiceHeader == null) {
invoiceEngine = new InvoiceEngine(orderHeader, user);
}
else {
invoiceEngine = new InvoiceEngine(invoiceHeader, user);
}
invoiceEngine.setShippingTotal(0);
ActionMessages messages = validateOther(form);
if (messages.size() == 0) {
invoiceEngine.setShippingTotal(Format.getFloat(form.getInputShippingTotal()));
}
initOrder(form, orderHeader, invoiceHeader, null, null, request);
boolean isValid = validateInput(form, request);
if (!isValid) {
initOrder(form, orderHeader, null, null, null, request);
Long id = null;
if (!Format.isNullOrEmpty(invoiceHeaderId)) {
id = Format.getLong(invoiceHeaderId);
}
calcTotal(form, id, invoiceEngine);
form.setEditable(true);
form.setNewInvoice(true);
if (!Format.isNullOrEmpty(invoiceHeaderId)) {
form.setNewInvoice(false);
}
em.getTransaction().rollback();
ActionForward actionForward = actionMapping.findForward("error");
return actionForward;
}
saveInput(form, invoiceEngine);
try {
invoiceEngine.payOrder(request);
invoiceEngine.saveOrder();
OrderEngine orderEngine = new OrderEngine(orderHeader, user);
orderHeader.setOrderStatus(orderEngine.calcStatus(orderHeader));
}
catch (OrderStateException e) {
initOrder(form, orderHeader, invoiceHeader, null, null, request);
calcTotal(form, invoiceHeader.getInvoiceHeaderId(), invoiceEngine);
ActionMessages errors = new ActionMessages();
errors.add("invoice", new ActionMessage("error.invoice.capture"));
ActionForward actionForward = actionMapping.findForward("error");
saveMessages(request, errors);
form.setAllowCapture(true);
form.setEditable(true);
form.setNewInvoice(false);
form.setAllowCredit(false);
em.getTransaction().rollback();
return actionForward;
}
catch (PaymentException e) {
initOrder(form, orderHeader, invoiceHeader, null, null, request);
calcTotal(form, invoiceHeader.getInvoiceHeaderId(), invoiceEngine);
ActionMessages errors = new ActionMessages();
errors.add("invoice", new ActionMessage("error.invoice.capture", e.getMessage()));
ActionForward actionForward = actionMapping.findForward("error");
saveMessages(request, errors);
form.setAllowCapture(true);
form.setEditable(true);
form.setNewInvoice(false);
form.setAllowCredit(false);
em.getTransaction().rollback();
return actionForward;
}
catch (AuthorizationException e) {
initOrder(form, orderHeader, invoiceHeader, null, null, request);
calcTotal(form, invoiceHeader.getInvoiceHeaderId(), invoiceEngine);
ActionMessages errors = new ActionMessages();
errors.add("invoice", new ActionMessage("error.invoice.capture", e.getMessage()));
ActionForward actionForward = actionMapping.findForward("error");
saveMessages(request, errors);
form.setAllowCapture(true);
form.setEditable(true);
form.setNewInvoice(false);
form.setAllowCredit(false);
em.getTransaction().rollback();
return actionForward;
}
catch (NotImplementedException e) {
initOrder(form, orderHeader, invoiceHeader, null, null, request);
calcTotal(form, invoiceHeader.getInvoiceHeaderId(), invoiceEngine);
ActionMessages errors = new ActionMessages();
errors.add("invoice", new ActionMessage("error.payment.notImplemeted", e.getMessage()));
ActionForward actionForward = actionMapping.findForward("error");
saveMessages(request, errors);
form.setAllowCapture(true);