本文介绍了Stripe API:获取即将取消的订阅的发票的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用此代码以按比例取消条带订阅
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);
我收到以下错误消息
您无法预览即将取消订阅的发票.
You cannot preview the upcoming invoice for a canceled subscription.
更新
$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返回null
payment_intent return as null
推荐答案
最后我得到了解决方案.参考
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;
}
这篇关于Stripe API:获取即将取消的订阅的发票的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!