Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Refund Stripe Subscription charged amount?

I am Implementing a case in which i need to cancel customer subscription and refund amount for the same. I am able to cancel Subscription using

$sub = \Stripe\Subscription::retrieve({SUBSCRIPTION_ID});
$sub->cancel();

now i have to refund charged amount

 $refund = \Stripe\Refund::create(array(
  "charge" => "{CHARGE_ID}"
));

here CHARGE_ID is compulsory. no option like SUBSCRIPTION_ID.

As it charged customer automatically i am not able to store CHARGE_ID. so how can i refund that subscription amount??

Please Help

Thanks

like image 315
Meera Tank Avatar asked Aug 24 '17 12:08

Meera Tank


1 Answers

@Meera, I see you solved your problem, my solution was:
1. once you've cancelled your Subscription
2. get all Invoice Collection using the subscriptionId from the Subscription
3. pull the first Invoice from the Invoice Collection
4. create a Refund using the chargeId from the Invoice

$subscriptionId = $objSubscription->id;
$objInvoiceCollection = \Stripe\Invoice::all([
    'subscription' => $subscriptionId
]);

if ($objInvoiceCollection->total_count === 0) {
    throw new \Exception("warning: \$subscriptionId={$subscriptionId} - no invoices found!");
} else {
    $objInvoice = current($objInvoiceCollection);
}

$chargeId = $objInvoice->charge;
$objRefund = \Stripe\Refund::create(['charge' => $chargeId]);
like image 70
Wee Zel Avatar answered Nov 18 '22 23:11

Wee Zel