mandarin-api 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc79f7e44ae3ead104e122e209f53cbf4a92f096
4
- data.tar.gz: 99aac970640a5b9a2460868c05813833e8386bf5
3
+ metadata.gz: 1643d81e6774e7c811abcf111b636a289aa8839a
4
+ data.tar.gz: 63300bd786765890c7111e1d8d820c3d5fbe2558
5
5
  SHA512:
6
- metadata.gz: c8fa1db3f95586312fc2d34c8ff53f60619364545dd8eabce526102edba61ce05ccb54a465deca925cbbaa2275441ee127c3140f2b053ad36028a5810757fc41
7
- data.tar.gz: 637e953fab1122831321a5a862fbcc0d0839d7ba28d823476dd07ef3d12c3b3dc432dbe7cebedecc5f6be60e136cd116b816b81b521631874f1748495a05dcd4
6
+ metadata.gz: 122c5becef312f7e3a912e6c507dfb73b35e7a967acf70609996d6187c05989fc415344277b92ed964a6a4ac85c45d3c26c2aa19fedb0aa476420f19bace4817
7
+ data.tar.gz: 8be0eb3138d8d5fc5e176d9b84cad6ac4e36e8df9d6bd4b5a80255ae6087f325ab6ec64d5cddec7ce104f860add2275d63e0660a670574112818ec0b80c9a913
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mandarin-api (0.1.1)
4
+ mandarin-api (0.2.0)
5
5
  rest-client (>= 2.0, < 3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -13,12 +13,22 @@ MandarinApi.configure do |config|
13
13
  config.request_url = 'https://secure.mandarinpay.com'
14
14
  end
15
15
  ```
16
+
17
+
18
+
16
19
  ###**Example of assigning a card:**
17
20
  ```ruby
18
- MandarinApi.assign_card user
21
+ MandarinApi.assign_card user, urls
19
22
  ```
20
23
  `user` should be an instance or a Struct, and should respond to `#email` and `#phone` methods
21
24
  `#phone` should be serialized, for example '+79091234567' is correctly serialized number.
25
+ `urls` is a hash that passes callback and redirect URLs:
26
+ ```ruby
27
+ {
28
+ return: 'https://www.your-page-to-return-user-to.com',
29
+ callback: 'https:/www.your-endpoint-for-callbacks.com'
30
+ }
31
+ ```
22
32
  `#assign_card` will return a hash.
23
33
  ###**Example:**
24
34
  ```ruby
@@ -30,6 +40,8 @@ MandarinApi.assign_card user
30
40
  ```
31
41
  Keep id, it will be used for pay/payouts requests. Use userWebLink to redirect user to Mandarin page for card data input.
32
42
 
43
+
44
+
33
45
  ###**Example of oneway card binding:**
34
46
  ```ruby
35
47
  MandarinApi.oneway_assign_card user, card_number
@@ -47,12 +59,14 @@ Keep id, it will be used for pay/payouts requests. Use userWebLink to redirect u
47
59
  Oneway binded card can only be used for payouts.
48
60
 
49
61
 
62
+
50
63
  ###**Example of performing payment:**
51
64
  ```ruby
52
65
  # order_id - id of order/bill, etc. in your system.
53
66
  # amount - sum of payout
54
67
  # assigned_card_uuid - the id you received assigning the card
55
- MandarinApi.payment(order_id, amount, assigned_card_uuid)
68
+ # extra - an hash to keep your additional data
69
+ MandarinApi.payment(order_id, amount, assigned_card_uuid, extra)
56
70
  ```
57
71
  `#payment` will return a hash with transaction id.
58
72
  ###**Example:**
@@ -60,12 +74,15 @@ MandarinApi.payment(order_id, amount, assigned_card_uuid)
60
74
  { 'id' => '721a5185314740aaa304278fb1d8ee63' }
61
75
  ```
62
76
 
77
+
78
+
63
79
  ###**Example of performing payout:**
64
80
  ```ruby
65
81
  # order_id - id of order/bill, etc. in your system.
66
82
  # amount - sum of payout
67
83
  # assigned_card_uuid - the id you received assigning the card
68
- MandarinApi.payout(order_id, amount, assigned_card_uuid)
84
+ # extra - an hash to keep your additional data
85
+ MandarinApi.payout(order_id, amount, assigned_card_uuid, extra)
69
86
  ```
70
87
  `#payout` will return a hash with transaction id.
71
88
  ###**Example:**
@@ -73,14 +90,27 @@ MandarinApi.payout(order_id, amount, assigned_card_uuid)
73
90
  { 'id' => '721a5185314740aaa304278fb1d8ee63' }
74
91
  ```
75
92
 
93
+
94
+
76
95
  ###**Example of charging user without card binding**
77
96
  ```ruby
78
97
  # order_id - id of order/bill, etc. in your system.
79
98
  # amount - sum of payout
80
- MandarinApi.charge(order_id, amount, user)
99
+ MandarinApi.charge(order_id, amount, user, optional)
81
100
  ```
82
101
  `user` should be an instance or a Struct, and should respond to `#email` and `#phone` methods
83
102
  `#phone` should be serialized, for example '+79091234567' is correctly serialized number.
103
+ `optional` is an hash to keep URLs, data visible on Mandarin page, and your technical data:
104
+ ```ruby
105
+ {
106
+ urls: {
107
+ return: 'https://www.your-page-to-return-user-to.com',
108
+ callback: 'https:/www.your-endpoint-for-callbacks.com'
109
+ },
110
+ visible: { a: 'This value will be visible during payment process on Mandarin page' },
111
+ invisible: { some_additional_id: 12} # this value won't be visible and will be returned in callback
112
+ }
113
+ ```
84
114
  `#charge` will return a hash.
85
115
  ###**Example:**
86
116
  ```ruby
@@ -91,6 +121,8 @@ MandarinApi.charge(order_id, amount, user)
91
121
  }
92
122
  ```
93
123
 
124
+
125
+
94
126
  ###**Example of performing refund:**
95
127
  ```ruby
96
128
  # order_id - id of order/bill, etc. in your system.
@@ -103,6 +135,8 @@ MandarinApi.refund(order_id, transaction_uuid)
103
135
  { 'id' => '721a5185314740aaa304278fb1d8ee63' }
104
136
  ```
105
137
 
138
+
139
+
106
140
  ###**Example of performing rebill:**
107
141
  ```ruby
108
142
  # order_id - id of order/bill, etc. in your system.
@@ -117,6 +151,7 @@ MandarinApi.rebill(order_id, amount, transaction_uuid)
117
151
  ```
118
152
 
119
153
 
154
+
120
155
  You will have to provide a link to receive callbacks from Mandarin.
121
156
  MandarinApi.process_callback takes as arguments body of a callback serialized
122
157
  to hash with symbolized keys and an instance with `#success` and `#failure` methods,
data/lib/mandarin_api.rb CHANGED
@@ -5,33 +5,35 @@ module MandarinApi
5
5
  attr_accessor :config
6
6
  end
7
7
 
8
- def self.assign_card(user)
9
- MandarinApi::CardManager.new.assign_card user
8
+ def self.assign_card(user, urls = {})
9
+ MandarinApi::CardManager.new.assign_card user, urls
10
10
  end
11
11
 
12
12
  def self.oneway_assign_card(user, card)
13
13
  MandarinApi::CardManager.new.one_side_assign_card user, card
14
14
  end
15
15
 
16
- def self.charge(order_id, amount, user)
16
+ def self.charge(order_id, amount, user, extra = {})
17
17
  params = {
18
- order_id: order_id, amount: amount, email: user.email, phone: user.phone
18
+ order_id: order_id, amount: amount, email: user.email, phone: user.phone,
19
+ urls: extra[:urls], custom_values: extra[:visible],
20
+ extra: extra[:invisible]
19
21
  }
20
22
  MandarinApi::PaymentManager.new.perform_charge params
21
23
  end
22
24
 
23
- def self.pay(order_id, amount, assigned_card_uuid)
25
+ def self.pay(order_id, amount, assigned_card_uuid, extra = {})
24
26
  params = {
25
- order_id: order_id, amount: amount,
27
+ order_id: order_id, amount: amount, extra: extra,
26
28
  assigned_card_uuid: assigned_card_uuid
27
29
  }
28
30
  MandarinApi::PaymentManager.new.perform_payment params
29
31
  end
30
32
 
31
- def self.payout(order_id, amount, assigned_card_uuid)
33
+ def self.payout(order_id, amount, assigned_card_uuid, extra = {})
32
34
  params = {
33
35
  order_id: order_id, amount: amount,
34
- assigned_card_uuid: assigned_card_uuid
36
+ assigned_card_uuid: assigned_card_uuid, extra: extra
35
37
  }
36
38
  MandarinApi::PaymentManager.new.perform_payout params
37
39
  end
@@ -43,8 +45,7 @@ module MandarinApi
43
45
 
44
46
  def self.rebill(order_id, amount, transaction_uuid)
45
47
  params = {
46
- order_id: order_id, amount: amount,
47
- transaction_uuid: transaction_uuid
48
+ order_id: order_id, amount: amount, transaction_uuid: transaction_uuid
48
49
  }
49
50
  MandarinApi::PaymentManager.new.perform_rebill params
50
51
  end
@@ -2,8 +2,9 @@
2
2
  module MandarinApi
3
3
  # Manages cards assignment
4
4
  class CardManager
5
- def assign_card(user)
5
+ def assign_card(user, urls)
6
6
  params = { customer_info: { email: user.email, phone: phone(user) } }
7
+ params[:urls] = urls unless urls.empty?
7
8
  MandarinApi::Wrapper.new(merchant_id: MandarinApi.config.merchant_id,
8
9
  secret: MandarinApi.config.secret)
9
10
  .request('/api/card-bindings', params)
@@ -32,18 +32,23 @@ module MandarinApi
32
32
  end
33
33
 
34
34
  def normal_request_body(params, action)
35
- {
35
+ body = {
36
36
  payment: payment(params, action),
37
- target: { card: params[:assigned_card_uuid] }
37
+ target: { card: params[:assigned_card_uuid] },
38
38
  }
39
+ body[:extra] = systemize(params[:extra]) unless params[:extra].empty?
40
+ body
39
41
  end
40
42
 
41
43
  def charge_request_body(params, action)
42
- {
44
+ body = {
43
45
  payment: payment(params, action),
44
- customer_info: { email: params[:email], phone: phone(params[:phone]) },
45
- custom_values: []
46
+ customer_info: { email: params[:email], phone: phone(params[:phone]) }
46
47
  }
48
+ body[:custom_values] = systemize(params[:custom_values]) if params[:custom_values]
49
+ body[:urls] = params[:urls] if params[:urls]
50
+ body[:extra] = systemize(params[:extra]) if params[:extra]
51
+ body
47
52
  end
48
53
 
49
54
  def refund_request_body(params, action)
@@ -64,6 +69,14 @@ module MandarinApi
64
69
  { order_id: params[:order_id], action: action, price: params[:amount] }
65
70
  end
66
71
 
72
+ def systemize(options)
73
+ result = []
74
+ options.each do |key, value|
75
+ result << { name: key.to_s, value: value }
76
+ end
77
+ result
78
+ end
79
+
67
80
  def phone(phone)
68
81
  if phone.nil?
69
82
  '+70000000000'
@@ -45,6 +45,8 @@ module MandarinApi
45
45
  hash.keys.each do |key|
46
46
  new_hash[camelize(key.to_s)] = if hash[key].class == Hash
47
47
  key_transform hash[key]
48
+ elsif hash[key].class == Array
49
+ hash[key].map { |e| key_transform e }
48
50
  else
49
51
  hash[key]
50
52
  end
data/mandarin-api.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'mandarin-api'
4
- s.version = '0.1.1'
4
+ s.version = '0.2.0'
5
5
  s.authors = ['Vladimir Bogaevsky']
6
6
  s.email = 'gitvbogaevsky@gmail.com'
7
7
  s.licenses = ['MIT']
@@ -3,11 +3,22 @@ RSpec.describe MandarinApi::CardManager do
3
3
  let(:merchant_id) { 234 }
4
4
  let(:secret) { 'secret' }
5
5
  let(:user) { User.new('ololo@gmail.com', '8-909-999-88-77') }
6
+ let(:urls) do
7
+ {
8
+ return: 'https://www.ololol.com',
9
+ callback: 'https://www.ololol.com/callbacks'
10
+ }
11
+ end
6
12
 
7
13
  User = Struct.new('User', :email, :phone)
8
14
  describe 'assign_card' do
9
15
  let(:params) do
10
- { customer_info: { email: 'ololo@gmail.com', phone: '+79099998877' } }
16
+ { customer_info: { email: 'ololo@gmail.com', phone: '+79099998877' },
17
+ urls: {
18
+ return: 'https://www.ololol.com',
19
+ callback: 'https://www.ololol.com/callbacks'
20
+ }
21
+ }
11
22
  end
12
23
  it 'calls wrapper instance with args' do
13
24
  allow(MandarinApi).to \
@@ -18,7 +29,7 @@ RSpec.describe MandarinApi::CardManager do
18
29
  .with('/api/card-bindings', params)
19
30
  expect_any_instance_of(MandarinApi::Wrapper).to receive(:request)
20
31
  .with('/api/card-bindings', params)
21
- card_manager.assign_card user
32
+ card_manager.assign_card user, urls
22
33
  end
23
34
  end
24
35
 
@@ -6,30 +6,36 @@ RSpec.describe MandarinApi::PaymentManager do
6
6
  let(:params) do
7
7
  {
8
8
  order_id: 123_321, amount: 35_000,
9
- assigned_card_uuid: '0eb51e74-e704-4c36-b5cb-8f0227621518'
9
+ assigned_card_uuid: '0eb51e74-e704-4c36-b5cb-8f0227621518',
10
+ extra: { a: 'A', b: 'B' }
10
11
  }
11
12
  end
12
13
  let(:normal_request_body) do
13
14
  {
14
15
  payment: { order_id: 123_321, action: action, price: 35_000 },
15
- target: { card: '0eb51e74-e704-4c36-b5cb-8f0227621518' }
16
+ target: { card: '0eb51e74-e704-4c36-b5cb-8f0227621518' },
17
+ extra: [{ name: 'a', value: 'A' }, { name: 'b', value: 'B' }]
16
18
  }
17
19
  end
18
20
  let(:charge_request_body) do
19
21
  {
20
22
  payment: { order_id: 123_321, action: action, price: 35_000 },
21
23
  customer_info: { email: email, phone: phone },
22
- custom_values: []
24
+ custom_values: [{ name: 'a', value: 'A'}, { name: 'b', value: 'B' }],
25
+ extra: [{ name: 'one', value: 'One'}, { name: 'two', value: 'Two' }],
26
+ urls: { callback: 'callback', return: 'return' }
23
27
  }
24
28
  end
29
+
25
30
  describe '#perform_charge' do
26
31
  let(:email) { Faker::Internet.free_email }
27
32
  let(:phone) { "+7#{Faker::Number.between(100_000_000, 999_999_999)}" }
28
33
  let(:action) { 'pay' }
29
34
  let(:params) do
30
35
  {
31
- order_id: 123_321, amount: 35_000,
32
- email: email, phone: phone, custom_values: []
36
+ order_id: 123_321, amount: 35_000, email: email, phone: phone,
37
+ custom_values: { a: 'A', b: 'B' }, extra: { one: 'One', two: 'Two' },
38
+ urls: { callback: 'callback', return: 'return' }
33
39
  }
34
40
  end
35
41
  it 'calls wrapper instance with args' do
@@ -46,7 +46,8 @@ RSpec.describe MandarinApi::Wrapper do
46
46
  },
47
47
  big_key_two: {
48
48
  key_a: 'a', key_b: 'b'
49
- }
49
+ },
50
+ a_r_r_a_y: [{ name: 'a_key', value: 'a' }]
50
51
  }
