require 'spec_helper' describe Spree::Gateway::VeritransGateway do let(:client_key) { 'XXXX-YYYY' } let(:server_key) { 'YYYY-ZZZZ' } let(:url_api) { 'https://api/url' } let(:email) { 'customer@example.com' } let(:source) { Spree::CreditCard.new(gateway_payment_profile_id: 'tok_profileid') } let(:payment) { double('Spree::Payment', source: source, order: double('Spree::Order', email: email, bill_address: bill_address ) ) } let(:provider) do double('provider').tap do |p| p.stub(:purchase) p.stub(:authorize) p.stub(:capture) end end let(:gateway_options) { { order_id: "xxx", email: email, billing_address: { name: "#{ Faker::Name.first_name } #{ Faker::Name.last_name }", address1: Faker::Address.street_address, address2: Faker::Address.street_name, city: Faker::Address.city, zip: Faker::AddressUS.zip_code, phone: Faker::PhoneNumber.phone_number, country: Faker::Address.country_code }, shipping_address: { name: "#{ Faker::Name.first_name } #{ Faker::Name.last_name }", address1: Faker::Address.street_address, address2: Faker::Address.street_name, city: Faker::Address.city, zip: Faker::AddressUS.zip_code, phone: Faker::PhoneNumber.phone_number, country: Faker::Address.country_code } } } before do subject.preferences = { client_key: client_key, server_key: server_key, url_api: url_api } subject.stub(:provider).and_return provider end context 'purchasing' do after do subject.purchase(19900000, source, gateway_options) end it 'send the payment to the provider' do provider.should_receive(:charge).with(anything, { url_api: url_api, server_key: server_key }) end end context 'authorizing' do after do subject.authorize(19900000, source, gateway_options) end it 'send the authorization to the provider' do provider.should_receive(:charge).with(anything, { url_api: url_api, server_key: server_key }) end end context 'capturing' do after do subject.capture(12000000, '1234', {}) end it 'convert the amount to gross_amount' do provider.should_receive(:capture).with('1234', 120000, { url_api: url_api, server_key: server_key }) end end context 'capture with payment class' do let(:gateway) do gateway = described_class.new(active: true) gateway.set_preference :server_key, server_key gateway.set_preference :url_api, url_api gateway.stub(:provider).and_return provider gateway.stub :source_required => true gateway end let(:order) { Spree::Order.create } let(:card) do # mock_model(Spree::CreditCard, :gateway_customer_profile_id => 'cus_abcde', # :imported => false) create :credit_card, gateway_customer_profile_id: 'cus_abcde', imported: false end let(:payment) do payment = Spree::Payment.new payment.source = card payment.order = order payment.payment_method = gateway payment.amount = 9855 payment.state = 'pending' payment.response_code = '12345' payment end let!(:success_response) do double('success_response', :success? => true, :authorization => '123', :avs_result => { 'code' => 'avs-code' }, :cvv_result => { 'code' => 'cvv-code', 'message' => "CVV Result"}) end after do payment.capture! end it 'gets correct amount' do provider.should_receive(:capture).with('12345', 9855,{ url_api: url_api, server_key: server_key }).and_return(success_response) end end end