Incorrect charge on orders on custom subscriptions app

I’m developing a subscriptions app for the company I work for.
I have an issue were some orders seems to be charged by an extra month incorrectly. We have customers wit a 1 month recurrent subscription that are charged for 2 months. We also have a 3 months subscriptions and customers are charged for 4 months. It doesn’t happen every time.

I’ve checked that the billing policy is correct on the selling plan and also on the subscription contract. What else can I check to know what’s happening?

{
    "data": {
        "sellingPlanGroup": {
            "id": "gid://shopify/SellingPlanGroup/785154137",
            "name": "Verified Subscriptions",
            "summary": "1 delivery frequency, $396 discount",
            "merchantCode": "Verified Subscriptions",
            "products": {
                "nodes": []
            },
            "productVariants": {
                "nodes": [
                    {
                        "id": "gid://shopify/ProductVariant/42266891944025",
                        "title": "QUARTERLY",
                        "sellingPlanGroupsCount": {
                            "count": 1
                        }
                    }
                ]
            },
            "sellingPlans": {
                "nodes": [
                    {
                        "id": "gid://shopify/SellingPlan/1604878425",
                        "name": "Smart Money Stocks - QUARTERLY - 3 MONTH",
                        "description": "",
                        "position": null,
                        "category": "SUBSCRIPTION",
                        "billingPolicy": {
                            "createdAt": "2025-03-29T11:55:09Z",
                            "interval": "MONTH",
                            "intervalCount": 3,
                            "minCycles": null,
                            "maxCycles": null,
                            "anchors": []
                        },
                        "deliveryPolicy": {
                            "createdAt": "2025-03-29T11:55:09Z",
                            "cutoff": null,
                            "interval": "MONTH",
                            "intervalCount": 3,
                            "anchors": []
                        },
                        "pricingPolicies": [
                            {
                                "adjustmentType": "PRICE",
                                "adjustmentValue": {
                                    "amount": "396.0",
                                    "currencyCode": "USD"
                                }
                            }
                        ]
                    }
                ]
            }
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 47,
            "actualQueryCost": 16,
            "throttleStatus": {
                "maximumAvailable": 4000.0,
                "currentlyAvailable": 3984,
                "restoreRate": 200.0
            }
        }
    }
}
{
    "data": {
        "subscriptionContract": {
            "id": "gid://shopify/SubscriptionContract/29293740121",
            "status": "ACTIVE",
            "nextBillingDate": "2025-07-03T13:00:00Z",
            "lines": {
                "nodes": [
                    {
                        "id": "gid://shopify/SubscriptionLinexxxxxxxxx",
                        "productId": "gid://shopify/Product/7936578355289",
                        "variantId": "gid://shopify/ProductVariant/42266891944025",
                        "quantity": 1,
                        "sellingPlanId": "gid://shopify/SellingPlan/1604878425",
                        "currentPrice": {
                            "amount": "396.0",
                            "currencyCode": "USD"
                        },
                        "pricingPolicy": null
                    }
                ]
            },
            "customer": {
                "id": "gid://shopify/Customer/xxxxx",
                "email": "xxxxxx"
            },
            "createdAt": "2025-03-30T22:08:23Z",
            "updatedAt": "2025-04-03T13:00:21Z",
            "billingPolicy": {
                "interval": "MONTH",
                "intervalCount": 3,
                "minCycles": null,
                "maxCycles": null
            }
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 11,
            "actualQueryCost": 7,
            "throttleStatus": {
                "maximumAvailable": 4000.0,
                "currentlyAvailable": 3993,
                "restoreRate": 200.0
            }
        }
    }
}

The issue may be caused by incorrect nextBillingDate updates, duplicate scheduled jobs, or your app triggering billing too soon. Check webhook events, ensure your background jobs aren’t queued twice, and confirm your logic prevents charging before nextBillingDate. Also verify your retry logic doesn’t retry after a successful charge. Add internal safeguards to block billing if the current date is earlier than nextBillingDate.

I’m launching each billing attempt on the same date as the nextBillingDate from the subscription contract. I then update the nextBillingDate value with the next date. Also, before sending the billing attempt, I get the billing cycle index from the nextBillingDate and verify that it’s not BILLED. Is this the correct way to do this?

I found some billing cycles whose next billing date doesn’t match the next billing date in the subscription contract. The status of each billing cycle is UNBILLED. How can I adjust the next billing date for the billing cycle?