Example #1
0
  @RequestMapping(value = "/order/create", method = RequestMethod.POST)
  public ResponseEntity<Response> createOrder(
      @RequestBody MerchantOrder order, UriComponentsBuilder ucBuilder) {

    Response response = new Response();
    try {
      validate(order);
      orderService.add(order);
      Payment payment = new Payment();
      payment.setUrl("http://localhost:8080/safeguard/payment/create/");
      payment.setAmount(order.getAmount());
      paymentService.add(payment);
      response.setPaymentId(payment.getId());
      response.setPaymentUrl(payment.getUrl() + payment.getId());
      response.setSuccess(true);
      return new ResponseEntity<Response>(response, HttpStatus.CREATED);
    } catch (Exception ex) {
      response.setSuccess(false);

      for (String message : ex.getMessage().split(System.getProperty("line.separator"))) {
        response.addMessage(message);
      }
      return new ResponseEntity<Response>(response, HttpStatus.BAD_REQUEST);
    }
  }
Example #2
0
  private void validate(MerchantOrder order) throws Exception {
    authenticateMerchant(order);

    StringBuilder sb = new StringBuilder();
    Calendar maxTimestamp = Calendar.getInstance();
    Calendar minTimestamp = Calendar.getInstance();

    maxTimestamp.add(Calendar.SECOND, 10);
    minTimestamp.add(Calendar.SECOND, -10);
    /*
    if(order.getTimestamp() == null ||
       order.getTimestamp().before(minTimestamp.getTime()) ||
       order.getTimestamp().after(maxTimestamp.getTime()))
    {
    	sb.append("Invalid timestamp. Please, make sure that your clock is in sync with server clock.");
    	sb.append(System.getProperty("line.separator"));
    }*/
    if (!(order.getAmount() > 0)) {
      sb.append("Invalid amount value! Please, provide number greater then zero.");
      sb.append(System.getProperty("line.separator"));
    }

    if (sb.length() > 0) {
      throw new Exception(sb.toString());
    }
  }
Example #3
0
  private void authenticateMerchant(MerchantOrder order) throws Exception {
    Merchant merchant = order.getMerchant();
    StringBuilder errorList = new StringBuilder();
    validateMerchant(merchant);

    Merchant dbMerchant = merchantService.getByIdentifier(merchant.getIdentifier());

    if (dbMerchant == null) {
      errorList.append("Merchant with provided identifier does not exist!");
    } else if (!merchant.getPassword().equals(dbMerchant.getPassword())) {
      errorList.append("Invalid password");
    }

    merchant.setId(dbMerchant.getId());

    if (errorList.length() > 0) {
      throw new Exception(errorList.toString());
    }
  }