def pay_autopayable_invoices(self, date_due): """ Pays the full balance of all autopayable invoices on date_due """ autopayable_invoices = Invoice.autopayable_invoices(date_due) for invoice in autopayable_invoices: log_accounting_info("[Autopay] Autopaying invoice {}".format(invoice.id)) amount = invoice.balance.quantize(Decimal(10) ** -2) if not amount: continue auto_payer = invoice.subscription.account.auto_pay_user payment_method = StripePaymentMethod.objects.get(web_user=auto_payer) autopay_card = payment_method.get_autopay_card(invoice.subscription.account) if autopay_card is None: continue try: payment_record = payment_method.create_charge( autopay_card, amount_in_dollars=amount, description='Auto-payment for Invoice %s' % invoice.invoice_number, ) except stripe.error.CardError: self._handle_card_declined(invoice, payment_method) continue except payment_method.STRIPE_GENERIC_ERROR as e: self._handle_card_errors(invoice, e) continue else: with transaction.atomic(): invoice.pay_invoice(payment_record) invoice.subscription.account.last_payment_method = LastPayment.CC_AUTO invoice.account.save() self._send_payment_receipt(invoice, payment_record)
def test_get_autopayable_invoices_returns_nothing(self): """ Invoice.autopayable_invoices() should not return invoices if the customer does not have an autopay method """ not_autopayable_invoice = Invoice.objects.filter(subscription=self.non_autopay_subscription) date_due = not_autopayable_invoice.first().date_due autopayable_invoices = Invoice.autopayable_invoices(date_due) self.assertItemsEqual(autopayable_invoices, [])
def pay_autopayable_invoices(self, date_due): """ Pays the full balance of all autopayable invoices on date_due """ autopayable_invoices = Invoice.autopayable_invoices(date_due) for invoice in autopayable_invoices: try: self._pay_invoice(invoice) except Exception as e: log_accounting_error("Error autopaying invoice %d: %s" % (invoice.id, e.message))
def test_get_autopayable_invoices_returns_nothing(self): """ Invoice.autopayable_invoices() should not return invoices if the customer does not have an autopay method """ not_autopayable_invoice = Invoice.objects.filter( subscription=self.non_autopay_subscription) date_due = not_autopayable_invoice.first().date_due autopayable_invoices = Invoice.autopayable_invoices(date_due) self.assertItemsEqual(autopayable_invoices, [])
def test_get_autopayable_invoices(self, fake_customer): self._create_autopay_method(fake_customer) autopayable_invoice = Invoice.objects.filter(subscription=self.subscription) date_due = autopayable_invoice.first().date_due autopayable_invoices = Invoice.autopayable_invoices(date_due) self.assertItemsEqual(autopayable_invoices, autopayable_invoice)
def test_get_autopayable_invoices(self, fake_customer): """ Invoice.autopayable_invoices() should return invoices that can be automatically paid """ self._create_autopay_method(fake_customer) autopayable_invoice = Invoice.objects.filter(subscription=self.subscription) date_due = autopayable_invoice.first().date_due autopayable_invoices = Invoice.autopayable_invoices(date_due) self.assertItemsEqual(autopayable_invoices, autopayable_invoice)
def test_get_autopayable_invoices(self, fake_customer): fake_customer.__get__ = mock.Mock(return_value=self.fake_stripe_customer) self.payment_method = StripePaymentMethod(web_user=self.web_user.username, customer_id=self.fake_stripe_customer.id) self.payment_method.set_autopay(self.fake_card, self.account, self.domain) self.payment_method.save() autopayable_invoice = Invoice.objects.filter(subscription=self.subscription) date_due = autopayable_invoice.first().date_due autopayable_invoices = Invoice.autopayable_invoices(date_due) self.assertItemsEqual(autopayable_invoices, autopayable_invoice)
def test_get_autopayable_invoices(self, fake_customer): """ Invoice.autopayable_invoices() should return invoices that can be automatically paid """ self._create_autopay_method(fake_customer) autopayable_invoice = Invoice.objects.filter( subscription=self.subscription) date_due = autopayable_invoice.first().date_due autopayable_invoices = Invoice.autopayable_invoices(date_due) self.assertItemsEqual(autopayable_invoices, autopayable_invoice)
def pay_autopayable_invoices(self, date_due=Ellipsis, domain=None): """ Pays the full balance of all autopayable invoices on date_due Note: we use Ellipsis as the default value for date_due because date_due can actually be None in the db. """ autopayable_invoices = Invoice.autopayable_invoices(date_due) if domain is not None: autopayable_invoices = autopayable_invoices.filter( subscription__subscriber__domain=domain) for invoice in autopayable_invoices: try: self._pay_invoice(invoice) except Exception as e: log_accounting_error("Error autopaying invoice %d: %s" % (invoice.id, e))
def pay_autopayable_invoices(self, date_due): """ Pays the full balance of all autopayable invoices on date_due """ autopayable_invoices = Invoice.autopayable_invoices(date_due) for invoice in autopayable_invoices: logging.info("[Billing][Autopay] Autopaying invoice {}".format(invoice.id)) amount = invoice.balance.quantize(Decimal(10) ** -2) auto_payer = invoice.subscription.account.auto_pay_user payment_method = StripePaymentMethod.objects.get(web_user=auto_payer) autopay_card = payment_method.get_autopay_card(invoice.subscription.account) if autopay_card is None: continue try: payment_record = payment_method.create_charge(autopay_card, amount_in_dollars=amount) except stripe.error.CardError: self._handle_card_declined(invoice, payment_method) continue except payment_method.STRIPE_GENERIC_ERROR as e: self._handle_card_errors(invoice, payment_method, e) continue else: invoice.pay_invoice(payment_record) self._send_payment_receipt(invoice, payment_record)