我試圖通過braintree.js在一個表單中提交創建新用戶(設計)和支付令牌的表單。我的html表單看起來像這樣。使用Braintree.js創建具有CC信息的用戶
<div class="container">
<h2 class='registration'>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {:class => 'col-sm-12'}) do |f| %>
<%= devise_error_messages! %>
<div class="form-horizontal">
<%= f.label :email, class: "col-sm-3 control-label" %>
<div class="col-sm-7">
<%= f.email_field :email, class: "form-control" %>
</div>
</div><br /><br />
<div class="form-horizontal">
<%= f.label :full_name, class: "col-sm-3 control-label" %>
<div class="col-sm-7">
<%= f.text_field :full_name, class: "form-control",autofocus: true %>
</div>
</div><br /><br />
<div class="form-horizontal">
<%= f.label :password, class: "col-sm-3 control-label" %>
<div class="col-sm-7">
<%= f.password_field :password, autocomplete: "off", class: "form-control" %>
</div>
</div><br /><br />
<div class="form-horizontal">
<%= f.label :password_confirmation, class: "col-sm-3 control-label" %>
<div class="col-sm-7">
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
</div>
</div><br /><br />
<input data-braintree-name="number" value="4111111111111111">
<input data-braintree-name="cvv" value="100">
<input data-braintree-name="expiration_month" value="10">
<input data-braintree-name="expiration_year" value="2020">
<div id="braintree-container"></div>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
braintree.setup("MY BRAINTREE TOKEN",
"custom",
{
container: "braintree-container",
paymentMethodNonceInputField: "payment-method-nonce"}
);
var client = new braintree.api.Client({clientToken: <%= @client_token %>});
client.tokenizeCard({number: "4111111111111111", expirationDate: "10/20"}, function (err, nonce) {
</script>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-6">
<%= f.submit "Sign up", class: "btn btn-primary form-registration", id: "user-form-submit" %>
</div>
</div><br /><br />
<% end %>
</div>
我認爲應該發生什麼,當我提交這是從。 Rails創建一個新用戶。然後Braintree.js通過CC信息向他們的服務發出請求,併發送帶有值的params [:payment method_nonce]。但是會發生什麼情況是用戶被創建,但我沒有收到有關payment_method_nonce的信息。
你可能會問爲什麼我想從一個頁面添加所有這些信息?我計劃在用戶註冊後創建一個後臺工作人員來創建訂閱。
控制器(我的用戶控制器)
class Users::RegistrationsController < Devise::RegistrationsController
def new
@client_token = Braintree::ClientToken.generate()
super
end
def create
super
end
def configure_sign_up_params
devise_parameter_sanitizer.for(:sign_up) << [:full_name, :credit_card, :cvc,
:expiration_month, :expiration_year, :token, :client_token]
end
我的模式是這樣的
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "full_name", null: false
t.string "token"
end
信息
-Rails 4 -Ruby 2.0 - Braintree.js V2
有了Braintree.js,您不需要創建兩個單獨的窗體。由於JS會處理提供的cc信息給Braintree的服務器。我想出了這個問題。我要寫一篇關於如何做我上面要求的小博客文章。 – jmoon90 2015-02-17 14:42:08