2014-09-23 53 views
0

我的目的是驗證用戶卡信息並將該信息存儲在客戶對象中。創建客戶w /簡單條紋簽出

我運行一個按需付費的送貨服務,並構建一個黑客來防止僞造訂單(如果他們發出虛假訂單或沒有出現,我們可以通過帶狀儀表板收費)。

完整的條紋整合是長期目標,但我需要一些儘快。我已閱讀(重新閱讀)文檔,但遇到麻煩。

簡單的條紋檢出效果很好,但我對如何從那裏創建客戶一無所知。

腳本:

<form action="/charge" method="POST"> 
     <script 
     src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
     data-key="TEST KEY" 
     data-image="/square-image.png" 
     data-name="Food Bazooka" 
     data-description="Customer Verification. No charges :)" 
     data-panel-label="Confirm and Verify" 
     data-label="Confirm"> 
     </script> 


    </form> 

任何意見或想法是極大的讚賞。

謝謝!

回答

1

您正在使用嵌入式表單,因爲您需要使用自定義表單和一些服務器端代碼。

您需要首先創建代表從形式 顧客卡單次使用令牌(假設你的表單包含信用卡號碼,有效期等)

從文檔摘自:

表標記:

<form action="/customer" method="POST" id="payment-form"> 
    <span class="payment-errors"></span> 

    <div class="form-row"> 
    <label> 
     <span>Card Number</span> 
     <input type="text" size="20" data-stripe="number"/> 
    </label> 
    </div> 

    <div class="form-row"> 
    <label> 
     <span>CVC</span> 
     <input type="text" size="4" data-stripe="cvc"/> 
    </label> 
    </div> 

    <div class="form-row"> 
    <label> 
     <span>Expiration (MM/YYYY)</span> 
     <input type="text" size="2" data-stripe="exp-month"/> 
    </label> 
    <span>/</span> 
    <input type="text" size="4" data-stripe="exp-year"/> 
    </div> 

    <button type="submit">Submit Payment</button> 
</form> 

的Javascript:

jQuery(function($) { 
    $('#payment-form').submit(function(event) { 
    var $form = $(this); 

    // Disable the submit button to prevent repeated clicks 
    $form.find('button').prop('disabled', true); 

    Stripe.card.createToken($form, stripeResponseHandler); 

    // Prevent the form from submitting with the default action 
    return false; 
    }); 
}); 

function stripeResponseHandler(status, response) { 
    var $form = $('#payment-form'); 

    if (response.error) { 
    // Show the errors on the form 
    $form.find('.payment-errors').text(response.error.message); 
    $form.find('button').prop('disabled', false); 
    } else { 
    // response contains id and card, which contains additional card details 
    var token = response.id; 
    // Insert the token into the form so it gets submitted to the server 
    $form.append($('<input type="hidden" name="stripeToken" />').val(token)); 
    // and submit 
    $form.get(0).submit(); 
    } 
}; 

這實質上是把你的形式,並增加了一個隱藏字段名爲stripeToken提交

通知的形式動作/客戶

我看你使用Ruby on Rails的從你的標籤之前 - 這樣你需要與控制器

這來處理客戶的POST是你需要做什麼:
https://stripe.com/docs/tutorials/charges#saving-credit-card-details-for-later

# Set your secret key: remember to change this to your live secret key in production 
# See your keys here https://dashboard.stripe.com/account 
Stripe.api_key = "sk_test_X9S2nHIxFy399uoNvakwJYSn" 

# Get the credit card details submitted by the form 
# notice stripeToken - this is the hidden field 
token = params[:stripeToken] 

# Create a Customer 
customer = Stripe::Customer.create(
    :card => token, 
    :description => "[email protected]" 
) 

# Charge the Customer instead of the card 
# ** I have commented this block out, as you say you do not want to charge the customer 
# Stripe::Charge.create(
    # :amount => 1000, # incents 
    # :currency => "gbp", 
    # :customer => customer.id 
#) 

# Save the customer ID in your database so you can use it later 
save_stripe_customer_id(user, customer.id)