51
52
  end
52
53
  let(:output) do
@@ -56,7 +57,8 @@ RSpec.describe MandarinApi::Wrapper do
56
57
  },
57
58
  'bigKeyTwo' => {
58
59
  'keyA' => 'a', 'keyB' => 'b'
59
- }
60
+ },
61
+ 'aRRAY' => [{ 'name' => 'a_key', 'value' => 'a' }]
60
62
  }
61
63
  end
62
64
  it 'input: multilevel hash with underscored keys-symbols, ' \
data/spec/mocks.rb CHANGED
@@ -9,7 +9,8 @@ RSpec.shared_context 'mocks', shared_contex: :metadata do
9
9
  payout_body =
10
10
  JSON.generate(
11
11
  payment: { orderId: 123_321, action: 'payout', price: 35_000 },
12
- target: { card: '0eb51e74-e704-4c36-b5cb-8f0227621518' }
12
+ target: { card: '0eb51e74-e704-4c36-b5cb-8f0227621518' },
13
+ customValues: [{ name: 'a', value: 'A' }, {name: 'b', value: 'B' }]
13
14
  )
14
15
  header = {
15
16
  'Accept' => '*/*', 'Accept-Encoding' => 'gzip, deflate',
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mandarin-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Bogaevsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-20 00:00:00.000000000 Z
11
+ date: 2017-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: webmock