コード例 #1
0
 def execute_transaction(self, username: str, shopping_cart,
                         payment_method: str, payment_details,
                         address: DeliveryAddress, account_number: int):
     if not self.check_availability_of_products(shopping_cart):
         return MessageResponse(
             False, 1, "Not all the products are available in this amount")
     TransactionManagement.transaction_counter += 1
     transaction = Transaction(self.transaction_counter,
                               shopping_cart['products'], username,
                               payment_method, payment_details, address,
                               account_number,
                               shopping_cart['store_number'])
     cart = ShoppingCart(shopping_cart["store_number"],
                         shopping_cart['products'])
     # until here yet
     transaction.total_price = cart.calculate_price()
     if self.payment_system.charge(username, payment_details[0],
                                   payment_details[1], payment_details[2],
                                   payment_details[3],
                                   payment_details[4]) == 'declined':
         return MessageResponse(False, 1, "payment problem")
     transaction.update_payment_status()
     if self.delivery_system.delivery(transaction.products,
                                      address) == 'undelivered':
         return MessageResponse(False, 1,
                                'delivery problem, payment were cancelled')
     transaction.update_delivery_status()
     self.transactions.append(transaction)
     self.remove_products_from_store(shopping_cart)
     return MessageResponse(
         transaction, 1,
         'succeed transaction' + str(transaction.transaction_id))
コード例 #2
0
 def test_calculate_price(self):
     discount_service = DiscountService()
     buying_policy = BuyingPolicyService()
     store = Store('schnitzale', 23444, 4, discount_service, buying_policy)
     cart = ShoppingCart(store)
     product_a = Product('france schnitzel', 'schnitzel', ['hot', 'crispy'],
                         35)
     product_b = Product('israeli schnitzel', 'schnitzel',
                         ['hot', 'casher'], 15)
     store.add_new_product(product_a)
     store.inc_product_amount(product_a.catalog_number, 5)
     store.add_new_product(product_b)
     store.inc_product_amount(product_b.catalog_number, 5)
     cart.products[product_a.catalog_number] = {
         'product': product_a,
         'amount': 2
     }
     self.assertEqual(cart.calculate_price(), 70)
     cart.products[product_b.catalog_number] = {
         'product': product_b,
         'amount': 1
     }
     self.assertEqual(cart.calculate_price(), 85)