#sergx_docs

1 messages ยท Page 1 of 1 (latest)

frigid gazelleBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1237346724389978194

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

rich mulchBOT
#

Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

urban scroll
#
public function getUpcomingInvoice(Request $request)
    {
        $request->validate([
            'businessQuantity' => 'required|numeric|min:1',
            'employeeQuantity' => 'required|numeric|min:0',
            'discount' => 'nullable|string',
        ]);

        $user = User::find(auth()->id());
        $priceIds = $this->retrievePriceIds($user);
        
        $payload = [
            'businessPriceId' => $priceIds['business_price'],
            'employeePriceId' => $priceIds['employee_price'],
            'businessQuantity' => $request->businessQuantity,
            'employeeQuantity' => $request->employeeQuantity,
        ];
        $items = (new StripeService)->getSubscriptionsItemsForUpcomingInvoice($user, $payload);
        
        $data = $this->stripe->invoices->upcoming([
            'customer' => $user->stripe_id,
            'subscription' => $user->subscriptionId(),
            'subscription_details' => [
                'items' => $items,
                'proration_behavior' => 'always_invoice',
            ],
        ]);
        if ($request->discount && !isset($data['discounts'])) {
            $discountExists = $user->findPromotionCode($request->discount);
            if ($discountExists) {
                $data['discount'] = $request->discount;
            }
        }

        return response()->json([
            'data' => $data,
            'status_code' => 200,
        ]);
    }
#

I'm building a checkout and filling data using upcoming invoice API. In the checkout I've created the typical input for applying coupons. I'm wondering how can I set the discount in the request of the upcoming invoice

rich mulchBOT
urban scroll
#

$request->discount has the discount string. For example "50SALES"

main steeple
#

hi! your code doesn't make sense, you are passing subscription and subscription_details at the same time, which can't be done.

main steeple
urban scroll
#

Yes I need to use subscription_details, in this case it wouldn't be neccesary to pass the subscription_id?

#

I used 'subscription' to pass the sub_XXXX

main steeple
#

ah sorry I am wrong, you can pass both of them, so ignore that.

urban scroll
#

Perfect! about the coupon you mean this?

#
if ($request->discount && !isset($data['discounts'])) {
            $discountExists = $user->findPromotionCode($request->discount);
            if ($discountExists) {
                $data['discount'] = [
                    ['coupon' => $request->discount]
                ];
            }
        }
#

Nope, I still did something wrong

#

getting the response without the discount

main steeple
#

hard for me to say since I don't know what part of the response of the object that is , or maybe you used a coupon that doesn't apply to the Products in the subscription, there's so many possibilities here

urban scroll
#

give me a moment please, checking it

frigid gazelleBOT
urban scroll
#

This is the upcoming invoice object

#

Now the discount is there applied, but the total, and total_discount_amounts is not updated

#

I mean, these prices are not being updated, do you know why?

mortal crown
urban scroll
#

Yes of course!

#

Hmm in logs I'm not able to find it, maybe it's because I'm testing in localhost?

mortal crown
#

Nope should still be a log of the API request if it's making it to us

#

Ideally I need to see the actual parameters/values you're passing as opposed to screenshots of JSON and your code

urban scroll
#

Alright got the req_id !

req_jE3JCpwIkgb1yS

mortal crown
#

Well you can see there's no discounts parameter in the request

mortal crown
urban scroll
#

Checking it!

#

There's a problem in how the discount is being passed.

I've set a descount as default to test it


$data = $this->stripe->invoices->upcoming([
            'customer' => $user->stripe_id,
            'subscription' => $user->subscriptionId(),
            'subscription_details' => [
                'items' => $items,
                'proration_behavior' => 'always_invoice',
            ],
            'discount' => [
                ['coupon' => 'DEV50']
            ]
        ]);

#

Getting that error

req_txwFfYY1jRIu8g

#

but if I put discounts instead of discount

#

In this case I get this:

req_tcblsnX19dk3bA

And amounts doesn't make sense

mortal crown
urban scroll
#

Of course, wait I have to delete sensible data

mortal crown
urban scroll
#

yes!

mortal crown
#

It's fine as long as there's nothing sensitive โ€“ this is a public forum

#

OK, and what is unexpected about the response?

urban scroll
#

In that case the invoice the total_discount_amounts is 0. Also is negative invoice value

#

Don't understand why is it negative

#

Ohh I think I understand, it's due to prorations. When applying DEV50 to the subscription

#

It's calculating all that was paid x 50% and giving credit balance

mortal crown
#

Yes essentially that is my understanding. The proration rebate for the unused time that we assume was paid in previous invoice

urban scroll
#

But the problem I see

#

Okay I guess it's nominal, have to think in how to do the UX, because the user may see weird a negative amount in the invoice.

I was thinking that for doing marketing it could cause a problem.

Imagine a user that is subscribed for 3 quantity of product.

And now we are negotiating with him, and he is willing to add 10 additional product quantity in exchange for 20% of discount. In this case, in the long run we would be losing money from the amount of 3 initial products, because we are giving credit balance to the client

In the sense that this 20% discount is not applied on the purchase made at the moment to the customer, but to the subscription as a whole.

#

Sorry for my english not native, I don't know if I explained it good

mortal crown
#

Yeah you can't really apply a discount to just a specific quantity of item. What you'd probably need to do is use a different item for the discounted product, and apply the discount directly on that: https://docs.stripe.com/api/invoices/upcoming#upcoming_invoice-subscription_details-items-discounts

#

The top-level discounts will apply to all items

urban scroll
#

I see, but I think it would be the same problem if I'm not wrong right?

Because in items, I would need to pass in quantity, the quantity the user already has + new quantity

mortal crown
#

No, it'd be a new item with separate quantity

urban scroll
#

Oh I see, it is possible to add separate items with the same id?

#

That would be possible too, when updating the subscription?

mortal crown
urban scroll
#

Perfec thank you so so much @mortal crown !