diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index 7e78b6aceeef..ac451c505805 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -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); } diff --git a/app/Http/Controllers/ClientPortal/SubscriptionPlanSwitchController.php b/app/Http/Controllers/ClientPortal/SubscriptionPlanSwitchController.php index 2d7609db78ee..022d4b343cb4 100644 --- a/app/Http/Controllers/ClientPortal/SubscriptionPlanSwitchController.php +++ b/app/Http/Controllers/ClientPortal/SubscriptionPlanSwitchController.php @@ -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, diff --git a/app/Http/Middleware/QueryLogging.php b/app/Http/Middleware/QueryLogging.php index cb44441b9dc0..a10ccfc0d2ed 100644 --- a/app/Http/Middleware/QueryLogging.php +++ b/app/Http/Middleware/QueryLogging.php @@ -52,8 +52,8 @@ class QueryLogging $timeEnd = microtime(true); $time = $timeEnd - $timeStart; - if($count > 150) - nlog($queries); + // if($count > 150) + // nlog($queries); $ip = ''; diff --git a/app/Jobs/Account/CreateAccount.php b/app/Jobs/Account/CreateAccount.php index 9436ff420e40..19c1e9dfb786 100644 --- a/app/Jobs/Account/CreateAccount.php +++ b/app/Jobs/Account/CreateAccount.php @@ -81,7 +81,7 @@ class CreateAccount { $sp794f3f->trial_started = now(); $sp794f3f->trial_plan = 'pro'; - $sp794f3f->plan = 'pro'; + // $sp794f3f->plan = 'pro'; $sp794f3f->save(); } diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index f2e8b8f5e78d..b5d030377416 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -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']; diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index 6c29f33d9f1f..de4518e295a1 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -386,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) @@ -394,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}"); @@ -409,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); @@ -521,27 +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); } @@ -552,7 +568,7 @@ class SubscriptionService * @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(); @@ -581,9 +597,11 @@ class SubscriptionService * @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(); @@ -593,7 +611,7 @@ class SubscriptionService $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, @@ -620,7 +638,7 @@ class SubscriptionService $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)); @@ -923,6 +941,7 @@ class SubscriptionService 'recurring_invoice' => $recurring_invoice_hashed_id, 'client' => $invoice->client->hashed_id, 'contact' => $invoice->client->primary_contact()->first()->hashed_id, + 'invoice' => $invoice->hashed_id, ]; $response = $this->triggerWebhook($context); diff --git a/tests/Unit/DatesTest.php b/tests/Unit/DatesTest.php index ebf6e1dd17b2..b0326c3e85e6 100644 --- a/tests/Unit/DatesTest.php +++ b/tests/Unit/DatesTest.php @@ -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))); + + } }