samurai 0.2.1 → 0.2.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/README.markdown +221 -0
- data/lib/samurai/payment_method.rb +4 -1
- data/lib/samurai/version.rb +1 -1
- metadata +4 -4
- data/README.md +0 -9
data/README.markdown
ADDED
@@ -0,0 +1,221 @@
|
|
1
|
+
Samurai
|
2
|
+
=======
|
3
|
+
|
4
|
+
The Samurai merchant gem give you a nice set of ruby object to play with
|
5
|
+
instead of hacking the Samurai API yourself. You'll need a merchant account
|
6
|
+
with samurai.feefighters.com and your credentials and you'll need your
|
7
|
+
application to track a few identifying tokens with your.
|
8
|
+
|
9
|
+
|
10
|
+
Installation
|
11
|
+
------------
|
12
|
+
|
13
|
+
Install Samurai just like any other gem. In Rails3 add the gem to your Gemfile:
|
14
|
+
|
15
|
+
gem "samurai"
|
16
|
+
|
17
|
+
then run:
|
18
|
+
|
19
|
+
bundle install
|
20
|
+
|
21
|
+
In Rails2 add the gem to your environment.rb:
|
22
|
+
|
23
|
+
config.gem "samurai"
|
24
|
+
|
25
|
+
then run:
|
26
|
+
|
27
|
+
rake gems:install
|
28
|
+
|
29
|
+
|
30
|
+
Configuration
|
31
|
+
-------------
|
32
|
+
|
33
|
+
Set the Samurai.options hash, after the gem has loaded and before you'll use
|
34
|
+
it. Typically this belongs in your environment.rb file or it's own initializer.
|
35
|
+
|
36
|
+
config.after_initialize do
|
37
|
+
Samurai.options = {
|
38
|
+
:merchant_key => 'your_merchant_key',
|
39
|
+
:merchant_password => 'your_merchant_password',
|
40
|
+
:gateway_token => 'your_default_gateway_token'
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
The :gateway_token param is optional. If you set it,
|
45
|
+
`Samurai::Gateway.the_gateway` will return the gateway with this token. You
|
46
|
+
can always call `Samurai::Gateway.find('an_arbitrary_gateway_token')` to
|
47
|
+
retrieve any of your gateways.
|
48
|
+
|
49
|
+
|
50
|
+
Payment Methods
|
51
|
+
---------------
|
52
|
+
|
53
|
+
A Payment Method is created each time a user stores their billing information
|
54
|
+
in Samurai.
|
55
|
+
|
56
|
+
### Creating Payment Methods
|
57
|
+
|
58
|
+
To let your customers create a Payment Method, place a credit card
|
59
|
+
entry form on your site like the one below.
|
60
|
+
|
61
|
+
<form action="https://api.ubergateway.com/v1/payment_methods" method="POST">
|
62
|
+
<fieldset>
|
63
|
+
<input name="redirect_url" type="hidden" value="http://yourdomain.com/anywhere" />
|
64
|
+
<input name="merchant_key" type="hidden" value="[Your Merchant Key]" />
|
65
|
+
|
66
|
+
<!-- Before populating the ‘custom’ parameter, remember to escape reserved xml characters
|
67
|
+
like <, > and & into their safe counterparts like <, > and & -->
|
68
|
+
<input name="custom" type="hidden" value="Any value you want us to save with this payment method" />
|
69
|
+
|
70
|
+
<label for="credit_card_first_name">First name</label>
|
71
|
+
<input id="credit_card_first_name" name="credit_card[first_name]" type="text" />
|
72
|
+
|
73
|
+
<label for="credit_card_last_name">Last name</label>
|
74
|
+
<input id="credit_card_last_name" name="credit_card[last_name]" type="text" />
|
75
|
+
|
76
|
+
<label for="credit_card_address_1">Address 1</label>
|
77
|
+
<input id="credit_card_address_1" name="credit_card[address_1]" type="text" />
|
78
|
+
|
79
|
+
<label for="credit_card_address_2">Address 2</label>
|
80
|
+
<input id="credit_card_address_2" name="credit_card[address_2]" type="text" />
|
81
|
+
|
82
|
+
<label for="credit_card_city">City</label>
|
83
|
+
<input id="credit_card_city" name="credit_card[city]" type="text" />
|
84
|
+
|
85
|
+
<label for="credit_card_state">State</label>
|
86
|
+
<input id="credit_card_state" name="credit_card[state]" type="text" />
|
87
|
+
|
88
|
+
<label for="credit_card_zip">Zip</label>
|
89
|
+
<input id="credit_card_zip" name="credit_card[zip]" type="text" />
|
90
|
+
|
91
|
+
<label for="credit_card_card_type">Card Type</label>
|
92
|
+
<select id="credit_card_card_type" name="credit_card[card_type]">
|
93
|
+
<option value="visa">Visa</option>
|
94
|
+
<option value="master">MasterCard</option>
|
95
|
+
</select>
|
96
|
+
|
97
|
+
<label for="credit_card_card_number">Card Number</label>
|
98
|
+
<input id="credit_card_card_number" name="credit_card[card_number]" type="text" />
|
99
|
+
|
100
|
+
<label for="credit_card_verification_value">Security Code</label>
|
101
|
+
<input id="credit_card_verification_value" name="credit_card[cvv]" type="text" />
|
102
|
+
|
103
|
+
<label for="credit_card_month">Expires on</label>
|
104
|
+
<input id="credit_card_month" name="credit_card[expiry_month]" type="text" />
|
105
|
+
<input id="credit_card_year" name="credit_card[expiry_year]" type="text" />
|
106
|
+
|
107
|
+
<button type="submit">Submit Payment</button>
|
108
|
+
</fieldset>
|
109
|
+
</form>
|
110
|
+
|
111
|
+
After the form submits to Samurai, the user's browser will be returned to the
|
112
|
+
URL that you specify in the redirect_url field, with an additional query
|
113
|
+
parameter containing the `payment_method_token`. You should save the
|
114
|
+
`payment_method_token` and use it from this point forward.
|
115
|
+
|
116
|
+
### Fetching a Payment Method
|
117
|
+
|
118
|
+
To retrieve the payment method and ensure that the sensitive data is valid:
|
119
|
+
|
120
|
+
payment_method = Samurai::PaymentMethod.find(payment_method_token)
|
121
|
+
payment_method.is_sensitive_data_valid # => true if the credit_card[card_number] passed checksum
|
122
|
+
# and the cvv (if included) is a number of 3 or 4 digits
|
123
|
+
|
124
|
+
**NB:** Samurai will not validate any non-sensitive data so it is up to your
|
125
|
+
application to perform any additional validation on the payment_method.
|
126
|
+
|
127
|
+
### Updating Payment Methods
|
128
|
+
|
129
|
+
You can update the payment method by directly updating its properties or by
|
130
|
+
loading it from a set of attributes and then saving the object:
|
131
|
+
|
132
|
+
payment_method.first_name = 'Graeme'
|
133
|
+
payment_method.save
|
134
|
+
|
135
|
+
OR
|
136
|
+
|
137
|
+
payment_method.load(hash_of_credit_card_values)
|
138
|
+
payment_method.save
|
139
|
+
|
140
|
+
### Retaining and Redacting Payment Methods
|
141
|
+
|
142
|
+
Unless you create a transaction on a payment method right away, that payment
|
143
|
+
method will be purged from Samurai. If you want to hang on to a payment method
|
144
|
+
for a while before making an authorization or purchase on it, you must retain it:
|
145
|
+
|
146
|
+
payment_method.retain
|
147
|
+
|
148
|
+
If you are finished with a payment method that you have either previously retained
|
149
|
+
or done one or more transactions with, you may redact the payment method. This
|
150
|
+
removes any sensitive information from Samurai related to the payment method,
|
151
|
+
but it still keeps the transaction data for reference. No further transactions
|
152
|
+
can be processed on a redacted payment method.
|
153
|
+
|
154
|
+
payment_method.redact
|
155
|
+
|
156
|
+
|
157
|
+
Processing Transactions
|
158
|
+
-----------------------
|
159
|
+
|
160
|
+
Your application needs to be prepared to track several identifiers. The payment_method_token
|
161
|
+
identifies a payment method stored in Samurai. Each transaction processed
|
162
|
+
has a transaction_token that identifies a group of transactions (initiated with
|
163
|
+
a purchase or authorization) and a reference_id that identifies the specific
|
164
|
+
transaction.
|
165
|
+
|
166
|
+
### Purchases and Authorizations
|
167
|
+
|
168
|
+
When you want to start to process a new purchase or authorization on a payment
|
169
|
+
method, Samurai needs to know which of your gateways you want to use. You can
|
170
|
+
initiate a purchase (if your gateway supports it) or an authorization against
|
171
|
+
a gateway by:
|
172
|
+
|
173
|
+
gateway = Samurai::Gateway.the_gateway # if you set Samurai.options[:gateway_token]
|
174
|
+
gateway = Samurai::Gateway.find('a_gateway_token') # if you have multiple gateways
|
175
|
+
purchase = gateway.purchase(payment_method_token, amount, options)
|
176
|
+
purchase_reference_id = purchase.reference_id # save this value, you can find the transaction with it later
|
177
|
+
|
178
|
+
An authorization is created the same way:
|
179
|
+
|
180
|
+
authorization = gateway.authorize(payment_method_token, amount, options)
|
181
|
+
authorization_reference_id = authorization.reference_id # save this value, you can find the transaction with it later
|
182
|
+
|
183
|
+
You can specify options for either transaction type. Options is a hash that may contain:
|
184
|
+
|
185
|
+
* descriptor: a string description of the charge
|
186
|
+
* billing_reference: a string reference for the transaction
|
187
|
+
* customer_reference: a string that identifies the customer to your application
|
188
|
+
* custom: a custom value that Samurai will store but not forward to the gateway
|
189
|
+
|
190
|
+
### Capturing an Authorization
|
191
|
+
|
192
|
+
An authorization only puts a hold on the funds that you specified. It won't
|
193
|
+
capture the money. You'll need to call capture on the authorization to do this.
|
194
|
+
|
195
|
+
authorization = Samurai::Transaction.find(authorization_reference_id) # get the authorization created previously
|
196
|
+
capture = authorization.capture # captures the full amount of the authorization
|
197
|
+
|
198
|
+
### Voiding a Transaction
|
199
|
+
|
200
|
+
A transaction that was recently created can be voided, if is has not been
|
201
|
+
settled. A transaction that has settled has already deposited funds into your
|
202
|
+
merchant account.
|
203
|
+
|
204
|
+
transaction = Samurai::Transaction.find(purchase_reference_id) # gets the purchase created before previously
|
205
|
+
void_transaction = transaction.void # voids the transaction
|
206
|
+
|
207
|
+
### Crediting a Transaction
|
208
|
+
|
209
|
+
Once a captured authorization or purchase has settled, you need to credit the
|
210
|
+
transaction if you want to reverse a charge.
|
211
|
+
|
212
|
+
purchase = Samurai::Transaction.find(purchase_reference_id)
|
213
|
+
credit = purchse.credit # credits the full amount of the original purchase
|
214
|
+
|
215
|
+
|
216
|
+
ActiveResource::Base
|
217
|
+
--------------------
|
218
|
+
|
219
|
+
Samurai is dependent on the ActiveResource gem version 2.2.2 or greater. Any
|
220
|
+
Samurai::Base objects descend from ActiveResource::Base, so you can call any
|
221
|
+
ActiveResource instance or class methods on the object or their classes.
|
@@ -6,6 +6,7 @@ class Samurai::PaymentMethod < Samurai::Base
|
|
6
6
|
self.token
|
7
7
|
end
|
8
8
|
|
9
|
+
# Alias for +payment_method_token+
|
9
10
|
def token
|
10
11
|
self.payment_method_token
|
11
12
|
end
|
@@ -16,11 +17,13 @@ class Samurai::PaymentMethod < Samurai::Base
|
|
16
17
|
self.post(:retain)
|
17
18
|
end
|
18
19
|
|
20
|
+
# Redacts sensitive information from the payment method, rendering it unusable.
|
19
21
|
def redact
|
20
22
|
self.post(:redact)
|
21
23
|
end
|
22
24
|
|
23
|
-
|
25
|
+
# Retrieves JSON formatted custom data that is encoded in the custom_data attribute
|
26
|
+
def custom_json_data
|
24
27
|
@custom_data ||= self.custom && (JSON.parse(self.custom) rescue {}).symbolize_keys
|
25
28
|
end
|
26
29
|
end
|
data/lib/samurai/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: samurai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Graeme Rouse
|
@@ -94,7 +94,7 @@ extra_rdoc_files: []
|
|
94
94
|
files:
|
95
95
|
- .gitignore
|
96
96
|
- Gemfile
|
97
|
-
- README.
|
97
|
+
- README.markdown
|
98
98
|
- Rakefile
|
99
99
|
- lib/samurai.rb
|
100
100
|
- lib/samurai/base.rb
|