I just wanted to test my paypal sandbox payment gateway in a simple way.
Unfortunately I'm getting the following error message:
"This transaction cannot be processed due to an invalid merchant
configuration."
This is my testfile. It's from an railscasts Episode:
require "rubygems"
require "active_merchant"
ActiveMerchant::Billing::Base.mode = :test
gateway = ActiveMerchant::Billing::PaypalGateway.new(
login: "bla-facilitator_api1.gmail.com",
password: "13234229172",
signature: "AFcWxY21C7fd0v3bYYYRCOSSrl31AsqKs2TwM-RkGcerk8QatsKAkfJt"
)
credit_card = ActiveMerchant::Billing::CreditCard.new(
brand: "visa",
number: "4111111111111111",
verification_value: "123",
month: 1,
year: 2014,
first_name: "Ryan",
last_name: "Bates"
)
if credit_card.valid?
response = gateway.purchase(1000, credit_card, ip: "127.0.0.1",
:billing_address => {
:name => "Ryan Bates",
:address1 => "123 Main St.",
:city => "New York",
:state => "NY",
:country => "US",
:zip => "10001"
}
)
if response.success?
puts "Purchase complete!"
else
puts "Error: #{response.message}"
end
else
puts "Error: credit card is not valid. #{credit_card.errors.full_messages.join('. ')}"
end
That error means you don't have Payments Pro active on the account you're trying to process with, or you haven't accepted the billing agreement for it after turning it on.
Related
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 want to add a birthday date to my user model, this is how I implemented it:
user.rb
validates_date :birthday_date
user_spec.rb
describe User do
before(:each) do
#attr = {
:birthday_date => Date.new(1992, 8, 27),
:nom => "Utilisateur",
:email => "user#example.com",
:password => "foobar",
:password_confirmation => "foobar"
}
end
it "..." do
User.create!(#attr)
end
it "must have a correct date" do
bad_guy = User.new(#attr.merge(:birthday_date => ""))
bad_guy.should_not be_valid
end
But when I want to test it, I have got this error :
Failure/Error: #follower = Factory(:user)
ActiveRecord::RecordInvalid:
Validation failed: Birthday date is not a valid date
Why is my date not accepted?
This below method hits the database when saving an account, when saving a user, when updating the unit with the account id, when create a new unit_users record (associating unit with a user). So that's at least 4 times it hits the database:
def activate_new_account(assign_units = [])
account = Account.new(
:name => self.name,
:email => self.email,
:phone => self.phone,
:street_address => self.street_address,
:city => self.city,
:state => self.state,
:postal_code => self.postal_code,
:country => self.country)
errors.clear
error_msgs = []
transaction do
if account.valid?
account.save
user = User.new(:name => self.name,
:email => self.email,
:password => self.password,
:password_confirmation => self.password_confirmation,
:phone => self.phone,
:address => formatted_address,
:role_id => self.user_role_id,
:account_id => account.id)
if user.valid?
user.save
if units_for_account
begin
units = Unit.find(units_for_account.split(" "))
units.each do |unit|
#hitting database twice
unit.update_attributes account_id: account.id
unit.users << user
end
rescue ActiveRecord::RecordNotFound
error_msgs << "Couldn't find all Units with serial numbers: #{units_for_account.split(' ')}"
rescue ActiveRecord::RecordInvalid => invalid
error_msgs << invalid.record.errors
end
end
else
account.destroy
error_msgs << user.errors.full_messages
end
else
error_msgs << account.errors.full_messages
end
end
if error_msgs.size > 0
error_msgs.each do |error|
errors.add :base, error
end
return false
end
return true
end
Is there a more Railsy way to do this without hitting database so much?
There is a much more Rails way to accomplish the activation, by using validates_associated.
But it does'n access the database less. You have four tables to update or add rows, so you need four DB statements.
I have an app that has some basic ecommerce activity. I tried it with a test authorize.net account, and it works fine. I entered in the APIs for production mode though, and I keep getting redirected to the failure screen when I try to purchase anything. I'm not getting any errors, nothing in the logs on heroku, and I don't even know where to start debugging. I am connecting to Authorize.net successfully, transactions are successful in development mode - I based it heavily off of Ryan Bate's RailsCast episode 145 (http://railscasts.com/episodes/145-integrating-active-merchant), but here are some highlights of my code (since I'm testing ti, I'm forcing it to do 1 cent transactions despite what I order)
in enviroments/production.rb
config.after_initialize do
ActiveMerchant::Billing::Base.mode = :production
::GATEWAY = ActiveMerchant::Billing::AuthorizeNetGateway.new(
:login => "scrubbed",
:password => "scrubbed",
:test => false
)
end
orders_controller.rb
def create
#order = Order.new(params[:order])
#order.cart = current_cart
if #order.save
if #order.purchase
#order.state = 'paid'
#order.save
render :action => "success"
end
else
render :action => "failure"
end
else
redirect_to home_page_path, notice: "The order failed to save"
end
end
def purchase
response = GATEWAY.purchase(1, credit_card, purchase_options)
transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
#cart.update_attribute(:purchased_at, Time.now) if response.success?
response.success?
end
order.rb
def purchase
response = GATEWAY.purchase(1, credit_card, purchase_options)
transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
#cart.update_attribute(:purchased_at, Time.now) if response.success?
response.success?
end
private
def purchase_options
{
:ip => ip_address,
:billing_address => {
:first_name => first_name,
:last_name => last_name,
:address1 => address_line_1,
:address2 => address_line_2,
:city => city,
:state => billing_state,
:country => "US",
:zip => zip_code,
:phone => phone_number,
:company => company
},
:shipping_address => {
:first_name => sfirst_name,
:last_name => slast_name,
:address1 => saddress_line_1,
:address2 => saddress_line_2,
:city => scity,
:state => sbilling_state,
:country => "US",
:zip => szip_code,
:phone => sphone_number,
:company => scompany
}
}
end
def validate_card
unless credit_card.valid?
credit_card.errors.full_messages.each do |message|
errors.add :base, message
end
end
end
def credit_card
#credit_card ||= ActiveMerchant::Billing::CreditCard.new(
:brand => card_type,
:number => card_number,
:verification_value => card_verification,
:month => card_expires_on.month,
:year => card_expires_on.year,
:first_name => first_name,
:last_name => last_name
)
end
I haven't personally dealt with authorize.net but I have used a similar web service which also required PCI compliance. In my case this meant that I needed a valid SSL certificate for transactions to work in production. In fact, if I remember correctly the transaction was failing in a very similar way and it wasn't obvious that was the cause.
It looks like authorize.net requires some type of security depending on which API you are making use of.
http://developer.authorize.net/integration/
I need to ask just to be sure - Have you done everything to get your merchant account setup with Authorize.net and your Bank for production usage already, and you've coordinated with them to start posting funds to your bank account when the money starts coming in?
If so, check out their developer documentation page here for the difference between test and production transactions
http://developer.authorize.net/guides/AIM/5_TestTrans.html
Transactions that work in test mode should work in production mode when everything is properly configured on the Gateway side. I would contact authorize.net there may be technical or administrative details which need to be finalized for your account to process live transactions.
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