Merge pull request #1502 from internetee/1422-record-payment-method-and-failed-payments

Record every payment attempt
This commit is contained in:
Timo Võhmar 2020-03-06 13:52:58 +02:00 committed by GitHub
commit 444a07c62a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 798 additions and 342 deletions

View file

@ -4,3 +4,10 @@ one:
quantity: 1
unit: pc
invoice: one
two:
description: Acme services
price: 5
quantity: 1
unit: pc
invoice: unpaid

View file

@ -24,3 +24,30 @@ one:
reference_no: 13
number: 1
description: Order nr 1 from registrar 1234567 second number 2345678
unpaid:
issue_date: <%= Date.parse '2010-07-05' %>
due_date: <%= Date.parse '2010-07-06' %>
currency: EUR
seller_name: Seller Ltd
seller_reg_no: 1234
seller_iban: US75512108001245126199
seller_bank: Main Bank
seller_swift: swift
seller_email: info@seller.test
seller_country_code: US
seller_street: Main Street 1
seller_city: New York
seller_contact_name: John Doe
buyer: bestnames
buyer_name: Buyer Ltd
buyer_reg_no: 12345
buyer_email: info@buyer.test
buyer_country_code: GB
buyer_street: Main Street 2
buyer_city: London
vat_rate: 0.1
total: 16.50
reference_no: 13
number: 2
description: Order nr 2 from registrar 1234567 second number 2345678

27
test/fixtures/payment_orders.yml vendored Normal file
View file

@ -0,0 +1,27 @@
everypay_issued:
type: PaymentOrders::EveryPay
status: issued
invoice: one
response:
notes:
banklink_issued:
type: PaymentOrders::Seb
status: issued
invoice: one
response:
notes:
paid:
type: PaymentOrders::EveryPay
status: paid
invoice: unpaid
response: "{}"
notes:
cancelled:
type: PaymentOrders::Seb
status: cancelled
invoice: unpaid
response: "{}"
notes: User failed to make payment. Bank responded with code 1911

View file

@ -6,34 +6,40 @@ class PaymentCallbackTest < ApplicationIntegrationTest
@user = users(:api_bestnames)
sign_in @user
@payment_order = payment_orders(:everypay_issued)
@invoice = invoices(:one)
@invoice.update!(account_activity: nil, total: 12)
end
def test_every_pay_callback_returns_status_200
invoice = payable_invoice
assert_matching_bank_transaction_exists(invoice)
request_params = every_pay_request_params.merge(invoice_id: invoice.id)
post "/registrar/pay/callback/every_pay", params: request_params
request_params = every_pay_request_params
post "/registrar/pay/callback/#{@payment_order.id}", params: request_params
assert_response :ok
end
def test_invoice_is_marked_as_paid
request_params = every_pay_request_params
post "/registrar/pay/callback/#{@payment_order.id}", params: request_params
assert @payment_order.invoice.paid?
end
def failure_log_is_created_if_unsuccessful_payment
request_params = every_pay_request_params.dup
request_params['payment_state'] = 'cancelled'
request_params['transaction_result'] = 'failed'
post "/registrar/pay/callback/#{@payment_order.id}", params: request_params
@payment_order.reload
assert @payment_order.cancelled?
assert_includes @payment_order.notes, 'Payment state: cancelled'
end
private
def payable_invoice
invoice = invoices(:one)
invoice.update!(account_activity: nil)
invoice
end
def assert_matching_bank_transaction_exists(invoice)
assert BankTransaction.find_by(
description: invoice.description,
currency: invoice.currency,
iban: invoice.seller_iban
), 'Matching bank transaction should exist'
end
def every_pay_request_params
{
nonce: "392f2d7748bc8cb0d14f263ebb7b8932",

View file

@ -8,6 +8,9 @@ class PaymentReturnTest < ApplicationIntegrationTest
sign_in @user
@invoice = invoices(:one)
@invoice.update!(account_activity: nil, total: 12)
@everypay_order = payment_orders(:everypay_issued)
@banklink_order = payment_orders(:banklink_issued)
end
def every_pay_request_params
@ -57,33 +60,78 @@ class PaymentReturnTest < ApplicationIntegrationTest
}
end
def test_every_pay_return_creates_activity_redirects_to_invoice_path
request_params = every_pay_request_params.merge(invoice_id: @invoice.id)
def test_successful_bank_payment_marks_invoice_as_paid
@invoice.update!(account_activity: nil)
request_params = bank_link_request_params
post "/registrar/pay/return/every_pay", params: request_params
post "/registrar/pay/return/#{@banklink_order.id}", params: request_params
@banklink_order.reload
assert @banklink_order.invoice.paid?
end
def test_every_pay_return_creates_activity_redirects_to_invoice_path
request_params = every_pay_request_params
post "/registrar/pay/return/#{@everypay_order.id}", params: request_params
assert_equal(302, response.status)
assert_redirected_to(registrar_invoice_path(@invoice))
end
def test_Every_Pay_return_raises_RecordNotFound
request_params = every_pay_request_params.merge(invoice_id: "178907")
def test_every_pay_return_raises_record_not_found
request_params = every_pay_request_params
assert_raises(ActiveRecord::RecordNotFound) do
post "/registrar/pay/return/every_pay", params: request_params
post '/registrar/pay/return/123456', params: request_params
end
end
def test_bank_link_return_redirects_to_invoice_paths
request_params = bank_link_request_params.merge(invoice_id: @invoice.id)
request_params = bank_link_request_params
post "/registrar/pay/return/seb", params: request_params
post "/registrar/pay/return/#{@banklink_order.id}", params: request_params
assert_equal(302, response.status)
assert_redirected_to(registrar_invoice_path(@invoice))
end
def test_bank_link_return
request_params = bank_link_request_params.merge(invoice_id: "178907")
request_params = bank_link_request_params
assert_raises(ActiveRecord::RecordNotFound) do
post "/registrar/pay/return/seb", params: request_params
post '/registrar/pay/return/123456', params: request_params
end
end
def test_marks_as_paid_and_adds_notes_if_failed_to_bind
request_params = bank_link_request_params
post "/registrar/pay/return/#{@banklink_order.id}", params: request_params
post "/registrar/pay/return/#{@banklink_order.id}", params: request_params
@banklink_order.reload
assert @banklink_order.notes.present?
assert @banklink_order.paid?
assert_includes @banklink_order.notes, 'Failed to bind'
end
def test_failed_bank_link_payment_creates_brief_error_explanation
request_params = bank_link_request_params.dup
request_params['VK_SERVICE'] = '1911'
post "/registrar/pay/return/#{@banklink_order.id}", params: request_params
@banklink_order.reload
assert_includes @banklink_order.notes, 'Bank responded with code 1911'
end
def test_failed_every_pay_payment_creates_brief_error_explanation
request_params = every_pay_request_params.dup
request_params['payment_state'] = 'cancelled'
request_params['transaction_result'] = 'failed'
post "/registrar/pay/return/#{@everypay_order.id}", params: request_params
@everypay_order.reload
assert_includes @everypay_order.notes, 'Payment state: cancelled'
end
end

View file

@ -8,7 +8,7 @@ class BankLinkTest < ActiveSupport::TestCase
super
@invoice = invoices(:one)
@invoice.update!(total: 12)
@invoice.update!(account_activity: nil, total: 12)
travel_to '2018-04-01 00:30 +0300'
create_new_bank_link
@ -36,11 +36,11 @@ class BankLinkTest < ActiveSupport::TestCase
'VK_MAC': 'CZZvcptkxfuOxRR88JmT4N+Lw6Hs4xiQfhBWzVYldAcRTQbcB/lPf9MbJzBE4e1/HuslQgkdCFt5g1xW2lJwrVDBQTtP6DAHfvxU3kkw7dbk0IcwhI4whUl68/QCwlXEQTAVDv1AFnGVxXZ40vbm/aLKafBYgrirB5SUe8+g9FE=',
'VK_ENCODING': 'UTF-8',
'VK_LANG': 'ENG'
}.with_indifferent_access
}.as_json
@completed_bank_link = PaymentOrders::BankLink.new(
'seb', @invoice, { response: params }
)
@completed_bank_link = PaymentOrder.new(type: 'PaymentOrders::Seb',
invoice: @invoice,
response: params)
end
def create_cancelled_bank_link
@ -55,16 +55,17 @@ class BankLinkTest < ActiveSupport::TestCase
'VK_MAC': 'PElE2mYXXN50q2UBvTuYU1rN0BmOQcbafPummDnWfNdm9qbaGQkGyOn0XaaFGlrdEcldXaHBbZKUS0HegIgjdDfl2NOk+wkLNNH0Iu38KzZaxHoW9ga7vqiyKHC8dcxkHiO9HsOnz77Sy/KpWCq6cz48bi3fcMgo+MUzBMauWoQ=',
'VK_ENCODING': 'UTF-8',
'VK_LANG': 'ENG'
}.with_indifferent_access
}.as_json
@cancelled_bank_link = PaymentOrders::BankLink.new(
'seb', @invoice, { response: params }
)
@cancelled_bank_link = PaymentOrder.new(type: 'PaymentOrders::Seb',
invoice: @invoice,
response: params)
end
def create_new_bank_link
params = { return_url: 'return.url', response_url: 'response.url' }
@new_bank_link = PaymentOrders::BankLink.new('seb', @invoice, params)
@new_bank_link = PaymentOrder.new(type: 'PaymentOrders::Seb', invoice: @invoice)
@new_bank_link.return_url = 'return.url'
@new_bank_link.response_url = 'response.url'
end
def test_response_is_not_valid_when_it_is_missing
@ -105,23 +106,14 @@ class BankLinkTest < ActiveSupport::TestCase
refute(@cancelled_bank_link.settled_payment?)
end
def test_complete_transaction_calls_methods_on_transaction
mock_transaction = MiniTest::Mock.new
mock_transaction.expect(:sum= , '12.00', ['12.00'])
mock_transaction.expect(:bank_reference= , '1', ['1'])
mock_transaction.expect(:buyer_bank_code= , 'testvpos', ['testvpos'])
mock_transaction.expect(:buyer_iban= , '1234', ['1234'])
mock_transaction.expect(:paid_at= , Date.parse('2018-04-01 00:30:00 +0300'), [Time.parse('2018-04-01T00:30:00+0300')])
mock_transaction.expect(:buyer_name=, 'John Doe', ['John Doe'])
mock_transaction.expect(:save!, true)
mock_transaction.expect(:binded?, false)
mock_transaction.expect(:bind_invoice, AccountActivity.new, [1])
mock_transaction.expect(:errors, [])
def test_successful_payment_creates_bank_transaction
@completed_bank_link.complete_transaction
BankTransaction.stub(:find_by, mock_transaction) do
@completed_bank_link.complete_transaction
end
transaction = BankTransaction.find_by(
sum: @completed_bank_link.response['VK_AMOUNT'],
buyer_name: @completed_bank_link.response['VK_SND_NAME']
)
mock_transaction.verify
assert transaction.present?
end
end

View file

@ -4,36 +4,38 @@ class EveryPayTest < ActiveSupport::TestCase
def setup
super
@invoice = invoices(:one)
@invoice = invoices(:unpaid)
@invoice.update!(total: 12)
params = {
response:
{
utf8: '✓',
_method: 'put',
authenticity_token: 'OnA69vbccQtMt3C9wxEWigs5Gpf/7z+NoxRCMkFPlTvaATs8+OgMKF1I4B2f+vuK37zCgpWZaWWtyuslRRSwkw==',
nonce: '392f2d7748bc8cb0d14f263ebb7b8932',
timestamp: '1524136727',
api_username: 'ca8d6336dd750ddb',
transaction_result: 'completed',
payment_reference: 'fd5d27b59a1eb597393cd5ff77386d6cab81ae05067e18d530b10f3802e30b56',
payment_state: 'settled',
amount: '12.00',
order_reference: 'e468a2d59a731ccc546f2165c3b1a6',
account_id: 'EUR3D1',
cc_type: 'master_card',
cc_last_four_digits: '0487',
cc_month: '10',
cc_year: '2018',
cc_holder_name: 'John Doe',
hmac_fields: 'account_id,amount,api_username,cc_holder_name,cc_last_four_digits,cc_month,cc_type,cc_year,hmac_fields,nonce,order_reference,payment_reference,payment_state,timestamp,transaction_result',
hmac: 'efac1c732835668cd86023a7abc140506c692f0d',
invoice_id: '1',
},
}
@every_pay = PaymentOrders::EveryPay.new('every_pay', @invoice, params)
@other_pay = PaymentOrders::EveryPay.new('every_pay', @invoice, {})
response = {
"utf8": '✓',
"_method": 'put',
"authenticity_token": 'OnA69vbccQtMt3C9wxEWigs5Gpf/7z+NoxRCMkFPlTvaATs8+OgMKF1I4B2f+vuK37zCgpWZaWWtyuslRRSwkw=="',
"nonce": '392f2d7748bc8cb0d14f263ebb7b8932',
"timestamp": '1524136727',
"api_username": 'ca8d6336dd750ddb',
"transaction_result": 'completed',
"payment_reference": 'fd5d27b59a1eb597393cd5ff77386d6cab81ae05067e18d530b10f3802e30b56',
"payment_state": 'settled',
"amount": '12.00',
"order_reference": 'e468a2d59a731ccc546f2165c3b1a6',
"account_id": 'EUR3D1',
"cc_type": 'master_card',
"cc_last_four_digits": '0487',
"cc_month": '10',
"cc_year": '2018',
"cc_holder_name": 'John Doe',
"hmac_fields": 'account_id,amount,api_username,cc_holder_name,cc_last_four_digits,cc_month,cc_type,cc_year,hmac_fields,nonce,order_reference,payment_reference,payment_state,timestamp,transaction_result',
"hmac": 'efac1c732835668cd86023a7abc140506c692f0d',
"invoice_id": '2'
}.as_json
@successful_payment = PaymentOrder.new(type: 'PaymentOrders::EveryPay',
invoice: @invoice,
response: response)
@failed_payment = @successful_payment.dup
@failed_payment.response['payment_state'] = 'cancelled'
travel_to Time.zone.parse('2018-04-01 00:30:00 +0000')
end
@ -47,39 +49,37 @@ class EveryPayTest < ActiveSupport::TestCase
transaction_type: 'charge',
hmac_fields: 'account_id,amount,api_username,callback_url,customer_url,hmac_fields,nonce,order_reference,timestamp,transaction_type'
}
form_fields = @every_pay.form_fields
form_fields = @successful_payment.form_fields
expected_fields.each do |k, v|
assert_equal(v, form_fields[k])
end
end
def test_valid_response_from_intermediary?
assert(@every_pay.valid_response_from_intermediary?)
refute(@other_pay.valid_response_from_intermediary?)
assert(@successful_payment.valid_response_from_intermediary?)
@failed_payment.response = { 'what': 'definitely not valid everypay response' }
refute(@failed_payment.valid_response_from_intermediary?)
end
def test_valid_and_successful_payment_is_determined
assert(@successful_payment.payment_received?)
refute(@failed_payment.payment_received?)
end
def test_settled_payment?
assert(@every_pay.settled_payment?)
other_pay = PaymentOrders::EveryPay.new(
'every_pay', @invoice, {response: {payment_state: 'CANCELLED'}}
)
refute(other_pay.settled_payment?)
assert(@successful_payment.settled_payment?)
refute(@failed_payment.settled_payment?)
end
def test_complete_transaction_calls_methods_on_transaction
mock_transaction = MiniTest::Mock.new
mock_transaction.expect(:sum= , '12.00', ['12.00'])
mock_transaction.expect(:paid_at= , Date.strptime('1524136727', '%s'), [Date.strptime('1524136727', '%s')])
mock_transaction.expect(:buyer_name=, 'John Doe', ['John Doe'])
mock_transaction.expect(:save!, true)
mock_transaction.expect(:binded?, false)
mock_transaction.expect(:bind_invoice, AccountActivity.new, [1])
mock_transaction.expect(:errors, [])
def test_successful_payment_creates_bank_transaction
@successful_payment.complete_transaction
BankTransaction.stub(:find_by, mock_transaction) do
@every_pay.complete_transaction
end
transaction = BankTransaction.find_by(
sum: @successful_payment.response['amount'],
buyer_name: @successful_payment.response['cc_holder_name']
)
mock_transaction.verify
assert transaction.present?
end
end

View file

