Recharge 1.0.1 → 1.0.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/examples/charge.rb +1 -24
- data/examples/customer.rb +8 -45
- data/examples/paymethod.rb +20 -0
- data/examples/product.rb +36 -1
- data/examples/transaction.rb +31 -7
- data/lib/recharge.rb +0 -40
- metadata +3 -3
data/examples/charge.rb
CHANGED
@@ -11,11 +11,6 @@ Recharge.api_key = "92ec3e4d8b623dd9baaaf1575b7c557cd113e3e8"
|
|
11
11
|
|
12
12
|
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
14
|
# Find an individual charge by the charge ID
|
20
15
|
puts "", "Find Individual Charge"
|
21
16
|
|
@@ -37,11 +32,6 @@ end
|
|
37
32
|
|
38
33
|
|
39
34
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
35
|
# create a new charge for a customer
|
46
36
|
puts "", "Create New Charge"
|
47
37
|
|
@@ -64,17 +54,11 @@ end
|
|
64
54
|
|
65
55
|
|
66
56
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
57
|
# Update an existing charge (change billing amount, card number, etc)
|
74
58
|
puts "", "Update Charge"
|
75
59
|
|
76
60
|
begin
|
77
|
-
# you must find the charge
|
61
|
+
# you must find the charge first
|
78
62
|
charge = Recharge::Charge.find(chargeID)
|
79
63
|
# then call update on it
|
80
64
|
# you only need to pass the attributes you want to update
|
@@ -92,13 +76,6 @@ end
|
|
92
76
|
|
93
77
|
|
94
78
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
79
|
# deletes an existing charge
|
103
80
|
# deletes the charge, cancels all future transactions that were scheduled for this charge
|
104
81
|
puts "", "Delete Charges"
|
data/examples/customer.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# Examples for the Recharge API Gem
|
2
|
-
#
|
2
|
+
# Customer Resource
|
3
3
|
|
4
4
|
# Only 2 lines are required to get started:
|
5
5
|
# Require the recharge.rb file
|
@@ -11,11 +11,6 @@ Recharge.api_key = "92ec3e4d8b623dd9baaaf1575b7c557cd113e3e8"
|
|
11
11
|
|
12
12
|
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
14
|
# Get a list of all active customers
|
20
15
|
puts "Find All Customers",''
|
21
16
|
|
@@ -51,16 +46,6 @@ end
|
|
51
46
|
|
52
47
|
|
53
48
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
49
|
# Find an individual customer by the customer ID
|
65
50
|
puts "", "Find Individual Customers"
|
66
51
|
|
@@ -86,13 +71,6 @@ end
|
|
86
71
|
|
87
72
|
|
88
73
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
74
|
# create a new customer
|
97
75
|
puts "", "Create New Customer"
|
98
76
|
|
@@ -113,21 +91,11 @@ end
|
|
113
91
|
|
114
92
|
|
115
93
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
94
|
# Update an existing customer (change email address, phone number, etc)
|
127
95
|
puts "", "Update Customer"
|
128
96
|
|
129
97
|
begin
|
130
|
-
# you must find the charge
|
98
|
+
# you must find the charge first
|
131
99
|
customer = Recharge::Customer.find(customerID)
|
132
100
|
|
133
101
|
# then call update on it
|
@@ -136,30 +104,25 @@ begin
|
|
136
104
|
:refID=> Time.now.to_i
|
137
105
|
)
|
138
106
|
|
139
|
-
# all updated information about the
|
107
|
+
# all updated information about the customer is now available
|
140
108
|
p customer
|
141
109
|
rescue Exception => e
|
110
|
+
# errors here can be
|
111
|
+
# Response::NotFound if the customer ID does not exist
|
112
|
+
# Response::BadRequest if there was something wrong with the request
|
142
113
|
puts ">> error #{e.message}"
|
143
114
|
end
|
144
115
|
|
145
116
|
|
146
117
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
118
|
# deletes an existing customer
|
158
119
|
# deletes the customer, payment methods, cancels all future transactions that were scheduled for this charge
|
159
120
|
puts "", "Delete Customer"
|
160
121
|
|
161
122
|
begin
|
123
|
+
# find the customer first
|
162
124
|
customer = Recharge::Customer.find(customerID)
|
125
|
+
# call destroy on it to delete it
|
163
126
|
customer.destroy
|
164
127
|
rescue Exception => e
|
165
128
|
puts ">> error #{e.message}"
|
data/examples/paymethod.rb
CHANGED
@@ -1,30 +1,50 @@
|
|
1
|
+
# Examples for the Recharge API Gem
|
2
|
+
# Payment Method Resource
|
3
|
+
|
4
|
+
# Only 2 lines are required to get started:
|
5
|
+
# Require the recharge.rb file
|
1
6
|
require '../lib/recharge'
|
2
7
|
|
8
|
+
# Set your API key here, found in Recharge Settings > API Settings > API Private Key
|
9
|
+
# If incorrect or blank, any calls will raise a Response::NotAuthorized
|
3
10
|
Recharge.api_key = "92ec3e4d8b623dd9baaaf1575b7c557cd113e3e8"
|
4
11
|
|
12
|
+
|
13
|
+
|
14
|
+
# Create a new payment method (and securely store the card number) for a customer
|
5
15
|
puts "", "Create New PayMethod"
|
6
16
|
|
7
17
|
begin
|
18
|
+
# paymethods can be created in one fell swoop
|
8
19
|
paymethod = Recharge::PayMethod.create(
|
9
20
|
:customer => "cu4f3782bf374a7",
|
10
21
|
:nameOnCard => "test jones",
|
11
22
|
:cardNumber => "4242424242424242",
|
12
23
|
:expDate => "1212"
|
13
24
|
)
|
25
|
+
# after the charge is saved, all attributes are available for you individually or by inspecting
|
14
26
|
paymethodID = paymethod.id
|
15
27
|
puts ">> paymethod id: #{paymethod.id}"
|
16
28
|
puts ">> card number: #{paymethod.cardType} ****#{paymethod.last4}"
|
17
29
|
p paymethod
|
18
30
|
rescue Exception => e
|
31
|
+
# if a required field is missing or invalid, a Response::BadRequest error will be raised
|
32
|
+
# e.message will give more info why the record creation failed
|
19
33
|
puts ">> error #{e.message}"
|
20
34
|
end
|
21
35
|
|
22
36
|
|
23
37
|
|
38
|
+
# deletes an existing paymethod and permanently deletes the stored credit card information
|
39
|
+
# paymethods cannot be deleted if they are currently being used by any recurring charges
|
40
|
+
# as doing so will cause the charge to fail
|
24
41
|
puts "", "Delete PayMethod"
|
25
42
|
|
26
43
|
begin
|
44
|
+
# just call destroy and pass the Paymethod ID to delete it
|
27
45
|
Recharge::PayMethod.destroy(paymethodID)
|
28
46
|
rescue Exception => e
|
47
|
+
# will raise Response::BadRequest if Paymethod can't be deleted
|
48
|
+
# otherwise, delete was successful
|
29
49
|
puts ">> error #{e.message}"
|
30
50
|
end
|
data/examples/product.rb
CHANGED
@@ -1,26 +1,44 @@
|
|
1
|
+
# Examples for the Recharge API Gem
|
2
|
+
# Product Resource
|
3
|
+
|
4
|
+
# Only 2 lines are required to get started:
|
5
|
+
# Require the recharge.rb file
|
1
6
|
require '../lib/recharge'
|
2
7
|
|
8
|
+
# Set your API key here, found in Recharge Settings > API Settings > API Private Key
|
9
|
+
# If incorrect or blank, any calls will raise a Response::NotAuthorized
|
3
10
|
Recharge.api_key = "92ec3e4d8b623dd9baaaf1575b7c557cd113e3e8"
|
4
11
|
|
5
|
-
productID = ""
|
6
12
|
|
13
|
+
|
14
|
+
# Get a list of all products
|
7
15
|
puts "Find All Products",''
|
8
16
|
|
9
17
|
begin
|
18
|
+
# call find_all to get a list of all products
|
10
19
|
Recharge::Product.find_all.each do |product|
|
20
|
+
|
21
|
+
# attributes can be accessed individually like this
|
11
22
|
puts "id: #{product.id}"
|
12
23
|
puts "name: #{product.name}"
|
13
24
|
puts "price: #{product.price}"
|
14
25
|
|
26
|
+
# or you may inspect the whole record and get a list of all keys/values like this
|
27
|
+
p product
|
28
|
+
|
15
29
|
productID = product.id
|
16
30
|
end
|
17
31
|
rescue Exception => e
|
18
32
|
puts ">> error #{e.message}"
|
19
33
|
end
|
20
34
|
|
35
|
+
|
36
|
+
|
37
|
+
# Find an individual product by the product ID
|
21
38
|
puts "", "Find Individual Products"
|
22
39
|
|
23
40
|
begin
|
41
|
+
# Records are fetched by their unique identifiers.
|
24
42
|
product = Recharge::Product.find(productID)
|
25
43
|
|
26
44
|
puts "id: #{product.id}"
|
@@ -33,15 +51,19 @@ end
|
|
33
51
|
|
34
52
|
|
35
53
|
|
54
|
+
# create a new product
|
36
55
|
puts "", "Create New Product"
|
37
56
|
|
38
57
|
begin
|
58
|
+
# products can be created in one fell swoop
|
39
59
|
product = Recharge::Product.create(
|
40
60
|
:name => "HeyHey",
|
41
61
|
:price => "100",
|
42
62
|
:intervalValue => 1,
|
43
63
|
:intervalUnit => 'm'
|
44
64
|
)
|
65
|
+
|
66
|
+
# after the product is saved, all attributes are available for you individually or by inspecting
|
45
67
|
productID = product.id
|
46
68
|
p product
|
47
69
|
rescue Exception => e
|
@@ -50,24 +72,37 @@ end
|
|
50
72
|
|
51
73
|
|
52
74
|
|
75
|
+
# Update an existing product (change price, product name, etc)
|
53
76
|
puts "", "Update Product"
|
54
77
|
|
55
78
|
begin
|
79
|
+
# you must find the product first
|
56
80
|
product = Recharge::Product.find(productID)
|
81
|
+
|
82
|
+
# then call update on it
|
83
|
+
# you only need to pass the attributes you want to update
|
57
84
|
product.update(
|
58
85
|
:price=> "101"
|
59
86
|
)
|
87
|
+
|
88
|
+
# all updated information about the product is now available
|
60
89
|
p product
|
61
90
|
rescue Exception => e
|
91
|
+
# errors here can be
|
92
|
+
# Response::NotFound if the customer ID does not exist
|
93
|
+
# Response::BadRequest if there was something wrong with the request
|
62
94
|
puts ">> error #{e.message}"
|
63
95
|
end
|
64
96
|
|
65
97
|
|
66
98
|
|
99
|
+
# deletes an existing product
|
67
100
|
puts "", "Delete Product"
|
68
101
|
|
69
102
|
begin
|
103
|
+
# find the product first
|
70
104
|
product = Recharge::Product.find(productID)
|
105
|
+
# call destroy on it to delete it
|
71
106
|
product.destroy
|
72
107
|
rescue Exception => e
|
73
108
|
puts ">> error #{e.message}"
|
data/examples/transaction.rb
CHANGED
@@ -1,19 +1,38 @@
|
|
1
|
+
# Examples for the Recharge API Gem
|
2
|
+
# Transaction Resource
|
3
|
+
# Lets you easily perform one-time credit card charges
|
4
|
+
|
5
|
+
# Only 2 lines are required to get started:
|
6
|
+
# Require the recharge.rb file
|
1
7
|
require '../lib/recharge'
|
2
8
|
|
9
|
+
# Set your API key here, found in Recharge Settings > API Settings > API Private Key
|
10
|
+
# If incorrect or blank, any calls will raise a Response::NotAuthorized
|
3
11
|
Recharge.api_key = "92ec3e4d8b623dd9baaaf1575b7c557cd113e3e8"
|
4
12
|
|
13
|
+
|
14
|
+
|
15
|
+
# charges a credit card
|
5
16
|
puts "", "Credit Card Purchase"
|
6
17
|
|
7
18
|
begin
|
19
|
+
# only :Amount, :AcctNum and :ExpDate are required
|
20
|
+
# other values are allowed such as CVV2 and ZIP code
|
21
|
+
# check API docs for more info
|
22
|
+
# https://www.rechargebilling.com/api
|
8
23
|
transaction = Recharge::Transaction.purchase(
|
9
24
|
:Amount => "#{rand(100)}.#{rand(100)}",
|
10
25
|
:AcctNum => "4242424242424242",
|
11
26
|
:ExpDate => "1212"
|
12
27
|
)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
28
|
+
|
29
|
+
# after transaction is processed attributes are available individually
|
30
|
+
puts transaction.responseDescription, transaction.amount, transaction.approvalCode
|
31
|
+
|
32
|
+
# ...or by inspecting
|
33
|
+
p transaction
|
34
|
+
|
35
|
+
transactionID = transaction.transactionID
|
17
36
|
rescue Exception => e
|
18
37
|
puts ">> error #{e.message}"
|
19
38
|
end
|
@@ -22,7 +41,7 @@ end
|
|
22
41
|
|
23
42
|
|
24
43
|
|
25
|
-
|
44
|
+
# refunds money back to a credit card using the card number and expiration date
|
26
45
|
puts "", "Credit Card Refund (with card number)"
|
27
46
|
|
28
47
|
begin
|
@@ -41,10 +60,12 @@ end
|
|
41
60
|
|
42
61
|
|
43
62
|
|
44
|
-
|
63
|
+
# refunds money back to a credit card using the original transactionID
|
45
64
|
puts "", "Credit Card Refund (without card number)"
|
46
65
|
|
47
66
|
begin
|
67
|
+
# amount is required
|
68
|
+
# amounts above the original purchase price for the transactionID given are sometimes rejected by the payment gateway
|
48
69
|
transaction = Recharge::Transaction.refund(
|
49
70
|
:TransactionID => transactionID,
|
50
71
|
:Amount => "0.#{rand(100)}"
|
@@ -61,7 +82,10 @@ end
|
|
61
82
|
|
62
83
|
|
63
84
|
|
64
|
-
|
85
|
+
# voids a previously processed sale or refund
|
86
|
+
# it is only possible to void transactions in the current batch...once the batch settles, the money will be taken off the card
|
87
|
+
# and put into your bank account. You will need to process a refund.
|
88
|
+
# batches usually settle once a day around 2AM PST
|
65
89
|
puts "", "Credit Card Void"
|
66
90
|
|
67
91
|
begin
|
data/lib/recharge.rb
CHANGED
@@ -16,44 +16,4 @@ module Recharge
|
|
16
16
|
end
|
17
17
|
attr_writer :api_key
|
18
18
|
end
|
19
|
-
|
20
|
-
def get(uri)
|
21
|
-
client = Client.new()
|
22
|
-
return client.request('GET', uri)
|
23
|
-
end
|
24
|
-
|
25
|
-
def post(uri, data=nil)
|
26
|
-
client = Client.new()
|
27
|
-
return client.request('POST', uri, data)
|
28
|
-
end
|
29
|
-
|
30
|
-
def delete(uri)
|
31
|
-
client = Client.new()
|
32
|
-
return client.request('DELETE', uri)
|
33
|
-
end
|
34
|
-
|
35
|
-
def returnImportantXML(data)
|
36
|
-
require "rexml/document"
|
37
|
-
doc = REXML::Document.new(data)
|
38
|
-
|
39
|
-
if doc.elements[1].elements[3].length == 1
|
40
|
-
return doc.elements[1].elements[3].elements[1]
|
41
|
-
else
|
42
|
-
return doc.elements[1].elements[3]
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def returnErrorXML(data)
|
47
|
-
require "rexml/document"
|
48
|
-
doc = REXML::Document.new(data)
|
49
|
-
|
50
|
-
doc.elements["/response/result/resultDescription"].text
|
51
|
-
end
|
52
|
-
|
53
|
-
def returnStatusXML(data)
|
54
|
-
require "rexml/document"
|
55
|
-
doc = REXML::Document.new(data)
|
56
|
-
|
57
|
-
doc.elements["/response/result/resultDescription"]
|
58
|
-
end
|
59
19
|
end
|
metadata
CHANGED