#sergx_docs
1 messages ยท Page 1 of 1 (latest)
๐ 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.
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.
- sergx_api, 4 days ago, 23 messages
- sergx_docs, 5 days ago, 14 messages
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
$request->discount has the discount string. For example "50SALES"
hi! your code doesn't make sense, you are passing subscription and subscription_details at the same time, which can't be done.
no, it should be an array of objects. Like [['promotion_code' => "promo_xxxx]] or [['coupon' => "50SALES"]] , depending on what "50SALES" is meant to be.
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
ah sorry I am wrong, you can pass both of them, so ignore that.
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
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
give me a moment please, checking it
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?
Can you share the ID (req_xxx) of the API request? https://support.stripe.com/questions/finding-the-id-for-an-api-request
Find help and support for Stripe. Our support site provides answers on all types of situations, including account information, charges and refunds, and subscriptions information. Get your questions answered and find international support for Stripe.
Yes of course!
Hmm in logs I'm not able to find it, maybe it's because I'm testing in localhost?
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
Alright got the req_id !
req_jE3JCpwIkgb1yS
Well you can see there's no discounts parameter in the request
So your logic here isn't working as you expect?
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
Unfortunately I can't see the JSON response for GET requests like this. Can you paste the JSON that is unexpected?
Of course, wait I have to delete sensible data
It's this coupon, right? https://dashboard.stripe.com/test/coupons/DEV50
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
yes!
It's fine as long as there's nothing sensitive โ this is a public forum
OK, and what is unexpected about the response?
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
Yes essentially that is my understanding. The proration rebate for the unused time that we assume was paid in previous invoice
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
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
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
The top-level discounts will apply to all items
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
No, it'd be a new item with separate quantity
Oh I see, it is possible to add separate items with the same id?
That would be possible too, when updating the subscription?
I am not sure, you'd need to test
Perfec thank you so so much @mortal crown !