#Sandeep
1 messages · Page 1 of 1 (latest)
https://stripe.com/docs/api/refunds/create?lang=php#create_refund Hi there, this is the API you can use the create a refund
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
$stripe = new \Stripe\StripeClient(
$Secretkey
);
$stripe->refunds->create([
'charge' => 'ch_3Kg1J9CCyjo9885A05OWHpcs',
]);
OK, does it work for you?
Then you should use variable instead of hard coded string.
Yes, But how to get 'chargeid'
From where or how can get 'ch_3Kg1J9CCyjo9885A05OWHpcs'
Are you using PaymentIntents API or Charges API?
No, I just create a customer and subscription
OK, so you are using Subscription API, then you should expand the latest_invoice hash of the subscription object, and get the payment_intent of the invoice.
You can also create a refund with a payment_intent (https://stripe.com/docs/api/refunds/create?lang=php#create_refund-payment_intent)
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
$stripe = new \Stripe\StripeClient(
$Secretkey
);
$customer = $stripe->customers->create([
'description' => '4 weeks recurring / 3 months programe',
'name' => $custname,
'email' => $email,
'payment_method'=> "pm_card_visa",
'invoice_settings'=> [
'default_payment_method'=> 'pm_card_visa'
]
]);
$customerid = $customer->id;
//create subscription schedule
$subscriptionsch = $stripe->subscriptionSchedules->create([
'customer' => $customerid,
'start_date' => 'now',
'end_behavior' => 'cancel',
'phases' => [
[
'items' => [
[
'price' => 'price_1M8Jd0CCyjo95AF5RE8dy6',
'quantity' => 1,
],
],
'iterations' => 2,
],
],
]);
This is my code. Please help me according to above code where exactly changes required..
Did you get my previous message? let me know if you need more help.
OK, then you can should retrieve the subscription from the schedule, retrieve the invoice and then payment_intent.
You mean to say I create Invoice..
No, the subscription will create an invoice which you can retrieve from its latest_invoice hash, and you can get the payment_intent from it.
I retrieve invoice_prefix, invoice_settings and next_invoice_sequence. But latest_invoice not
The latest_invoice is a property in subscription, not subscription schedule. You need to retrieve the subscription from the subscriptions schedule first.
👋 taking over for my colleague. Let me know if there's any follow-up Qs I can answer!
any more context please?
$stripe = new \Stripe\StripeClient(
$Secretkey
);
$refundpm = $stripe->refunds->create([
'charge' => 'ch_3M8OxHCCyjo9885A0DxEbSw7',
]);
print_r($refundpm);
This code is working. But this is hard code
so you want to get the Charge Id for what exactly?
where does that Charge Id come from?
ok but how are you getting the refund request?
Client will request me on email.
with what information exactly?
Invoice ID, payment amount, date etc.
But invoice page is created in PHP not from Stripe
which Invoice ID? in_xxx?
No, I am not generating the invoice ID with the help of Stripe but with the help of my own developed code for generating random alphanumerical value as Invoice ID
$stripe = new \Stripe\StripeClient(
$Secretkey
);
$customer = $stripe->customers->create([
'description' => '4 weeks recurring / 3 months programe',
'name' => $custname,
'email' => $email,
'payment_method'=> "pm_card_visa",
'invoice_settings'=> [
'default_payment_method'=> 'pm_card_visa'
]
]);
$customerid = $customer->id;
//create subscription schedule
$subscriptionsch = $stripe->subscriptionSchedules->create([
'customer' => $customerid,
'start_date' => 'now',
'end_behavior' => 'cancel',
'phases' => [
[
'items' => [
[
'price' => 'price_1M8Jd0CCyjo95AF5RE8dy6',
'quantity' => 1,
],
],
'iterations' => 2,
],
],
]);
$subscriptionschid = $subscriptionsch->id;
I am using Customer and Subscription schedule APIs only..
what is the Invoice Id that your customer will send you?
Invoice #1103
ok let me check
Please do the required changes in the code.. Please
For the Refunding of Payment could start
do you save this data anywhere at Stripe?
Which data?
you said you number your Invoices outside of Stripe
so Invoice #1103 is not saved in Stripe right? do you save in your db any Stripe Ids related to that invoice?
Yes you are right. I save cus_xxx and sub_sched_xxx only regarding payment
If it is possible then please join me on team viewer or any other software for remote control you have to sort out this problem. Thank you..
sorry that' not possible
It's ok.
you need to save the invoice ID in_xxx and not the sub_sched_xxx
this way it would be tied to a particular invoice
Ok. Now please tell me how to do this..
I shared my code with you. Now please guide me the required changes to be done in the code.
you haven't shared the code where you store the info in your database
when are you generating the invoice in your System instead of storing the sub_sched_xxx you need to save the in_xxx
to do so you can retrieve the Subscription Schedule, expand on the subscription field and get the latest_invoice and store that id
from there if you want to refund you can do what my colleague suggested above
If I do not store sub_sched_xxx then i am not able to cancel the subscription..
you can store both in that case
This is the file in which I store the info in database
Hey! Taking over for my colleague. Let me catch up.
Could you please summarize you latest follow-up question ?
yes sure
actually we are using two api i.e. one customers and subscription schedules.. and we are storing cus_xx and sub_sch_xx and now I want to execute refund of payment with the help of these two parameters so how that would be possible ?
and for your sake of information we are sending invoice from our own website instead of stripe invoice
and shared all the code with you .. please help regarding refund
So in order to refund the customer, you need to cancel the Subscription issued from the subscription schedule:
https://stripe.com/docs/billing/subscriptions/cancel#canceling
You can't refund an active subscription
yes we are canceling through sub_sch_xx parameter
and now I want to make refund after cancelation with php code
if you want i can share the code for subscription cancelation
You need to get the PaymentIntent used to pay the last Invoice of the subscription and refund it. You can find it in the lastest Invoice object of the subscription:
From the sub_ you get the invoiceId (or simply expand the invoice property)
https://stripe.com/docs/api/subscriptions/object#subscription_object-latest_invoice
Then from the PaymentIntent Id on that invoice
https://stripe.com/docs/api/invoices/object#invoice_object-payment_intent
You create the refund:
https://stripe.com/docs/api/refunds/create
https://stripe.com/docs/refunds?dashboard-or-api=api
I am not using subscription API. I am using subscription schedule API and latest Invoice object only we can retrieve from subscription API.
You can check my code also..
For each subscription_scheulde you have a related Subscription. So from the Subscription Schedule Id you get the related Subscription Id and so on...
https://stripe.com/docs/api/subscription_schedules/object#subscription_schedule_object-subscription
Yes, I am getting sub_xxx when we cancel the subscription schedule. But how to store this value. Please guide me.
Stripe\StripeObject Object ( ) [on_behalf_of] => [proration_behavior] => create_prorations [start_date] => 1669711806 [transfer_data] => [trial_end] => ) ) [released_at] => [released_subscription] => [renewal_interval] => [status] => canceled [subscription] => sub_xxxxxx [test_clock] => )
How can I help?
Sure, so use the sub_xxx to get the latest_invoice.payment_intent field, which you can then use to create the refund
But problem is this how to store the sub_xxx
What do you mean by 'store'?
$stripe = new \Stripe\StripeClient(
$Secretkey
);
$stripe->subscriptionSchedules->cancel(
$subscriptionschid,
[]
);
Subscription cancelation code
store means.. How to store in database..
How to get sub_xxx from cancelation code..
If should be returned in response to the API call to cancel: https://stripe.com/docs/api/subscription_schedules/cancel
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
See the subscription field
Stripe\StripeObject Object ( ) [on_behalf_of] => [proration_behavior] => create_prorations [start_date] => 1669711806 [transfer_data] => [trial_end] => ) ) [released_at] => [released_subscription] => [renewal_interval] => [status] => canceled [subscription] => sub_xxxxxx [test_clock] => )
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 center 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.
When i run cancel subscription schedule I got sub_xx
Or the sub_sched_xxx ID
sub_sched_1M9PVyCCyjo9885AeyIKBlD0
Well, this is the Subscription ID: sub_1M9PVyCCyjo9885AcQwm6P1l
It was returned in the cancellation request: https://dashboard.stripe.com/test/logs/req_ENqD3FWC0CMawE
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
Not sure why your code is redacting that to sub_xxx
Ok, now what's the next step?
Next is this how to refund..
Outlined here: #1047059320539451422 message
I want to get this sub_1M9PVyCCyjo9885AcQwm6P1l dynamically
You can probably do it when you make the call to cancel the schedule by expanding fields:
$stripe->subscriptionSchedules->cancel(
'sub_sched_xxx',
['expand' => ['subscription.latest_invoice.payment_intent']]
);
That payment_intent hash will include the pi_xxx ID you need to create the refund
Thank you. I try this code..
I run above code. But its not retrieve pi_xxx
Any other ID
Well, looking at the invoice associated with that subscription (https://dashboard.stripe.com/test/invoices/in_1M9S85CCyjo9885As5mV1uiq), there's no payment due. So we won't have created a Payment Intent
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
And there's nothing to even refund
sub_sched_1M9SbBCCyjo9885AUVsDNRt8
Same for the initial invoice on that subscription, too: https://dashboard.stripe.com/test/invoices/in_1M9SdGCCyjo9885ABwwmGV8o
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
Ok. But how it is possible...
$stripe->subscriptionSchedules->cancel(
'sub_sched_xxx',
['expand' => ['subscription.latest_invoice.payment_intent']]
);
I run above code.. which you suggest..
Well because you created the subscription which generated an invoice (https://dashboard.stripe.com/test/invoices/in_1M9SbBCCyjo9885ArsNYRTc2) and then ~immediately cancelled it so we create a refund/credit amount for the customer (https://dashboard.stripe.com/test/invoices/in_1M9SdGCCyjo9885ABwwmGV8o)
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
That last will be finalised and the customer will be 'refunded' via a credit balance adjustment in an hour
But the initial invoice was never paid, so there's nothing really to refund
Do you want to say the code you sent for the cancellation the same code will make refund as well as the same time?? Is that so?? I am little confused.
The original invoice I shared (https://dashboard.stripe.com/test/invoices/in_1M9SbBCCyjo9885ArsNYRTc2) that was generated when you created the subscription was closed unpaid – the customer never made a payment for that invoice
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
With that in mind, what exactly do you want to refund?
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
please check the link above where payment has been made and also refund option showing there in the form of button
but i want refund of payment with the code from my website admin panel for which the code i sent you already not through the stripe admin panel..
https://stripe.com/docs/refunds?dashboard-or-api=api#issuing is the code used to issue a refund from the API.
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
what's your question?
hello, my question is this
I want two separate options.. One is cancel the subscription. In this process there should not be any refund and it should stop the subscription so that future pending payments not to be processed and in the second case I want the refund option for the past payments. So basically I do not want to club these two together..
One is cancel the subscription. In this process there should not be any refund and it should stop the subscription so that future pending payments not to be processed
https://stripe.com/docs/api/subscriptions/cancel
in the second case I want the refund option for the past payments.
you'd cancel the subscription as above, and then also list all previous payments for that customer and call the refund API for each one
I used customer and subscription schedule API
$stripe = new \Stripe\StripeClient(
$Secretkey
);
$op = $stripe->subscriptionSchedules->cancel(
$subschid,
[]
);
ok, and what's the question?
Now question is this how to refund...
$stripe = new \Stripe\StripeClient(
$Secretkey
);
$refundpm = $stripe->refunds->create([
'charge' => 'ch_3M8OxHCCyjo9885A0DxEbSw7',
]);
This is my payment refund code. But I need to know How to get ch_xxx
Or any other way to refund the payment by using code..
$payment_intent->charges->data[0]->id
but you don't need it
per https://stripe.com/docs/refunds?dashboard-or-api=api#issuing linked above , it shows that you can pass the PaymentIntent ID pi_xxx to the refund API and it works.
Now Question is this how to get pi_xxxx
invoice->payment_intent
Please guide me as per my code where I add the code which you sent me.
$stripe = new \Stripe\StripeClient(
$Secretkey
);
$refundpm = $stripe->refunds->create([
'charge' => 'ch_3M8OxHCCyjo9885A0DxEbSw7',
]);
I tried above code to refund the payment...
But this is a hard code. I need dynamic code.. How to get ch_xxxx to refund the payment..
you would have an $invoice object you retrieved from the API previously, and then from there, you can pass 'charge' => $invoice->payment_intent',
As per my code where I add these parameters..
which parameters?
which you sent me
I told you in the answer
sorry this is really simple and I don't know how else I can explain it
But i am little confuse
$refundpm = $stripe->refunds->create([
'charge' => $invoice->payment_intent,
]);
Thank you..
How to get the invoice object..
Because I am not creating the invoice through stripe panel..
what do you think, where have you looked?
you said you wanted to refund all invoices for a customer's subscription which you're cancelling, right?
I just want the refund for the recent payment..
Not all the invoices for a particular customer.
and in your code you don't have that payment?
like your question is "I want to refund a payment, but I don't know how, and I don't know which payment it is", which makes it quite hard to help you.
not its not like that..
https://stripe.com/docs/api/invoices/list#list_invoices-customer is one way to list recent invoices for a customer
can you please help me only for single payment done in starting which is paid during subscription
and if in case customer want to cancel the subscription then that will cancel first or we can cancel even the subscription and thereafter for that payment we can process the refund..
I don't understand your English in this sentence sorry
you can refund the payment at any time regardless of if the subscription it's associated with is cancelled or not.
ok and that can be done from php code .. right ?
yes of course that is the exact problem where php code is running perfectly but only when i put charge_id which is taken from the stripe panel
but i want that charge_id from my own database and my query is simple how can i store that charge_id while payment was done..
actually please try to understand my query.. when i get the charge_id from the stripe panel and put as hard code in refund php code then it work fine.. no problem..
I know that
$invoice->payment_intent
you e.g. listen to the invoice.paid webhook event to be notified when a payment happens and can save the details to your database at that point, such as the Invoice's PaymentIntent.
but i dont want to get charge_id from the stripe panel .. i want to store here in my own database.. and how can i do that ?
@timber pier makes sense?
yes this make sense absolutely..
now can you help me or you can send me the php code to listen webhook for invoice.paid when a payment happen
and please one thing that i want to clear to you
i am not generating invoice from stripe
but instead i am sending invoice from my own system
@timber pier that's incorrect
you explicitly said you're using the Subscription and SubscriptionSchedule APIs
so you are using our Invoices and generating them in Stripe.
e.g. look at them all here https://dashboard.stripe.com/test/invoices
I am using Customer and Subscription Schedule API for creating customer and creating subscription schedule
yep
and those create Invoices
a SubscriptionSchedule manages a Subscription, a Subscription creates Invoices, that's what a Subscription is
ok that is correct... Subscription create invoice but I am not using that..
yes, you are.
so according to you is that mandatory to use Subscription module
to generate invoice and I have to send Invoice only from stripe panel automatically
I mean that's what our "subscription module" is.
yes i got that..
no you can do this all through the API
sorry but you're so completely lost I don't know how I can help you. You need to spend some time reading the docs and writing your own code
ok please give me one answer please
yes please..
please confirm me that can i skip Subscription module which is used to generate invoice while payment ?
and this is mandatory if I want to implement refund code ?
what do you mean by "skipping" it?
skipping means I am not using Subscription module at all in my php code..
ok ,you might not be using $stripe->subscription. But you are using $stripe->subscriptionSchedules
a SubscriptionSchedule manages a Subscription, a Subscription creates Invoices
you might not actually interact directly with the Subscription objects in your code, sure, that is possible. But you are still "using" Stripe subscriptions.
ok got it.. so according to this scenario .. can i make refund with php code because i havent used subscription and thus no invoice is there in stripe..
wrong
there is an invoice in Stripe
again, what do you think https://dashboard.stripe.com/test/invoices these are?
if you want to refund the invoice, you can use https://stripe.com/docs/api/invoices/list#list_invoices-customer to list the invoices for a given customer, and each $invoice object returned has a payment_intent field, and you can pass that ID to the refund API. I explained all that above. If you take 30 minutes to write some code and try it out, it will make sense.
ok ok... sure i do that first and then let you know if find some problem..