@ -5,23 +5,21 @@ class PaymentOrdersTest < ActiveSupport::TestCase
super
@original_methods = ENV['payment_methods']
@original_seb_URL = ENV['seb_payment_url']
ENV['payment_methods'] = 'seb, swed, credit_card'
@original_seb_url = ENV['seb_payment_url']
ENV['payment_methods'] = 'seb, swed, every_pay'
ENV['seb_payment_url'] = nil
@not_implemented_payment = PaymentOrders::Base.new(
'not_implemented', Invoice.new
)
@not_implemented_payment = PaymentOrder.new(invoice: Invoice.new)
end
def teardown
super
ENV['payment_methods'] = @original_methods
ENV['seb_payment_url'] = @original_seb_URL
ENV['seb_payment_url'] = @original_seb_url
end
def test_variable_assignment
assert_equal 'not_implemented', @not_implemented_payment.type
assert_nil @not_implemented_payment.type
assert_nil @not_implemented_payment.response_url
assert_nil @not_implemented_payment.return_url
assert_nil @not_implemented_payment.form_url
@ -45,14 +43,61 @@ class PaymentOrdersTest < ActiveSupport::TestCase
end
end
def test_that_create_with_type_raises_argument_error
assert_raise ArgumentError do
PaymentOrders.create_with_type("not_implemented", Invoice.new)
def test_correct_channel_is_assigned
everypay_channel = PaymentOrder.new_with_type(type: 'every_pay', invoice: @invoice)
assert_equal everypay_channel.channel, 'EveryPay'
assert_equal everypay_channel.class.config_namespace_name, 'every_pay'
swed_channel = PaymentOrder.new_with_type(type: 'swed', invoice: @invoice)
assert_equal swed_channel.channel, 'Swed'
assert_equal swed_channel.class.config_namespace_name, 'swed'
seb_channel = PaymentOrder.new_with_type(type: 'seb', invoice: @invoice)
assert_equal seb_channel.channel, 'Seb'
assert_equal seb_channel.class.config_namespace_name, 'seb'
lhv_channel = PaymentOrder.new_with_type(type: 'lhv', invoice: @invoice)
assert_equal lhv_channel.channel, 'Lhv'
assert_equal lhv_channel.class.config_namespace_name, 'lhv'
admin_channel = PaymentOrder.new_with_type(type: 'admin_payment', invoice: @invoice)
assert_equal admin_channel.channel, 'AdminPayment'
assert_equal admin_channel.class.config_namespace_name, 'admin_payment'
system_channel = PaymentOrder.new_with_type(type: 'system_payment', invoice: @invoice)
assert_equal system_channel.channel, 'SystemPayment'
assert_equal system_channel.class.config_namespace_name, 'system_payment'
end
def test_can_not_create_order_for_paid_invoice
invoice = invoices(:one)
payment_order = PaymentOrder.new_with_type(type: 'every_pay', invoice: invoice)
assert payment_order.invalid?
assert_includes payment_order.errors[:invoice], 'is already paid'
end
def test_order_without_channel_is_invalid
payment_order = PaymentOrder.new
assert payment_order.invalid?
assert_includes payment_order.errors[:type], 'is not supported'
end
def test_can_not_create_order_with_invalid_type
assert_raise NameError do
PaymentOrder.new_with_type(type: 'not_implemented', invoice: Invoice.new)
end
end
def test_create_with_correct_subclass
payment = PaymentOrders.create_with_type('seb', Invoice.new)
assert_equal PaymentOrders::BankLink, payment.class
def test_supported_method_bool_does_not_fail
assert_not PaymentOrder.supported_method?('not_implemented', shortname: true)
assert PaymentOrder.supported_method?('every_pay', shortname: true)
assert_not PaymentOrder.supported_method?('PaymentOrders::NonExistant')
assert PaymentOrder.supported_method?('PaymentOrders::EveryPay')
end
def test_can_create_with_correct_subclass
payment = PaymentOrder.new_with_type(type: 'seb', invoice: Invoice.new)
assert_equal PaymentOrders::Seb, payment.class
end
end

View file

@ -58,6 +58,30 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase
assert @invoice.paid?
end
def test_attaches_paid_payment_order_to_invoice
assert @invoice.unpaid?
capture_io { run_task }
@invoice.reload
payment_order = @invoice.payment_orders.last
assert_equal 'PaymentOrders::SystemPayment', payment_order.type
assert payment_order.paid?
end
def test_attaches_failed_payment_order_to_invoice
assert @invoice.unpaid?
account = accounts(:cash)
account.update!(registrar: registrars(:goodnames))
capture_io { run_task }
@invoice.reload
payment_order = @invoice.payment_orders.last
assert_equal 'PaymentOrders::SystemPayment', payment_order.type
assert payment_order.failed?
end
def test_output
assert_output "Transactions processed: 1\n" do
run_task
@ -75,4 +99,4 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase
invoice.update!({ account_activity: nil, cancelled_at: nil }.merge(attributes))
invoice
end
end
end