require 'spec_helper'

# verifies that all webhooks work, except for the failed_payment_notification hook, since I don't have an example of it.
# because the other 3 types work, I feel pretty confident it will work

# testing with CURL:
# curl -X POST -d @filename.txt http://localhost:3000/api/recurly/webhook --header "Content-Type:text/xml"  --user monkeytoesspeartoss:frizzyfloppymushface
# where @filename.txt is either empty (creates no row), or the contents of one of the create_from_xml tests below (replacing the account_code with a real user_id in our system)

describe RecurlyTransactionWebHook do


  let(:refund_xml) {'<?xml version="1.0" encoding="UTF-8"?>
<successful_refund_notification>
  <account>
    <account_code>56d5b2c6-2a4b-46e4-a984-ec1fbe83a50d</account_code>
    <username nil="true"></username>
    <email>sethcall@gmail.com</email>
    <first_name>Seth</first_name>
    <last_name>Call</last_name>
    <company_name nil="true"></company_name>
  </account>
  <transaction>
    <id>2de439790e8fceb7fc385a4a89b89883</id>
    <invoice_id>2da71ad9c657adf9fe618e4f058c78bb</invoice_id>
    <invoice_number_prefix></invoice_number_prefix>
    <invoice_number type="integer">1033</invoice_number>
    <subscription_id>2da71ad97c826a7b784c264ac59c04de</subscription_id>
    <action>refund</action>
    <date type="datetime">2015-04-01T14:41:40Z</date>
    <amount_in_cents type="integer">216</amount_in_cents>
    <status>success</status>
    <message>Successful test transaction</message>
    <reference>3819545</reference>
    <source>subscription</source>
    <cvv_result code=""></cvv_result>
    <avs_result code="D">Street address and postal code match.</avs_result>
    <avs_result_street nil="true"></avs_result_street>
    <avs_result_postal nil="true"></avs_result_postal>
    <test type="boolean">true</test>
    <voidable type="boolean">true</voidable>
    <refundable type="boolean">false</refundable>
  </transaction>
</successful_refund_notification>'
  }

  let(:void_xml) {
'<?xml version="1.0" encoding="UTF-8"?>
<void_payment_notification>
  <account>
    <account_code>56d5b2c6-2a4b-46e4-a984-ec1fbe83a50d</account_code>
    <username nil="true"></username>
    <email>sethcall@gmail.com</email>
    <first_name>Seth</first_name>
    <last_name>Call</last_name>
    <company_name nil="true"></company_name>
  </account>
  <transaction>
    <id>2de4370332f709c768313d4f47a9af1d</id>
    <invoice_id>2da71ad9c657adf9fe618e4f058c78bb</invoice_id>
    <invoice_number_prefix></invoice_number_prefix>
    <invoice_number type="integer">1033</invoice_number>
    <subscription_id>2da71ad97c826a7b784c264ac59c04de</subscription_id>
    <action>refund</action>
    <date type="datetime">2015-04-01T14:38:59Z</date>
    <amount_in_cents type="integer">216</amount_in_cents>
    <status>void</status>
    <message>Successful test transaction</message>
    <reference>3183996</reference>
    <source>subscription</source>
    <cvv_result code=""></cvv_result>
    <avs_result code="D">Street address and postal code match.</avs_result>
    <avs_result_street nil="true"></avs_result_street>
    <avs_result_postal nil="true"></avs_result_postal>
    <test type="boolean">true</test>
    <voidable type="boolean">false</voidable>
    <refundable type="boolean">false</refundable>
  </transaction>
</void_payment_notification>'
  }

  let(:success_xml) {
'<?xml version="1.0" encoding="UTF-8"?>
<successful_payment_notification>
  <account>
    <account_code>56d5b2c6-2a4b-46e4-a984-ec1fbe83a50d</account_code>
    <username nil="true"></username>
    <email>seth@jamkazam.com</email>
    <first_name>Seth</first_name>
    <last_name>Call</last_name>
    <company_name nil="true"></company_name>
  </account>
  <transaction>
    <id>2de4448533db12d6d92b4c4b4e90a4f1</id>
    <invoice_id>2de44484fa4528b504555f43ac8bf42f</invoice_id>
    <invoice_number_prefix></invoice_number_prefix>
    <invoice_number type="integer">1037</invoice_number>
    <subscription_id>2de44484b460d95863799a431383b165</subscription_id>
    <action>purchase</action>
    <date type="datetime">2015-04-01T14:53:44Z</date>
    <amount_in_cents type="integer">216</amount_in_cents>
    <status>success</status>
    <message>Successful test transaction</message>
    <reference>6249355</reference>
    <source>subscription</source>
    <cvv_result code=""></cvv_result>
    <avs_result code="D">Street address and postal code match.</avs_result>
    <avs_result_street nil="true"></avs_result_street>
    <avs_result_postal nil="true"></avs_result_postal>
    <test type="boolean">true</test>
    <voidable type="boolean">true</voidable>
    <refundable type="boolean">true</refundable>
  </transaction>
</successful_payment_notification>'
  }
  describe "sales integrity maintanence" do

    before(:each) do
      @user = FactoryGirl.create(:user, id: '56d5b2c6-2a4b-46e4-a984-ec1fbe83a50d')
    end

    it "deletes jam_track_right when refunded" do

      sale = Sale.create_jam_track_sale(@user)
      sale.recurly_invoice_id = '2da71ad9c657adf9fe618e4f058c78bb'
      sale.recurly_total_in_cents = 216
      sale.save!
      # create a jam_track right, which should be whacked as soon as we craete the web hook
      jam_track_right = FactoryGirl.create(:jam_track_right, user: @user, recurly_adjustment_uuid: 'bleh')

      shopping_cart = ShoppingCart.create(@user, jam_track_right.jam_track)
      SaleLineItem.create_from_shopping_cart(sale, shopping_cart, nil, '2da71ad9c657adf9fe618e4f058c78bb', nil)

      document = Nokogiri::XML(refund_xml)

      RecurlyTransactionWebHook.create_from_xml(document)

      JamTrackRight.find_by_id(jam_track_right.id).should_not be_nil
    end

    it "deletes jam_track_right when voided" do

      sale = Sale.create_jam_track_sale(@user)
      sale.recurly_invoice_id = '2da71ad9c657adf9fe618e4f058c78bb'
      sale.recurly_total_in_cents = 216
      sale.save!
      # create a jam_track right, which should be whacked as soon as we craete the web hook
      jam_track_right = FactoryGirl.create(:jam_track_right, user: @user, recurly_adjustment_uuid: 'blah')

      shopping_cart = ShoppingCart.create(@user, jam_track_right.jam_track)
      SaleLineItem.create_from_shopping_cart(sale, shopping_cart, nil, '2da71ad9c657adf9fe618e4f058c78bb', nil)

      document = Nokogiri::XML(void_xml)

      RecurlyTransactionWebHook.create_from_xml(document)

      JamTrackRight.find_by_id(jam_track_right.id).should_not be_nil
    end
  end


  describe "is_transaction_web_hook?" do

    it "successful payment" do
      document = Nokogiri::XML('<?xml version="1.0" encoding="UTF-8"?><successful_payment_notification/>')
      RecurlyTransactionWebHook.is_transaction_web_hook?(document).should be_true
    end

    it "successful refund" do
      document = Nokogiri::XML('<?xml version="1.0" encoding="UTF-8"?><successful_refund_notification/>')
      RecurlyTransactionWebHook.is_transaction_web_hook?(document).should be_true
    end

    it "failed payment" do
      document = Nokogiri::XML('<?xml version="1.0" encoding="UTF-8"?><failed_payment_notification/>')
      RecurlyTransactionWebHook.is_transaction_web_hook?(document).should be_true
    end

    it "void" do
      document = Nokogiri::XML('<?xml version="1.0" encoding="UTF-8"?><void_payment_notification/>')
      RecurlyTransactionWebHook.is_transaction_web_hook?(document).should be_true
    end

    it "not a transaction web hook" do
      document = Nokogiri::XML('<?xml version="1.0" encoding="UTF-8"?><something/>')
      RecurlyTransactionWebHook.is_transaction_web_hook?(document).should be_false
    end
  end
  describe "create_from_xml" do

    before(:each) do
      @user = FactoryGirl.create(:user, id: '56d5b2c6-2a4b-46e4-a984-ec1fbe83a50d')
    end

    it "successful payment" do

      document = Nokogiri::XML(success_xml)

      transaction = RecurlyTransactionWebHook.create_from_xml(document)
      transaction.valid?.should be_true
      transaction.user.should eq(@user)
      transaction.transaction_type.should eq('payment')
      transaction.subscription_id.should eq('2de44484b460d95863799a431383b165')
      transaction.invoice_id.should eq('2de44484fa4528b504555f43ac8bf42f')
      transaction.invoice_number_prefix.should eq('')
      transaction.invoice_number.should eq(1037)
      transaction.recurly_transaction_id.should eq('2de4448533db12d6d92b4c4b4e90a4f1')
      transaction.action.should eq('purchase')
      transaction.transaction_at.should eq(Time.parse('2015-04-01T14:53:44Z'))
      transaction.amount_in_cents.should eq(216)
      transaction.status.should eq('success')
      transaction.message.should eq('Successful test transaction')
      transaction.reference.should eq('6249355')
    end

    it "successful refund" do
      document = Nokogiri::XML(refund_xml)

      transaction = RecurlyTransactionWebHook.create_from_xml(document)
      transaction.valid?.should be_true
      transaction.user.should eq(@user)
      transaction.transaction_type.should eq('refund')
      transaction.subscription_id.should eq('2da71ad97c826a7b784c264ac59c04de')
      transaction.invoice_id.should eq('2da71ad9c657adf9fe618e4f058c78bb')
      transaction.invoice_number_prefix.should eq('')
      transaction.invoice_number.should eq(1033)
      transaction.recurly_transaction_id.should eq('2de439790e8fceb7fc385a4a89b89883')
      transaction.action.should eq('refund')
      transaction.transaction_at.should eq(Time.parse('2015-04-01T14:41:40Z'))
      transaction.amount_in_cents.should eq(216)
      transaction.status.should eq('success')
      transaction.message.should eq('Successful test transaction')
      transaction.reference.should eq('3819545')

    end

    it "successful void" do
      document = Nokogiri::XML(void_xml)

      transaction = RecurlyTransactionWebHook.create_from_xml(document)
      transaction.valid?.should be_true
      transaction.user.should eq(@user)
      transaction.transaction_type.should eq('void')
      transaction.subscription_id.should eq('2da71ad97c826a7b784c264ac59c04de')
      transaction.invoice_id.should eq('2da71ad9c657adf9fe618e4f058c78bb')
      transaction.invoice_number_prefix.should eq('')
      transaction.invoice_number.should eq(1033)
      transaction.recurly_transaction_id.should eq('2de4370332f709c768313d4f47a9af1d')
      transaction.action.should eq('refund')
      transaction.transaction_at.should eq(Time.parse('2015-04-01T14:38:59Z'))
      transaction.amount_in_cents.should eq(216)
      transaction.status.should eq('void')
      transaction.message.should eq('Successful test transaction')
      transaction.reference.should eq('3183996')
    end
  end
end

# https://github.com/killbilling/recurly-java-library/blob/master/src/main/java/com/ning/billing/recurly/model/push/payment/FailedPaymentNotification.java
# failed_payment_notification