Skip to content

Commit

Permalink
[BE] return stripe error in displayable format while creating StripeC…
Browse files Browse the repository at this point in the history
…ustomer
  • Loading branch information
poxip committed Dec 4, 2017
1 parent 9e2c60b commit c0078c5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
4 changes: 2 additions & 2 deletions aa_stripe/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def create(self, validated_data):
instance.create_at_stripe()
except stripe.StripeError as e:
logging.error(
"[AA-Stripe] creating customer failed for user {user.id}: {{ error }}".format(user=user, error=e)
"[AA-Stripe] creating customer failed for user {user.id}: {error}".format(user=user, error=e)
)
raise ValidationError({"stripe_js_response": e})
raise ValidationError({"stripe_error": e._message})

return instance

Expand Down
89 changes: 54 additions & 35 deletions tests/test_customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,48 +58,67 @@ def test_user_create(self):
self.assertEqual(response.status_code, 400)

with requests_mock.Mocker() as m:
m.register_uri('POST', 'https://api.stripe.com/v1/customers', [{'text': json.dumps({
"id": "cus_9Oop0gQ1R1ATMi",
"object": "customer",
"account_balance": 0,
"created": 1476810921,
"currency": "usd",
"default_source": None,
"delinquent": False,
"description": None,
"discount": None,
"email": None,
"livemode": False,
"metadata": {
m.register_uri('POST', 'https://api.stripe.com/v1/customers', [
{
"text": json.dumps({
"error": {"message": "Your card was declined.", "type": "card_error", "param": "",
"code": "card_declined", "decline_code": "do_not_honor"}
}),
"status_code": 400
},
"shipping": None,
"sources": {
"object": "list",
"data": [
{
"text": json.dumps({
"id": "cus_9Oop0gQ1R1ATMi",
"object": "customer",
"account_balance": 0,
"created": 1476810921,
"currency": "usd",
"default_source": None,
"delinquent": False,
"description": None,
"discount": None,
"email": None,
"livemode": False,
"metadata": {
},
"shipping": None,
"sources": {
"object": "list",
"data": [

],
"has_more": False,
"total_count": 0,
"url": "/v1/customers/cus_9Oop0gQ1R1ATMi/sources"
},
"subscriptions": {
"object": "list",
"data": [
],
"has_more": False,
"total_count": 0,
"url": "/v1/customers/cus_9Oop0gQ1R1ATMi/sources"
},
"subscriptions": {
"object": "list",
"data": [

],
"has_more": False,
"total_count": 0,
"url": "/v1/customers/cus_9Oop0gQ1R1ATMi/subscriptions"
}})
}])

],
"has_more": False,
"total_count": 0,
"url": "/v1/customers/cus_9Oop0gQ1R1ATMi/subscriptions"
}
})}])
# test response error
stripe_customer_qs = StripeCustomer.objects.filter(is_created_at_stripe=True)
data = {"stripe_js_response": stripe_js_response}
self.client.force_authenticate(user=self.user)
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, 201)

self.assertEqual(response.status_code, 400)
self.assertEqual(m.call_count, 1)
self.assertEqual(StripeCustomer.objects.count(), 1)
customer = StripeCustomer.objects.first()
self.assertEqual(set(response.data.keys()), {"stripe_error"})
self.assertEqual(response.data["stripe_error"], "Your card was declined.")
self.assertEqual(stripe_customer_qs.count(), 0)

# test success response from Stripe
response = self.client.post(url, data, format="json")
self.assertEqual(response.status_code, 201)
self.assertEqual(m.call_count, 2)
self.assertEqual(stripe_customer_qs.count(), 1)
customer = stripe_customer_qs.first()
self.assertTrue(customer.is_active)
self.assertEqual(customer.user, self.user)
self.assertEqual(customer.stripe_js_response, stripe_js_response)
Expand Down

0 comments on commit c0078c5

Please sign in to comment.