在轨道上使用braintree进行简单结账

时间:2015-07-29 19:40:29

标签: ruby-on-rails paypal braintree

我尝试使用braintree创建一个简单的结帐。

我有一个沙箱帐户,并已使用braintree.rbmerchant_idpublic_key设置private_key

我的transactions控制器:

class TransactionsController < ApplicationController
  def new
    gon.client_token = generate_client_token
  end

  def create
    nonce = params[:payment_method_nonce]
    result = Braintree::Transaction.sale(
      :amount => "100.00",
      :payment_method_nonce => params[:payment_method_nonce]
    )

    if result.success?
        puts result.transaction.status
          flash[:notice] = "Transaction successful"
          redirect_to root_url
        else
          flash.now[:alert] = result.errors.to_json
          gon.client_token = generate_client_token
          render :new
        end
  end

  private

  def generate_client_token
    Braintree::ClientToken.generate
  end
end

我的观点:

<h1>Braintree transaction</h1>

<%= form_tag transactions_path, role: 'form', id: 'test' do %>
  <div class="form-group">
    <%= label_tag 'Cardholder name' %>
    <%= text_field_tag 'Cardholder name', nil, name: nil, placeholder: 'Cardholder name', data: {'braintree-name' => 'cardholder_name'}, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= label_tag 'Card number' %>
    <%= text_field_tag 'Card number', nil, name: nil, placeholder: 'Card number', data: {'braintree-name' => 'number'}, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= label_tag 'Cvv number' %>
    <%= text_field_tag 'Cvv number', nil, name: nil, placeholder: 'CVV', data: {'braintree-name' => 'cvv'}, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= label_tag 'Month' %>
    <%= text_field_tag 'Month', nil, name: nil, placeholder: 'Month in MM format', data: {'braintree-name' => 'expiration_month'}, class: 'form-control' %>
    <%= label_tag 'Year' %>
    <%= text_field_tag 'Year', nil, name: nil, placeholder: 'Year in YY format', data: {'braintree-name' => 'expiration_year'}, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%=submit_tag 'Charge', class: 'btn btn-default' %>
  </div>

<% end %>

<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
// We generated a client token for you so you can test out this code
// immediately. In a production-ready integration, you will need to
// generate a client token on your server (see section below).

braintree.setup(gon.client_token, "custom", {
  id: "test"
});

// braintree.setup(clientToken, "custom", {id: "test"});
</script>

请注意,控制器flash.now[:alert] = result.errors.to_json to_json中有一行。一旦我提交表单,我就会收到一堆错误,但真正引起我注意的是:

code":"91569","attribute":"payment_method_nonce","message":"payment_method_nonce does not contain a valid payment instrument type.

enter image description here

编辑:

根据评论中的要求编辑了我的create操作以添加puts nonce

def create
    nonce = params[:payment_method_nonce]
    puts nonce
    puts 'before this is nonce'
    result = Braintree::Transaction.sale(
      :amount => "100.00",
      :payment_method_nonce => params[:payment_method_nonce]
    )
    puts result
    puts result.success?

    if result.success?
        puts result.transaction.status
          flash[:notice] = "Transaction successful"
          redirect_to root_url
        else
          # flash[:alert] = "Transaction error: #{result.errors}"
          flash[:alert] = result.errors.to_json
          gon.client_token = generate_client_token
          render :new
        end
  end

当我post

时,我得到以下结果

enter image description here

编辑:

能够使用他们的dropin表单并且使用他们的信用卡系统成功交易,但是当您尝试登录到paypal时它会出错。

enter image description here

0 个答案:

没有答案