Class: PaySimple::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/paysimple.rb

Class Method Summary collapse

Class Method Details

.charge(customer_number, options, auth_only = false) ⇒ Object

Process one-time sale against existing subscription

begin customer_number = 12345 response = PaySimple::Subscription.charge(customer_number, :Amount => 34.56)

if response['Response'] == "Approved"
puts "One-time charge successful."
else
puts "An error occurred: #{response['Error']}"
end

rescue Exception => e puts "An error occurred: #ee.message" end



202
203
204
# File 'lib/paysimple.rb', line 202

def charge(customer_number, options, auth_only = false)
  PaySimple.send_request(:runCustomerSale, customer_number, options, auth_only)
end

.create(options) ⇒ Object

Bill Jennifer $12.00 monthly

begin customer_number = PaySimple::Subscription.create( :CustomerID => 12345, :BillingAddress => { :FirstName => "Jennifer", :LastName => "Smith" }, :CreditCardData => { :CardNumber => '4444555566667779', :CardExpiration => '0908' }, :Schedule => :monthly, :Next => "2008-09-05", :Amount => 12.00 )

puts "Subscription created with Customer Number: #{customer_number}"

rescue Exception => e puts "An error occurred: #ee.message" end



64
65
66
# File 'lib/paysimple.rb', line 64

def create(options)
  PaySimple.send_request(:addCustomer, { :NumLeft => 0, :Enabled => true }.merge(options))
end

.delete(customer_number) ⇒ Object

Delete subscription

begin customer_number = 12345 response = PaySimple::Subscription.delete(customer_number)

puts "Subscription removed from active use."

rescue Exception => e puts "An error occurred: #ee.message" end



172
173
174
# File 'lib/paysimple.rb', line 172

def delete(customer_number)
  PaySimple.send_request(:deleteCustomer, customer_number)
end

.find(customer_number) ⇒ Object

Find an existing subscription

begin customer_number = 12345 customer = PaySimple::Subscription.find(customer_number)

puts "Found subscription for #{ [customer["BillingAddress"]["FirstName"], customer["BillingAddress"]["LastName"]].join(" ")}"

rescue Exception => e puts "An error occurred: #ee.message" end



185
186
187
# File 'lib/paysimple.rb', line 185

def find(customer_number)
  PaySimple.send_request(:getCustomer, customer_number)
end

.query(options) ⇒ Object

Search for transactions

begin response = PaySimple::Subscription.query( [ { :Field => 'amount', :Type => 'gt', :Value => '5.0' } ] )

response.transactions.each do |transaction|
puts "CustomerID = #{transaction['CustomerID']}, Amount = #{transaction['Details']['Amount']}"
end

rescue Exception => e puts "An error occurred: #ee.message" end



220
221
222
223
224
225
# File 'lib/paysimple.rb', line 220

def query(options)
  match_all = options.delete(:match_all)
  start = options.delete(:start) || 0
  limit = options.delete(:limit) || 100
  PaySimple.send_request(:searchTransactions, options, match_all, start, limit)
end

.update(customer_number, options) ⇒ Object

Update subscription to use new credit card

begin customer_number = 12345 response = PaySimple::Subscription.update( customer_number, :CreditCardData => { :CardNumber => '4444555566667779', :CardExpiration => '0908' } )

puts "Subscription updated"

rescue Exception => e puts "An error occurred: #ee.message" end



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/paysimple.rb', line 83

def update(customer_number, options)
  customer = find(customer_number)
  options = PaySimple.symbolize_hash(options)
  
  # Add the existing customer properties to the options hash unless they already exist
  [
    :CustomerID,
    :SendReceipt, 
    :ReceiptNote, 
    :Notes, 
    :User, 
    :Source, 
    :Schedule, 
    :Next, 
    :NumLeft, 
    :Amount, 
    :Enabled, 
    :CustomData, 
    :Description, 
    :OrderID
  ].each do |property|
    options[property] = customer[property.to_s] unless options.has_key?(property)
  end
  
  # Add the existing customer address properties to the options hash unless they already exist
  options[:BillingAddress] ||= {}
  [
    :FirstName,
    :LastName,
    :Company,
    :Street,
    :Street2,
    :City,
    :State,
    :Zip,
    :Country,
    :Phone,
    :Fax,
    :Email
  ].each do |property|
    options[:BillingAddress][property] = customer["BillingAddress"][property.to_s] unless options[:BillingAddress].has_key?(property)
  end
  
  # Add the existing customer credit card properties to the options hash unless they already exist
  options[:CreditCardData] ||= {}
  [
    :CardNumber,
    :CardExpiration,
    :CardCode,
    :AvsStreet,
    :AvsZip,
    :CardPresent,
    :MagStripe,
    :TermType,
    :MagSupport,
    :XID,
    :CAVV,
    :ECI,
    :InternalCardAuth,
    :Pares
  ].each do |property|
    options[:CreditCardData][property] = customer["CreditCardData"][property.to_s] unless options[:CreditCardData].has_key?(property)
  end
  
  # Add the existing customer check properties to the options hash unless they already exist
  options[:CheckData] ||= {}
  [
    :CheckNumber,
    :Routing,
    :Account,
    :SSN,
    :DriversLicense,
    :DriversLicenseState
  ].each do |property|
    options[:CheckData][property] = customer["CheckData"][property.to_s] unless options[:CheckData].has_key?(property)
  end
  
  PaySimple.send_request(:updateCustomer, customer_number, options)
end