2014-07-16 104 views
5

從條紋文檔:條紋API退款取消

要取消訂閱,客戶的卡不會被再次 充電,但沒有錢會退還任何。如果您想要 退款,您可以通過Stripe的儀表板或通過 API來完成。

我創建了月度訂閱,我只想退還訂閱月份內尚未通過的天數的金額。我怎樣才能從尚未完成Stripe API的訂閱中退款?

+2

這是中期-2016,它的驚人的條紋仍然沒有實現整個預訂生命週期(特別是生命週期結束) – DeepSpace101

回答

3

如果您想讓Stripe處理退款計算,您可以將訂購數量更改爲0,然後取消計劃。

+0

要詳細說明此技巧,您需要將數量設置爲零,然後檢索即將到來的發票以獲取已退回的金額(comingInvoice.total * -1)。要簽發退款,請抓取最新的付款發票(不是即將推出的),並使用付款ID發出退款。 – claviska

0

研究了一段時間後,我來到這個流程編寫的JavaScript的Node.js:

refundAndUnsubscribe = async function() { 
try { 

    // Set proration date to this moment: 
    const proration_date = Math.floor(Date.now()/1000); 

    let sub = await stripe.subscriptions.retrieve("sub_CILnalN9HpvADj"); 

    // See what the next invoice would look like with a plan switch 
    // and proration set: 
    let items = [{ 
     quantity: 0, 
     id: sub.items.data[0].id, 
     plan: "your_plan" // Switch to new plan 
    }]; 


    let invoices = await stripe.invoices.retrieveUpcoming('cus_CIP9dtlq143gq7', 'sub_CILnalN9HpvADj', { 
     subscription_items: items, 
     subscription_proration_date: proration_date 
    }); 

    //List all invoices 
    let payedInvoicesList = await stripe.invoices.list({ 
     customer: 'cus_CIP9dtlq143gq7' 
    }); 

    // Calculate the proration cost: 
    let current_prorations = []; 
    let cost = 0; 
    for (var i = 0; i < invoices.lines.data.length; i++) { 
     let invoice_item = invoices.lines.data[i]; 
     if (invoice_item.period.start == proration_date) { 
      current_prorations.push(invoice_item); 
      cost += invoice_item.amount; 
     } 
    } 

    //create a refund 
    if (cost !== 0) { 
     cost = (cost < 0) ? cost * -1 : cost //A positive integer in cents 

     let refund = await stripe.refunds.create({ 
      charge: payedInvoicesList.data[0].charge, 
      amount: cost 
     }); 
    } 

    // delete subscription 
    return stripe.subscriptions.del('sub_CILnalN9HpvADj'); 

} catch (e) { 
    console.log(e); 
} 

}