Stripe Rails: Invalid integer: 1.06 - ruby-on-rails

I can't get my head around this one.
My form passes a params of 106 which is £1.06.
Charging the card:
amount = params[:amount].to_f
begin
charge = Stripe::Charge.create(
:amount => amount / 100,
:currency => "gbp",
:source => token,
:description => "Example charge"
)
rescue Stripe::CardError => e
# The card has been declined
end
How to prevent:
Invalid integer: 1.06
Where does the integer comes from? I have converted that to float.

According to Stripe API Reference amount parameter should be integer and it is perceived as cents. So you should pass params[:amount] directly as amount.
begin
charge = Stripe::Charge.create(
:amount => params[:amount],
:currency => "gbp",
:source => token,
:description => "Example charge"
)
rescue Stripe::CardError => e
# The card has been declined
end

Related

use trial with gem paypal-sdk-rest

I have my site in ruby on rails and for subscription payments I am using the paypal paypal-sdk-rest gem. But I need the first payment to have a lower value and then the next payment to have the normal value so that users can subscribe and test the subscription. I have been reading in the paypal api and a trial with a lower value can be applied. Is it possible to apply trial to the paypal gem paypal-sdk-rest? this is my code of how I create the plans:
def creacion_planes
plan = Plan.new({
:name => 'Monthly $20',
:description => '$20 plan (monthly charge)',
:type => 'infinite',
:payment_definitions => [{
:name => 'Plan monthly $20',
:type => 'REGULAR',
:frequency_interval => '1',
:frequency => 'MONTH', #WEEK, DAY, YEAR, MONTH.
:cycles => '0',
:amount => {
:currency => 'USD',
:value => '20.00'
}
}],
:merchant_preferences => {
:return_url => complete_paypal_checkouts_planes_url,
:cancel_url => update_plan_url,
:max_fail_attempts => '0',
:auto_bill_amount => 'YES',
:initial_fail_amount_action => 'CONTINUE'
}
})
# Create plan
if plan.create
# Plan update activation object
plan_update = {
:op => 'replace',
:path => '/',
:value => {
:state => 'ACTIVE'
}
}
# Activate plan
if plan.update(plan_update)
puts("Billing plan activated with ID [#{plan.id}]")
redirect_to success_payment_path(:plan_id => plan.id, :plan => "plan_mensual20")
else
logger.error payment.error.inspect
end
else
logger.error payment.error.inspect
end
end
And this is my code of how I create the agreement:
def create
agreement = Agreement.new({
:name => params[:name],
:description => params[:description],
:start_date => (Time.now + 20*60).utc.iso8601, #'2020-04-26T23:00:04Z' (Time.now + 1.days).utc.iso8601
:plan => {
:id => params[:plan_id]
},
:payer => {
:payment_method => 'paypal'
}
})
if agreement.create
redirect = agreement.links.find{|v| v.rel == "approval_url" }.href
redirect_to redirect
else
logger.error agreement.error.inspect
end
end
Do not use that old paypal-ruby-sdk. It is obsolete and uses an old version of billing plans, which is not compatible with the current subscriptions API.
Instead, integrate the subscriptions API via direct HTTPS calls. Here is the guide: https://developer.paypal.com/docs/subscriptions/
An example with a trial period is provided.

Unable to establish a secure connection to Stripe using Rails

I'm following an online tutorial to establish stripe payments. After entering the card details in test mode, the instructor's page displayed a message "Payment successful" from create.html.erb file.
However, I'm stuck with an error saying Stripe::APIConnectionError in ChargesController#create Please check the screenshot below, it says it has something to do with Openssl version although I've the latest version installed.
After running the command it asked me to, I got this displayed in my cmd: Verify return code: 20 (unable to get local issuer certificate)
charges_controller.rb
class ChargesController < ApplicationController
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 => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
I recently started learning Rails, any help on this would be appreciated. Thank you!

You cannot use a Stripe token more than once

