def test_post_on_error(self, CreateMock): CreateMock.side_effect = stripe.CardError("Bad card", "Param", "CODE") self.client.login(username=self.user.username, password=self.password) response = self.client.post( reverse("pinax_stripe_payment_method_create"), {}) self.assertEquals(response.status_code, 200) self.assertTrue("errors" in response.context_data)
def mock_stripe_Charge_create(error_msg=None): json_body = {'error': {'charge': 'charge_id'}} with patch('stripe.Charge.create') as mocked_charge_create: if error_msg: mocked_charge_create.side_effect = stripe.CardError( error_msg, param=None, code=None, json_body=json_body) else: mocked_charge_create.side_effect = lambda **kwargs: {} yield mocked_charge_create
def test_change_card_error(self, retry_mock, send_mock, update_mock): update_mock.side_effect = stripe.CardError("Bad card", "Param", "CODE") self.client.login(username=self.user.username, password=self.password) response = self.client.post(reverse("payments_ajax_change_card"), {"stripe_token": "XXXXX"}, HTTP_X_REQUESTED_WITH="XMLHttpRequest") self.assertEqual(update_mock.call_count, 1) self.assertEqual(send_mock.call_count, 0) self.assertEqual(retry_mock.call_count, 0) self.assertEqual(response.status_code, 200)
def UserAddCard(request, username): ''' Adds a card from either the reservation page or the user profile page. Displays success or error message and returns user to originating page.''' user = User.objects.get(username=username) if not request.method == 'POST' or request.user != user: return HttpResponseRedirect('/404') token = request.POST.get('stripeToken') if not token: messages.add_message(request, messages.INFO, "No credit card information was given.") return HttpResponseRedirect("/people/%s" % username) reservation_id = request.POST.get('res-id') if reservation_id: reservation = Reservation.objects.get(id=reservation_id) stripe.api_key = settings.STRIPE_SECRET_KEY try: customer = stripe.Customer.create(card=token, description=user.email) profile = user.profile profile.customer_id = customer.id profile.save() # if the card is being added from the reservation page, then charge the card if reservation_id: try: # charges card, saves payment details and emails a receipt to # the user reservation.reconcile.charge_card() reservation.status = Reservation.CONFIRMED reservation.save() days_until_arrival = (reservation.arrive - datetime.date.today()).days if days_until_arrival < settings.WELCOME_EMAIL_DAYS_AHEAD: send_guest_welcome([ reservation, ]) messages.add_message( request, messages.INFO, 'Thank you! Your payment has been processed and a receipt emailed to you at %s' % user.email) return HttpResponseRedirect("/reservation/%d" % int(reservation_id)) except stripe.CardError, e: raise stripe.CardError(e) # if the card is being added from the user profile page, just save it. else:
def test_subscription_is_not_created_when_charging_fails(self): self.assertEqual(Invoice.objects.count(), 0) self.assertEqual(Subscription.objects.count(), 1) exc_message = 'big nono' fake_exc = stripe.CardError(message=exc_message, param=None, code=None) with mock.patch('apps.billing.models.Invoice.create_charge', side_effect=fake_exc) as create_charge_mock: response = self.client.post(self.url, {'commitment': self.commitment}) self.assertTrue(create_charge_mock.called) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(Invoice.objects.count(), 0) self.assertEqual(Subscription.objects.count(), 1) self.assertTrue(response.data['detail'].endswith(exc_message))
def test_retry_payment_failing(self): invoice = self.create_invoice() self.assertIsNotNone( self.client.get( reverse('v1:billing-profile')).data['failed_invoice']) url = reverse('v1:invoice-retry-payment', args=(invoice.id, )) exc_message = 'ohmygawdness' fake_exc = stripe.CardError(message=exc_message, param=None, code=None) with mock.patch('apps.billing.models.Invoice.create_charge', side_effect=fake_exc) as create_charge_mock: response = self.client.post(url) self.assertTrue(create_charge_mock.called) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertTrue(response.data['detail'].endswith(exc_message))
def UserAddCard(request, username): ''' Adds a card from either the reservation page or the user profile page. Displays success or error message and returns user to originating page.''' user = User.objects.get(username=username) if not request.method == 'POST' or request.user != user: return HttpResponseRedirect('/404') token = request.POST.get('stripeToken') if not token: messages.add_message(request, messages.INFO, "No credit card information was given.") return HttpResponseRedirect("/people/%s" % username) reservation_id = request.POST.get('res-id') if reservation_id: reservation = Reservation.objects.get(id=reservation_id) stripe.api_key = settings.STRIPE_SECRET_KEY try: customer = stripe.Customer.create( card=token, description=user.email ) profile = user.profile profile.customer_id = customer.id profile.save() # if the card is being added from the reservation page, then charge the card if reservation_id: try: # charges card, saves payment details and emails a receipt to # the user payment_gateway.charge_card(reservation) send_receipt(reservation) reservation.confirm() days_until_arrival = (reservation.arrive - datetime.date.today()).days if days_until_arrival <= reservation.location.welcome_email_days_ahead: guest_welcome(reservation) messages.add_message(request, messages.INFO, 'Thank you! Your payment has been processed and a receipt emailed to you at %s. You will receive an email with house access information and other details %d days before your arrival.' % (user.email, reservation.location.welcome_email_days_ahead)) return HttpResponseRedirect(reverse('reservation_detail', args=(reservation.location.slug, reservation.id))) except stripe.CardError, e: raise stripe.CardError(e) # if the card is being added from the user profile page, just save it. else: