Class: Grassy::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/grassy/token.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Token

Returns a new instance of Token.



3
4
5
6
7
# File 'lib/grassy/token.rb', line 3

def initialize(params)
  @merchant_id = params[:merchant_id]
  @key = params[:key]
  @iv = params[:iv]
end

Instance Method Details

#create(params = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/grassy/token.rb', line 9

def create(params = {})
  aes = AES.new(@key, @iv)

  response = Net::HTTP.post(
    URI("#{Grassy.base_url}/Merchant/GetTokenbyTrade"),
    JSON.dump({
      'MerchantID'  => @merchant_id,
      'RqHeader'    => {
        'Timestamp' => Time.new.to_i,
        'Revision'  => '1.0.0',
      },
      'Data'        => aes.encrypt(ERB::Util.url_encode(JSON.dump(params))),
    }),
    "Content-Type"  => "application/json"
  )

  # do some error handling

  JSON.parse(URI.decode(aes.decrypt(JSON.parse(response.body)['Data'])))
end