mirror of
https://github.com/internetee/registry.git
synced 2025-07-29 22:16:19 +02:00
creating sync with billing
This commit is contained in:
parent
162647acba
commit
0a4444b556
9 changed files with 577 additions and 60 deletions
306
test/integration/eis_billing/invoices_test.rb
Normal file
306
test/integration/eis_billing/invoices_test.rb
Normal file
|
@ -0,0 +1,306 @@
|
|||
require 'test_helper'
|
||||
|
||||
class StubAuthorization < ApplicationController
|
||||
skip_authorization_check
|
||||
|
||||
def authorized
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
EisBilling::BaseController = StubAuthorization
|
||||
|
||||
class EInvoiceResponseTest < ApplicationIntegrationTest
|
||||
setup do
|
||||
sign_in users(:api_bestnames)
|
||||
@invoice = invoices(:one)
|
||||
|
||||
response_message = {
|
||||
message: 'got it'
|
||||
}
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_status')
|
||||
.to_return(status: 200, body: response_message.to_json, headers: {})
|
||||
end
|
||||
|
||||
test 'it should update status of invoice if payment order is existed' do
|
||||
@invoice.update(total: 120.0)
|
||||
@invoice.reload
|
||||
|
||||
incoming_params = {
|
||||
invoice: {
|
||||
invoice_number: @invoice.number,
|
||||
initiator: 'registry',
|
||||
payment_reference: '93b29d54ae08f7728e72ee3fe0e88855cd1d266912039d7d23fa2b54b7e1b349',
|
||||
transaction_amount: 120.0,
|
||||
status: 'paid',
|
||||
in_directo: false,
|
||||
everypay_response: {
|
||||
'some' => 'some'
|
||||
},
|
||||
sent_at_omniva: Time.zone.now - 10.minutes
|
||||
}
|
||||
}
|
||||
|
||||
@invoice.account_activity.delete && @invoice.reload
|
||||
|
||||
assert_equal @invoice.payment_orders.pluck(:status), %w[issued issued]
|
||||
put eis_billing_invoices_path, params: incoming_params
|
||||
@invoice.reload
|
||||
@invoice.payment_orders.each(&:reload)
|
||||
|
||||
invoice = Invoice.find(@invoice.id)
|
||||
assert_equal invoice.payment_orders.pluck(:status), %w[issued issued paid]
|
||||
end
|
||||
|
||||
test 'it should update invoice data as directo and omniva' do
|
||||
incoming_params = {
|
||||
invoice: {
|
||||
invoice_number: @invoice.number,
|
||||
initiator: 'registry',
|
||||
payment_reference: '93b29d54ae08f7728e72ee3fe0e88855cd1d266912039d7d23fa2b54b7e1b349',
|
||||
transaction_amount: 270.0,
|
||||
status: 'unpaid',
|
||||
in_directo: true,
|
||||
everypay_response: {
|
||||
'some' => 'some'
|
||||
},
|
||||
sent_at_omniva: Time.zone.now - 10.minutes
|
||||
}
|
||||
}
|
||||
|
||||
assert_equal @invoice.payment_orders.pluck(:status), %w[issued issued]
|
||||
assert_nil @invoice.e_invoice_sent_at
|
||||
refute @invoice.in_directo
|
||||
|
||||
put eis_billing_invoices_path, params: incoming_params
|
||||
|
||||
@invoice.payment_orders.each(&:reload)
|
||||
@invoice.reload
|
||||
|
||||
assert_equal @invoice.payment_orders.pluck(:status), %w[issued issued]
|
||||
assert @invoice.in_directo
|
||||
assert_not_nil @invoice.e_invoice_sent_at
|
||||
end
|
||||
|
||||
test 'it should create new payment order if payment order and activity are missing, but status has paid status' do
|
||||
invoice = invoices(:one)
|
||||
|
||||
invoice.payment_orders.destroy_all and invoice.account_activity.destroy
|
||||
invoice.update(total: 120.0)
|
||||
invoice.reload
|
||||
|
||||
incoming_params = {
|
||||
invoice: {
|
||||
invoice_number: invoice.number,
|
||||
initiator: 'registry',
|
||||
payment_reference: '93b29d54ae08f7728e72ee3fe0e88855cd1d266912039d7d23fa2b54b7e1b349',
|
||||
transaction_amount: 120.0,
|
||||
status: 'paid',
|
||||
in_directo: false,
|
||||
everypay_response: {
|
||||
'some' => 'some'
|
||||
},
|
||||
sent_at_omniva: Time.zone.now - 10.minutes
|
||||
}
|
||||
}
|
||||
|
||||
assert invoice.payment_orders.empty?
|
||||
assert_nil invoice.account_activity
|
||||
|
||||
put eis_billing_invoices_path, params: incoming_params
|
||||
|
||||
invoice.reload
|
||||
invoice.payment_orders.each(&:reload)
|
||||
|
||||
assert_equal invoice.payment_orders.count, 1
|
||||
assert invoice.payment_orders.first.paid?
|
||||
assert invoice.account_activity
|
||||
end
|
||||
|
||||
test 'it should ignore payment order creation if payment status is not paid and payment order not existed' do
|
||||
incoming_params = {
|
||||
invoice: {
|
||||
invoice_number: @invoice.number,
|
||||
initiator: 'registry',
|
||||
payment_reference: '93b29d54ae08f7728e72ee3fe0e88855cd1d266912039d7d23fa2b54b7e1b349',
|
||||
transaction_amount: 270.0,
|
||||
status: 'cancelled',
|
||||
in_directo: false,
|
||||
everypay_response: {
|
||||
'some' => 'some'
|
||||
},
|
||||
sent_at_omniva: Time.zone.now - 10.minutes
|
||||
}
|
||||
}
|
||||
|
||||
@invoice.payment_orders.destroy_all and @invoice.account_activity.destroy
|
||||
@invoice.reload
|
||||
|
||||
assert @invoice.payment_orders.empty?
|
||||
assert_nil @invoice.account_activity
|
||||
|
||||
put eis_billing_invoices_path, params: incoming_params and @invoice.reload
|
||||
|
||||
assert @invoice.payment_orders.empty?
|
||||
assert_nil @invoice.account_activity
|
||||
end
|
||||
|
||||
test 'it should add balance if payment order mark as paid' do
|
||||
invoice = invoices(:one)
|
||||
item = invoice.items.first
|
||||
|
||||
invoice.payment_orders.destroy_all and invoice.account_activity.destroy
|
||||
invoice.update(total: 120.0) && invoice.reload
|
||||
item.update(price: 100.0) && item.reload
|
||||
|
||||
incoming_params = {
|
||||
invoice: {
|
||||
invoice_number: invoice.number,
|
||||
initiator: 'registry',
|
||||
payment_reference: '93b29d54ae08f7728e72ee3fe0e88855cd1d266912039d7d23fa2b54b7e1b349',
|
||||
transaction_amount: 120.0,
|
||||
status: 'paid',
|
||||
in_directo: false,
|
||||
everypay_response: {
|
||||
'some' => 'some'
|
||||
},
|
||||
sent_at_omniva: Time.zone.now - 10.minutes
|
||||
}
|
||||
}
|
||||
|
||||
assert invoice.payment_orders.empty?
|
||||
assert_nil invoice.account_activity
|
||||
|
||||
account = invoice.buyer.accounts.first
|
||||
|
||||
assert_equal account.balance.to_f, 100.0
|
||||
|
||||
put eis_billing_invoices_path, params: incoming_params
|
||||
|
||||
invoice.reload
|
||||
invoice.payment_orders.each(&:reload)
|
||||
account.reload
|
||||
|
||||
assert_equal invoice.payment_orders.count, 1
|
||||
assert invoice.payment_orders.first.paid?
|
||||
assert invoice.account_activity
|
||||
|
||||
assert_equal account.balance.to_f, 200.0
|
||||
end
|
||||
|
||||
test 'should change nothing if invoice is already paid' do
|
||||
assert @invoice.account_activity.present?
|
||||
assert @invoice.payment_orders.present?
|
||||
|
||||
account = @invoice.buyer.accounts.first
|
||||
assert_equal account.balance.to_f, 100.0
|
||||
assert @invoice.paid?
|
||||
|
||||
incoming_params = {
|
||||
invoice: {
|
||||
invoice_number: @invoice.number,
|
||||
initiator: 'registry',
|
||||
payment_reference: '93b29d54ae08f7728e72ee3fe0e88855cd1d266912039d7d23fa2b54b7e1b349',
|
||||
transaction_amount: @invoice.total,
|
||||
status: 'paid',
|
||||
in_directo: false,
|
||||
everypay_response: {
|
||||
'some' => 'some'
|
||||
},
|
||||
sent_at_omniva: Time.zone.now - 10.minutes
|
||||
}
|
||||
}
|
||||
|
||||
put eis_billing_invoices_path, params: incoming_params
|
||||
account.reload
|
||||
|
||||
assert_equal account.balance.to_f, 100.0
|
||||
end
|
||||
|
||||
test 'it should decrease balance and again add if user change paid invoice to cancel and then again to paid' do
|
||||
invoice = invoices(:one)
|
||||
item = invoice.items.first
|
||||
|
||||
invoice.payment_orders.destroy_all and invoice.account_activity.destroy
|
||||
invoice.update(total: 120.0) && invoice.reload
|
||||
item.update(price: 100.0) && item.reload
|
||||
|
||||
add_balance_params = {
|
||||
invoice: {
|
||||
invoice_number: invoice.number,
|
||||
initiator: 'registry',
|
||||
payment_reference: '93b29d54ae08f7728e72ee3fe0e88855cd1d266912039d7d23fa2b54b7e1b349',
|
||||
transaction_amount: 120.0,
|
||||
status: 'paid',
|
||||
in_directo: false,
|
||||
everypay_response: {
|
||||
'some' => 'some'
|
||||
},
|
||||
sent_at_omniva: Time.zone.now - 10.minutes
|
||||
}
|
||||
}
|
||||
|
||||
assert invoice.payment_orders.empty?
|
||||
assert_nil invoice.account_activity
|
||||
|
||||
account = invoice.buyer.accounts.first
|
||||
|
||||
assert_equal account.balance.to_f, 100.0
|
||||
|
||||
put eis_billing_invoices_path, params: add_balance_params
|
||||
|
||||
invoice.reload
|
||||
invoice.payment_orders.each(&:reload)
|
||||
account.reload
|
||||
|
||||
assert_equal invoice.payment_orders.count, 1
|
||||
assert invoice.payment_orders.first.paid?
|
||||
assert invoice.account_activity
|
||||
|
||||
assert_equal account.balance.to_f, 200.0
|
||||
|
||||
decrease_balance_params = {
|
||||
invoice: {
|
||||
invoice_number: invoice.number,
|
||||
initiator: 'registry',
|
||||
payment_reference: '93b29d54ae08f7728e72ee3fe0e88855cd1d266912039d7d23fa2b54b7e1b349',
|
||||
transaction_amount: 120.0,
|
||||
status: 'cancelled',
|
||||
in_directo: false,
|
||||
everypay_response: {
|
||||
'some' => 'some'
|
||||
},
|
||||
sent_at_omniva: Time.zone.now - 10.minutes
|
||||
}
|
||||
}
|
||||
|
||||
put eis_billing_invoices_path, params: decrease_balance_params
|
||||
invoice.reload
|
||||
invoice.payment_orders.each(&:reload)
|
||||
account.reload
|
||||
|
||||
assert_equal account.balance.to_f, 100.0
|
||||
end
|
||||
|
||||
test 'it should return an error if invoice not existing' do
|
||||
incoming_params = {
|
||||
invoice: {
|
||||
invoice_number: 'nonexisted-invoice',
|
||||
initiator: 'registry',
|
||||
payment_reference: '93b29d54ae08f7728e72ee3fe0e88855cd1d266912039d7d23fa2b54b7e1b349',
|
||||
transaction_amount: 120.0,
|
||||
status: 'paid',
|
||||
in_directo: false,
|
||||
everypay_response: {
|
||||
'some' => 'some'
|
||||
},
|
||||
sent_at_omniva: Time.zone.now - 10.minutes
|
||||
}
|
||||
}
|
||||
|
||||
put eis_billing_invoices_path, params: incoming_params
|
||||
registry_response = JSON.parse(response.body).with_indifferent_access[:error]
|
||||
|
||||
assert_equal registry_response[:message], 'Invoice with nonexisted-invoice number not found'
|
||||
end
|
||||
end
|
|
@ -9,10 +9,7 @@ class PaymentStatusTest < ApplicationIntegrationTest
|
|||
Spy.on_instance_method(EisBilling::BaseController, :authorized).and_return(true)
|
||||
end
|
||||
|
||||
def shoudl_update_buyer_balance
|
||||
assert @invoice.paid?
|
||||
assert_equal @invoice.buyer.balance.to_f, 100.0
|
||||
|
||||
test 'should mark an invoice as paid' do
|
||||
payload = {
|
||||
payment_state: 'settled',
|
||||
order_reference: @unpaid.number,
|
||||
|
@ -20,13 +17,57 @@ class PaymentStatusTest < ApplicationIntegrationTest
|
|||
transaction_time: Time.zone.now,
|
||||
}
|
||||
|
||||
put eis_billing_payment_status_path, params: payload
|
||||
item = @unpaid.items.first
|
||||
|
||||
@invoice.reload
|
||||
@invoice.buyer.reload
|
||||
@registrar.reload
|
||||
refute @unpaid.paid?
|
||||
assert_equal @unpaid.buyer.balance.to_f, 100.0
|
||||
assert_equal item.price, 5.0
|
||||
|
||||
put eis_billing_payment_status_path, params: payload
|
||||
@unpaid.reload
|
||||
assert_equal @unpaid.buyer.balance.to_f, 105.0
|
||||
end
|
||||
|
||||
test 'ignore additonal callbacks if invoice is already paid' do
|
||||
payload = {
|
||||
payment_state: 'settled',
|
||||
order_reference: @unpaid.number,
|
||||
standing_amount: @unpaid.total,
|
||||
transaction_time: Time.zone.now,
|
||||
}
|
||||
|
||||
item = @unpaid.items.first
|
||||
|
||||
refute @unpaid.paid?
|
||||
assert_equal @unpaid.buyer.balance.to_f, 100.0
|
||||
assert_equal item.price, 5.0
|
||||
|
||||
put eis_billing_payment_status_path, params: payload
|
||||
@unpaid.reload
|
||||
assert_equal @unpaid.buyer.balance.to_f, 105.0
|
||||
assert @unpaid.paid?
|
||||
|
||||
put eis_billing_payment_status_path, params: payload
|
||||
@unpaid.reload
|
||||
|
||||
assert_equal @unpaid.buyer.balance.to_f, 105.0
|
||||
assert @unpaid.paid?
|
||||
end
|
||||
|
||||
test 'send callback to already paid invoice' do
|
||||
payload = {
|
||||
payment_state: 'settled',
|
||||
order_reference: @invoice.number,
|
||||
standing_amount: @invoice.total,
|
||||
transaction_time: Time.zone.now,
|
||||
}
|
||||
|
||||
assert @invoice.paid?
|
||||
assert_equal @invoice.buyer.balance.to_f, 100.0
|
||||
|
||||
put eis_billing_payment_status_path, params: payload
|
||||
@invoice.reload
|
||||
assert_equal @invoice.buyer.balance.to_f, 100.0
|
||||
assert @invoice.paid?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,6 +6,8 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
setup do
|
||||
@invoice = invoices(:one)
|
||||
Spy.on_instance_method(EisBilling::BaseController, :authorized).and_return(true)
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_status')
|
||||
.to_return(status: :ok, headers: {})
|
||||
end
|
||||
|
||||
def test_fixture_is_valid
|
||||
|
@ -124,32 +126,32 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
transaction.sum = 250
|
||||
|
||||
invoice_n = Invoice.order(number: :desc).last.number
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator")
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator')
|
||||
.to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}", headers: {})
|
||||
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_generator")
|
||||
.to_return(status: 200, body: "{\"everypay_link\":\"http://link.test\"}", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_generator')
|
||||
.to_return(status: 200, body: '{"everypay_link":"http://link.test"}', headers: {})
|
||||
|
||||
stub_request(:put, "https://registry:3000/eis_billing/e_invoice_response").
|
||||
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}, {\"date\":\"#{Time.zone.now-10.minutes}\"}", headers: {})
|
||||
stub_request(:put, 'https://registry:3000/eis_billing/e_invoice_response')
|
||||
.to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}, {\"date\":\"#{Time.zone.now - 10.minutes}\"}", headers: {})
|
||||
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/e_invoice/e_invoice").
|
||||
to_return(status: 200, body: "", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/e_invoice/e_invoice')
|
||||
.to_return(status: 200, body: '', headers: {})
|
||||
|
||||
invoice = Invoice.create_from_transaction!(transaction)
|
||||
assert_equal 250, invoice.total
|
||||
|
||||
invoice_n = Invoice.order(number: :desc).last.number
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
|
||||
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 4}\"}", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator')
|
||||
.to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 4}\"}", headers: {})
|
||||
|
||||
transaction.sum = 146.88
|
||||
invoice = Invoice.create_from_transaction!(transaction)
|
||||
assert_equal 146.88, invoice.total
|
||||
|
||||
invoice_n = Invoice.order(number: :desc).last.number
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator").
|
||||
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 5}\"}", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_number_generator')
|
||||
.to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 5}\"}", headers: {})
|
||||
|
||||
transaction.sum = 0.99
|
||||
invoice = Invoice.create_from_transaction!(transaction)
|
||||
|
@ -158,14 +160,14 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
|
||||
def test_emails_invoice_after_creating_topup_invoice
|
||||
invoice_n = Invoice.order(number: :desc).last.number
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/invoice_generator/invoice_generator").
|
||||
to_return(status: 200, body: "{\"everypay_link\":\"http://link.test\"}", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/invoice_generator/invoice_generator')
|
||||
.to_return(status: 200, body: '{"everypay_link":"http://link.test"}', headers: {})
|
||||
|
||||
stub_request(:put, "https://registry:3000/eis_billing/e_invoice_response").
|
||||
to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}, {\"date\":\"#{Time.zone.now-10.minutes}\"}", headers: {})
|
||||
stub_request(:put, 'https://registry:3000/eis_billing/e_invoice_response')
|
||||
.to_return(status: 200, body: "{\"invoice_number\":\"#{invoice_n + 3}\"}, {\"date\":\"#{Time.zone.now - 10.minutes}\"}", headers: {})
|
||||
|
||||
stub_request(:post, "https://eis_billing_system:3000/api/v1/e_invoice/e_invoice").
|
||||
to_return(status: 200, body: "", headers: {})
|
||||
stub_request(:post, 'https://eis_billing_system:3000/api/v1/e_invoice/e_invoice')
|
||||
.to_return(status: 200, body: '', headers: {})
|
||||
|
||||
registrar = registrars(:bestnames)
|
||||
transaction = bank_transactions(:one).dup
|
||||
|
@ -179,4 +181,73 @@ class InvoiceTest < ActiveSupport::TestCase
|
|||
Invoice.create_from_transaction!(transaction)
|
||||
end
|
||||
end
|
||||
|
||||
def test_create_payment_order
|
||||
everypay_response = {
|
||||
'some' => 'some'
|
||||
}
|
||||
@invoice.payment_orders.delete_all
|
||||
@invoice.account_activity.delete and @invoice.reload
|
||||
assert @invoice.payment_orders.empty?
|
||||
assert_nil @invoice.account_activity
|
||||
|
||||
@invoice.process_payment(payment_type: 'PaymentOrders::EveryPay',
|
||||
everypay_response: everypay_response,
|
||||
payment_status: 'paid',
|
||||
sum: @invoice.total,
|
||||
transaction_time: Time.zone.now - 10.minutes - 23.seconds)
|
||||
|
||||
@invoice.reload
|
||||
|
||||
assert_equal @invoice.payment_orders.count, 1
|
||||
assert @invoice.account_activity.present?
|
||||
end
|
||||
|
||||
def test_should_raise_error_if_bill_already_paid
|
||||
everypay_response = {
|
||||
'some' => 'some'
|
||||
}
|
||||
assert @invoice.payment_orders.present?
|
||||
|
||||
assert_raises(ActiveRecord::RecordInvalid) do
|
||||
@invoice.process_payment(payment_type: 'PaymentOrders::EveryPay',
|
||||
everypay_response: everypay_response,
|
||||
payment_status: 'paid',
|
||||
sum: @invoice.total,
|
||||
transaction_time: Time.zone.now - 10.minutes - 23.seconds)
|
||||
end
|
||||
end
|
||||
|
||||
def test_should_manually_autobin_invoice
|
||||
@invoice.payment_orders.destroy_all && @invoice.account_activity.destroy
|
||||
@invoice.reload
|
||||
|
||||
account = @invoice.buyer.accounts.first
|
||||
item = @invoice.items.first
|
||||
|
||||
assert @invoice.payment_orders.empty?
|
||||
assert @invoice.account_activity.nil?
|
||||
assert_equal account.balance.to_f, 100.0
|
||||
assert_equal item.price, 5.0
|
||||
|
||||
@invoice.autobind_manually
|
||||
@invoice.reload
|
||||
account.reload
|
||||
|
||||
assert_equal account.balance.to_f, 105.0
|
||||
assert @invoice.payment_orders.present?
|
||||
assert @invoice.account_activity.present?
|
||||
end
|
||||
|
||||
def test_cannot_to_increase_balance_already_paid_invoice_by_manually_autobind
|
||||
assert @invoice.paid?
|
||||
|
||||
account = @invoice.buyer.accounts.first
|
||||
assert_equal account.balance.to_f, 100.0
|
||||
|
||||
@invoice.autobind_manually
|
||||
@invoice.reload
|
||||
|
||||
assert_equal account.balance.to_f, 100.0
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue