require File.dirname(__FILE__) + '/../remote_helper' class RemoteOrderTransactionTest < ActiveSupport::TestCase context "Remote Order Transaction" do setup do MuckCommerce::Configure.enable_live_test_gateway @amount = rand(10000) # This helps prevent 'duplicate transaction' errors from the live gateway @user, @billing_information = setup_live_user_and_billing @options = { :order_id => ActiveMerchant::Utils.generate_unique_id, :description => 'A store purchase', :ip => '127.0.0.1' } end teardown do MuckCommerce::Configure.disable_live_test_gateway end context "authorize credit card" do should "successfully authorize the user's card" do @authorization = OrderTransaction.authorize(@amount, @billing_information, @options) assert @authorization.success?, @authorization.message end context "invalid credit card" do setup do @invalid_billing = Factory.build(:live_billing_information, :credit_card_number => '4111111111111211') end should "raise invalid card exception" do assert_raise(MuckCommerce::Exceptions::PaymentGatewayError) { OrderTransaction.authorize(@amount, @invalid_billing, @options) } end end end context 'purchase' do should "successfully charge the user's credit card" do payment = OrderTransaction.purchase(@amount, @billing_information, @options) assert payment.success?, payment.message assert_equal I18n.translate('muck.commerce.first_time_purchase'), payment.action assert_equal 'This transaction has been approved', payment.message end end context "purchase with credit card" do should "be successful" do payment = OrderTransaction.purchase(@amount, @billing_information, @options) assert payment.success?, payment.message assert_equal I18n.translate('muck.commerce.first_time_purchase'), payment.action end end context "transfer money" do setup do enable_test_paypal_gateway end teardown do MuckCommerce::Configure.enable_live_test_gateway end should "send money via paypal" do payment = OrderTransaction.purchase(@amount, @billing_information, @options) assert payment.success?, payment.message options = { :subject => "The money I owe you", :note => "Sorry it's so late" } result = OrderTransaction.send_money_via_paypal(@amount, 'test@example.com', options) # Paypal won't let you transfer money out of a test account :-) # Keep this here to make sure send_money_via_paypal is functioning assert !result.success?, result.message assert_equal "Account is restricted", result.message end end # # context "purchase with vault id" do # # setup do # @user, @billing = setup_live_user_and_billing # @first_payment = OrderTransaction.purchase(@amount, @billing, @options) # this should give us a vault id # @billing.vault_id = @first_payment.params['customer_vault_id'] # @billing.save! # assert @first_payment.success? # @options = { :order_id => generate_unique_id, :description => 'A second store purchase' } # need a new order id # end # # should "be successful" do # payment = OrderTransaction.purchase(@amount, @billing, false, @options) # assert payment.success? # assert_equal 'purchase using vault', payment.action # assert_equal 'This transaction has been approved', payment.message # #assert_equal @amount.to_s, payment.params['paid_amount'] # #{"response"=>"1", "avsresponse"=>nil, "orderid"=>"0616adec4e9fb02ddddf692ae56db61e", "responsetext"=>"SUCCESS", "authcode"=>"123456", "transactionid"=>"858635334", "type"=>"sale", "response_code"=>"100", "cvvresponse"=>nil} nil # end # # end # context "purchase and update vault id" do # # setup do # @user, @billing = setup_live_user_and_billing # @first_payment = OrderTransaction.purchase(@amount, @billing, @options) # this should give us a vault id # @billing.vault_id = @first_payment.params['customer_vault_id'] # @billing.save! # assert @first_payment.success?, 'first payment to setup for vault id test failed' # @options = { :order_id => generate_unique_id, :description => 'A second store purchase' } # need a new order id # @billing.credit_card_type = 'master' # @billing.credit_card_number = MuckCommerceTestDefinitions::BRAIN_MASTERCARD # @billing.verification_value = '999' # # @billing.save! # end # # should "be successful" do # payment = OrderTransaction.purchase(@amount, @billing, @options) # assert payment.success? # assert_equal 'purchase using vault', payment.action # assert_equal 'This transaction has been approved', payment.message # #assert_equal @amount.to_s, payment.params['paid_amount'] # end # # end context "decline credit card" do should "be decline" do payment = OrderTransaction.purchase(0, @billing_information, @options) assert !payment.success? assert_equal I18n.translate('muck.commerce.first_time_purchase'), payment.action end end context "authorize.net cim" do setup do @options = { :order_id => ActiveMerchant::Utils.generate_unique_id, :description => 'A second store purchase' } # need a new order id end context "management actions" do should " create and delete cim entry" do # create entry response = try_cim(@user, @billing_information) { |user, billing_information| OrderTransaction.cim_create(user, billing_information) } assert response.success?, response.message assert_equal @user.customer_profile_id, response.reference assert_equal @billing_information.customer_payment_profile_id, response.params["customer_payment_profile_id_list"]["numeric_string"] # delete entry response = OrderTransaction.cim_delete(@billing_information.billable.customer_profile_id) assert response.success?, response.message end context "cim entry actions" do setup do response = try_cim(@user, @billing_information) { |user, billing_information| OrderTransaction.cim_create(user, billing_information) } @customer_profile_id = response.params['customer_profile_id'] @customer_payment_profile_id = response.params["customer_payment_profile_id_list"]["numeric_string"] assert response.success?, response.message assert @billing_information.has_billing_profile? assert_equal @customer_profile_id, @billing_information.billable.customer_profile_id assert_equal @customer_payment_profile_id, @billing_information.customer_payment_profile_id end teardown do cleanup_cim(@billing_information) end should "update cim" do # update entry @user.email = 'somerandomthing@example.com' response = OrderTransaction.cim_update(@user) assert response.test? assert response.success?, response.message response = OrderTransaction.cim_get_customer_profile(@user.customer_profile_id) assert response.test? assert response.success?, response.message assert_equal @user.email, response.params['profile']['email'] end should "call cim_create_update and determine to update entry" do # update entry @billing_information.first_name = 'two' @billing_information.last_name = 'first' response = OrderTransaction.cim_create_update(@user, @billing_information) assert response.success?, response.message response = OrderTransaction.cim_get_customer_profile(@user.customer_profile_id) assert_equal @billing_information.first_name, response.params['profile']['payment_profiles']['bill_to']['first_name'] assert_equal @billing_information.last_name, response.params['profile']['payment_profiles']['bill_to']['last_name'] end should "validate user cim profile" do assert_equal @user, @billing_information.billable assert_equal @customer_profile_id, @billing_information.billable.customer_profile_id assert_equal @customer_payment_profile_id, @billing_information.customer_payment_profile_id response = OrderTransaction.cim_validate_customer_payment_profile(@billing_information) assert response.success?, response.message assert_equal I18n.translate('muck.commerce.cim_validate_profile'), response.action end should "get cim payment profile" do response = OrderTransaction.cim_get_customer_payment_profile(@user.customer_profile_id, @billing_information.customer_payment_profile_id) assert response.success?, response.message assert response.params['payment_profile'] end should "update cim payment profile" do # update entry @billing_information.first_name = 'payment' @billing_information.last_name = 'update' response = OrderTransaction.cim_update_payment_profile(@billing_information) assert response.test? assert response.success?, response.message response = OrderTransaction.cim_get_customer_payment_profile(@user.customer_profile_id, @billing_information.customer_payment_profile_id) assert response.test? assert response.success?, response.message assert_equal @billing_information.first_name, response.params['payment_profile']['bill_to']['first_name'] assert_equal @billing_information.last_name, response.params['payment_profile']['bill_to']['last_name'] end end should "call cim_create_update and determine to create a new entry" do # create entry assert !@billing_information.has_billing_profile? response = try_cim(@user, @billing_information) { |user, billing_information| OrderTransaction.cim_create_update(user, billing_information) } assert response.success?, response.message response = OrderTransaction.cim_get_customer_profile(@user.customer_profile_id) assert response.success?, response.message cleanup_cim(@billing_information) end end context "payment actions" do setup do response = try_cim(@user, @billing_information) { |user, billing_information| OrderTransaction.cim_create(user, billing_information) } assert @billing_information.has_billing_profile? end teardown do cleanup_cim(@billing_information) end should "purchase" do payment = OrderTransaction.cim_purchase(1234, @billing_information, @options) assert payment.success?, payment.message assert_equal I18n.translate('muck.commerce.purchase_cim'), payment.action end should "authorize" do payment = OrderTransaction.cim_authorize(1235, @billing_information, @options) assert payment.success?, payment.message assert_equal I18n.translate('muck.commerce.authorization_cim'), payment.action end end end end end