I cannot seem to charge a card then create a customer on the fly in Rails 4.
def charge
token = params[:stripeToken] # can only be used once.
begin
charge = Stripe::Charge.create(
:amount => 5000,
:currency => "gbp",
:source => token,
:description => "Example charge"
)
rescue Stripe::CardError => e
# The card has been declined
end
if current_user.stripeid == nil
customer = Stripe::Customer.create(card: token, ...)
current_user.stripeid = customer.id
current_user.save
end
end
I have looked at this but there is no such thing as token.id as token is just a String.
It looks like you're using the token in two locations:
charge = Stripe::Charge.create(
:amount => 5000,
:currency => "gbp",
:source => token,
:description => "Example charge"
)
And also here:
customer = Stripe::Customer.create(card: token, ...)
In fact, creating a Stripe charge from a token should also create a customer along with the card, if it didn't already exist. Your step of creating a customer is unnecessary. Therefore, just fetch the Stripe customer from the source:
current_user.update_attribute(:stripeid, charge.source.customer)
Relevant Stripe documentation:
https://stripe.com/docs/api/ruby#create_charge
EDIT
If you want to have more control over the charge process, create each object independently:
customer = Stripe::Customer.create(
description: "Example customer",
email: current_user.email
)
card = customer.sources.create(
source: "<stripe token>"
customer: customer.id
)
Stripe::Charge.create(
amount: 5000,
currency: "gbp",
source: card.id,
customer: customer.id
)

I get error that You cannot use a Stripe token more than once

I run my local server using stripe and I got error when I submitted my payment form.
class ChargesController < ApplicationController
def create
//below is the reason I got error for
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => params[:amount],
:description => 'Growth Hacking Crash Course',
:currency => 'jpy'
)
purchase = Purchase.create(email: params[:stripeEmail], card: params[:stripeToken], amount: params[:amount], description: charge.description, currency: charge.currency, customer_id: customer.id, product_id: 1, uuid: SecureRandom.uuid)
redirect_to purchase
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
I don't know why I get that error. I need your help!!!
Your code is correct. However, this error message means that the card token you're trying to use in your customer creation request (in the params[:stripeToken] variable) has already been used.
Stripe tokens can only be used in a single request. Afterwards, the token is consumed and cannot be used again.
You'd have to create a new card token, using Checkout or Stripe.js, or directly by using the create token API endpoint.

ActiveMerchant: Buyer Account Balance Doesn't Decrease

I'm using ActiveMerchant gem with Ruby 1.9.3 and Rails 3.1
I already set up a buyer dummy account and a seller dummy account on PayPal using WebPayments Pro. When I run this script and look in my paypal sandbox, my seller account is deposited $10 in funds correctly.
The problem is that when I look at the sandbox for my buyer account, the balance does not decrease. Where is my seller getting the money from?
My code is here:
require "rubygems"
require "active_merchant"
ActiveMerchant::Billing::Base.mode = :test
gateway = ActiveMerchant::Billing::PaypalGateway.new(
:login => "seller_1328509472_biz_api1.gmail.com",
:password => "*******",
:signature => "******"
)
credit_card ||= ActiveMerchant::Billing::CreditCard.new(
:type => "visa",
:number => "4193536536887351",
:verification_value => "123",
:month => 2,
:year => 2017,
:first_name => "TESTING",
:last_name => "BUYER"
)
if credit_card.valid?
response = gateway.authorize(1000, credit_card, :ip => "98.248.144.120", :billing_address => { :name => 'Test User', :company => '', :address1 => '1 Main St', :address2 => '', :city => 'San Jose', :state => 'CA', :zip => '95131'})
if response.success?
gateway.capture(1000, response.authorization)
else
puts "Response Unsuccessful Error: #{response.message}"
end
else
puts "Error: credit card is not valid. #{credit_card.errors.full_messages.join('. ')}"
end
Please help me! I have been stuck on this for ages, and I am very confused.
Because you're specifying a credit card directly, there is no 'account' as such that it takes the funds from.
The API method you're using, DoDirectPayment, charges a credit card directly. It is not connected to a PayPal account, nor does it use the balance of a PayPal account.
If you want to charge a PayPal account rather than a credit card, use PayPal Express Checkout: http://railscasts.com/episodes/146-paypal-express-checkout

Resources