#Mathieu-inovices

1 messages ยท Page 1 of 1 (latest)

marble ivy
#

Hi Mathieu, could you please share with me the subscription ID?

stark herald
#

sub_1Jtab7D9WJsCYyaSCRVPGJ6h

marble ivy
#

Thanks for the ID, can you also share with me the code that give you the error?

stark herald
#

You mean my backend code ?

marble ivy
#

Yes please

stark herald
#
public function getEstimatedAmounts(Organization $organization, array $items, bool $withLines = false): array
    {
        $invoiceOption = [
            'customer' => $this->getCustomerId($organization),
            'subscription_items' => $this->getFormattedSubscriptionItems($items),
            'subscription_default_tax_rates' => [$this->getDefaultTaxRateId()],
        ];

        if ($withLines) {
            $invoiceOption['expand'] = ['lines.data.price.product'];
        }
        $subscriptionId = $organization->getStripeSubscriptionId();
        if ($subscriptionId) {
            $invoiceOption['subscription'] = $subscriptionId;
            $invoiceOption['subscription_proration_behavior'] = 'always_invoice';
        }

        $invoice = $this->stripeClient->invoices->upcoming($invoiceOption);

        $toReturn = [
            'subtotal' => NumberUtils::convertCentsToEuro($invoice->subtotal),
            'tax' => NumberUtils::convertCentsToEuro($invoice->tax),
            'total' => NumberUtils::convertCentsToEuro($invoice->total),
        ];

        if ($withLines) {
            $lines = [];
            foreach ($invoice['lines'] as $line) {
                $lines[$line['price']['product']['name']] = [
                    'amount' => NumberUtils::convertCentsToEuro($line['amount']),
                    'quantity' => $line['quantity']
                ];
            }

            $toReturn['lines'] = $lines;
        }

        return $toReturn;
    }
#

the getFormattedSubscriptionItems method return items like this :

[
           "price" => price_XXX,
           "quantity" => 1,
]
marble ivy
#

Is this statement $invoice = $this->stripeClient->invoices->upcoming($invoiceOption); giving you the error ?

stark herald
#

yes

#

if in subscription_items there is price id that are not in the subscription it's working well but if the price id are already on the subscription it fails with the mention error "Cannot add multiple subscription items with the same plan: price_XXXXXXXXX"

marble ivy
#

Are you able to print out the $invoiceOption object that is passed to the $stripeClient->invoices->upcoming?

stark herald
#

hum should i pass the subscription item id in the subscription_items array for it to work ?

#

yeah it's working now that i pass it in the subscription_items i think it is what i was missing

marble ivy
#

That is great, so you just add in the id into the subscription_items array?

stark herald
#

yep, if the user want's to update the quantity of an item on his subscription i must provide the subscription item id. I'm gonna update my model to take that in account.
Thanks for your help on debugging this with me ๐Ÿ˜‰

marble ivy
#

Don't mention it, the problem is mostly solved by you ๐Ÿ™‚