Repository: dj-stripe/dj-stripe

Issue:
Issue with syncing pricing tiers. 
**Describe the bug**
When using a product with volume pricing tiers, the tiers aren't synced into djstripe models properly. This prevents viewing the models in the admin by throwing the following error:

File "C:\Users\david\anaconda3\envs\django\lib\site-packages\djstripe\models\core.py", line 2254, in human_readable_price tier_1 = self.tiers[0]
Exception Type: TypeError at /admin/djstripe/price/
Exception Value: 'NoneType' object is not subscriptable

**To Reproduce**
Steps to reproduce the behavior:

1. Create product with subscription volume pricing tiers, and a flat rate of $0.00
2. Sync the models by calling the "python manage.py djstripe_sync_plans_from_stripe" command. 
3. Create a superuser and log into admin and go to the djstripe "Plans" or "Prices" admin view, which is what is throwing the error. 

**Expected behavior**
I expected to view the models in the admin and interact with the tiers but it throws an error if I try to access the "Plan" or "Price" models. 
I checked the database as well and the JSON field that is suppose to contain the tiers is empty. 
I also changed the "Price.expand_fields" from "['tiers']" to "['data.tiers']" but this didn't fix the issue. 

**Environment**
- dj-stripe version: 2.6.2
- Your Stripe account's default API version: 2020-08-27
- Database: sqlite 3.38.5
- Python version: 3.8
- Django version: 3.2.5

**Can you reproduce the issue with the latest version of master?**

Yes



Discussion:
@sparkle-motion-2 I am aware of this error and have pushed a partial fix to `master` already. Would be raising another `PR` over the weekend closing this issue.


After upgrading to the most recent version of djstripe and making the changes that you put into your PR, it still isn't working. The only difference is that the error is now TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
I found the issue: 
For pricing tiers, if the per unit price has >2 decimal places, it returns a null value in the "unit_amount" field in the tiers JSON object. This is what is causing the "TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'" error. 

Hi there!

We are facing the same issue but the root cause seems different in our case. We're using graduated pricing as shown in the screenshot below.

<img width="587" alt="image" src="https://user-images.githubusercontent.com/759215/183294212-c4af4bac-4318-4706-8c5f-8a74be76565d.png">

When you set `0` as unit price and define a flat fee, the API returns `null` for the `unit_amount` field, as shown by the Stripe CLI:
```bash
$ stripe prices retrieve price_1L97tyCc5y2xTJ35YeyvM2v1 --expand tiers
{
  "id": "price_1L97tyCc5y2xTJ35YeyvM2v1",
  "object": "price",
  "active": true,
  "billing_scheme": "tiered",
  "created": 1654867766,
  "currency": "eur",
  "custom_unit_amount": null,
  "livemode": false,
  "lookup_key": null,
  "metadata": {
  },
  "nickname": "***REDACTED***",
  "product": "prod_LqpZhazpkzxr7A",
  "recurring": {
    "aggregate_usage": "last_during_period",
    "interval": "month",
    "interval_count": 1,
    "trial_period_days": null,
    "usage_type": "metered"
  },
  "tax_behavior": "exclusive",
  "tiers": [
    {
      "flat_amount": 12000,
      "flat_amount_decimal": "12000",
      "unit_amount": null,
      "unit_amount_decimal": null,
      "up_to": 30
    },
    {
      "flat_amount": null,
      "flat_amount_decimal": null,
      "unit_amount": 500,
      "unit_amount_decimal": "500",
      "up_to": null
    }
  ],
  "tiers_mode": "graduated",
  "transform_quantity": null,
  "type": "recurring",
  "unit_amount": null,
  "unit_amount_decimal": null
}
```

Resolving patch:
diff --git a/djstripe/models/core.py b/djstripe/models/core.py
--- a/djstripe/models/core.py
+++ b/djstripe/models/core.py
@@ -2327,7 +2327,7 @@ def human_readable_price(self):
             tier_1 = self.tiers[0]
             flat_amount_tier_1 = tier_1["flat_amount"]
             formatted_unit_amount_tier_1 = get_friendly_currency_amount(
-                tier_1["unit_amount"] / 100, self.currency
+                (tier_1["unit_amount"] or 0) / 100, self.currency
             )
             amount = f"Starts at {formatted_unit_amount_tier_1} per unit"
 


Test patch: