1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/tests/acceptance/GatewayFeesCest.php

182 lines
6.3 KiB
PHP
Raw Normal View History

2017-03-19 14:31:25 +01:00
<?php
use Faker\Factory;
class GatewayFeesCest
{
/**
* @var \Faker\Generator
*/
private $faker;
public function _before(AcceptanceTester $I)
{
$I->checkIfLogin($I);
$this->faker = Factory::create();
}
public function checkLineItemFee(AcceptanceTester $I)
{
$clientEmail = $this->faker->safeEmail;
$productKey = $this->faker->word();
$taxName = $this->faker->word();
//$cost = $this->faker->randomFloat(2, 50, 100);
//$feeAmount = $this->faker->randomFloat(2, 1, 5);
//$taxRate = $this->faker->randomFloat(3, 5, 15);
//$feePercent = $this->faker->randomFloat(3, 1, 5);
$cost = 100;
$feeAmount = 10;
$taxRate = 10;
$feePercent = 10;
$total = $cost + ($cost * $taxRate / 100);
$fee = $feeAmount + ($total * $feeAmount / 100);
$feeWithTax = $fee + ($fee * $taxRate / 100);
$partialFee = $feeAmount + ($total / 2 * $feeAmount / 100);
$partialFeeWithTax = $partialFee + ($partialFee * $taxRate / 100);
2017-04-12 14:46:46 +02:00
$I->createClient($I, $clientEmail);
2017-03-26 21:04:26 +02:00
$I->createGateway($I);
$this->configureFees($I, $feeAmount, $feePercent);
2017-03-19 14:31:25 +01:00
// line item gateway fee
$this->configureLineItemTaxRates($I, $taxName, $taxRate);
$productKey = $this->faker->word();
$I->createProduct($I, $productKey, $cost, $taxName, $taxRate);
// without taxing the fee
$this->configureGatewayFeeTax($I);
2017-04-12 14:46:46 +02:00
$this->createInvoice($I, $clientEmail, $productKey, $total, $fee);
2017-03-19 14:31:25 +01:00
// with taxing the fee
$this->configureGatewayFeeTax($I, $taxName, $taxRate);
2017-04-12 14:46:46 +02:00
$this->createInvoice($I, $clientEmail, $productKey, $total, $feeWithTax);
2017-03-19 14:31:25 +01:00
2017-04-09 19:24:17 +02:00
// partial invoice (resaving invoice between payments)
2017-04-12 14:46:46 +02:00
$invitationKey = $this->createInvoice($I, $clientEmail, $productKey, $total, $partialFeeWithTax, $total / 2);
2017-04-09 19:24:17 +02:00
$invoiceId = $I->grabFromDatabase('invitations', 'invoice_id', ['invitation_key' => $invitationKey]);
2017-04-09 20:08:37 +02:00
$invoicePublicId = $I->grabFromDatabase('invoices', 'public_id', ['id' => $invoiceId]);
2017-04-09 19:24:17 +02:00
$I->amOnPage('/invoices/' . $invoicePublicId . '/edit');
2017-04-12 16:44:17 +02:00
$I->wait(3);
2017-04-09 22:52:42 +02:00
$I->click('Save Invoice');
2017-04-12 16:44:17 +02:00
$I->wait(3);
2017-03-19 16:44:35 +01:00
$this->createPayment($I, $invitationKey, $total + $partialFeeWithTax, 0, $partialFeeWithTax);
2017-03-19 14:31:25 +01:00
}
private function configureGatewayFeeTax($I, $taxName = '', $taxRate = '')
{
if ($taxName && $taxRate) {
$taxOption = $taxName . ': ' . number_format($taxRate, 3) . '%';
} else {
$taxOption = '';
}
$I->amOnPage('/settings/online_payments#fees');
$I->executeJS('javascript:showLimitsModal(\'Credit Card\', 1)');
$I->click('Fees');
$I->selectOption('tax_rate1', $taxOption);
$I->click('#modalSave');
$I->executeJS('javascript:showLimitsModal(\'Bank Transfer\', 2)');
$I->click('Fees');
$I->selectOption('tax_rate1', $taxOption);
$I->click('#modalSave');
}
private function configureLineItemTaxRates($I, $taxName, $taxRate)
{
$taxOption = $taxName . ': ' . number_format($taxRate, 3) . '%';
2017-03-26 11:55:47 +02:00
$I->createTaxRate($I, $taxName, $taxRate);
2017-03-19 14:31:25 +01:00
2017-03-26 14:44:23 +02:00
$I->amOnPage('/settings/tax_rates');
$I->checkOption('#invoice_item_taxes');
$I->click('Save');
2017-03-19 14:31:25 +01:00
// disable the default invoice tax
2017-03-26 10:46:23 +02:00
//$I->amOnPage('/settings/tax_rates');
//$I->selectOption('#default_tax_rate_id', '');
//$I->click('Save');
2017-03-19 14:31:25 +01:00
}
private function configureFees($I, $feeAmount, $feePercent)
{
$I->createGateway($I);
// enable gateway fees
$I->amOnPage('/settings/online_payments#fees');
2017-03-26 21:04:26 +02:00
$I->checkOption('gateway_fee_enabled');
2017-03-19 14:31:25 +01:00
$I->click('#formSave');
$I->executeJS('javascript:showLimitsModal(\'Credit Card\', 1)');
$I->click('Fees');
$I->fillField(['name' => 'fee_amount'], $feeAmount);
$I->fillField(['name' => 'fee_percent'], $feePercent);
$I->click('#modalSave');
$I->executeJS('javascript:showLimitsModal(\'Bank Transfer\', 2)');
$I->click('Fees');
$I->fillField(['name' => 'fee_amount'], $feeAmount * 2);
$I->fillField(['name' => 'fee_percent'], $feePercent * 2);
$I->click('#modalSave');
}
2017-04-12 14:46:46 +02:00
private function createInvoice($I, $clientEmail, $productKey, $amount, $fee, $partial = false)
2017-03-19 14:31:25 +01:00
{
2017-04-12 14:46:46 +02:00
$invoiceNumber = $I->fillInvoice($I, $clientEmail, $productKey);
2017-03-19 14:31:25 +01:00
if ($partial) {
$amount = ($partial * 2);
$I->fillField('#partial', $partial);
}
$I->click('Mark Sent');
2017-03-20 09:17:14 +01:00
//$I->see($invoiceNumber);
//$I->see('Successfully created invoice');
2017-03-19 14:31:25 +01:00
2017-04-12 16:04:09 +02:00
$clientId = $I->grabFromDatabase('contacts', 'client_id', ['email' => $clientEmail]);
//$clientId = $I->grabFromDatabase('clients', 'id', ['name' => $clientEmail]);
2017-03-19 16:44:35 +01:00
$invoiceId = $I->grabFromDatabase('invoices', 'id', ['client_id' => $clientId, 'invoice_number' => $invoiceNumber]);
$invitationKey = $I->grabFromDatabase('invitations', 'invitation_key', ['invoice_id' => $invoiceId]);
2017-03-19 14:31:25 +01:00
$balance = $partial ? ($amount - $partial) : 0;
2017-03-19 16:44:35 +01:00
$this->createPayment($I, $invitationKey, $amount, $balance, $fee);
2017-03-19 14:31:25 +01:00
2017-03-19 16:44:35 +01:00
return $invitationKey;
2017-03-19 14:31:25 +01:00
}
2017-03-19 16:44:35 +01:00
private function createPayment($I, $invitationKey, $amount, $balance, $fee)
2017-03-19 14:31:25 +01:00
{
2017-03-31 10:54:36 +02:00
$invoiceId = $I->grabFromDatabase('invitations', 'invoice_id', ['invitation_key' => $invitationKey]);
2017-03-19 14:31:25 +01:00
2017-03-31 10:54:36 +02:00
// check we correctly remove/add back the gateway fee
2017-03-19 14:31:25 +01:00
$I->amOnPage('/payment/' . $invitationKey . '/credit_card');
2017-03-31 10:54:36 +02:00
$I->seeInDatabase('invoices', [
'id' => $invoiceId,
'amount' => ($amount + $fee),
]);
2017-03-19 14:31:25 +01:00
$I->amOnPage('/payment/' . $invitationKey . '/bank_transfer');
2017-03-31 10:54:36 +02:00
$I->seeInDatabase('invoices', [
'id' => $invoiceId,
'amount' => ($amount + $fee * 2),
]);
2017-03-19 14:31:25 +01:00
$I->amOnPage('/view/' . $invitationKey);
2017-03-31 10:54:36 +02:00
$I->seeInDatabase('invoices', [
'id' => $invoiceId,
2017-09-05 15:38:29 +02:00
'amount' => ($amount + $fee * 2),
2017-03-31 10:54:36 +02:00
]);
2017-03-19 14:31:25 +01:00
2017-03-19 16:44:35 +01:00
$I->createOnlinePayment($I, $invitationKey);
2017-03-19 14:31:25 +01:00
$I->seeInDatabase('invoices', [
2017-03-20 10:09:18 +01:00
'id' => $invoiceId,
2017-03-19 14:31:25 +01:00
'amount' => ($amount + $fee),
'balance' => $balance
]);
}
}