簡體   English   中英

Stripe - 檢查客戶是否存在

[英]Stripe - check if a customer exists

在條紋文檔中,他們有這個:

customer = Stripe::Customer.create(email: params[:stripeEmail], card: params[:stripeToken])
charge = Stripe::Charge.create(customer: customer.id, amount: price, description: '123', currency: 'usd')

但我認為對於每次付款我們必須首先檢查客戶是否存在是錯誤的,我只是用一個測試帳戶進行了嘗試,結果發現創建了許多具有相同 email 但不同 ID 的不同客戶。

如何檢查客戶是否已經存在?

沒有檢查 Stripe 的一端以確保客戶的唯一性(電子郵件、卡片、姓名等),這是您必須在自己端做的事情。

通常,當您使用特定電子郵件地址創建客戶時,您需要將從 API 返回的客戶 ID 與電子郵件地址相關聯。 然后下次,您檢查此電子郵件地址是否已在您的系統中,然后創建新客戶或重新使用上一次的客戶 ID。

您可以通過使用電子郵件作為表單 urlencoded參數調用GET /customers來檢查客戶是否存在。 您將收到 200 OK 響應,但如果此客戶電子郵件不存在,則返回的 data[] 將為空。

https://stripe.com/docs/api/customers/list

我認為一個簡單的“存在”測試對於避免生成不需要的異常是必不可少的。

如果您的客戶是新客戶,那么他們將沒有條帶 ID,這很好。 但是,您可能會遇到這樣一種情況:您使用具有現有條帶 ID 的現有客戶,但執行了某種休眠的條帶側清理。 在這種情況下,最好測試提供的 stripeID 以查看其是否仍然有效。

例如,我將 stripeID 與客戶資料一起存儲。 當我付款時,我會檢查配置文件中的 stripeID 字段是否存在。 如果是,我會向我的后端發出 API 請求以檢查它的存在。

有些像這樣。

    [HttpGet]
    public ActionResult<bool> CheckCustomer(string id)
    {

        var service = new CustomerService();

        try
        {
            service.Get(id);
        }
        catch
        {
            return Ok(false);
        }

        return Ok(true);
    }

我不需要結果,只要不拋出異常就足夠了。

然后,前端可以使用 stripeId 進行付款,也可以先創建新的 Stripe 客戶,然后再進行付款。

您可以通過email地址搜索客戶...

public async Task<Customer> GetCustomerByEmail(string oneEmail)
{
    var service = new CustomerService();
    try
    {
            StripeList<Customer> customerList = await service.ListAsync(new CustomerListOptions() { Email=oneEmail },null);
            Debug.WriteLine(customerList.ToList().Count);
            return customerList.ToList().FirstOrDefault();
    }
    catch
    {
        return null;
    }

}

NoMethodError `sources' for # <stripe::customer with rails< div><div id="text_translate"><p> 當我嘗試從 Stripe 客戶接收數據時,我收到錯誤未定義方法“來源”。</p><p> 這是我使用的代碼:</p><pre> def update_payment if.current_user:stripe_id customer = Stripe:.Customer:create( email. current_user,email: source: params[:stripeToken] ) else customer = Stripe:.Customer.update( current_user,stripe_id: source: params[.stripeToken] ) end if current_user:update(stripe_id. customer,id: stripe_last_4. customer.sources.data:first["last4"]) flash[:notice] = "New card is saved" else flash[.alert] = "Invalid card" end redirect_to request:referrer rescue Stripe::CardError =&gt; e flash[.alert] = e.message redirect_to request.referrer end</pre><p> 客戶變量中的 output 不為空,所以我不明白這個錯誤是如何發生的。</p></div></stripe::customer>

[英]NoMethodError `sources' for #<Stripe::Customer with Rails

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM