@Test
  public void testSingleItemDiscountPromotion() throws Exception {

    final PromotionService promotionService =
        ctx().getBean("promotionService", PromotionService.class);

    MutableShoppingCart shoppingCart = new ShoppingCartImpl();
    shoppingCart.initialise(
        ctx().getBean("amountCalculationStrategy", AmountCalculationStrategy.class));
    final ShoppingCartCommandFactory commands =
        ctx().getBean("shoppingCartCommandFactory", ShoppingCartCommandFactory.class);

    // basic init
    commands.execute(shoppingCart, (Map) singletonMap(ShoppingCartCommand.CMD_SETSHOP, 10));
    commands.execute(
        shoppingCart, (Map) singletonMap(ShoppingCartCommand.CMD_CHANGECURRENCY, "EUR"));

    // create discount promotion
    final Promotion amount50 =
        promotionService.getGenericDao().getEntityFactory().getByIface(Promotion.class);
    amount50.setCode("ORDER_50");
    amount50.setShopCode(shoppingCart.getShoppingContext().getShopCode());
    amount50.setCurrency("EUR");
    amount50.setName("50 off on orders over 200");
    amount50.setPromoType(Promotion.TYPE_ORDER);
    amount50.setPromoAction(Promotion.ACTION_FIXED_AMOUNT_OFF);
    amount50.setEligibilityCondition("shoppingCartItemTotal.priceSubTotal > 200.00");
    amount50.setPromoActionContext("50");
    amount50.setEnabled(true);

    promotionService.create(amount50);

    try {
      // add qualifying items
      Map<String, String> param = new HashMap<String, String>();
      param.put(ShoppingCartCommand.CMD_SETQTYSKU, "CC_TEST4");
      param.put(ShoppingCartCommand.CMD_SETQTYSKU_P_QTY, "2.00");
      commands.execute(shoppingCart, (Map) param);

      assertEquals(1, shoppingCart.getCartItemList().size());

      final CartItem cc_test4 = shoppingCart.getCartItemList().get(0);

      assertEquals("CC_TEST4", cc_test4.getProductSkuCode());
      assertFalse(cc_test4.isPromoApplied());
      assertNull(cc_test4.getAppliedPromo());
      assertEquals("2", cc_test4.getQty().toString());
      assertEquals("123.00", cc_test4.getListPrice().toString());
      assertEquals("123.00", cc_test4.getSalePrice().toString());
      assertEquals("123.00", cc_test4.getPrice().toString());

      assertEquals("246.00", shoppingCart.getTotal().getListSubTotal().toString());
      assertEquals("196.00", shoppingCart.getTotal().getSubTotal().toString());
      assertTrue(shoppingCart.getTotal().isOrderPromoApplied());
      assertEquals("ORDER_50", shoppingCart.getTotal().getAppliedOrderPromo());
    } finally {
      // clean test
      promotionService.delete(amount50);
    }
  }
  @Test
  public void testHandlePaymentCallbackOutOfStock() throws Exception {
    Customer customer = createCustomer();
    ShoppingCart shoppingCart = getShoppingCart2(customer.getEmail());

    final ShoppingCartCommandFactory commands =
        ctx().getBean("shoppingCartCommandFactory", ShoppingCartCommandFactory.class);
    Map<String, String> param = new HashMap<String, String>();
    param = new HashMap<String, String>();
    param.put(ShoppingCartCommand.CMD_SETQTYSKU, "CC_TEST1");
    param.put(ShoppingCartCommand.CMD_SETQTYSKU_P_QTY, "20000.00");
    commands.execute(shoppingCart, (Map) param);

    CustomerOrder customerOrder = orderAssembler.assembleCustomerOrder(shoppingCart);
    customerOrder = deliveryAssembler.assembleCustomerOrder(customerOrder, shoppingCart, true);
    customerOrder.setPgLabel("testExtFormPaymentGatewayLabel");
    customerOrder = customerOrderService.create(customerOrder);
    assertEquals(
        "Order must be in ORDER_STATUS_NONE state",
        CustomerOrder.ORDER_STATUS_NONE,
        customerOrder.getOrderStatus());
    final String ordGuid = customerOrder.getCartGuid();
    ShopCodeContext.setShopCode(customerOrder.getShop().getCode());
    paymentCallBackHandlerFacade.handlePaymentCallback(
        new HashMap<String, String>() {
          {
            put(TestExtFormPaymentGatewayImpl.ORDER_GUID_PARAM_KEY, ordGuid);
            put(TestExtFormPaymentGatewayImpl.RESPONSE_CODE_PARAM_KEY, "1"); // 1 - means ok
          }
        },
        "testExtFormPaymentGatewayLabel");
    ShopCodeContext.clear();
    customerOrder = customerOrderService.findByGuid(customerOrder.getCartGuid());
    assertEquals(
        "Order must be in ORDER_STATUS_CANCELLED state", // because item is out of stock
        CustomerOrder.ORDER_STATUS_CANCELLED,
        customerOrder.getOrderStatus());

    final List<CustomerOrderPayment> payments =
        customerOrderPaymentService.findBy(customerOrder.getOrdernum(), null, null, null);
    assertNotNull(payments);
    assertEquals(payments.size(), 2);
    assertEquals(PaymentGateway.AUTH_CAPTURE, payments.get(0).getTransactionOperation());
    assertEquals("1", payments.get(0).getTransactionOperationResultCode());
    assertEquals(PaymentGateway.REFUND, payments.get(1).getTransactionOperation());
    assertEquals("1", payments.get(1).getTransactionOperationResultCode());
  }
 /** Execute logout command. */
 protected void executeLogoutCommand() {
   shoppingCartCommandFactory.execute(
       ShoppingCartCommand.CMD_LOGIN,
       cartMixin.getCurrentCart(),
       new HashMap<String, Object>() {
         {
           put(ShoppingCartCommand.CMD_LOGOUT, ShoppingCartCommand.CMD_LOGOUT);
         }
       });
 }
 /**
  * Execute login command.
  *
  * @param email customer.
  * @param password password.
  */
 protected void executeLoginCommand(final String email, final String password) {
   shoppingCartCommandFactory.execute(
       ShoppingCartCommand.CMD_LOGIN,
       cartMixin.getCurrentCart(),
       new HashMap<String, Object>() {
         {
           put(ShoppingCartCommand.CMD_LOGIN_P_EMAIL, email);
           put(ShoppingCartCommand.CMD_LOGIN_P_PASS, password);
           put(ShoppingCartCommand.CMD_LOGIN, ShoppingCartCommand.CMD_LOGIN);
         }
       });
 }
 /** Execute password reset command. */
 protected boolean executePasswordResetCommand(final String token) {
   try {
     shoppingCartCommandFactory.execute(
         ShoppingCartCommand.CMD_RESET_PASSWORD,
         cartMixin.getCurrentCart(),
         new HashMap<String, Object>() {
           {
             put(ShoppingCartCommand.CMD_RESET_PASSWORD, token);
           }
         });
     return true;
   } catch (BadCredentialsException bce) {
     return false;
   }
 }
Exemplo n.º 6
0
 /** Clean shopping cart end prepare it to reusing. */
 private void cleanCart() {
   shoppingCartCommandFactory.execute(
       ShoppingCartCommand.CMD_CLEAN,
       ApplicationDirector.getShoppingCart(),
       Collections.singletonMap(ShoppingCartCommand.CMD_CLEAN, null));
 }