2017-10-21 78 views
0

當有人目前註冊一個服務器,他們點擊一個計劃,並彈出顯示帶條紋結賬彈出窗口(要求信用卡信息等)使用API​​,我怎麼知道客戶有一個信用卡存檔?

如果有人更新他們的計劃,我不' t需要顯示分條結賬彈出窗口。我怎樣才能確定客戶不僅存在條紋,而且他們擁有條紋系統中的信用卡,所以我只需更新/更改他們的計劃,而不要求他們重新輸入他們的信用卡信息。

回答

0

您可以在客戶表中添加一列,其中包含值true或false,並且在創建客戶時,它可以創建默認值false。當客戶卡被確認存檔時,該值可以更改爲真。

1

我會建議創建一個條紋客戶並將條紋客戶ID存儲在您的用戶表中。在此,我假設您的客戶/用戶屬於User模型的一部分,並在數據庫中的users表中進行跟蹤。

Stripe文檔有一個recipe用於創建Stripe客戶。我們將在這裏借鑑。

首先,您需要將Stripe gem添加到您的Gemfile中。

接下來,您將需要運行遷移以將字符串列添加到名爲stripe_customer_idusers表中。

下一頁添加以下到你的用戶模式:

def get_or_create_stripe_customer!(stripe_token, stripe_email = nil) 
    return self.stripe_customer_id if self.stripe_customer_id.present? 
    stripe_email = self.email if stripe_email.nil? 

    customer = customer = Stripe::Customer.create(
    :email => stripe_email, 
    :source => stripe_token, 
) 

    self.update_attribute(:stripe_customer_id, customer.id) 
    return customer.id 
end 

從你的控制器,處理付款,你可以撥打

current_user.get_or_create_stripe_customer! params[:stripe_token], params[:stripe_email] 

這將創建一個新的條紋客戶或檢索條紋顧客ID。注意:在此代碼示例中,current_user是代表登錄用戶的變量。

您可以簡單地查詢current_user.stripe_customer_id.present?以確定客戶是否有信用卡存檔。您也可以使用stripe_customer_id創建未來費用。

Stripe Charges documentation可以引導您瞭解有關創建Stripe客戶(即保留信用卡信息)和使用Stripe客戶ID創建新費用的更多詳細信息。

相關問題