nihaopay-ruby 0.2.0 → 0.2.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +29 -26
- data/lib/nihaopay-ruby.rb +1 -1
- data/lib/nihaopay/merchant.rb +6 -4
- data/lib/nihaopay/transactions/authorize.rb +1 -0
- data/lib/nihaopay/transactions/cancel.rb +7 -2
- data/lib/nihaopay/transactions/capture.rb +12 -2
- data/lib/nihaopay/transactions/refund.rb +5 -11
- data/lib/nihaopay/transactions/release.rb +1 -8
- data/lib/nihaopay/version.rb +1 -1
- data/spec/transactions/authorize_spec.rb +16 -0
- data/spec/transactions/capture_spec.rb +7 -0
- data/spec/transactions/refund_spec.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7b4c64acc91099c56a7c9aa62ead290eeeb578b
|
4
|
+
data.tar.gz: d93246938e0dcfda73a733ea91ab71acb94a0be8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd977863d45dadb49ea0a0035a32835cd28533e40931d68df91cc4a9ff9677e1c5a129e0c333a3308575ca48d493fce93fd935e08b63bbe92e1e95dba2093c2a
|
7
|
+
data.tar.gz: d90b8510e72895eb4864f4e04c2066fa4db41dab443834a5202ce89b99cf0d7d57f7c6c84d07808c28be995128779969993d9971cf01de60400e13b05623a144
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -20,7 +20,7 @@ and run `bundle install`
|
|
20
20
|
|
21
21
|
Add a file *config/initializers/nihaopay.rb*.
|
22
22
|
|
23
|
-
```
|
23
|
+
```ruby
|
24
24
|
Nihaopay.configure do |nihaopay|
|
25
25
|
nihaopay.test_mode = true
|
26
26
|
nihaopay.token = <your-merchant-token>
|
@@ -121,7 +121,7 @@ response = merchant.wechat_pay(amount, currency, options)
|
|
121
121
|
|
122
122
|
You need the credit card details for initiating the transaction. You can build the credit card object as below:
|
123
123
|
|
124
|
-
```
|
124
|
+
```ruby
|
125
125
|
credit_card = Nihaopay::CreditCard.new(
|
126
126
|
number: '6221558812340000',
|
127
127
|
expiry_year: 2017,
|
@@ -133,13 +133,13 @@ Acceptable values for `expiry_month` are `01` through `12`.
|
|
133
133
|
|
134
134
|
Now initiate the transaction using above credit card.
|
135
135
|
|
136
|
-
```
|
136
|
+
```ruby
|
137
137
|
express_pay = Nihaopay::Transactions::Authorize.start(amount, credit_card)
|
138
138
|
```
|
139
139
|
|
140
140
|
This returns an instance of `Nihaopay::Transactions::ExpressPay` on which you can access following methods:
|
141
141
|
|
142
|
-
```
|
142
|
+
```ruby
|
143
143
|
express_pay.transaction_id # => "20160714132438002485"
|
144
144
|
express_pay.status # => "success"
|
145
145
|
express_pay.reference # => "3461fcc31aec471780ad1a4dc6111947"
|
@@ -153,7 +153,7 @@ Other methods available are `note` and `time`.
|
|
153
153
|
|
154
154
|
#### Purchase transaction on ExpressPay
|
155
155
|
|
156
|
-
```
|
156
|
+
```ruby
|
157
157
|
express_pay = Nihaopay::Transactions::Purchase.start(amount, credit_card)
|
158
158
|
```
|
159
159
|
|
@@ -163,18 +163,21 @@ This again returns an instance of `Nihaopay::Transactions::ExpressPay`.
|
|
163
163
|
|
164
164
|
For `authorize` and `purchase`, you can pass `currency`, `description`, `note`, and `reference` as options.
|
165
165
|
|
166
|
-
```
|
166
|
+
```ruby
|
167
167
|
express_pay = Nihaopay::Transactions::Authorize.start(amount, credit_card, { currency: 'USD',
|
168
168
|
description: 'Your order description',
|
169
169
|
note: 'Something to remember',
|
170
|
-
reference: 'A unique alphanumeric string'
|
170
|
+
reference: 'A unique alphanumeric string',
|
171
|
+
sub_mid: 'unique ID for nihaopay merchant' })
|
171
172
|
```
|
172
173
|
|
173
174
|
Acceptable currency codes are 'USD' and 'JPY'.
|
174
175
|
|
176
|
+
The option `sub_mid` will be passed as a JSON string `{reserved: "{\"sub_mid\":\"XXX\"}" }` in the params. All the different types of ExpressPay transactions accept this option.
|
177
|
+
|
175
178
|
#### Capture a transaction
|
176
179
|
|
177
|
-
```
|
180
|
+
```ruby
|
178
181
|
captured = express_pay.capture
|
179
182
|
captured.transaction_id # => "20160718111604002633"
|
180
183
|
captured.status # => "success"
|
@@ -185,7 +188,7 @@ captured.time # => 2017-01-18 12:08:42 +0900
|
|
185
188
|
|
186
189
|
If you want to capture a partial amount, you can do:
|
187
190
|
|
188
|
-
```
|
191
|
+
```ruby
|
189
192
|
captured = express_pay.partial_capture(amount)
|
190
193
|
```
|
191
194
|
|
@@ -195,7 +198,7 @@ Authorizations not captured within 30 days are automatically released and cannot
|
|
195
198
|
|
196
199
|
Release an uncaptured transaction.
|
197
200
|
|
198
|
-
```
|
201
|
+
```ruby
|
199
202
|
released = express_pay.release
|
200
203
|
released.transaction_id # => "20160718111604002633"
|
201
204
|
released.status # => "success"
|
@@ -206,7 +209,7 @@ released.time # => 2017-01-18 12:08:42 +0900
|
|
206
209
|
|
207
210
|
#### Cancel a transaction
|
208
211
|
|
209
|
-
```
|
212
|
+
```ruby
|
210
213
|
cancelled = express_pay.cancel
|
211
214
|
cancelled.transaction_id # => "20160718111604002633"
|
212
215
|
cancelled.status # => "success"
|
@@ -224,33 +227,33 @@ Transactions can only be cancelled before the daily settlement deadline. Transac
|
|
224
227
|
|
225
228
|
Only SecurePay, ExpressPay, and Captured transactions can be returned. For details on Released, Cancelled, and Refunded transactions, please navigate to the [TMS](https://tms.nihaopay.com). Transactions are returned with the most recent transactions appearing first.
|
226
229
|
|
227
|
-
```
|
230
|
+
```ruby
|
228
231
|
transactions = Nihaopay::Transactions::Base.fetch
|
229
232
|
```
|
230
233
|
|
231
234
|
By default, only 10 transactions are returned at a time. This can be adjusted by calling `limit` before `fetch`. `limit` can range between 1 and 100.
|
232
235
|
|
233
|
-
```
|
236
|
+
```ruby
|
234
237
|
transactions = Nihaopay::Transactions::Base.limit(5).fetch
|
235
238
|
```
|
236
239
|
|
237
240
|
To retrieve transactions that were processed after the specified time, you can all `after` with `Time` object.
|
238
241
|
|
239
|
-
```
|
242
|
+
```ruby
|
240
243
|
yesterday = Time.now - 24 * 60 * 60
|
241
244
|
transactions = Nihaopay::Transactions::Base.after(yesterday).fetch
|
242
245
|
```
|
243
246
|
|
244
247
|
Similarly, you can fetch the transactions that were processed before the specified time.
|
245
248
|
|
246
|
-
```
|
249
|
+
```ruby
|
247
250
|
yesterday = Time.now - 24 * 60 * 60
|
248
251
|
transactions = Nihaopay::Transactions::Base.before(yesterday).fetch
|
249
252
|
```
|
250
253
|
|
251
254
|
You can chain methods to use multiple options:
|
252
255
|
|
253
|
-
```
|
256
|
+
```ruby
|
254
257
|
yesterday = Time.now - 24 * 60 * 60
|
255
258
|
week_ago = Time.now - 7 * 24 * 60 * 60
|
256
259
|
transactions = Nihaopay::Transactions::Base.before(yesterday)
|
@@ -262,7 +265,7 @@ OR
|
|
262
265
|
|
263
266
|
you can pass the options to `fetch`:
|
264
267
|
|
265
|
-
```
|
268
|
+
```ruby
|
266
269
|
yesterday = Time.now - 24 * 60 * 60
|
267
270
|
week_ago = Time.now - 7 * 24 * 60 * 60
|
268
271
|
transactions = Nihaopay::Transactions::Base.fetch(before: yesterday,
|
@@ -274,7 +277,7 @@ transactions = Nihaopay::Transactions::Base.fetch(before: yesterday,
|
|
274
277
|
|
275
278
|
Provide a unique transaction ID that was returned from a previous response in order to retrieve corresponding transaction’s details.
|
276
279
|
|
277
|
-
```
|
280
|
+
```ruby
|
278
281
|
transaction = Nihaopay::Transactions::Base.find(transaction_id)
|
279
282
|
transaction.transaction_id # => "20160718111604002633"
|
280
283
|
transaction.type # => "charge"
|
@@ -289,7 +292,7 @@ Only SecurePay (UnionPay and AliPay), ExpressPay, and Captured transaction detai
|
|
289
292
|
|
290
293
|
Full refunds can only be created once per transaction.
|
291
294
|
|
292
|
-
```
|
295
|
+
```ruby
|
293
296
|
refunded = express_pay.refund
|
294
297
|
refunded.transaction_id # => "20160718111604002633"
|
295
298
|
refunded.status # => "success"
|
@@ -300,13 +303,13 @@ refunded.time # => 2017-01-18 12:08:42 +0900
|
|
300
303
|
|
301
304
|
You can pass a `reason` when refunding a transaction:
|
302
305
|
|
303
|
-
```
|
306
|
+
```ruby
|
304
307
|
refunded = express_pay.refund(reason: 'Out of stock')
|
305
308
|
```
|
306
309
|
|
307
310
|
Partial refunds can be created multiple times up to the amount of the Transaction.
|
308
311
|
|
309
|
-
```
|
312
|
+
```ruby
|
310
313
|
refunded = express_pay.partial_refund(amount)
|
311
314
|
refunded = express_pay.partial_refund(amount, reason: 'Cancellation fee')
|
312
315
|
```
|
@@ -315,7 +318,7 @@ refunded = express_pay.partial_refund(amount, reason: 'Cancellation fee')
|
|
315
318
|
|
316
319
|
If you have multiple merchants configured to the Nihaopay payment gateway, they will have different tokens each. You can do transactions per merchant as below:
|
317
320
|
|
318
|
-
```
|
321
|
+
```ruby
|
319
322
|
merchant_token = "6c4dc4828474fa73c5f438a9eb2fbf3092e44"
|
320
323
|
nihaopay_merchant = Nihaopay::Merchant.new(merchant_token)
|
321
324
|
express_pay = nihaopay_merchant.authorize(amount, credit_card)
|
@@ -323,15 +326,15 @@ express_pay = nihaopay_merchant.authorize(amount, credit_card)
|
|
323
326
|
|
324
327
|
OR
|
325
328
|
|
326
|
-
```
|
329
|
+
```ruby
|
327
330
|
express_pay = nihaopay_merchant.authorize(amount, credit_card, options)
|
328
331
|
```
|
329
332
|
|
330
|
-
`options` may include `currency`, `description`, `reference`, and `
|
333
|
+
`options` may include `currency`, `description`, `reference`, `note`, and `sub_mid`.
|
331
334
|
|
332
335
|
Similarly, you can do other transactions directly on `Nihaopay::Merchant` object:
|
333
336
|
|
334
|
-
```
|
337
|
+
```ruby
|
335
338
|
# capture
|
336
339
|
express_pay = nihaopay_merchant.capture(transaction_id, amount, currency)
|
337
340
|
|
@@ -342,7 +345,7 @@ express_pay = nihaopay_merchant.purchase(amount, credit_card, options)
|
|
342
345
|
# release
|
343
346
|
express_pay = nihaopay_merchant.release(transaction_id)
|
344
347
|
|
345
|
-
#refund
|
348
|
+
# refund
|
346
349
|
express_pay = nihaopay_merchant.refund(transaction_id, amount, currency)
|
347
350
|
express_pay = nihaopay_merchant.refund(transaction_id, amount, currency, reason: 'Cancellation fee')
|
348
351
|
```
|
data/lib/nihaopay-ruby.rb
CHANGED
@@ -19,8 +19,8 @@ require 'nihaopay/secure_pay/we_chat_pay'
|
|
19
19
|
|
20
20
|
require 'nihaopay/transactions/base'
|
21
21
|
require 'nihaopay/transactions/authorize'
|
22
|
-
require 'nihaopay/transactions/cancel'
|
23
22
|
require 'nihaopay/transactions/capture'
|
23
|
+
require 'nihaopay/transactions/cancel'
|
24
24
|
require 'nihaopay/transactions/purchase'
|
25
25
|
require 'nihaopay/transactions/refund'
|
26
26
|
require 'nihaopay/transactions/release'
|
data/lib/nihaopay/merchant.rb
CHANGED
@@ -26,8 +26,9 @@ module Nihaopay
|
|
26
26
|
Nihaopay::Transactions::Authorize.start(amount, credit_card, options)
|
27
27
|
end
|
28
28
|
|
29
|
-
def capture(transaction_id, amount, currency)
|
30
|
-
|
29
|
+
def capture(transaction_id, amount, currency, options = {})
|
30
|
+
options[:token] = token
|
31
|
+
Nihaopay::Transactions::Capture.start(transaction_id, amount, currency, options)
|
31
32
|
end
|
32
33
|
|
33
34
|
def purchase(amount, credit_card, options = {})
|
@@ -35,8 +36,9 @@ module Nihaopay
|
|
35
36
|
Nihaopay::Transactions::Purchase.start(amount, credit_card, options)
|
36
37
|
end
|
37
38
|
|
38
|
-
def release(transaction_id)
|
39
|
-
|
39
|
+
def release(transaction_id, options = {})
|
40
|
+
options[:token] = token
|
41
|
+
Nihaopay::Transactions::Release.start(transaction_id, options)
|
40
42
|
end
|
41
43
|
|
42
44
|
def refund(transaction_id, amount, currency, options = {})
|
@@ -20,6 +20,7 @@ module Nihaopay
|
|
20
20
|
params.merge! Nihaopay::HashUtil.slice(options, *valid_options)
|
21
21
|
params[:capture] = capture_param
|
22
22
|
params[:amount] = amount
|
23
|
+
params[:reserved] = { 'sub_mid' => options[:sub_mid].to_s }.to_json if options.key?(:sub_mid)
|
23
24
|
params[:currency] ||= Nihaopay.currency
|
24
25
|
params
|
25
26
|
end
|
@@ -1,13 +1,14 @@
|
|
1
1
|
module Nihaopay
|
2
2
|
module Transactions
|
3
|
-
class Cancel <
|
3
|
+
class Cancel < Capture
|
4
4
|
attr_accessor :cancelled, :cancel_transaction_id
|
5
5
|
|
6
6
|
class << self
|
7
7
|
def start(transaction_id, options = {})
|
8
8
|
@token = options.delete(:token)
|
9
9
|
url = request_url(transaction_id)
|
10
|
-
|
10
|
+
params = request_params(options)
|
11
|
+
response = HTTParty.post(url, headers: request_headers, body: request_body(params))
|
11
12
|
build_from_response!(response)
|
12
13
|
end
|
13
14
|
|
@@ -15,6 +16,10 @@ module Nihaopay
|
|
15
16
|
"#{base_url}/transactions/#{transaction_id}/cancel"
|
16
17
|
end
|
17
18
|
|
19
|
+
def valid_options
|
20
|
+
[]
|
21
|
+
end
|
22
|
+
|
18
23
|
def valid_attributes
|
19
24
|
%i(transaction_id status cancelled cancel_transaction_id time)
|
20
25
|
end
|
@@ -7,8 +7,8 @@ module Nihaopay
|
|
7
7
|
def start(transaction_id, amount, currency, options = {})
|
8
8
|
@token = options.delete(:token)
|
9
9
|
url = request_url(transaction_id)
|
10
|
-
params =
|
11
|
-
response = HTTParty.post(url, headers: request_headers, body: params)
|
10
|
+
params = request_params(options.merge(amount: amount, currency: currency))
|
11
|
+
response = HTTParty.post(url, headers: request_headers, body: request_body(params))
|
12
12
|
build_from_response!(response)
|
13
13
|
end
|
14
14
|
|
@@ -16,6 +16,16 @@ module Nihaopay
|
|
16
16
|
"#{base_url}/transactions/#{transaction_id}/capture"
|
17
17
|
end
|
18
18
|
|
19
|
+
def request_params(options = {})
|
20
|
+
params = Nihaopay::HashUtil.slice(options, *valid_options)
|
21
|
+
params[:reserved] = { 'sub_mid' => options[:sub_mid].to_s }.to_json if options.key?(:sub_mid)
|
22
|
+
params
|
23
|
+
end
|
24
|
+
|
25
|
+
def valid_options
|
26
|
+
%i(amount currency)
|
27
|
+
end
|
28
|
+
|
19
29
|
def valid_attributes
|
20
30
|
%i(transaction_id status captured capture_transaction_id time)
|
21
31
|
end
|
@@ -1,23 +1,17 @@
|
|
1
1
|
module Nihaopay
|
2
2
|
module Transactions
|
3
|
-
class Refund <
|
4
|
-
VALID_OPTIONS = %i(reason).freeze
|
5
|
-
|
3
|
+
class Refund < Capture
|
6
4
|
attr_accessor :refunded, :refund_transaction_id
|
7
5
|
|
8
6
|
class << self
|
9
|
-
def start(transaction_id, amount, currency, options = {})
|
10
|
-
@token = options.delete(:token)
|
11
|
-
url = request_url(transaction_id)
|
12
|
-
params = Nihaopay::HashUtil.slice(options, *VALID_OPTIONS).merge(amount: amount, currency: currency)
|
13
|
-
response = HTTParty.post(url, headers: request_headers, body: request_body(params))
|
14
|
-
build_from_response!(response)
|
15
|
-
end
|
16
|
-
|
17
7
|
def request_url(transaction_id)
|
18
8
|
"#{base_url}/transactions/#{transaction_id}/refund"
|
19
9
|
end
|
20
10
|
|
11
|
+
def valid_options
|
12
|
+
super | %i(reason)
|
13
|
+
end
|
14
|
+
|
21
15
|
def valid_attributes
|
22
16
|
%i(transaction_id status refunded refund_transaction_id time)
|
23
17
|
end
|
@@ -1,16 +1,9 @@
|
|
1
1
|
module Nihaopay
|
2
2
|
module Transactions
|
3
|
-
class Release <
|
3
|
+
class Release < Cancel
|
4
4
|
attr_accessor :released, :release_transaction_id
|
5
5
|
|
6
6
|
class << self
|
7
|
-
def start(transaction_id, options = {})
|
8
|
-
@token = options.delete(:token)
|
9
|
-
url = request_url(transaction_id)
|
10
|
-
response = HTTParty.post(url, headers: request_headers, body: request_body)
|
11
|
-
build_from_response!(response)
|
12
|
-
end
|
13
|
-
|
14
7
|
def request_url(transaction_id)
|
15
8
|
"#{base_url}/transactions/#{transaction_id}/release"
|
16
9
|
end
|
data/lib/nihaopay/version.rb
CHANGED
@@ -103,6 +103,22 @@ describe Nihaopay::Transactions::Authorize do
|
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
|
+
context 'with :sub_mid in options' do
|
107
|
+
let(:options) { { sub_mid: 'foobar' } }
|
108
|
+
let(:body) do
|
109
|
+
'card_number=6221558812340000'\
|
110
|
+
'&card_exp_year=17'\
|
111
|
+
'&card_exp_month=11'\
|
112
|
+
'&card_cvv=123'\
|
113
|
+
'&capture=false'\
|
114
|
+
'&amount=1000'\
|
115
|
+
'&reserved={"sub_mid":"foobar"}'\
|
116
|
+
'¤cy=USD'
|
117
|
+
end
|
118
|
+
it { expect(HTTParty).to receive(:post).with(url, headers: headers, body: body) }
|
119
|
+
after { described_class.start(1000, cc, options) }
|
120
|
+
end
|
121
|
+
|
106
122
|
describe '.build_from_response!' do
|
107
123
|
it 'should return transaction object' do
|
108
124
|
txn = described_class.start(1000, cc)
|
@@ -61,6 +61,13 @@ describe Nihaopay::Transactions::Capture do
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
+
context 'with :sub_mid in options' do
|
65
|
+
let(:options) { { sub_mid: 'foobar' } }
|
66
|
+
let(:body) { 'amount=1000¤cy=JPY&reserved={"sub_mid":"foobar"}' }
|
67
|
+
it { expect(HTTParty).to receive(:post).with(url, headers: headers, body: body) }
|
68
|
+
after { described_class.start('1111', 1000, 'JPY', options) }
|
69
|
+
end
|
70
|
+
|
64
71
|
describe '.build_from_response!' do
|
65
72
|
it 'should return transaction object' do
|
66
73
|
txn = described_class.start('1111', 1000, 'JPY')
|
@@ -50,7 +50,7 @@ describe Nihaopay::Transactions::Refund do
|
|
50
50
|
|
51
51
|
context 'with options' do
|
52
52
|
let(:options) { { reason: 'out of stock' } }
|
53
|
-
let(:body) { 'reason=out of stock
|
53
|
+
let(:body) { 'amount=1000¤cy=JPY&reason=out of stock' }
|
54
54
|
it { expect(HTTParty).to receive(:post).with(url, headers: headers, body: body) }
|
55
55
|
after { described_class.start('1111', 1000, 'JPY', options) }
|
56
56
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nihaopay-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JagdeepSingh
|
@@ -9,8 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-01-
|
12
|
+
date: 2017-01-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1'
|
14
28
|
- !ruby/object:Gem::Dependency
|
15
29
|
name: httparty
|
16
30
|
requirement: !ruby/object:Gem::Requirement
|