ActiveMerchant是Shopify电子商务系统的提取(extraction)。它发展了RubyonRails网络应用程序的用途,能够作为一个Rails插件实现无缝集成,当然它也可以单独作为一个Ruby库使用。
ActiveMerchant自创建以后就被用于现在Ruby应用程序中,用来处理金融交易。
下面程序示例展示了如何使用个人信用卡购买东西的过程:
require 'active_merchant'# Use the TrustCommerce test serversActiveMerchant::Billing::Base.mode = :testgateway = ActiveMerchant::Billing::TrustCommerceGateway.new( :login => 'TestMerchant', :password => 'password')# ActiveMerchant accepts all amounts as Integer values in centsamount = 1000 # $10.00# The card verification value is also known as CVV2, CVC2, or CIDcredit_card = ActiveMerchant::Billing::CreditCard.new( :first_name => 'Bob', :last_name => 'Bobsen', :number => '4242424242424242', :month => '8', :year => Time.now.year+1, :verification_value => '000')# Validating the card automatically detects the card typeif credit_card.validate.empty? # Capture $10 from the credit card response = gateway.purchase(amount, credit_card) if response.success? puts "Successfully charged $#{sprintf("%.2f", amount / 100)} to the credit card #{credit_card.display_number}" else raise StandardError, response.message endend
评论