def _pay_invoice(self, invoice): log_accounting_info("[Autopay] Autopaying invoice {}".format( invoice.id)) amount = invoice.balance.quantize(Decimal(10)**-2) if not amount: return 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: return try: with transaction.atomic(): payment_record = PaymentRecord.create_record( payment_method, 'temp_transaction_id', amount) invoice.pay_invoice(payment_record) invoice.subscription.account.last_payment_method = LastPayment.CC_AUTO invoice.account.save() transaction_id = payment_method.create_charge( autopay_card, amount_in_dollars=amount, description='Auto-payment for Invoice %s' % invoice.invoice_number, ) except stripe.error.CardError as e: self._handle_card_declined(invoice, e) except payment_method.STRIPE_GENERIC_ERROR as e: self._handle_card_errors(invoice, e) else: payment_record.transaction_id = transaction_id payment_record.save() self._send_payment_receipt(invoice, payment_record)
def _pay_invoice(self, invoice): log_accounting_info("[Autopay] Autopaying invoice {}".format(invoice.id)) amount = invoice.balance.quantize(Decimal(10) ** -2) if not amount: return 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: return try: with transaction.atomic(): payment_record = PaymentRecord.create_record(payment_method, 'temp_transaction_id', amount) invoice.pay_invoice(payment_record) invoice.subscription.account.last_payment_method = LastPayment.CC_AUTO invoice.account.save() transaction_id = 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) except payment_method.STRIPE_GENERIC_ERROR as e: self._handle_card_errors(invoice, e) else: payment_record.transaction_id = transaction_id payment_record.save() self._send_payment_receipt(invoice, payment_record)
def process_request(self, request): customer = None amount = self.get_charge_amount(request) card = request.POST.get('stripeToken') remove_card = request.POST.get('removeCard') is_saved_card = request.POST.get('selectedCardType') == 'saved' save_card = request.POST.get('saveCard') and not is_saved_card autopay = request.POST.get('autopayCard') billing_account = BillingAccount.get_account_by_domain(self.domain) generic_error = { 'error': { 'message': _("Something went wrong while processing your payment. " "We're working quickly to resolve the issue. No charges " "were issued. Please try again in a few hours."), }, } try: with transaction.atomic(): if remove_card: self.payment_method.remove_card(card) return { 'success': True, 'removedCard': card, } if save_card: card = self.payment_method.create_card(card, billing_account, self.domain, autopay=autopay) if save_card or is_saved_card: customer = self.payment_method.customer payment_record = PaymentRecord.create_record( self.payment_method, 'temp', amount) self.update_credits(payment_record) charge = self.create_charge(amount, card=card, customer=customer) payment_record.transaction_id = charge.id payment_record.save() self.update_payment_information(billing_account) except stripe.error.CardError as e: # card was declined return e.json_body except ( stripe.error.AuthenticationError, stripe.error.InvalidRequestError, stripe.error.APIConnectionError, stripe.error.StripeError, ) as e: log_accounting_error( "A payment for %(cost_item)s failed due " "to a Stripe %(error_class)s: %(error_msg)s" % { 'error_class': e.__class__.__name__, 'cost_item': self.cost_item_name, 'error_msg': e.json_body['error'] }, show_stack_trace=True, ) return generic_error except Exception as e: log_accounting_error( "A payment for %(cost_item)s failed due to: %(error_msg)s" % { 'cost_item': self.cost_item_name, 'error_msg': e, }, show_stack_trace=True, ) return generic_error try: self.send_email(payment_record) except Exception: log_accounting_error( "Failed to send out an email receipt for " "payment related to PaymentRecord No. %s. " "Everything else succeeded." % payment_record.id, show_stack_trace=True, ) return { 'success': True, 'card': card, 'wasSaved': save_card, 'changedBalance': amount, }
def process_request(self, request): customer = None amount = self.get_charge_amount(request) card = request.POST.get('stripeToken') remove_card = request.POST.get('removeCard') is_saved_card = request.POST.get('selectedCardType') == 'saved' save_card = request.POST.get('saveCard') and not is_saved_card autopay = request.POST.get('autopayCard') billing_account = BillingAccount.get_account_by_domain(self.domain) generic_error = { 'error': { 'message': _( "Something went wrong while processing your payment. " "We're working quickly to resolve the issue. No charges " "were issued. Please try again in a few hours." ), }, } try: with transaction.atomic(): if remove_card: self.payment_method.remove_card(card) return {'success': True, 'removedCard': card, } if save_card: card = self.payment_method.create_card(card, billing_account, self.domain, autopay=autopay) if save_card or is_saved_card: customer = self.payment_method.customer payment_record = PaymentRecord.create_record( self.payment_method, 'temp', amount ) self.update_credits(payment_record) charge = self.create_charge(amount, card=card, customer=customer) payment_record.transaction_id = charge.id payment_record.save() self.update_payment_information(billing_account) except stripe.error.CardError as e: # card was declined return e.json_body except ( stripe.error.AuthenticationError, stripe.error.InvalidRequestError, stripe.error.APIConnectionError, stripe.error.StripeError, ) as e: log_accounting_error( "A payment for %(cost_item)s failed due " "to a Stripe %(error_class)s: %(error_msg)s" % { 'error_class': e.__class__.__name__, 'cost_item': self.cost_item_name, 'error_msg': e.json_body['error'] } ) return generic_error except Exception as e: log_accounting_error( "A payment for %(cost_item)s failed due to: %(error_msg)s" % { 'cost_item': self.cost_item_name, 'error_msg': e, } ) return generic_error try: self.send_email(payment_record) except Exception: log_accounting_error( "Failed to send out an email receipt for " "payment related to PaymentRecord No. %s. " "Everything else succeeded." % payment_record.id ) return { 'success': True, 'card': card, 'wasSaved': save_card, 'changedBalance': amount, }
def process_request(self, request): customer = None amount = self.get_charge_amount(request) card = request.POST.get('stripeToken') remove_card = request.POST.get('removeCard') is_saved_card = request.POST.get('selectedCardType') == 'saved' save_card = request.POST.get('saveCard') and not is_saved_card generic_error = { 'error': { 'message': _( "Something went wrong while processing your payment. " "We're working quickly to resolve the issue. No charges " "were issued. Please try again in a few hours." ), }, } try: if remove_card: customer = get_or_create_stripe_customer(self.payment_method) customer.cards.retrieve(card).delete() return { 'success': True, 'removedCard': card, } if save_card: customer = get_or_create_stripe_customer(self.payment_method) card = customer.cards.create(card=card) customer.default_card = card customer.save() card = card if is_saved_card: customer = get_or_create_stripe_customer(self.payment_method) charge = self.create_charge(amount, card=card, customer=customer) payment_record = PaymentRecord.create_record( self.payment_method, charge.id, amount ) self.update_credits(payment_record) try: self.send_email(payment_record) except Exception: logger.error( "[BILLING] Failed to send out an email receipt for " "payment related to PaymentRecord No. %s. " "Everything else succeeded." % payment_record.id, exc_info=True ) except stripe.error.CardError as e: # card was declined return e.json_body except ( stripe.error.AuthenticationError, stripe.error.InvalidRequestError, stripe.error.APIConnectionError, stripe.error.StripeError, ) as e: logger.error( "[BILLING] A payment for %(cost_item)s failed due " "to a Stripe %(error_class)s: %(error_msg)s" % { 'error_class': e.__class__.__name__, 'cost_item': self.cost_item_name, 'error_msg': e.json_body['error'] }, exc_info=True) return generic_error except Exception as e: logger.error( "[BILLING] A payment for %(cost_item)s failed due " "to: %(error_msg)s" % { 'cost_item': self.cost_item_name, 'error_msg': e, }, exc_info=True) return generic_error return { 'success': True, 'card': card, 'wasSaved': save_card, 'changedBalance': amount, }
def process_request(self, request): customer = None amount = self.get_charge_amount(request) card = request.POST.get('stripeToken') remove_card = request.POST.get('removeCard') is_saved_card = request.POST.get('selectedCardType') == 'saved' save_card = request.POST.get('saveCard') and not is_saved_card generic_error = { 'error': { 'message': _( "Something went wrong while processing your payment. " "We're working quickly to resolve the issue. No charges " "were issued. Please try again in a few hours." ), }, } try: if remove_card: customer = get_or_create_stripe_customer(self.payment_method) customer.cards.retrieve(card).delete() return { 'success': True, 'removedCard': card, } if save_card: customer = get_or_create_stripe_customer(self.payment_method) card = customer.cards.create(card=card) customer.default_card = card customer.save() card = card if is_saved_card: customer = get_or_create_stripe_customer(self.payment_method) charge = self.create_charge(amount, card=card, customer=customer) payment_record = PaymentRecord.create_record( self.payment_method, charge.id, amount ) self.update_credits(payment_record) try: self.send_email(payment_record) except Exception: logger.error( "[BILLING] Failed to send out an email receipt for " "payment related to PaymentRecord No. %s. " "Everything else succeeded." % payment_record.id, exc_info=True ) except stripe.error.CardError as e: # card was declined return e.json_body except ( stripe.error.AuthenticationError, stripe.error.InvalidRequestError, stripe.error.APIConnectionError, stripe.error.StripeError, ) as e: logger.error( "[BILLING] A payment for %(cost_item)s failed due " "to a Stripe %(error_class)s: %(error_msg)s" % { 'error_class': e.__class__.__name__, 'cost_item': self.cost_item_name, 'error_msg': e.json_body['error'] }, exc_info=True) return generic_error except Exception as e: logger.error( "[BILLING] A payment for %(cost_item)s failed due " "to: %(error_msg)s" % { 'cost_item': self.cost_item_name, 'error_msg': e, }, exc_info=True) return generic_error return { 'success': True, 'card': card, 'wasSaved': save_card, }