craftcms / stripe

Sync and extend Stripe products and subscriptions.
MIT License
5 stars 2 forks source link

Product default price is an array when using tiered prices #40

Open okolvik-avento opened 4 days ago

okolvik-avento commented 4 days ago

Description

Default price is an array when using tiered prices. This causes the getDefaultPrice function on the Product element to return null since it expects data["default_price"] to be the id of the price. When using tiers the default price is in data["default_price"]["id"].

The easiest way to fix this is to add an additional array check in the price filter function.

Here's the data-json for a default price with no tiers mode (when the first tier is equal to the next tier).

{
    "id": "price_xxx",
    "object": "price",
    "active": true,
    "billing_scheme": "per_unit",
    "created": 1671093832,
    "currency": "nok",
    "custom_unit_amount": null,
    "livemode": false,
    "lookup_key": null,
    "metadata": [],
    "nickname": null,
    "product": "prod_xxx",
    "recurring": {
        "aggregate_usage": null,
        "interval": "month",
        "interval_count": 1,
        "meter": null,
        "trial_period_days": null,
        "usage_type": "licensed"
    },
    "tax_behavior": "unspecified",
    "tiers_mode": null,
    "transform_quantity": null,
    "type": "recurring",
    "unit_amount": 100,
    "unit_amount_decimal": "100"
}

And here is one with tiers mode graduated (when the next tier changes price based on count):

{
    "id": "price_yyy",
    "object": "price",
    "active": true,
    "billing_scheme": "tiered",
    "created": 1678099575,
    "currency": "nok",
    "custom_unit_amount": null,
    "livemode": false,
    "lookup_key": null,
    "metadata": [],
    "nickname": "2023",
    "product": "prod_yyy",
    "recurring": {
        "aggregate_usage": null,
        "interval": "year",
        "interval_count": 1,
        "meter": null,
        "trial_period_days": null,
        "usage_type": "licensed"
    },
    "tax_behavior": "unspecified",
    "tiers_mode": "graduated",
    "transform_quantity": null,
    "type": "recurring",
    "unit_amount": null,
    "unit_amount_decimal": null
}

Steps to reproduce

  1. Set up tiered prices with a default price
  2. Try to run product.defaultPrice
  3. Get null

Additional info

i-just commented 1 day ago

Hi, thanks for reporting! I raised a PR for this.