public OrderRepresentation(DateTimeZone tenantZone, Order order) { this.slug = order.getSlug(); if (order.getCreationDate() != null) { this.creationDate = new DateTime(order.getCreationDate().getTime(), tenantZone); } if (order.getUpdateDate() != null) { this.updateDate = new DateTime(order.getUpdateDate().getTime(), tenantZone); } this.currency = order.getCurrency(); this.numberOfItems = order.getNumberOfItems(); this.itemsTotal = order.getItemsTotal(); this.shipping = order.getShipping(); this.grandTotal = order.getGrandTotal(); this.status = order.getStatus(); this.additionalInformation = order.getAdditionalInformation(); this.data = order.getOrderData(); if (order.getBillingAddress() != null) { this.setBillingAddress(new AddressRepresentation(order.getBillingAddress())); } if (order.getDeliveryAddress() != null) { this.setDeliveryAddress(new AddressRepresentation(order.getDeliveryAddress())); } if (order.getCustomer() != null) { this.setCustomer(new CustomerRepresentation(order.getCustomer())); } }
@POST @Path("{orderId}/payment") public Object internalPayment(@PathParam("orderId") UUID orderId, MultivaluedMap<String, String> data) { try { Order order = orderStore.get().findById(orderId); Map<PaymentData, Object> paymentData = Maps.newHashMap(); paymentData.put(BasePaymentData.CURRENCY, order.getCurrency()); paymentData.put(BasePaymentData.ORDER_ID, order.getId()); paymentData.put(BasePaymentData.CUSTOMER, order.getCustomer()); if (order.getBillingAddress() != null) { paymentData.put(BasePaymentData.BILLING_ADDRESS, order.getBillingAddress()); } paymentData.put(BasePaymentData.DELIVERY_ADDRESS, order.getDeliveryAddress()); paymentData.put(BasePaymentData.ORDER, order); if (data.containsKey("cardNumber")) { paymentData.put(CreditCardPaymentData.CARD_NUMBER, data.getFirst("cardNumber")); paymentData.put(CreditCardPaymentData.HOLDER_NAME, data.getFirst("holderName")); paymentData.put(CreditCardPaymentData.EXPIRATION_MONTH, data.getFirst("expiryMonth")); paymentData.put(CreditCardPaymentData.EXPIRATION_YEAR, data.getFirst("expiryYear")); paymentData.put(CreditCardPaymentData.VERIFICATION_CODE, data.getFirst("cvv")); } PaymentRequest paymentRequest = paymentProcessor.requestPayment(order, paymentData); CheckoutResponse response = new CheckoutResponse(order, paymentRequest); return generateCheckoutResponse(response); } catch (Exception e) { return renderError(e.getMessage()); } }
@Override public Order create(@Valid Order order) throws EntityAlreadyExistsException, InvalidEntityException { this.dao.begin(); String slug = String.format("%08d", lastOrderNumber() + 1); order.setSlug(slug); order.setId(UUID.randomUUID()); this.dao.createEntity(order, ORDER_TABLE_NAME, getTenant()); this.dao.createOrder(order); for (OrderItem item : order.getOrderItems()) { item.setId(UUID.randomUUID()); item.setOrderId(order.getId()); } this.dao.insertOrderItems(order.getOrderItems()); this.dao.commit(); return order; }
@Override public void onEvent(Event event, Object source, Object data) { final Order order = (Order) source; Map<String, Object> payload = Maps.newHashMap(); payload.put("customer", order.getCustomer().getEmail()); payload.put("order", order.getSlug()); webhooks.notifyHook(new OrderPaid(), payload); } }
throws EntityAlreadyExistsException, InvalidEntityException { Order order; order = new Order(); order.setBillingAddressId(billingAddressId); order.setDeliveryAddressId(deliveryAddressId); order.setCustomerId(customerId); if (extraOrderData.containsKey("additionalInformation")) { order.setAdditionalInformation((String) extraOrderData.get("additionalInformation")); webContext )).toList(); order.setItemsTotal(cart.itemsTotal().incl()); order.setItemsTotalExcl(cart.itemsTotal().excl()); order.setOrderItems(items); order.setNumberOfItems(Long.valueOf(items.size())); final Carrier carrier = shippingService.getCarrier( cart.selectedShippingOption().get().getCarrierId()); order.setShipping(cart.selectedShippingOption().get().getPrice().incl()); order.setShippingExcl(cart.selectedShippingOption().get().getPrice().excl()); data.put(Order.ORDER_DATA_SHIPPING, new HashMap<String, Object>() order.setCreationDate(new Date()); order.setUpdateDate(order.getCreationDate()); order.setCurrency(cart.currency()); order.setStatus(Order.Status.NONE); order.setGrandTotal(cart.total().incl()); order.setGrandTotalExcl(cart.total().excl());
Optional<Address> da, Tenant tenant, Locale locale) List<OrderItem> items = order.getOrderItems(); Map<String, Object> context = Maps.newHashMap(); .toFormatter(); CurrencyUnit currencyUnit = CurrencyUnit.of(order.getCurrency()); String grandTotal = formatter.withLocale(locale) .print(Money.of(currencyUnit, order.getGrandTotal(), RoundingMode.HALF_EVEN)); String itemsTotal = formatter.withLocale(locale) .print(Money.of(currencyUnit, order.getItemsTotal(), RoundingMode.HALF_EVEN)); if (order.getShipping() != null) { String shippingTotal = formatter.withLocale(locale) .print(Money.of(currencyUnit, order.getShipping(), RoundingMode.HALF_EVEN)); context.put("shippingTotal", shippingTotal); context.put("shipping", order.getOrderData().get("shipping")); context.put("orderId", order.getSlug()); context.put("grandTotal", grandTotal); context.put("additionalInformation", order.getAdditionalInformation()); context.put("orderDate", new DateWebObject().withDate(order.getCreationDate(), locale));
List<OrderItem> items = order.getOrderItems(); Map<String, Object> context = Maps.newHashMap(); .toFormatter(); CurrencyUnit currencyUnit = CurrencyUnit.of(order.getCurrency()); String grandTotal = formatter.withLocale(locale) .print(Money.of(currencyUnit, order.getGrandTotal(), RoundingMode.HALF_EVEN)); String itemsTotal = formatter.withLocale(locale) .print(Money.of(currencyUnit, order.getItemsTotal(), RoundingMode.HALF_EVEN)); if (order.getShipping() != null) { String shippingTotal = formatter.withLocale(locale) .print(Money.of(currencyUnit, order.getShipping(), RoundingMode.HALF_EVEN)); context.put("shippingTotal", shippingTotal); context.put("shipping", order.getOrderData().get("shipping")); context.put("orderId", order.getSlug()); context.put("grandTotal", grandTotal);
protected Map<String, Object> prepareInvoiceContext(final Order order) throws InvoicingException { Map<String, Object> result = Maps.newHashMap(); final Tenant tenant = tenantStore.get().findById(order.getTenantId()); GeneralSettings settings = configurationService.getSettings(GeneralSettings.class, tenant); final InvoicingSettings invoicingSettings = configurationService.getSettings(InvoicingSettings.class, tenant); FluentIterable.from(order.getOrderItems()).transform(new Function<OrderItem, OrderItemWebObject>() result.put("order", orderWebObject); if (order.getCustomer() != null) { CustomerWebObject customerWebObject = new CustomerWebObject(); customerWebObject.withCustomer(order.getCustomer()); if (order.getDeliveryAddress() != null) { customerWebObject.withDeliveryAddress(order.getDeliveryAddress()); if (order.getBillingAddress() != null) { customerWebObject.withBillingAddress(order.getBillingAddress());
Order order = new Order(); order.setId((UUID) data.getObject("entity_id")); orderItem.setOrderId(order.getId()); order.setOrderData(orderData); orders.add(order); updateOrders.setObject(1, mapper.writeValueAsString(order.getOrderData())); updateOrders.setObject(2, order.getId()); updateOrders.addBatch();
@Override public Order map(int index, ResultSet resultSet, StatementContext ctx) throws SQLException Order order = new Order(); fillOrderSummary(resultSet, order); order.setOrderItems(items); } catch (IOException e) { logger.error("Failed to deserialize order data", e); resultSet.findColumn("email"); Customer customer = new Customer(); customer.setId(order.getCustomerId()); customer.setSlug(resultSet.getString("customer_slug")); customer.setEmail(resultSet.getString("email")); customer.setPhoneNumber(resultSet.getString("phone_number")); customer.setCompany(resultSet.getString("company")); order.setCustomer(customer); } catch (SQLException e) { billing.setCountry(resultSet.getString("billing_address_country")); billing.setNote(resultSet.getString("billing_address_note")); order.setBillingAddress(billing); delivery.setCountry(resultSet.getString("delivery_address_country")); delivery.setNote(resultSet.getString("delivery_address_note")); order.setDeliveryAddress(delivery);
"payment/" + order.getId() + "/acknowledgement/" + gatewayId)); GatewayResponse gatewayResponse = gateway.purchase(order.getGrandTotal(), data); order.setStatus(Order.Status.PAID); } else if (gatewayResponse.getOperation().getResult().equals(PaymentOperation.Result.REFUSED)){ builder.withStatus(PaymentStatus.REFUSED); order.setStatus(Order.Status.PAYMENT_PENDING); operation.setOrderId(order.getId()); paymentOperationStore.get().create(operation);
@GET @Path(PAYMENT_RETURN_PATH + "/{order}") public WebView returnFromPaymentService(@Context UriInfo uriInfo, @PathParam("order") String orderId) { Map<String, Object> bindings = new HashMap<>(); if (StringUtils.isNotBlank(orderId)) { Order order = orderStore.get().findById(UUID.fromString(orderId)); if (order != null) { Optional<Address> da = order.getDeliveryAddress() != null ? Optional.fromNullable(order.getDeliveryAddress()) : Optional.<Address>absent(); Optional<Address> ba = order.getBillingAddress() != null ? Optional.fromNullable(order.getBillingAddress()) : Optional.<Address>absent(); bindings.putAll( prepareContext(order, order.getCustomer(), ba, da, webContext.getTenant(), configurationService.getSettings(GeneralSettings.class).getLocales().getMainLocale() .getValue())); } } return new WebView().template("checkout/success.html").with(WebView.Option.FALLBACK_ON_DEFAULT_THEME) .data(bindings); }
@Override public void update(@Valid Order order) throws EntityDoesNotExistException, InvalidEntityException { this.dao.begin(); Order originalOrder = this.findBySlug(order.getSlug()); if (originalOrder == null) { this.dao.commit(); throw new EntityDoesNotExistException(); } order.setId(originalOrder.getId()); Integer updatedRows = this.dao.updateOrder(order); this.dao.commit(); if (updatedRows <= 0) { throw new StoreException("No rows was updated when updating order"); } }
order.setStatus(Order.Status.PAID); orderStore.get().update(order); getObservationManager().notify(new OrderPaidEvent(), order, order.getOrderData()); } catch (Exception e) { this.logger.error("Failed to update order status", e);
@Override public void delete(@Valid Order entity) throws EntityDoesNotExistException { Integer updatedRows = 0; this.dao.begin(); updatedRows += this.dao.deleteEntityEntityById(ORDER_TABLE_NAME, entity.getId()); updatedRows += this.dao.detachChildren(entity.getId()); updatedRows += this.dao.deleteEntityAndChildrenById(entity.getId()); this.dao.commit(); if (updatedRows <= 0) { throw new EntityDoesNotExistException("No rows was updated when trying to delete order"); } }
@Override public void onEvent(Event event, Object source, Object data) { // Update stocks for bought products Order order = (Order) source; List<OrderItem> items = order.getOrderItems(); for (OrderItem item : items) { UUID itemId = item.getPurchasableId(); if (itemId == null) { return; } Long quantity = item.getQuantity(); try { Product product = productStore.get().findById(itemId); if (product.getStock() != null) { productStore.get().updateStock(itemId, -quantity.intValue()); } else if (product.getParentId() != null) { Product parent = productStore.get().findById(product.getParentId()); productStore.get().updateStock(product.getParentId(), -quantity.intValue()); } } catch (EntityDoesNotExistException e) { // Ignore, there is just no stock to update } } } }
@Path("{slug}") @POST @Authorized // Partial update : NOT idempotent public Response updateOrder(@PathParam("slug") String slug, OrderRepresentation updatedOrderRepresentation) { try { Order order = this.orderStore.get().findBySlug(slug); if (order == null) { return Response.status(404).build(); } else { order.setStatus(updatedOrderRepresentation.getStatus()); this.orderStore.get().update(order); return Response.ok().build(); } } catch (InvalidEntityException e) { return Response.status(Response.Status.BAD_REQUEST).build(); } catch (EntityDoesNotExistException e) { return Response.status(Response.Status.NOT_FOUND) .entity("No product with this slug could be found\n").type(MediaType.TEXT_PLAIN_TYPE).build(); } } }
@Override public Map<Integer, List<Order>> getOrdersForCustomer(Company company) { List<Order> orders = orderDao.findAll(company); Map<Integer, List<Order>> map = new HashMap<>(); for(Order order: orders){ int customerId = order.getCustomerId(); if(!map.containsKey(customerId)){ map.put(customerId, new ArrayList<Order>()); } map.get(customerId).add(order); } return map; }
public OrderItemWebObject apply(OrderItem orderItem) { return new OrderItemWebObject().withOrderItem(orderItem, order.getCurrency(), locale); } }).toList());
Customer customer = order.getCustomer(); if (order.getBillingAddress() != null) { billingAddress = Optional.of(order.getBillingAddress()); } else { billingAddress = Optional.absent(); if (order.getDeliveryAddress() != null) { deliveryAddress = Optional.of(order.getDeliveryAddress()); } else { deliveryAddress = Optional.absent();