#Sandeep

1 messages · Page 1 of 1 (latest)

stark kettleBOT
topaz flume
timber pier
#

$stripe = new \Stripe\StripeClient(
$Secretkey
);
$stripe->refunds->create([
'charge' => 'ch_3Kg1J9CCyjo9885A05OWHpcs',
]);

topaz flume
#

OK, does it work for you?

timber pier
#

yes its working

#

But its static code. I want dynamic

topaz flume
#

Then you should use variable instead of hard coded string.

timber pier
#

Yes, But how to get 'chargeid'

#

From where or how can get 'ch_3Kg1J9CCyjo9885A05OWHpcs'

topaz flume
#

Are you using PaymentIntents API or Charges API?

timber pier
#

No, I just create a customer and subscription

topaz flume
#

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.

timber pier
#

$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..

topaz flume
#

Did you get my previous message? let me know if you need more help.

timber pier
#

Yes, I got.

#

I am using subscription schedule API

topaz flume
#

OK, then you can should retrieve the subscription from the schedule, retrieve the invoice and then payment_intent.

timber pier
#

You mean to say I create Invoice..

topaz flume
#

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.

timber pier
#

I retrieve invoice_prefix, invoice_settings and next_invoice_sequence. But latest_invoice not

topaz flume
#

The latest_invoice is a property in subscription, not subscription schedule. You need to retrieve the subscription from the subscriptions schedule first.

thorny gulch
#

👋 taking over for my colleague. Let me know if there's any follow-up Qs I can answer!

timber pier
#

Hello

#

How to get Charge Id

thorny gulch
#

any more context please?

timber pier
#

$stripe = new \Stripe\StripeClient(
$Secretkey
);
$refundpm = $stripe->refunds->create([
'charge' => 'ch_3M8OxHCCyjo9885A0DxEbSw7',
]);
print_r($refundpm);

#

This code is working. But this is hard code

thorny gulch
#

so you want to get the Charge Id for what exactly?

#

where does that Charge Id come from?

timber pier
#

I want to refund the payment using PHP

#

So I use Refund API

thorny gulch
#

ok but how are you getting the refund request?

timber pier
#

Client will request me on email.

thorny gulch
#

with what information exactly?

timber pier
#

Invoice ID, payment amount, date etc.

#

But invoice page is created in PHP not from Stripe

thorny gulch
#

which Invoice ID? in_xxx?

timber pier
#

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..

thorny gulch
#

what is the Invoice Id that your customer will send you?

timber pier
#

Invoice #1103

thorny gulch
#

ok let me check

timber pier
#

Please do the required changes in the code.. Please

#

For the Refunding of Payment could start

thorny gulch
timber pier
#

Which data?

thorny gulch
#

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?

timber pier
#

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..

timber pier
#

It's ok.

thorny gulch
#

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

timber pier
#

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.

thorny gulch
#

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

thorny gulch
timber pier
#

If I do not store sub_sched_xxx then i am not able to cancel the subscription..

thorny gulch
#

you can store both in that case

timber pier
fleet marsh
#

Hey! Taking over for my colleague. Let me catch up.

timber pier
#

Hello.

#

Please help me..

fleet marsh
#

Could you please summarize you latest follow-up question ?

timber pier
#

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

fleet marsh
timber pier
#

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

fleet marsh
#

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

timber pier
#

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..

fleet marsh
timber pier
#

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] => )

harsh wren
#

How can I help?

timber pier
#

Hello

#

I want to refund the payment using PHP

harsh wren
#

Sure, so use the sub_xxx to get the latest_invoice.payment_intent field, which you can then use to create the refund

timber pier
#

But problem is this how to store the sub_xxx

harsh wren
#

What do you mean by 'store'?

timber pier
#

$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..

harsh wren
#

See the subscription field

timber pier
#

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] => )

harsh wren
timber pier
#

When i run cancel subscription schedule I got sub_xx

harsh wren
#

Or the sub_sched_xxx ID

timber pier
#

sub_sched_1M9PVyCCyjo9885AeyIKBlD0

harsh wren
#

Not sure why your code is redacting that to sub_xxx

timber pier
#

sub_1M9PVyCCyjo9885AcQwm6P1l

#

Yes, It is right

harsh wren
#

Ok, now what's the next step?

timber pier
#

Next is this how to refund..

harsh wren
timber pier
#

I want to get this sub_1M9PVyCCyjo9885AcQwm6P1l dynamically

harsh wren
#

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

timber pier
#

Thank you. I try this code..

#

I run above code. But its not retrieve pi_xxx

#

Any other ID

harsh wren
#

And there's nothing to even refund

timber pier
#

sub_sched_1M9SbBCCyjo9885AUVsDNRt8

harsh wren
timber pier
#

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..

harsh wren
#

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)

#

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

timber pier
#

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.

harsh wren
#

With that in mind, what exactly do you want to refund?

timber pier
#

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..

strong portal
timber pier
strong portal
#

what's your question?

timber pier
#

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..

strong portal
#

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

timber pier
#

I used customer and subscription schedule API

#

$stripe = new \Stripe\StripeClient(
$Secretkey
);
$op = $stripe->subscriptionSchedules->cancel(
$subschid,
[]
);

strong portal
#

ok, and what's the question?

timber pier
#

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..

strong portal
#

$payment_intent->charges->data[0]->id

#

but you don't need it

timber pier
#

Now Question is this how to get pi_xxxx

strong portal
#

invoice->payment_intent

timber pier
#

Please guide me as per my code where I add the code which you sent me.

strong portal
#

what have you tried and where are you stuck?

#

we're not writing code for you.

timber pier
#

$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..

strong portal
timber pier
#

As per my code where I add these parameters..

strong portal
#

which parameters?

timber pier
#

which you sent me

strong portal
#

I told you in the answer

#

sorry this is really simple and I don't know how else I can explain it

timber pier
#

But i am little confuse

strong portal
#
  $refundpm = $stripe->refunds->create([
    'charge' => $invoice->payment_intent,
    ]);
timber pier
#

Thank you..

#

How to get the invoice object..

#

Because I am not creating the invoice through stripe panel..

strong portal
#

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?

timber pier
#

I just want the refund for the recent payment..

#

Not all the invoices for a particular customer.

strong portal
#

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.

timber pier
#

not its not like that..

strong portal
timber pier
#

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..

strong portal
strong portal
timber pier
strong portal
#

yes

#

that's what we've been talking about for the last 5 hours

timber pier
#

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..

strong portal
#

I know that

strong portal
#

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.

timber pier
#

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 ?

strong portal
#

@timber pier makes sense?

timber pier
#

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

strong portal
#

@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.

timber pier
#

I am using Customer and Subscription Schedule API for creating customer and creating subscription schedule

strong portal
#

yep

#

and those create Invoices

#

a SubscriptionSchedule manages a Subscription, a Subscription creates Invoices, that's what a Subscription is

timber pier
#

ok that is correct... Subscription create invoice but I am not using that..

strong portal
#

yes, you are.

timber pier
#

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

strong portal
#

I mean that's what our "subscription module" is.

timber pier
strong portal
#

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

timber pier
#

ok please give me one answer please

strong portal
#

oh another one "just one"?

#

go ahead

timber pier
#

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 ?

strong portal
#

what do you mean by "skipping" it?

timber pier
#

skipping means I am not using Subscription module at all in my php code..

strong portal
#

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.

timber pier
#

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..

strong portal
#

there is an invoice in Stripe

#

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.

timber pier
#

ok ok... sure i do that first and then let you know if find some problem..