问题
I used this code to cancel stripe subscription with prorate
$user = User::find(Auth::id());
$subscription = \Stripe\Subscription::retrieve(
$user->subscription('main')->stripe_id
);
$subscription->delete([
'prorate' => true
]);
in the stripe dashboard subscription cancelled and it is created upcoming invoice with prorate amount. I want to refund prorate amount to customer's card. I tried to get prorate amount using below code.
$subscription = \Stripe\Subscription::retrieve($user->subscription('main')->stripe_id);
$subscription_item = array(
"id" => $subscription->items->data[0]->id,
"plan" => $subscription->items->data[0]->plan->id,
"quantity" => 0,
);
$proration_date = time();
$upcoming_prorated_invoice = \Stripe\Invoice::upcoming([
"customer" => $subscription->customer,
"subscription" => $subscription->id,
"subscription_items" => array($subscription_item),
"subscription_proration_date" => $proration_date, // optional
]);
dd($upcoming_prorated_invoice);
I got below error message
Stripe\Exception\InvalidRequestException
You cannot preview the upcoming invoice for a canceled subscription.
UPDATE
$subscription->delete([
'prorate' => true,
'invoice_now' => true
]);
//$latest_invoice = \Stripe\Invoice::retrieve($subscription->latest_invoice);
//$latest_charge_id = $latest_invoice->charge;
$invoice = \Stripe\Invoice::retrieve([ 'id' => $subscription->latest_invoice, 'expand' => ['payment_intent.charges'], ]);
//$charge = $invoice->payment_intent->charges->data[0];
dd($invoice);
payment_intent return as null
回答1:
Finally I got solution. ref
$subscription = \Stripe\Subscription::retrieve($user->subscription('main')->stripe_id);
$proration_date = time();
$refund = $this->prorate($subscription, $proration_date);
if ($refund) {
$latest_invoice = \Stripe\Invoice::retrieve($subscription->latest_invoice);
if ($latest_invoice) {
\Stripe\Refund::create([
'charge' => $latest_invoice->charge,
'amount' => $refund,
]);
}
}
public function prorate(\Stripe\Subscription $subscription, $proration_time)
{
$period_start = $subscription->current_period_start;
$period_end = $subscription->current_period_end;
$amount = $subscription->plan->amount;
$period_length = $period_end - $period_start;
$elapsed_since_start = $proration_time - $period_start;
$refund = $amount - floor(($elapsed_since_start / $period_length) * $amount);
return $refund > 0 ? (int) $refund : 0;
}
来源:https://stackoverflow.com/questions/62403075/stripe-api-get-upcoming-invoice-for-cancelled-subscription