gpt4 book ai didi

payment-gateway - 订阅取消后,Stripe API退款

转载 作者:行者123 更新时间:2023-12-04 11:05:59 25 4
gpt4 key购买 nike

从Stripe文档中:

When you cancel the subscription, the customer's card will not be charged again, but no money will be refunded either. If you'd like to issue a refund, you can do so through Stripe's dashboard or via the API.



我创建了每月订阅,我只想退还订阅月份内尚未过去的天数。在Stripe API尚未完成的日子里,如何只退还订阅中的金额?

最佳答案

Stripe现在将处理此问题。它可以立即取消订阅并创建发票,将月内未使用的费用退还给他们。

https://stripe.com/docs/api/subscriptions/cancel

添加“比例”标签以退还剩余的每月费用。

在PHP中:

$subscription = \Stripe\Subscription::retrieve(
'SUBSCRIPTION_ID'
);

$subscription->delete([
'prorate' => true
]);

编辑:

@Dobes Vandermeer在下面评论并指出了我的错误。上述方法实际上不会按比例退还客户款项,而只会创建一个帐户信用,该信用将应用于下一张发票。

以下代码实际上会将按比例分配的金额退还给卡。如果客户将其订购数量切换为零,它将生成按比例分配的发票示例。然后,它会从其有效订阅的最后一张发票中按比例退还该金额。

“subscription_proration_date”标记是可选的,但对于需要从特定时间点按比例分配订阅的情况很有用。

// get active subscription
$subscription = \Stripe\Subscription::retrieve(' SUBSCRIPTION_ID ');

// sample prorated invoice for a subscription with quantity of 0
$sample_subscription_item = array(
"id" => $subscription->items->data[0]->id,
"plan" => $subscription->items->data[0]->plan->id,
"quantity" => 0,
);

$upcoming_prorated_invoice = \Stripe\Invoice::upcoming([
"customer" => $subscription->customer,
"subscription" => $subscription->id,
"subscription_items" => array($sample_subscription_item),
"subscription_proration_date" => ' PRORATED_DATE ', // optional
]);

// find prorated amount
$prorated_amount = 0;
foreach($upcoming_prorated_invoice->lines->data as $invoice) {
if ($invoice->type == "invoiceitem") {

$prorated_amount = ($invoice->amount < 0) ? abs($invoice->amount) : 0;

break;

}
}

// find charge id on the active subscription's last invoice
$latest_invoice = \Stripe\Invoice::retrieve($subscription->latest_invoice);
$latest_charge_id = $latest_invoice->charge;

// refund amount from last invoice charge
if ($prorated_amount > 0) {

$refund = \Stripe\Refund::create([
'charge' => $latest_charge_id,
'amount' => $prorated_amount,
]);

}

// delete subscription
$subscription->delete();

关于payment-gateway - 订阅取消后,Stripe API退款,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24790776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com