Skip to content

Instantly share code, notes, and snippets.

@HostedPCI
Last active August 29, 2015 14:03
Show Gist options
  • Save HostedPCI/9fe38a6dec326d834171 to your computer and use it in GitHub Desktop.
Save HostedPCI/9fe38a6dec326d834171 to your computer and use it in GitHub Desktop.
Ruby samples
require 'rubygems'
require 'active_merchant'
# add ActiveMerchant gem from https://github.com/HostedPCI-Inc/active_merchant.git, branch: 'hostedpci_gateway'
# Initialize gateway, best place in Application.yml
gateway = ActiveMerchant::Billing::HostedpciGateway.new(
:login => 'ObtainTestAccount',
:password => '****',
:hpci_api_host => 'Obtain API host'
)
# ActiveMerchant accepts all amounts as Integer values in cents
amount = 1000 # $10.00
# The card verification value is also known as CVV2, CVC2, or CID
credit_card = ActiveMerchant::Billing::CreditCard.new(
:first_name => 'John',
:last_name => 'Smith',
:number => '4242000000014242', ##HPCI Token, not CC #
:month => '8',
:year => '2015',
:verification_value => '123')
# Billing address, required for options object
address = { :address1 => '567 Elm St.',
:city => 'Westwood',
:state => 'CA',
:country => 'US',
:zip => '90256'
}
# additional options hash object
options = {
:order_id => '1',
:billing_address => address,
:description => 'Store Purchase',
:ip => '173.32.21.248',
:customer => 'tstuser1',
:email => '[email protected]'
}
# Validating the card automatically detects the card type
#if credit_card.valid? ##Can't validate card, token not Mod10 compatible
# Capture $10 from the credit card
response = gateway.purchase(amount, credit_card, options)
puts 'completed gateway.purchase'
puts response
if response.success?
puts "Successfully charged $#{sprintf("%.2f", amount / 100)} to the credit card #{credit_card.display_number}"
else
raise StandardError, response.message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment