trustcommerce 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/trustcommerce_sync_agent.rb +126 -0
- data/lib/version.rb +1 -1
- metadata +3 -2
@@ -0,0 +1,126 @@
|
|
1
|
+
#
|
2
|
+
# This agent will pull down recent transactions from the TrustCommerce vault using the
|
3
|
+
# following schema:
|
4
|
+
#
|
5
|
+
# create_table :subscriptions do |t|
|
6
|
+
# t.column :account_id, :integer, :null => false
|
7
|
+
# t.column :created_on, :datetime, :null => false
|
8
|
+
# t.column :billing_id, :string
|
9
|
+
# t.column :length, :integer, :null => false
|
10
|
+
# t.column :cents, :integer, :null => false
|
11
|
+
# t.column :billing_full_name, :string
|
12
|
+
# t.column :billing_address, :string
|
13
|
+
# t.column :billing_zip_code, :string
|
14
|
+
# t.column :billing_country, :string
|
15
|
+
# t.column :billing_card_type, :string
|
16
|
+
# t.column :billing_credit_card, :string
|
17
|
+
# t.column :billing_expiration_date, :datetime
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# add_index :subscriptions, :account_id
|
21
|
+
#
|
22
|
+
# create_table :subscription_transactions do |t|
|
23
|
+
# t.column :subscription_id, :integer, :null => false
|
24
|
+
# t.column :account_id, :integer, :null => false
|
25
|
+
# t.column :transaction_date, :datetime, :null => false
|
26
|
+
# t.column :transaction_id, :string, :null => false
|
27
|
+
# t.column :transaction_type, :string, :null => false
|
28
|
+
# t.column :amount, :decimal, :null => false
|
29
|
+
# t.column :card_number, :string, :null => false
|
30
|
+
# t.column :card_type, :string, :null => false
|
31
|
+
# t.column :cardholder_name, :string, :null => false
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# add_index :subscription_transactions, :subscription_id
|
35
|
+
# add_index :subscription_transactions, :account_id
|
36
|
+
#
|
37
|
+
# Install as cron job or test it out on the console:
|
38
|
+
#
|
39
|
+
# $ ./script/runner -e production "TrustcommerceSyncAgent.sync('3h')"
|
40
|
+
#
|
41
|
+
class TrustcommerceSyncAgent
|
42
|
+
|
43
|
+
# time_ago examples:
|
44
|
+
# 30m => 30 minutes
|
45
|
+
# 1h => 1 hour
|
46
|
+
# 3d => 3 days
|
47
|
+
def self.sync(time_ago = nil)
|
48
|
+
time = case time_ago
|
49
|
+
when /^(\d+)m$/ then Time.now.utc - $1.to_i.minute
|
50
|
+
when /^(\d+)h$/ then Time.now.utc - $1.to_i.hour
|
51
|
+
when /^(\d+)d$/ then Time.now.utc - $1.to_i.day
|
52
|
+
else Time.now.utc - 1.hour
|
53
|
+
end
|
54
|
+
# --- [ find recently created paying subscriptions ] ---
|
55
|
+
subscriptions = Subscription.find(:all, :conditions => ['cents > 0 AND created_on > ?', time])
|
56
|
+
subscriptions.each do |subscription|
|
57
|
+
sync_subscription(subscription)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def self.sync_subscription(subscription)
|
64
|
+
|
65
|
+
# --- [ get TC data ] ---
|
66
|
+
tc = TrustCommerce::Subscription.query(
|
67
|
+
:querytype => 'transaction',
|
68
|
+
:billingid => subscription.billing_id
|
69
|
+
)
|
70
|
+
return if !tc.kind_of? Net::HTTPOK
|
71
|
+
|
72
|
+
# --- [ create index by CSV header ]
|
73
|
+
field_names = tc.body.split("\n")[0].split(',')
|
74
|
+
indexes = field_names.inject({}) {|h, field| h[field.to_sym] = field_names.index(field); h }
|
75
|
+
|
76
|
+
# --- [ build transaction array ] ---
|
77
|
+
transactions = tc.body.split("\n")
|
78
|
+
transactions.shift # get rid of header
|
79
|
+
|
80
|
+
transactions.each do |line|
|
81
|
+
transaction = line.split(',')
|
82
|
+
|
83
|
+
#log_transaction(indexes, transaction)
|
84
|
+
|
85
|
+
subscription_transaction = subscription.transactions.find_by_transaction_id(transaction[indexes[:transid]])
|
86
|
+
if subscription_transaction.nil?
|
87
|
+
SubscriptionTransaction.create(
|
88
|
+
:subscription_id => subscription.id,
|
89
|
+
:account_id => subscription.account_id,
|
90
|
+
:transaction_date => convert_date(transaction[indexes[:trans_date]]),
|
91
|
+
:transaction_id => transaction[indexes[:transid]],
|
92
|
+
:transaction_type => transaction[indexes[:action_name]],
|
93
|
+
:amount => transaction[indexes[:bank_amount]],
|
94
|
+
:card_number => transaction[indexes[:cc]],
|
95
|
+
:card_type => convert_card_type(transaction[indexes[:media_name]]),
|
96
|
+
:cardholder_name => transaction[indexes[:name]]
|
97
|
+
)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
# TC returns mm-dd-yyyy HH:mm:ss
|
104
|
+
def self.convert_date(str)
|
105
|
+
date = str.split(' ')[0].split('-')
|
106
|
+
Time.local(date[2], date[0], date[1])
|
107
|
+
end
|
108
|
+
|
109
|
+
# TC returns VISA-D, MC-D, AMEX-D
|
110
|
+
def self.convert_card_type(str)
|
111
|
+
case str
|
112
|
+
when /VISA/i then 'Visa'
|
113
|
+
when /MC/i then 'MasterCard'
|
114
|
+
when /AMEX/i then 'American Express'
|
115
|
+
else str
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# --- [ helpful for debugging ] ---
|
120
|
+
def self.log_transaction(indexes, transaction)
|
121
|
+
puts '---------- transaction --------------------'
|
122
|
+
indexes.each{|k,v| puts "#{k} => #{transaction[indexes[k]]}" if !transaction[indexes[k]].blank? }
|
123
|
+
puts '-------------------------------------------'
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: trustcommerce
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.5.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.5.2
|
7
|
+
date: 2007-08-27 00:00:00 -07:00
|
8
8
|
summary: TrustCommerce Subscription Library
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -31,6 +31,7 @@ authors:
|
|
31
31
|
files:
|
32
32
|
- Rakefile
|
33
33
|
- lib/trustcommerce.rb
|
34
|
+
- lib/trustcommerce_sync_agent.rb
|
34
35
|
- lib/version.rb
|
35
36
|
- support/rdoc/code_info.rb
|
36
37
|
- README
|