mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-09-21 00:41:34 +02:00
Merge pull request #6276 from turbo124/v5-develop
Fixes for subscriptions
This commit is contained in:
commit
36ce02074a
@ -243,6 +243,10 @@ class PaymentController extends Controller
|
||||
->get();
|
||||
}
|
||||
|
||||
if(!$is_credit_payment){
|
||||
$credit_totals = 0;
|
||||
}
|
||||
|
||||
$hash_data = ['invoices' => $payable_invoices->toArray(), 'credits' => $credit_totals, 'amount_with_fee' => max(0, (($invoice_totals + $fee_totals) - $credit_totals))];
|
||||
|
||||
if ($request->query('hash')) {
|
||||
@ -257,11 +261,19 @@ class PaymentController extends Controller
|
||||
|
||||
$payment_hash->save();
|
||||
|
||||
if($is_credit_payment){
|
||||
$amount_with_fee = max(0, (($invoice_totals + $fee_totals) - $credit_totals));
|
||||
}
|
||||
else{
|
||||
$credit_totals = 0;
|
||||
$amount_with_fee = max(0, $invoice_totals + $fee_totals);
|
||||
}
|
||||
|
||||
$totals = [
|
||||
'credit_totals' => $credit_totals,
|
||||
'invoice_totals' => $invoice_totals,
|
||||
'fee_total' => $fee_totals,
|
||||
'amount_with_fee' => max(0, (($invoice_totals + $fee_totals) - $credit_totals)),
|
||||
'amount_with_fee' => $amount_with_fee,
|
||||
];
|
||||
|
||||
$data = [
|
||||
@ -273,7 +285,7 @@ class PaymentController extends Controller
|
||||
'amount_with_fee' => $invoice_totals + $fee_totals,
|
||||
];
|
||||
|
||||
if ($is_credit_payment) {
|
||||
if ($is_credit_payment || $totals <= 0) {
|
||||
return $this->processCreditPayment($request, $data);
|
||||
}
|
||||
|
||||
|
@ -43,8 +43,7 @@ class SubscriptionPlanSwitchController extends Controller
|
||||
*/
|
||||
if(is_null($amount))
|
||||
render('subscriptions.denied');
|
||||
|
||||
|
||||
|
||||
return render('subscriptions.switch', [
|
||||
'subscription' => $recurring_invoice->subscription,
|
||||
'recurring_invoice' => $recurring_invoice,
|
||||
|
@ -52,8 +52,8 @@ class QueryLogging
|
||||
$timeEnd = microtime(true);
|
||||
$time = $timeEnd - $timeStart;
|
||||
|
||||
if($count > 150)
|
||||
nlog($queries);
|
||||
// if($count > 150)
|
||||
// nlog($queries);
|
||||
|
||||
$ip = '';
|
||||
|
||||
|
@ -81,7 +81,7 @@ class CreateAccount
|
||||
{
|
||||
$sp794f3f->trial_started = now();
|
||||
$sp794f3f->trial_plan = 'pro';
|
||||
$sp794f3f->plan = 'pro';
|
||||
// $sp794f3f->plan = 'pro';
|
||||
$sp794f3f->save();
|
||||
}
|
||||
|
||||
|
@ -195,7 +195,8 @@ class StripePaymentDriver extends BaseDriver
|
||||
$fields[] = ['name' => 'client_country_id', 'label' => ctrans('texts.country'), 'type' => 'text', 'validation' => 'required'];
|
||||
}
|
||||
|
||||
$fields[] = ['name' => 'client_postal_code', 'label' => ctrans('texts.postal_code'), 'type' => 'text', 'validation' => 'required'];
|
||||
if($this->company_gateway->require_postal_code)
|
||||
$fields[] = ['name' => 'client_postal_code', 'label' => ctrans('texts.postal_code'), 'type' => 'text', 'validation' => 'required'];
|
||||
|
||||
if ($this->company_gateway->require_shipping_address) {
|
||||
$fields[] = ['name' => 'client_shipping_address_line_1', 'label' => ctrans('texts.shipping_address1'), 'type' => 'text', 'validation' => 'required'];
|
||||
|
@ -97,8 +97,6 @@ class SubscriptionService
|
||||
|
||||
$response = $this->triggerWebhook($context);
|
||||
|
||||
// nlog($response);
|
||||
|
||||
$this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
|
||||
|
||||
}
|
||||
@ -388,6 +386,7 @@ class SubscriptionService
|
||||
|
||||
$pro_rata_charge_amount = 0;
|
||||
$pro_rata_refund_amount = 0;
|
||||
$is_credit = false;
|
||||
|
||||
$last_invoice = Invoice::where('subscription_id', $recurring_invoice->subscription_id)
|
||||
->where('client_id', $recurring_invoice->client_id)
|
||||
@ -396,7 +395,22 @@ class SubscriptionService
|
||||
->orderBy('id', 'desc')
|
||||
->first();
|
||||
|
||||
if($last_invoice->balance > 0)
|
||||
if(!$last_invoice){
|
||||
|
||||
$is_credit = true;
|
||||
|
||||
$last_invoice = Credit::where('subscription_id', $recurring_invoice->subscription_id)
|
||||
->where('client_id', $recurring_invoice->client_id)
|
||||
->where('is_deleted', 0)
|
||||
->withTrashed()
|
||||
->orderBy('id', 'desc')
|
||||
->first();
|
||||
|
||||
$pro_rata_refund_amount = $this->calculateProRataRefund($last_invoice, $old_subscription);
|
||||
|
||||
}
|
||||
|
||||
elseif($last_invoice->balance > 0)
|
||||
{
|
||||
$pro_rata_charge_amount = $this->calculateProRataCharge($last_invoice, $old_subscription);
|
||||
nlog("pro rata charge = {$pro_rata_charge_amount}");
|
||||
@ -411,7 +425,7 @@ class SubscriptionService
|
||||
|
||||
nlog("total payable = {$total_payable}");
|
||||
|
||||
$credit = $this->createCredit($last_invoice, $target_subscription);
|
||||
$credit = $this->createCredit($last_invoice, $target_subscription, $is_credit);
|
||||
|
||||
$new_recurring_invoice = $this->createNewRecurringInvoice($recurring_invoice);
|
||||
|
||||
@ -523,26 +537,27 @@ class SubscriptionService
|
||||
*/
|
||||
private function handlePlanChange($payment_hash)
|
||||
{
|
||||
nlog("handle plan change");
|
||||
nlog("handle plan change");
|
||||
|
||||
$old_recurring_invoice = RecurringInvoice::find($payment_hash->data->billing_context->recurring_invoice);
|
||||
|
||||
$recurring_invoice = $this->createNewRecurringInvoice($old_recurring_invoice);
|
||||
|
||||
$context = [
|
||||
'context' => 'change_plan',
|
||||
'recurring_invoice' => $recurring_invoice->hashed_id,
|
||||
'invoice' => $this->encodePrimaryKey($payment_hash->fee_invoice_id),
|
||||
'client' => $recurring_invoice->client->hashed_id,
|
||||
'subscription' => $this->subscription->hashed_id,
|
||||
'contact' => auth('contact')->user()->hashed_id,
|
||||
];
|
||||
$context = [
|
||||
'context' => 'change_plan',
|
||||
'recurring_invoice' => $recurring_invoice->hashed_id,
|
||||
'invoice' => $this->encodePrimaryKey($payment_hash->fee_invoice_id),
|
||||
'client' => $recurring_invoice->client->hashed_id,
|
||||
'subscription' => $this->subscription->hashed_id,
|
||||
'contact' => auth('contact')->user()->hashed_id,
|
||||
];
|
||||
|
||||
|
||||
$response = $this->triggerWebhook($context);
|
||||
$response = $this->triggerWebhook($context);
|
||||
|
||||
nlog($response);
|
||||
nlog($response);
|
||||
|
||||
return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
|
||||
return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id);
|
||||
|
||||
}
|
||||
|
||||
@ -553,7 +568,7 @@ nlog("handle plan change");
|
||||
* @param RecurringInvoice $old_recurring_invoice
|
||||
* @return RecurringInvoice
|
||||
*/
|
||||
private function createNewRecurringInvoice($old_recurring_invoice) :RecurringInvoice
|
||||
public function createNewRecurringInvoice($old_recurring_invoice) :RecurringInvoice
|
||||
{
|
||||
|
||||
$old_recurring_invoice->service()->stop()->save();
|
||||
@ -582,9 +597,11 @@ nlog("handle plan change");
|
||||
* @param Subscription $target
|
||||
* @return Credit
|
||||
*/
|
||||
private function createCredit($last_invoice, $target)
|
||||
private function createCredit($last_invoice, $target, $is_credit = false)
|
||||
{
|
||||
|
||||
$last_invoice_is_credit = $is_credit ? false : true;
|
||||
|
||||
$subscription_repo = new SubscriptionRepository();
|
||||
$credit_repo = new CreditRepository();
|
||||
|
||||
@ -594,7 +611,7 @@ nlog("handle plan change");
|
||||
|
||||
$line_items = $subscription_repo->generateLineItems($target, false, true);
|
||||
|
||||
$credit->line_items = array_merge($line_items, $this->calculateProRataRefundItems($last_invoice, true));
|
||||
$credit->line_items = array_merge($line_items, $this->calculateProRataRefundItems($last_invoice, $last_invoice_is_credit));
|
||||
|
||||
$data = [
|
||||
'client_id' => $last_invoice->client_id,
|
||||
@ -621,7 +638,7 @@ nlog("handle plan change");
|
||||
|
||||
$invoice = InvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id);
|
||||
$invoice->date = now()->format('Y-m-d');
|
||||
$invoice->subscription_id = $this->subscription->id;
|
||||
$invoice->subscription_id = $target->id;
|
||||
|
||||
$invoice->line_items = array_merge($subscription_repo->generateLineItems($target), $this->calculateProRataRefundItems($last_invoice));
|
||||
|
||||
@ -916,24 +933,19 @@ nlog("handle plan change");
|
||||
|
||||
public function planPaid($invoice)
|
||||
{
|
||||
nlog("this is a plan that has been paid");
|
||||
|
||||
$recurring_invoice_hashed_id = $invoice->recurring_invoice()->exists() ? $invoice->recurring_invoice->hashed_id : null;
|
||||
|
||||
|
||||
$context = [
|
||||
'context' => 'plan_paid',
|
||||
'subscription' => $this->subscription->hashed_id,
|
||||
'recurring_invoice' => $recurring_invoice_hashed_id,
|
||||
'client' => $invoice->client->hashed_id,
|
||||
'contact' => $invoice->client->primary_contact()->first()->hashed_id,
|
||||
'invoice' => $invoice->hashed_id,
|
||||
];
|
||||
|
||||
nlog($context);
|
||||
|
||||
$response = $this->triggerWebhook($context);
|
||||
|
||||
nlog($response);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
13643
package-lock.json
generated
13643
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -11,9 +11,9 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/compat-data": "7.9.0",
|
||||
"@babel/plugin-proposal-class-properties": "^7.13.0",
|
||||
"@babel/plugin-proposal-class-properties": "^7.14.5",
|
||||
"laravel-mix-purgecss": "^5.0.0",
|
||||
"vue-template-compiler": "^2.6.12"
|
||||
"vue-template-compiler": "^2.6.14"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tailwindcss/ui": "^0.7",
|
||||
@ -25,8 +25,8 @@
|
||||
"laravel-mix": "^5.0.9",
|
||||
"linkify-urls": "^3.1.1",
|
||||
"lodash": "^4.17.21",
|
||||
"resolve-url-loader": "^3.1.3",
|
||||
"sass": "^1.32.13",
|
||||
"resolve-url-loader": "^3.1.4",
|
||||
"sass": "^1.35.2",
|
||||
"sass-loader": "^8.0.0",
|
||||
"tailwindcss": "^1.9.6"
|
||||
}
|
||||
|
@ -53,4 +53,24 @@ class DatesTest extends TestCase
|
||||
|
||||
$this->assertEquals(7, $x);
|
||||
}
|
||||
|
||||
public function testFourteenDaysFromNow()
|
||||
{
|
||||
$date_in_past = '2020-01-01';
|
||||
|
||||
$date_in_future = Carbon::parse('2020-01-16');
|
||||
|
||||
$this->assertTrue($date_in_future->gt(Carbon::parse($date_in_past)->addDays(14)));
|
||||
|
||||
}
|
||||
|
||||
public function testThirteenteenDaysFromNow()
|
||||
{
|
||||
$date_in_past = '2020-01-01';
|
||||
|
||||
$date_in_future = Carbon::parse('2020-01-15');
|
||||
|
||||
$this->assertFalse($date_in_future->gt(Carbon::parse($date_in_past)->addDays(14)));
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user