Class: Grassy::Payment

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

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Payment

Returns a new instance of Payment.



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

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

Instance Method Details

#create(params) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/grassy/payment.rb', line 35

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

  response = Net::HTTP.post(
    URI("#{Grassy.base_url}/Merchant/CreatePayment"),
    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

#retrieve(params) ⇒ Object



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

def retrieve(params)
  uri = URI({
    staging:    "https://ecpayment-stage.ecpay.com.tw/1.0.0/Cashier/QueryTrade",
    production: "https://ecpayment.ecpay.com.tw/1.0.0/Cashier/QueryTrade",
  }[Grassy.environment])

  aes = Grassy::AES.new(key = @key, iv = @iv)

  response = Net::HTTP.post(
    uri,
    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