Module: TokenManager

Defined in:
lib/paystack/modules/tokenmanager.rb

Constant Summary collapse

CONCATENATOR =
'*'
RSA_PUBLIC_KEY =
"-----BEGIN PUBLIC KEY-----\nMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANIsL+RHqfkBiKGn/D1y1QnNrMkKzxWP\n2wkeSokw2OJrCI+d6YGJPrHHx+nmb/Qn885/R01Gw6d7M824qofmCvkCAwEAAQ==\n-----END PUBLIC KEY-----\n"

Class Method Summary collapse

Class Method Details

.concatCardFields(card) ⇒ Object

Raises:

  • (PayStackCardError)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/paystack/modules/tokenmanager.rb', line 23

def TokenManager.concatCardFields(card)
  raise PayStackCardError, 'Invalid Card' unless (!card.nil? && card.instance_of?(PaystackCard)) 

  number = Utils.nullifyString(card.number)
  cvc = card.cvc
  expiryMonth = card.expiryMonth
  expiryYear = card.expiryYear
  
  raise PayStackCardError, 'Card number cannot be empty or null' unless (!number.nil?)

  return "#{number}#{CONCATENATOR}#{cvc}#{CONCATENATOR}#{expiryMonth}#{CONCATENATOR}#{expiryYear}"

end

.create(card, publishableKey) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/paystack/modules/tokenmanager.rb', line 11

def TokenManager.create(card, publishableKey)
  if(card == nil)
    raise PayStackCardError, "Card cannot be null"
  elsif !card.isValidCard()
    raise PayStackCardError, "Invalid card details"
  end
      
  card_str = concatCardFields(card)
  enc_card_str = Crypto.encrypt_string(card_str, RSA_PUBLIC_KEY)
  return createServerToken enc_card_str, publishableKey
end

.createServerToken(encrypted_card, publishableKey) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/paystack/modules/tokenmanager.rb', line 37

def TokenManager.createServerToken(encrypted_card, publishableKey)
  token = nil;
  begin
    response = RestClient.post "#{API::TOKEN_URL}", :clientdata => encrypted_card, :publishablekey => publishableKey 
    unless (response.code == 200 || response.code == 201)
        raise PayStackServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
    end
    result = JSON.parse(response.body)
    unless(result['status'] != '0' )
      raise PayStackServerError.new(response), "Server Message: #{result['message']}"
    end
    token = {:token => result['token'], :last4 => result['last4']}
  rescue JSON::ParserError => jsonerr
    raise PayStackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"

  rescue PayStackServerError => e
    puts e.response.code
    Utils.serverErrorHandler(e)
  end
  return token

end