def test_save_stripe_token(self, mock_save_customer: mock.Mock, mock_save_card: mock.Mock, mock_retrieve_customer: mock.Mock, mock_create_customer: mock.Mock, mock_list_sources: mock.Mock, mock_create_source: mock.Mock, mock_billing_logger_info: mock.Mock) -> None: self.assertFalse(Customer.objects.filter(realm=self.realm)) number_of_cards = save_stripe_token(self.user, self.token) self.assertEqual(number_of_cards, 1) description = "{} ({})".format(self.realm.name, self.realm.string_id) mock_create_customer.assert_called_once_with( description=description, source=self.token, metadata={'string_id': self.realm.string_id}) mock_list_sources.assert_called_once() mock_save_card.assert_called_once() mock_billing_logger_info.assert_called() customer_object = Customer.objects.get(realm=self.realm) # Add another card number_of_cards = save_stripe_token(self.user, self.token) # Note: customer.sources.list is mocked to return 2 cards all the time. self.assertEqual(number_of_cards, 2) mock_retrieve_customer.assert_called_once_with( customer_object.stripe_customer_id) create_source_metadata = { 'added_user_id': self.user.id, 'added_user_email': self.user.email } mock_create_source.assert_called_once_with( metadata=create_source_metadata, source='token') mock_save_customer.assert_called_once() mock_billing_logger_info.assert_called()
def add_payment_method(request: HttpRequest) -> HttpResponse: user = request.user ctx = { "publishable_key": STRIPE_PUBLISHABLE_KEY, "email": user.email, } # type: Dict[str, Any] if not user.is_realm_admin: ctx["error_message"] = ( _("You should be an administrator of the organization %s to view this page.") % (user.realm.name,)) return render(request, 'zilencer/billing.html', context=ctx) try: if request.method == "GET": ctx["num_cards"] = count_stripe_cards(user.realm) return render(request, 'zilencer/billing.html', context=ctx) if request.method == "POST": token = request.POST.get("stripeToken", "") ctx["num_cards"] = save_stripe_token(user, token) ctx["payment_method_added"] = True return render(request, 'zilencer/billing.html', context=ctx) except StripeError as e: ctx["error_message"] = e.msg return render(request, 'zilencer/billing.html', context=ctx)
def add_payment_method(request: HttpRequest) -> HttpResponse: user = request.user ctx = { "publishable_key": STRIPE_PUBLISHABLE_KEY, "email": user.email, } # type: Dict[str, Any] if not user.is_realm_admin: ctx["error_message"] = (_( "You should be an administrator of the organization %s to view this page." ) % (user.realm.name, )) return render(request, 'zilencer/billing.html', context=ctx) try: if request.method == "GET": ctx["num_cards"] = count_stripe_cards(user.realm) return render(request, 'zilencer/billing.html', context=ctx) if request.method == "POST": token = request.POST.get("stripeToken", "") ctx["num_cards"] = save_stripe_token(user, token) ctx["payment_method_added"] = True return render(request, 'zilencer/billing.html', context=ctx) except StripeError as e: ctx["error_message"] = e.msg return render(request, 'zilencer/billing.html', context=ctx)
def test_save_stripe_token(self, mock_save_customer: mock.Mock, mock_save_card: mock.Mock, mock_retrieve_customer: mock.Mock, mock_create_customer: mock.Mock, mock_list_sources: mock.Mock, mock_create_source: mock.Mock, mock_billing_logger_info: mock.Mock) -> None: self.assertFalse(Customer.objects.filter(realm=self.realm)) number_of_cards = save_stripe_token(self.user, self.token) self.assertEqual(number_of_cards, 1) description = "{} ({})".format(self.realm.name, self.realm.string_id) mock_create_customer.assert_called_once_with(description=description, source=self.token, metadata={'string_id': self.realm.string_id}) mock_list_sources.assert_called_once() mock_save_card.assert_called_once() mock_billing_logger_info.assert_called() customer_object = Customer.objects.get(realm=self.realm) # Add another card number_of_cards = save_stripe_token(self.user, self.token) # Note: customer.sources.list is mocked to return 2 cards all the time. self.assertEqual(number_of_cards, 2) mock_retrieve_customer.assert_called_once_with(customer_object.stripe_customer_id) create_source_metadata = {'added_user_id': self.user.id, 'added_user_email': self.user.email} mock_create_source.assert_called_once_with(metadata=create_source_metadata, source='token') mock_save_customer.assert_called_once() mock_billing_logger_info.assert_called()