Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add token support for KomojuGateway #1941

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 22 additions & 11 deletions lib/active_merchant/billing/gateways/komoju.rb
Expand Up @@ -41,20 +41,31 @@ def refund(money, identification, options = {})
commit("/payments/#{identification}/refund", {})
end

private
def store(payment, options = {})
post = {}
add_payment_details(post, payment, options)

def add_payment_details(post, payment, options)
details = {}
commit("/tokens", post)
end

details[:type] = 'credit_card'
details[:number] = payment.number
details[:month] = payment.month
details[:year] = payment.year
details[:verification_value] = payment.verification_value
details[:given_name] = payment.first_name
details[:family_name] = payment.last_name
details[:email] = options[:email] if options[:email]
private

def add_payment_details(post, payment, options)
case payment
when CreditCard
details = {}

details[:type] = 'credit_card'
details[:number] = payment.number
details[:month] = payment.month
details[:year] = payment.year
details[:verification_value] = payment.verification_value
details[:given_name] = payment.first_name
details[:family_name] = payment.last_name
details[:email] = options[:email] if options[:email]
else
details = payment
end
post[:payment_details] = details
end

Expand Down
30 changes: 30 additions & 0 deletions test/unit/gateways/komoju_test.rb
@@ -1,6 +1,8 @@
require 'test_helper'

class KomojuTest < Test::Unit::TestCase
include CommStub

def setup
@gateway = KomojuGateway.new(:login => 'login')

Expand Down Expand Up @@ -29,6 +31,14 @@ def test_successful_credit_card_purchase
assert response.test?
end

def test_successful_credit_card_purchase_with_token
response = stub_comms(@gateway, :ssl_request) do
@gateway.purchase(@amount, "tok_xxx", @options)
end.check_request do |method, endpoint, data, headers|
assert_match('"payment_details":"tok_xxx"', data)
end.respond_with(JSON.generate(successful_credit_card_purchase_response))
end

def test_failed_purchase
raw_response = mock
raw_response.expects(:body).returns(JSON.generate(failed_purchase_response))
Expand Down Expand Up @@ -66,6 +76,17 @@ def test_successful_credit_card_refund
assert response.test?
end

def test_successful_credit_card_store
successful_response = successful_credit_card_store_response
@gateway.expects(:ssl_post).returns(JSON.generate(successful_response))

response = @gateway.store(@credit_card, @options)
assert_success response

assert_equal "tok_e4075d73cd3767a57d324ac38b16d203b7ab2c4c6208d039356451578f236d85a6fd1af715ffef3ccd4b37d3d2456002", response.authorization
assert response.test?
end

private

def successful_credit_card_purchase_response
Expand Down Expand Up @@ -124,6 +145,15 @@ def successful_credit_card_refund_response
}
end

def successful_credit_card_store_response
{
"id" => "tok_e4075d73cd3767a57d324ac38b16d203b7ab2c4c6208d039356451578f236d85a6fd1af715ffef3ccd4b37d3d2456002",
"resource" => "token",
"used" => false,
"created_at" => "2015-03-20T04:51:48Z"
}
end

def failed_purchase_response
{
"error" => {
Expand Down