def job(): products = Product.get_products_dict() paying_subscription_product_codes = [ code for code, p in products.iteritems() if p.is_subscription and p.price and '_' not in p.code ] run_job(_qry, [Order.STATUS_SIGNED], _replace_subscription_order, [products, paying_subscription_product_codes])
def test_change_order_to_free(self): self.set_datastore_hr_probability(1) products_to_order = [(u'MSUP', 12), # monthly subscription, $50.00 (u'KSUP', 12), # subscription discount, -$15.00 (Product.PRODUCT_EXTRA_CITY, 12), # extra city, $5.00 (Product.PRODUCT_EXTRA_CITY, 12), # extra city, $5.00 (Product.PRODUCT_EXTRA_CITY, 12)] # extra city, $5.00 old_subscription_order, customer = self._create_customer_and_subscription_order(products_to_order) old_subscription_order.next_charge_date -= 12 * 31 * 86400 # Turn back next_charge_date more than 12 months old_subscription_order.put() self._create_service(customer, [u'be-loc', u'be-berlare', u'be-beveren', u'es-madrid', App.APP_ID_ROGERTHAT, App.APP_ID_OSA_LOYALTY]) # Creating some random order with a discount to be sure this discount isn't applied in recurrent billing job self._create_order(customer, old_subscription_order.contact_id, self._create_items(customer, [(u'KLUP', 1), (u'LSUP', 1)])) products_to_order = [(Product.PRODUCT_FREE_SUBSCRIPTION, 1), (Product.PRODUCT_EXTRA_CITY, 1), # extra city, $5.00 (Product.PRODUCT_EXTRA_CITY, 1), # extra city, $5.00 (Product.PRODUCT_EXTRA_CITY, 1)] # extra city, $5.00 order_items = self._create_items(customer, products_to_order) new_subscription_order, customer = self._create_order(customer, old_subscription_order.contact_id, order_items, replace=True) new_subscription_order.next_charge_date -= 12 * 31 * 86400 # Turn back next_charge_date more than 12 months new_subscription_order.put() # Execute recurrent billing code products = Product.get_products_dict() charge = _create_charge(new_subscription_order.key(), now(), products) self.assertIsNotNone(charge) expected_charge_amount = 3 * products[Product.PRODUCT_EXTRA_CITY].price # 3x $5.00 self.assertEqual(expected_charge_amount, charge.amount) def trans_invoice_number(): return InvoiceNumber.next(get_mobicage_legal_entity()) invoice_number = run_in_transaction(trans_invoice_number) create_invoice(customer.id, new_subscription_order.order_number, charge.id, invoice_number, new_subscription_order.manager, payment_type=Invoice.PAYMENT_ON_SITE) dt = datetime.datetime.utcnow() invoices = export_invoices(dt.year, dt.month) self.assertEqual(1, len(invoices)) self.assertEqual(len(products_to_order), len(invoices[0]['order_items'])) order_items_cost = sum(order_item['price'] * order_item['count'] for order_item in invoices[0]['order_items']) self.assertEqual(expected_charge_amount, order_items_cost)
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 _recurrent_billing(): today = now() products = Product.get_products_dict() run_job(_qry, [today], _create_charge, [today, products])