def calc_expire_date(self, date=None): if date is None: date = timezone.now() if self.subscription.expire_unit == "DAY": expiredate = date + timezone.timedelta(days=self.expire_length) else: expiredate = add_month(date, n=self.expire_length) return expiredate
def calc_expire_date(self, date=None): if date is None: date = timezone.now() if self.expire_unit == "DAY": expiredate = date + timezone.timedelta(days=self.expire_length) else: expiredate = add_month(date, n=self.expire_length) return expiredate
def get_recurring_charge_data(self, testing=False): """Build the list of dictionaries needed to process a recurring charge. Because Authorize can only take one subscription at a time, we build a list of the transaction dictionaries, for later sequential posting. """ if not self.arb_enabled: return [] # get all subscriptions from the order subscriptions = self.get_recurring_orderitems() if len(subscriptions) == 0: self.log_extra('No subscription items') return [] settings = self.settings # set up the base dictionary trans = {} if self.is_live(): conn = settings.ARB_CONNECTION.value self.log_extra('Using live recurring charge connection.') else: conn = settings.ARB_CONNECTION_TEST.value self.log_extra('Using test recurring charge connection.') shop_config = Config.objects.get_current() trans['connection'] = conn trans['config'] = { 'merchantID' : settings.LOGIN.value, 'transactionKey' : settings.TRANKEY.value, 'shop_name' : shop_config.store_name, } trans['order'] = self.order trans['card'] = self.order.credit_card start_date = timezone.now().date() if self.order.credit_card: trans['card_expiration'] = "%4i-%02i" % (self.order.credit_card.expire_year, self.order.credit_card.expire_month) translist = [] taxer = get_tax_processor(user = self.order.contact.user) for subscription in subscriptions: product = subscription.product subtrans = trans.copy() subtrans['subscription'] = subscription subtrans['product'] = product sub = product.subscriptionproduct trial = sub.get_trial_terms(0) if trial: price = trunc_decimal(trial.price, 2) trial_amount = price if price and subscription.product.taxable: trial_amount = taxer.by_price(subscription.product.taxClass, price) #todo, maybe add shipping for trial? amount = sub.recurring_price() trial_occurrences = trial.occurrences if not trial_occurrences: self.log.warn("Trial expiration period is less than one recurring billing cycle. " + "Authorize does not allow this, so the trial period has been adjusted to be equal to one recurring cycle.") trial_occurrences = 1 # add discounts for trial_amount and amount for recurring subscriptions if subscription.discount: discount = Discount.objects.by_code(self.order.discount_code) if discount.amount: amount = amount - discount.amount if amount > discount.amount else Decimal("0") trial_amount = trial_amount - discount.amount if trial_amount > discount.amount else Decimal("0") elif discount.percentage: discount_result = amount * discount.percentage / 100 amount -= discount_result.quantize(Decimal("0.01")) discount_result = trial_amount * discount.percentage / 100 trial_amount -= discount_result.quantize(Decimal("0.01")) else: trial_occurrences = 0 trial_amount = Decimal('0.00') amount = subscription.total_with_tax occurrences = sub.recurring_times + trial_occurrences if occurrences > 9999: occurrences = 9999 subtrans['occurrences'] = occurrences subtrans['trial_occurrences'] = trial_occurrences subtrans['start_date'] = start_date if trial and not trial_amount: if sub.expire_unit == "DAY": subtrans['start_date'] = start_date + timezone.timedelta(trial.expire_length) else: subtrans['start_date'] = add_month(start_date, n=trial.expire_length) trial = None subtrans['trial'] = trial subtrans['trial_amount'] = trunc_decimal(trial_amount, 2) subtrans['amount'] = trunc_decimal(amount, 2) if trial: charged_today = trial_amount else: charged_today = amount charged_today = trunc_decimal(charged_today, 2) subtrans['charged_today'] = charged_today if trial_amount or amount: translist.append(subtrans) return translist
def get_recurring_charge_data(self, testing=False): """Build the list of dictionaries needed to process a recurring charge. Because Authorize can only take one subscription at a time, we build a list of the transaction dictionaries, for later sequential posting. """ if not self.arb_enabled: return [] # get all subscriptions from the order subscriptions = self.get_recurring_orderitems() if len(subscriptions) == 0: self.log_extra('No subscription items') return [] settings = self.settings # set up the base dictionary trans = {} if self.is_live(): conn = settings.ARB_CONNECTION.value self.log_extra('Using live recurring charge connection.') else: conn = settings.ARB_CONNECTION_TEST.value self.log_extra('Using test recurring charge connection.') shop_config = Config.objects.get_current() trans['connection'] = conn trans['config'] = { 'merchantID': settings.LOGIN.value, 'transactionKey': settings.TRANKEY.value, 'shop_name': shop_config.store_name, } trans['order'] = self.order trans['card'] = self.order.credit_card start_date = timezone.now().date() if self.order.credit_card: trans['card_expiration'] = "%4i-%02i" % ( self.order.credit_card.expire_year, self.order.credit_card.expire_month) translist = [] taxer = get_tax_processor(user=self.order.contact.user) for subscription in subscriptions: product = subscription.product subtrans = trans.copy() subtrans['subscription'] = subscription subtrans['product'] = product sub = product.subscriptionproduct trial = sub.get_trial_terms(0) if trial: price = trunc_decimal(trial.price, 2) trial_amount = price if price and subscription.product.taxable: trial_amount = taxer.by_price( subscription.product.taxClass, price) #todo, maybe add shipping for trial? amount = sub.recurring_price trial_occurrences = trial.occurrences if not trial_occurrences: self.log.warn( "Trial expiration period is less than one recurring billing cycle. " + "Authorize does not allow this, so the trial period has been adjusted to be equal to one recurring cycle." ) trial_occurrences = 1 # add discounts for trial_amount and amount for recurring subscriptions if subscription.discount: discount = Discount.objects.by_code( self.order.discount_code) if discount.amount: amount = amount - discount.amount if amount > discount.amount else Decimal( "0") trial_amount = trial_amount - discount.amount if trial_amount > discount.amount else Decimal( "0") elif discount.percentage: discount_result = amount * discount.percentage / 100 amount -= discount_result.quantize(Decimal("0.01")) discount_result = trial_amount * discount.percentage / 100 trial_amount -= discount_result.quantize( Decimal("0.01")) else: trial_occurrences = 0 trial_amount = Decimal('0.00') amount = subscription.total_with_tax occurrences = sub.recurring_times or 9999 occurrences += trial_occurrences if occurrences > 9999: occurrences = 9999 subtrans['occurrences'] = occurrences subtrans['trial_occurrences'] = trial_occurrences subtrans['start_date'] = start_date if trial and not trial_amount: if sub.expire_unit == "DAY": subtrans['start_date'] = start_date + timezone.timedelta( trial.expire_length) else: subtrans['start_date'] = add_month(start_date, n=trial.expire_length) trial = None subtrans['trial'] = trial subtrans['trial_amount'] = trunc_decimal(trial_amount, 2) subtrans['amount'] = trunc_decimal(amount, 2) if trial: charged_today = trial_amount else: charged_today = amount charged_today = trunc_decimal(charged_today, 2) subtrans['charged_today'] = charged_today if trial_amount or amount: translist.append(subtrans) return translist