取消订阅后 Stripe API 退款 [英] Stripe API refund after subscription cancelled

查看:121
本文介绍了取消订阅后 Stripe API 退款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Stripe 文档:

From the Stripe docs:

当您取消订阅时,客户的卡将不会被再次收费,但也不会退还任何款项.如果你愿意退款,您可以通过 Stripe 的仪表板或通过API.

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 完成的那几天的订阅金额?

I created a monthly subscription, and I only want to refund the amount of money for the number of days that have not yet passed within the month of the subscription. How can I refund only the amount of money from the subscription for the days that are not complete yet with the Stripe API?

推荐答案

Stripe 现在会处理这个问题.它可以立即取消订阅并创建发票,退还他们当月未使用部分的费用.

Stripe will handle this now. It can immediately cancel the subscription and create an invoice refunding them the cost of the unused part of the month.

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

添加按比例分配"标签以退还剩余的每月费用.

Add the "prorate" tag to refund the remaining monthly cost.

在 PHP 中:

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

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

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

@Dobes Vandermeer commented below and pointed out my mistake. The above method will not actually refund the customer the prorated amount but only create an account credit which will be applied to the next invoice.

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

The code below will actually refund the prorated amount to the card. It generates a sample prorated invoice for if the customer switched their subscription quantity to zero. It then refunds that prorated amount from the last invoice on their active subscription.

subscription_proration_date"标签是可选的,但在您需要从某个时间点按比例分配订阅时很有用.

The "subscription_proration_date" tag is optional but useful for if you need to prorate the subscription from a certain point in time.

// 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();

这篇关于取消订阅后 Stripe API 退款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