def _create_items(self, customer, products): items = list() for x, product_info in enumerate(products): if isinstance(product_info, tuple): product_code, product_count = product_info else: product_code = product_info product_count = None product = Product.get_by_code(product_code) order_item = OrderItemTO() order_item.comment = product.default_comment(customer.language) order_item.count = product.default_count if product_count is None else product_count if product_code == u'MSUP' and product_count is None: order_item.count *= 2 order_item.number = x order_item.product = product_code items.append(order_item) return items
def test_recurrent_billing(self): self.set_datastore_hr_probability(1) products_to_order = [(u'MSUP', 12), (Product.PRODUCT_BEACON, 1)] subscription_order, customer = self._create_customer_and_subscription_order(products_to_order) self._create_service(customer) # Turn back next_charge_date more than 12 months subscription_order.next_charge_date -= 367 * 86400 subscription_order.put() # execute recurrent billing code _create_charge(subscription_order.key(), now(), Product.get_products_dict()) # check if a ShopTask was created task = ShopTask.all().get() self.assertEqual(task.type, ShopTask.TYPE_SUPPORT_NEEDED) task.delete() # Check if an expiredsubscription was created expired_subscription = ExpiredSubscription.get_by_customer_id(customer.id) self.assertIsNotNone(expired_subscription) # first set the expired subscription as 'customer will link his credit card' set_expired_subscription_status(customer.id, ExpiredSubscription.STATUS_WILL_LINK_CREDIT_CARD) task = ShopTask.all().get() self.assertIsNotNone(task) self.assertEqual(task.type, ShopTask.TYPE_CHECK_CREDIT_CARD) task.delete() # extend the customer's (expired) subscription charge = set_expired_subscription_status(customer.id, ExpiredSubscription.STATUS_EXTEND_SUBSCRIPTION) subscription_order, \ expired_subscription = db.get((Order.create_key(customer.id, customer.subscription_order_number), ExpiredSubscription.create_key(customer.id))) self.assertEqual(expired_subscription, None) product_subscription = Product.get_by_code(u'MSUP') charge_total = product_subscription.price * 12 # subscription extensions should always be 12 months long self.assertIsNotNone(charge) self.assertEqual(Charge.TYPE_SUBSCRIPTION_EXTENSION, charge.type) self.assertEqual(charge_total, charge.amount) one_year_from_now_plus_one_day = datetime.datetime.utcfromtimestamp(now()) + relativedelta.relativedelta( months=12, days=1) one_year_from_now_minus_one_day = datetime.datetime.utcfromtimestamp(now()) + relativedelta.relativedelta( months=12, days=-1) self.assertLess(subscription_order.next_charge_date, int(one_year_from_now_plus_one_day.strftime('%s'))) self.assertGreater(subscription_order.next_charge_date, int(one_year_from_now_minus_one_day.strftime('%s')))
def _create_customer_and_subscription_order(self, products): customer = create_or_update_customer( self.current_user, customer_id=None, vat=u'BE4863 456 123', name=u'Test customer', address1=u'', address2=None, zip_code=u'9080', city=u'Lochristi', country=u'BE', language=DEFAULT_LANGUAGE, organization_type=OrganizationType.PROFIT, prospect_id=None, team_id=RegioManagerTeam.all().get().id) contact = create_contact(customer.id, u'Bart', u'example', u'*****@*****.**', u'+32 9 324 25 64') items = list() for x, product_info in enumerate(products): if isinstance(product_info, tuple): product_code, product_count = product_info else: product_code = product_info product_count = None product = Product.get_by_code(product_code) order_item = OrderItemTO() order_item.comment = product.default_comment(customer.language) order_item.count = product.default_count if product_count is None else product_count if product_code == u'MSUP' and product_count is None: order_item.count *= 2 order_item.number = x order_item.product = product_code items.append(order_item) order = create_order(customer.id, contact.id, items) sign_order(customer.id, order.order_number, 'image/png,%s' % base64.b64encode(UNKNOWN_AVATAR)) return db.get((order.key(), customer.key()))