For my application, I have Users. I am trying to utilize Balanced Payments to allow users to save their credit card information for use at a later time. I have followed the two links below as guide and currently have the following error:
Routing Error
No route matches [POST] "/store_credit_card"
I followed this GIST and Rails form submit handler not being executed.
- balanced_controller.rb- created
- _credit_card_form.html.haml- created and put under- app>view>balanced
- credit_card_submission.js- created and put under- app>assets>javascript
- Added a field :balanced_account_uri stringin my user model
- Replaced the placeholder text in the .js files for the Balanced marketplace uri
- Updated routes.rb for the methods in balanced_controller.rb
Does somebody know why it is not working?
routes.rb
resources :users do
    resources :balanced do
      member do
        post :store_credit_card, :create_balanced_account
      end
    end
end
view/users/edit.html.erb
<div class = "container">
<script type="text/javascript" src="https://js.balancedpayments.com/v1/balanced.js"></script>
<script type="text/javascript">
    balanced.init('YOUR MARKETPLACE URI HERE');
</script>
<%= render 'balanced/credit_card_form' %>
</div>
view/balanced/_credit_card_form.html.haml
= form_tag "/store_credit_card", :method => :post, :id => "credit-card-form", :class => "form-horizontal" do
  .control-group
    = label_tag "cc-number", "Card Number", :class => "control-label"
    .controls
      = text_field_tag "cc-number", nil, :placeholder => "Enter Credit Card Number", :class => "cc-number", :autocomplete => "off"
  .control-group
    = label_tag "cc-em", "Expiration", :class => "control-label"
    .controls
      = text_field_tag "cc-em", nil, :placeholder => "Expiration Month", :class => "cc-em", :autocomplete => "off"
      = text_field_tag "cc-em", nil, :placeholder => "Expiration Year", :class => "cc-ey", :autocomplete => "off"
  .control-group
    = label_tag "cc-csc", "Security Code", :class => "control-label"
    .controls
      = text_field_tag "cc-csc", nil, :placeholder => "Security Code", :class => "cc-csc", :autocomplete => "off"
  .control-group
    .controls
      = submit_tag "Submit", :class => "btn btn-primary btn-large"
assets/javascript/credit_card_submissions.js
var marketplaceUri = 'YOUR MARKETPLACE URI HERE';
var requestBinUrl = '/store_credit_card'
var debug = function(tag, content) {
  $('<' + tag + '>' + content + '</' + tag + '>').appendTo('#result');
};
try {
balanced.init(marketplaceUri);
} catch (e) {
debug('code', 'balanced.init error!');
}
function balancedCallback(response) {
var tag = (response.status < 300) ? 'pre' : 'code';
debug(tag, JSON.stringify(response));
switch(response.status) {
    case 201:
        console.log(response.data);
        var $form = $("#credit-card-form");
        var card_token_uri = response.data['uri'];
        $('<input>').attr({
            type: 'hidden',
            value: card_token_uri,
            name: 'balancedCreditCardURI'
        }).appendTo($form);
        $form.attr({action: requestBinUrl});
        $form.get(0).submit();
        break;
    case 400:
        console.log(response.error);
        break;
    case 404:
        console.log(response.error);
        break;
}
}
var tokenizeCreditCard = function(e) {
e.preventDefault();
var $form = $('#credit-card-form');
var creditCardData = {
    card_number: $form.find('.cc-number').val(),
    expiration_month: $form.find('.cc-em').val(),
    expiration_year: $form.find('.cc-ey').val(),
    security_code: $form.find('.cc-csc').val()
};
balanced.card.create(creditCardData, balancedCallback);
};
$(function(){
$('#credit-card-form').submit(tokenizeCreditCard);
});
balanced_controller.rb
class BalancedController < ApplicationController
  def create_balanced_account
    current_user.balanced_account_uri = Balanced::Marketplace.my_marketplace.create_account(:email => current_user.email, :type => 'person').uri
    current_user.save
    # add a redirect to the desired path
    redirect_to root_url
  end
  def store_credit_card
    balanced_account = Balanced::Account.find(current_user.balanced_account_uri)
    balanced_account.add_card(params[:balancedCreditCardURI])
    # add a redirect to the desired path
    redirect_to root_url
  end
  def store_bank_account
    balanced_account = Balanced::Account.find(current_user.balanced_account_uri)
    balanced_account.add_card(params[:balancedBankAccountURI])
    # add a redirect to the desired path
    redirect_to root_url
  end
  def process_payment
    balanced_account = Balanced::Account.find(current_user.balanced_account_uri)
    account.debit(
      :amount => 1000, # or params[:amount]
      :description => "Balanced Payments transaction",
      :appears_on_statement_as => "Balanced Payments")
    # add a redirect to the desired path
    redirect_to root_url
  end
end