Stripe charges retrieving shipping information - ruby-on-rails

I'm trying to create an order through a stripe charge. Currently, I can grab the billing address from a charge with billing_address: charge.source.address_line1 but I cannot grab the shipping information.
ChargesController
class ChargesController < ApplicationController
def create
#product = Product.find(params[:product_id])
#1 Create a charge
customer = Stripe::Customer.create(
email: current_user.email,
card: params[:stripeToken]
)
charge = Stripe::Charge.create(
customer: customer.id, # Note -- this is NOT the user_id in your app
amount: (#product.price * 100).to_i,
description: #product.title,
currency: 'usd',
)
#3 Create an Order
Order.create(
user: current_user,
billing_address: charge.source.address_line1
)
flash[:notice] = "Thank you for your order, #{current_user.email}! "
redirect_to books_path # or wherever
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
The form
<%= form_tag charges_path do %>
<h4>Click the button!</h4>
<script
class='stripe-button'
src="https://checkout.stripe.com/checkout.js"
data-key="<%= #stripe_btn_data[:key] %>"
data-amount=<%= #stripe_btn_data[:amount] %>
data-description="<%= #stripe_btn_data[:description] %>"
data-shipping-address="<%= #stripe_btn_data[:shipping] %>"
data-billing-address="<%= #stripe_btn_data[:billing] %>"
></script>
<%= hidden_field_tag :product_id, #product.id %>
PostsController
def show
#stripe_btn_data = {
key: "#{ Rails.configuration.stripe[:publishable_key] }",
description: #product.title,
amount: (#product.price * 100),
shipping: :true,
billing: :true
}
How do I grab the shipping information from the stripe charge?

After some trial and error, I figured out how to insert the Stripe charge into my db with... shipping_address: params[:stripeShippingAddressLine1]
Order.create(
user: current_user,
billing_address: charge.source.address_line1,
billing_city: charge.source.address_city,
billing_state: charge.source.address_state,
billing_zip: charge.source.address_zip,
shipping_address: params[:stripeShippingAddressLine1]
)

Related

Trying to pass/fetch data for stripe api

I'm quite new to Ruby on Rails and I have been following this tutorial on how to implement an online marketplace by using Stripe's Connect API. This tutorial guides you on how to make single item purchases, I have tried to advance myself past this tutorial and create a marketplace where a user can purchase multiple items and put them in a basket and checkout, a nice challenge as the tutorial is focused on one item. However, I am stuck on the checkout _form.html.erb.
Before, to fetch the publishable_key,
<%= form_with model: current_user, local: true, url: subscription_url, method: :post, html: { id: "payment-form", class: "stripe-form" }, **data: { stripe_key: product.user.publishable_key }** do |form| %>
but now because I am submitting a cart full of products instead of one single item the data: { stripe_key: } is now dysfunctional. Instead, I thought it might be a good idea to do data: { stripe_key: #account.publishable_key } to fetch the publishable_key from the database but it is not working.
I am most likely forgetting something important. Any help would be sound!
http://localhost:3000/subscription/new?account=4&amount=17
The Error
NoMethodError in Subscriptions#new
Showing /Users/kaneandrewgibson/Desktop/Charlie/WOP/app/views/subscriptions/_form.html.erb where line #1 raised:
undefined method `publishable_key' for nil:NilClass
Subscriptions/_form.html.erb
<%= form_with model: current_user, local: true, url: subscription_url, method: :post, html: { id: "payment-form", class: "stripe-form" }, data: { stripe_key: #account.publishable_key } do |form| %>
<div>
<label for="card-element" class="label">
Credit or debit card
</label>
<div id="card-element">
<!-- A Stripe Element will be inserted here. -->
</div>
<!-- Used to display Element errors. -->
<div id="card-errors" role="alert" class="text-sm text-red-400"></div>
</div>
<input type="hidden" name="account" value="<%= params[:account] %>">
<button>Back <%= number_to_currency(params[:amount]) %> /mo toward <em><%= %></em></button>
<% end %>
SubscriptionsController
class SubscriptionsController < ApplicationController
before_action :authenticate_user!
def new
#account = User.find_by_id(params[:id])
end
# Reference:
# https://stripe.com/docs/connect/subscriptions
def create
#account = User.find_by_id(params[:id])
key = #account.user.access_code
Stripe.api_key = key
plan_id = params[:plan]
plan = Stripe::Plan.retrieve(plan_id)
token = params[:stripeToken]
customer = if current_user.stripe_id?
Stripe::Customer.retrieve(current_user.stripe_id)
else
Stripe::Customer.create(email: current_user.email, source: token)
end
Stripe::PaymentIntent.create({
customer: customer,
amount: #cart.total_price * 100,
confirm: true,
currency: 'gbp',
payment_method_types: ['card'],
application_fee_percent: 3,
}, {
stripe_account: 'key',
})
options = {
stripe_id: customer.id,
subscribed: true,
}
options.merge!(
card_last4: params[:user][:card_last4],
card_exp_month: params[:user][:card_exp_month],
card_exp_year: params[:user][:card_exp_year],
card_type: params[:user][:card_brand]
)
current_user.perk_subscriptions << plan_id
current_user.update(options)
# Update project attributes
project_updates = {
backings_count: #project.backings_count.next,
current_donation_amount: #project.current_donation_amount + (plan.amount/100).to_i,
}
#project.update(project_updates)
redirect_to root_path, notice: "Your subscription was setup successfully!"
end
def destroy
subscription_to_remove = params[:id]
plan_to_remove = params[:plan_id]
customer = Stripe::Customer.retrieve(current_user.stripe_id)
customer.subscriptions.retrieve(subscription_to_remove).delete
current_user.subscribed = false
current_user.perk_subscriptions.delete(plan_to_remove)
current_user.save
redirect_to root_path, notice: "Your subscription has been cancelled."
end
end
Kane.
It looks like your account ID isn't getting into your controller. I would expect the URL to look something like this (account_id instead of account):
http://localhost:3000/subscription/new?account_id=4&amount=17
And I would expect the SubscriptionsController#new to be looking in the Account model rather than the User model, and to be using the same account_id param:
#account = Account.find_by_id(params[:account_id])

Using multiple Stripe checkout buttons, all items use last items price and name on rails

I'm working on a simple store front, and wanted to use stripe checkout buttons for the items. I have a DB of items that I'm iterating through, and generating payment buttons for with JS, here's that code:
<%= form_tag charges_path, id: 'stripeForm' do -%>
<script src="https://checkout.stripe.com/checkout.js"></script>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<%= hidden_field_tag 'stripeAmount' %>
<p>
<% puts p.name %>
<% #price= p.price %>
Buy <% p.name %> for <% number_to_currency(#price, :unit=> '$') %>
<button id="<%=p.id %>">Purchase: <%=p.name%></button>
</p>
<script>
var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: function(token, args) {
$("#stripeToken").val(token.id);
$("#stripeEmail").val(token.email);
$("#stripeAmount").val('<%= #price %>');
$("#stripeForm").submit();
}
});
document.getElementById('<%= p.id %>').addEventListener('click', function(e) {
handler.open({
name: 'Malus Cider',
description: '<%=p.name%>',
shippingAddress:true,
billingAddress:true,
amount: <%= #price %>
});
e.preventDefault();
});
window.addEventListener('popstate', function() {
handler.close();
});
</script>
<% end %>
<% end %>
my controller:
def create
# Amount in cents
amount = params[:stripeAmount].to_i * 100
puts amount
# Create the customer in Stripe
customer = Stripe::Customer.create(
email: params[:stripeEmail],
source: params[:stripeToken]
)
# Create the charge using the customer data returned by Stripe API
charge = Stripe::Charge.create(
customer: customer.id,
amount: amount,
description: 'Rails Stripe customer',
currency: 'usd'
)
end
private
def product_params
params.require(:product).permit(:id, :name, :price)
end
this works! Except that at the last second the hidden field values change to the last item in the DB. So it almost works. Any ideas?

Stripe payments routes charging the same for different charge buttons

I am trying to add two buttons to my payment platform. But stripe is charging me the same amount even when the other button is pressed. This is my attempt but it isn't working. I am not sure what is going wrong.
charges_controller.rb
def create
# Amount in cents - €1000.00
#amount = 100000
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => #amount,
:description => 'Thanks, on behalf of CMRF',
:currency => 'eur'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
View
<%= form_tag charges_path, id: 'chargeForm' do %>
<script src="https://checkout.stripe.com/checkout.js"></script>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<button id="customButton" class="btn btn-large btn-primary">Sponsor a Hole</button>
<script>
var handler = StripeCheckout.configure({
key: '<%= ENV["PUBLISHABLE_KEY"] %>',
token: function(token, args) {
document.getElementById("stripeToken").value = token.id;
document.getElementById("stripeEmail").value = token.email;
document.getElementById("chargeForm").submit();
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'My Company',
description: 'Entry (€1000.00)',
currency: 'eur',
amount: 100000,
billingAddress: true,
});
e.preventDefault();
});
</script>
<% end %>
<%= form_tag charges_path, id: 'chargeForm1' do %>
<script src="https://checkout.stripe.com/checkout.js"></script>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<button id="customButton1" class="btn btn-large btn-success">Enter A Team</button>
<script>
var handler = StripeCheckout.configure({
key: '<%= ENV["PUBLISHABLE_KEY"] %>',
// image: '/assets/my_logo.png',
token: function(token, args) {
document.getElementById("stripeToken").value = token.id;
document.getElementById("stripeEmail").value = token.email;
document.getElementById("chargeForm").submit();
}
});
document.getElementById('customButton1').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'My Company',
description: 'Sponsor (€200.00)',
currency: 'eur',
amount: 20000,
billingAddress: true,
// shippingAddress: true
});
e.preventDefault();
});
</script>
<% end %>
Routes.rb
Rails.application.routes.draw do
resources :charges
root 'pages#index'
get 'pages/about'
get 'pages/contact'
get 'pages/pay'
The amount and currency parameters that you pass to Checkout (in the call to handler.open(...)) are used for display purposes only.
The actual amount and currency that are used for the charge are the ones you provide in the amount and currency parameters of the charge creation request that is sent by your backend code.
In this case, the charge is created in your charges_controller.rb file, and the amount is fixed. If you want to charge different amounts, you'd need to send some other field along with the token so that your backend code can know which amount to charge.
Unless the amount is explicitly set by the customer (e.g. if you're accepting donations), you should not rely on an amount that is provided by the customer's browser as it would be very easy to change it. Instead, you likely want to use some identifier that you can use in your backend code to retrieve the correct amount.

Stripe standalone :source => params[:stripeToken] error

I'm new to Stripe connect, building a marketplace app using stripe connect standalone, and require the user to enter a custom amount to pay the other user. The old form I was using worked fine, but once I changed to the new form, my :source => params[:stripeToken] no longer generates, as well as :stripeEmail. What is causing this?
Invalid source object: must be a dictionary or a non-empty string
my original charges_controller.rb
class ChargesController < ApplicationController
def new
end
def create
# Amount in cents
#amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => #amount,
:description => 'Wage Payment',
:currency => 'cad'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
then I added the methods from the stripe recipe for custom amounts:
def create
#amount = params[:amount]
#amount = #amount.gsub('$', '').gsub(',', '')
begin
#amount = Float(#amount).round(2)
rescue
flash[:error] = 'Charge not completed. Please enter a valid amount in CAD ($).'
redirect_to new_charge_path
return
end
#amount = (#amount * 100).to_i # Must be an integer!
if #amount < 500
flash[:error] = 'Charge not completed. Payment amount must be at least $5.'
redirect_to new_charge_path
return
end
charge = Stripe::Charge.create(
:amount => #amount,
:customer => customer.id,
:currency => 'cad',
:source => params[:stripeToken],
:description => ‘Payment'
)
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
I also modified the form, and added the javascript in charges/new.html.erb:
old form:
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey('pk_test_my_key');
</script>
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: $5.00</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"
data-locale="auto"></script>
<% end %>
new form:
<%= form_tag charges_path, id: 'form' do %>
<div id="error_explanation">
<% if flash[:error].present? %>
<p><%= flash[:error] %></p>
<% end %>
</div>
<article>
<%= label_tag(:amount, 'Payment Amount:') %>
<%= text_field_tag(:amount) %>
</article>
<article>
<%= hidden_field_tag(:stripeToken) %>
<%= hidden_field_tag(:stripeEmail) %>
</article>
<button id='donateButton'>Pay Now</button>
<% end %>
<script src="https://checkout.stripe.com/checkout.js"></script>
<script type="text/javascript">
Stripe.setPublishableKey('pk_test_CcUZ1IJxEqLR5RvVE3p5tx3U');
</script>
<script>
var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
locale: 'auto',
name: 'Payments',
description: 'Wage',
token: function(token) {
$('input#stripeToken').val(token.id);
$('input#stripeEmail').val(token.id);
$('form').submit();
}
});
$('#donateButton').on('click', function(e) {
e.preventDefault();
$('#error_explanation').html('');
var amount = $('input#amount').val();
amount = amount.replace(/\$/g, '').replace(/\,/g, '')
amount = parseFloat(amount);
if (isNaN(amount)) {
$('#error_explanation').html('<p>Please enter a valid amount in CAD ($).</p>');
}
else if (amount < 5.00) {
$('#error_explanation').html('<p>Wage amount must be at least $5.</p>');
}
else {
amount = amount * 100; // Needs to be an integer!
handler.open({
amount: Math.round(amount)
})
}
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>
Now when I submit the custom amount, I am given the following error:
You have passed a blank string for 'source'. You should remove the
'source' parameter from your request or supply a non-blank value
Extracted source (around line #27): Stripe::Charge.create(
Not sure how to proceed. This error seems to be associated to creating charges, as the error is in the charges controller. Or it is in the javascript of the new custom form.
What am I missing?
I had exactly the similar problem. But after few hours of struggle I figured it out. The reason is following. If you are using Rails 4.+ and using Turbolinks along with it, then it loads entire javascript for that page and when you are loading modal the javascript on that modal page wont work. You have to switch off the turbolink.
For me I switched off at the link level which is the link responsible for loading the charge/new page. Example:
<%= link_to "Buy", new_charge_path(product_id: #product.id),'data-no-turbolink' => true %>

Stripe / Rails Issue: "Invalid source object: must be a dictionary or a non-empty string"

I'm building a custom checkout form in my rails app and getting this error when posting to the controller:
"Invalid source object: must be a dictionary or a non-empty string"
Here's my form:
<script src="https://checkout.stripe.com/checkout.js"></script>
<%= form_tag registrations_path, :id=>"stripeForm" do |f| %>
<h2>Register for the <%= #workshop.name %> Workshop ($<%= #workshop.price %>)</h2>
<div class="row">
<div class="form-group col-sm-6">
<%= text_field_tag(:f_name, nil, :placeholder=>"First Name", :class=>"form-control") %>
</div>
<div class="form-group col-sm-6">
<%= text_field_tag(:l_name, nil, :placeholder=>"Last Name", :class=>"form-control") %>
</div>
</div>
<div class="row text-center">
<button class="buy-button" id="stripe-button">Buy Ticket</button>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<%= hidden_field_tag 'stripeAmount' %>
</div>
<script>
var handler = StripeCheckout.configure({
key: "<%= ENV['stripe_publishable_key'] %>",
token: function (token, args) {
$("#stripeToken").value = token.id;
$("#stripeEmail").value = token.email;
$("#stripeAmount").value = <%= #workshop.price * 100 %>;
$("#stripeForm").submit();
}
});
$('#stripe-button').on('click', function (e) {
// Open Checkout with further options
$name = $('input[name=f_name]').val() + " " + $('input[name=l_name]').val();
handler.open({
name: $name,
description: "<%= #workshop.name %>" + " workshop",
amount: <%= #workshop.price * 100 %>
});
e.preventDefault();
});
$(window).on('popstate', function() {
handler.close();
});
</script>
<% end %>
Here's my controller action:
def create
# Amount in cents
amount = params[:stripeAmount].to_i * 100
# Create the customer in Stripe
customer = Stripe::Customer.create(
email: params[:stripeEmail],
card: params[:stripeToken]
)
# Create the charge using the customer data returned by Stripe API
charge = Stripe::Charge.create(
customer: customer.id,
amount: amount,
description: 'Rails Stripe customer',
currency: 'usd'
)
# place more code upon successfully creating the charge
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
flash[:notice] = "Please try again"
end
Basically the user fills out first and last name, clicks on pay button. Then they fill out stripe info and submit. I've tried using debugger right after they click submit, and all the token info etc is there as it should be.
But then I get an error as soon as it gets to the create action in the controller, and shows empty strings for all the stripe params.
Where am I going wrong?
Edit: added below strong params to the controller but had no effect:
protected
def registration_params
params.require(:registration).permit(:stripeEmail, :stripeToken, :stripeAmount)
end
Looks like a minor issue with your jQuery. The token is there, but it doesn't look like its being submitted through the form. That should be the reason Stripe is complaining about an empty string. Try this instead:
$("#stripeToken").val(token.id);
$("#stripeEmail").val(token.email);
$("#stripeAmount").val('<%= j #workshop.price * 100 %>');
The issue arises only when you try to update the user subscription but the payment source is not attached with the Stripe customer.
If you are trying to update subscription from Trial to Paid then please take a look at the answer which may helps you out.

Resources