You want to charge a credit card from within your Ruby application.
To charge credit cards online, you need an account with a credit card merchant. Although there are many to choose from, Authorize.Net is one of the best and most widely used. The payment library encapsulates the logic of making a credit card payments with Authorize.Net, and soon it will support other gateways as well. Its available as the payment gem.
require ubygems require payment/authorize_net transaction = Payment::AuthorizeNet.new( :login => username, :transaction_key => my_key, :amount => 49.95, :card_number => 4012888818888, :expiration => 310, :first_name => John, :last_name => Doe )
The submit method sends a payment request. If theres a problem with your payment (probably due to an invalid credit card), the submit method will raise a Payment::PaymentError:
begin transaction.submit puts "Card processed successfully: #{transaction.authorization}" rescue Payment::PaymentError puts "Card was rejected: #{transaction.error_message}" end # Card was rejected: The merchant login ID or password is invalid # or the account is inactive.
Some of the information sent during initialization of the Payment::AuthorizeNet class represent your account with Authorize.Net, and will never change (at least, not for the lifetime of the account). You can store this information in a YAML file called .payment.yml in your home directory, and have the payment library load it automatically. A .payment.yml file might look like this:
login: username transaction_key: my_key
That way you don have to hardcode login and transaction_key within your Ruby code.
If you e using the payment library from within a Rails application, you might want to put your YAML hash in the config directory with other configuration files, instead of in your home directory. You can override the location for the defaults file by specifying the :prefs key while initializing the object:
payment = Payment::AuthorizeNet .new(:prefs => "#{RAILS_ROOT}/config/payment.yml") payment.amount = 20 payment.card_number = ogus payment.submit rescue "That didn work"
Notice that after the Payment::AuthorizeNet object has been initialized, you can change its configuration with accessor methods.
Like most online merchants, Authorize.Net uses its own XML-formatted responses to do transactions over HTTPS. Some merchants, such as Payflow Pro, use proprietary interfaces to their backend that require a bridge with their Java or C libraries. If you e using Ruby, this approach can be cumbersome and difficult. Its worth investing some time into researching how flexible the backend is before you decide on a merchant platform for your Ruby application.
Strings
Numbers
Date and Time
Arrays
Hashes
Files and Directories
Code Blocks and Iteration
Objects and Classes8
Modules and Namespaces
Reflection and Metaprogramming
XML and HTML
Graphics and Other File Formats
Databases and Persistence
Internet Services
Web Development Ruby on Rails
Web Services and Distributed Programming
Testing, Debugging, Optimizing, and Documenting
Packaging and Distributing Software
Automating Tasks with Rake
Multitasking and Multithreading
User Interface
Extending Ruby with Other Languages
System Administration