Skip to content

Commit

Permalink
removed 'getToken' method from PaystackTransactions
Browse files Browse the repository at this point in the history
  • Loading branch information
IkoroVictor committed Feb 12, 2016
1 parent bc7f004 commit 50f62f8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 45 deletions.
3 changes: 0 additions & 3 deletions lib/paystack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ def initialize paystack_public_key=nil, paystack_private_key=nil

end

def getToken card
return TokenManager.create(card, @public_key)
end

def setPublicKey public_key
@public_key = public_key
Expand Down
8 changes: 4 additions & 4 deletions lib/paystack/modules/tokenmanager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ def TokenManager.createServerToken(encrypted_card, publishableKey)
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}"
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']}"
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}"
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"

rescue PayStackServerError => e
rescue PaystackServerError => e
puts e.response.code
Utils.serverErrorHandler(e)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/paystack/objects/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def self.initGetRequest(paystackObj, url)
rescue JSON::ParserError => jsonerr
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"

rescue PayStackServerError => e
rescue PaystackServerError => e
Utils.serverErrorHandler(e)
end
return result
Expand All @@ -52,7 +52,7 @@ def self.initPostRequest(paystackObj, url, data = {}, json=false )
rescue JSON::ParserError => jsonerr
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"

rescue PayStackServerError => e
rescue PaystackServerError => e
Utils.serverErrorHandler(e)
end
return result
Expand All @@ -73,7 +73,7 @@ def self.initPutRequest(paystackObj, url, data = {} )
rescue JSON::ParserError => jsonerr
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"

rescue PayStackServerError => e
rescue PaystackServerError => e
Utils.serverErrorHandler(e)
end
return result
Expand All @@ -94,7 +94,7 @@ def self.initDeleteRequest(paystackObj, url)
rescue JSON::ParserError => jsonerr
raise PaystackServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"

rescue PayStackServerError => e
rescue PaystackServerError => e
Utils.serverErrorHandler(e)
end
return result
Expand Down
7 changes: 0 additions & 7 deletions lib/paystack/objects/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ def totals page=1
return PaystackTransactions.totals(@paystack, page)
end

def chargeToken(token, amount,args = {})
return PaystackTransactions.chargeToken(@paystack,token, amount, args)
end

def chargeAuthorization(authorization_code, email, amount,args = {})
return PaystackTransactions.chargeAuthorization(@paystack,authorization_code,email, amount, args)
Expand Down Expand Up @@ -56,10 +53,6 @@ def PaystackTransactions.totals(paystackObj, page=1)
initGetRequest(paystackObj, "#{API::TRANSACTION_PATH}/totals?page=#{page}")
end

def PaystackTransactions.chargeToken(paystackObj,token, amount,args = {})
hash = {:token => token, :amount => amount}.merge(args)
initPostRequest(paystackObj,"#{API::TRANSACTION_PATH}/charge_token", hash, true)
end
def PaystackTransactions.chargeAuthorization(paystackObj,authorization_code,email, amount,args = {})
hash = {:authorization_code => authorization_code, :amount => amount, :email => email}.merge(args)
initPostRequest(paystackObj,"#{API::TRANSACTION_PATH}/charge_authorization", hash, true)
Expand Down
47 changes: 20 additions & 27 deletions spec/paystack_transactions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
end

it "should initialize a transaction and expect an authorization url" do
reference = Random.new_seed.to_s[0..9]
paystack = Paystack.new(public_test_key, private_test_key)
transactions = PaystackTransactions.new(paystack)
expect(transactions.nil?).to eq false
temp = transactions.initializeTransaction(
:reference => Random.new_seed.to_s[0..9],
:email => "ikoro.victor@gmail.com",
:reference => reference,
:email => "xxxrr@gmail.com",
:amount => 30000,
)
#puts temp
puts temp
expect(temp.nil?).to eq false
expect(temp['data']['authorization_url'].nil?).to eq false

Expand All @@ -63,7 +64,6 @@
#puts hash
expect(hash.nil?).to eq false
#expect(hash['data']['id'].nil?).to eq false

end

it "should return a list of transaction totals" do
Expand All @@ -75,32 +75,25 @@
expect(totals.nil?).to eq false
end

it "should generate a valid token and charge client using token" do

card = PaystackCard.new(:name => 'Victor Ikoro', :number => '4123450131001381', :cvc => '883', :expiryMonth => '09', :expiryYear => '19')
paystack = Paystack.new(public_test_key, private_test_key)
token = paystack.getToken(card)
puts token
expect(token.nil?).to eq false
transaction = PaystackTransactions.new(paystack)
result = transaction.chargeToken(token[:token], 100000.00, :email => "[email protected]", :reference => Random.new_seed.to_s)
#puts result
expect(result.nil?).to eq false
end

it "should recharge an authorization for returning customers" do
it "should recharge an authorization for returning customers.\n Test is bound to fail for invaild reference " do

card = PaystackCard.new(:name => 'Victor Ikoro', :number => '4123450131001381', :cvc => '883', :expiryMonth => '09', :expiryYear => '19')
#TODO: Manually get valid reference for this test
# i.e Initailize transaction with your reference, redirect to authorization url, fill card details,
# if transaction successful, replace your reference with the value below
reference = "2425847597"

paystack = Paystack.new(public_test_key, private_test_key)
token = paystack.getToken(card)
puts token
expect(token.nil?).to eq false
transaction = PaystackTransactions.new(paystack)
charge = transaction.chargeToken(token[:token], 100000.00, :email => "[email protected]", :reference => Random.new_seed.to_s)
#puts charge
expect(charge.nil?).to eq false
result = transaction.chargeAuthorization(charge['data']['authorization']['authorization_code'], charge['data']['customer']['email'], 2000, :reference => Random.new_seed.to_s )
puts result
transactions = PaystackTransactions.new(paystack)
expect(transactions.nil?).to eq false

v = transactions.verify(reference)
expect(v.nil?).to eq false
#puts v
auth = v['data']['authorization']['authorization_code']
email = v['data']['customer']['email']
result = transactions.chargeAuthorization(auth, email , 2000, :reference => Random.new_seed.to_s )
#puts result
expect(result.nil?).to eq false

end
Expand Down

0 comments on commit 50f62f8

Please sign in to comment.