如何退还 Stripe 订阅费用?

Mee*_*ank 3 php laravel stripe-payments

我正在实施一个案例,我需要取消客户订阅并退还相同的金额。我可以使用取消订阅

$sub = \Stripe\Subscription::retrieve({SUBSCRIPTION_ID});
$sub->cancel();
Run Code Online (Sandbox Code Playgroud)

现在我必须退还已收取的金额

 $refund = \Stripe\Refund::create(array(
  "charge" => "{CHARGE_ID}"
));
Run Code Online (Sandbox Code Playgroud)

这里CHARGE_ID是强制性的。没有像SUBSCRIPTION_ID.

由于它自动向客户收费,我无法存储CHARGE_ID。那么我该如何退还订阅金额??

请帮忙

谢谢

Wee*_*Zel 5

@Meera,我看到您解决了您的问题,我的解决方案是:
1. 一旦您取消订阅
2. 使用订阅中的 subscriptionId 获取所有发票集合
3. 从发票集合中提取第一个发票
4. 创建退款使用发票中的chargeId

$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]);
Run Code Online (Sandbox Code Playgroud)