-
Notifications
You must be signed in to change notification settings - Fork 27
/
charge.rb
executable file
·67 lines (51 loc) · 1.73 KB
/
charge.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
$:.unshift File.join(File.dirname(__FILE__))
require 'bigdecimal'
$AMOUNT = ARGV[0]
$CCNUM = ARGV[1]
$CCEXP = ARGV[2]
$CVV2 = ARGV[3]
$FIRST_NAME = ARGV[4]
$LAST_NAME = ARGV[5]
# Optional billing address
$BILLING_NAME = ARGV[6]
$BILLING_ADDR = ARGV[7]
$BILLING_CITY = ARGV[8]
$BILLING_STATE = ARGV[9]
$BILLING_ZIP = ARGV[10]
$BILLING_COUNTRY = ARGV[11]
$BILLING_PHONE = ARGV[12]
def usage
puts "./charge.rb <amount> <ccnum> <ccexp> [cvv2] [first_name] [last_name] [billing_name] [billing_address] [billing_city] [billing_state] [billing_zip] [billing_country] [billing_phone]"
puts ""
puts "Note: <ccexp> must be of form 'xx/yyyy'"
puts ""
puts "To enter an empty string for any optional argument, put \"\" (empty set of quotes)"
end
# Convert dollars to cents
$AMOUNT = (BigDecimal($AMOUNT) * 100).to_i unless $AMOUNT.nil?
unless $AMOUNT && $CCNUM && $CCEXP
usage
exit
end
require 'rubygems'
require 'active_merchant'
# Provides @gateway instance
require 'gateway'
$CCEXP_MONTH, $CCEXP_YEAR = $CCEXP.split('/')
cc_hash = {
:number => $CCNUM,
:month => $CCEXP_MONTH.to_i,
:year => $CCEXP_YEAR,
:first_name => $FIRST_NAME,
:last_name => $LAST_NAME
}
unless $CVV2.nil? || $CVV2.empty?
cc_hash.merge!(:verification_value => $CVV2)
end
creditcard = ActiveMerchant::Billing::CreditCard.new(cc_hash)
if $BILLING_NAME
@billing_address = { :name => $BILLING_NAME, :address1 => $BILLING_ADDR, :city => $BILLING_CITY, :state => $BILLING_STATE, :zip => $BILLING_ZIP, :country => $BILLING_COUNTRY, :phone => $BILLING_PHONE }
end
response = @gateway.purchase($AMOUNT, creditcard, :ip => '127.0.0.1', :billing_address => @billing_address)
puts response.to_yaml