From c7d7916b047a3456e79ab24ecac4b060f5f99ad1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 21 Mar 2021 16:35:09 +1100 Subject: [PATCH 1/3] Update defaults --- app/Console/Commands/CreateSingleAccount.php | 1 + .../Billing/WebhookConfiguration.php | 6 ++++ .../BillingSubscriptionService.php | 35 +++++++++++++++++++ database/factories/CompanyFactory.php | 1 + 4 files changed, 43 insertions(+) diff --git a/app/Console/Commands/CreateSingleAccount.php b/app/Console/Commands/CreateSingleAccount.php index 082f30842f34..bcddc6b2b75d 100644 --- a/app/Console/Commands/CreateSingleAccount.php +++ b/app/Console/Commands/CreateSingleAccount.php @@ -101,6 +101,7 @@ class CreateSingleAccount extends Command $company = Company::factory()->create([ 'account_id' => $account->id, 'slack_webhook_url' => config('ninja.notification.slack'), + 'default_password_timeout' => 30*60000, ]); $account->default_company_id = $company->id; diff --git a/app/DataMapper/Billing/WebhookConfiguration.php b/app/DataMapper/Billing/WebhookConfiguration.php index fe562650b938..591ee23697d1 100644 --- a/app/DataMapper/Billing/WebhookConfiguration.php +++ b/app/DataMapper/Billing/WebhookConfiguration.php @@ -35,12 +35,18 @@ class WebhookConfiguration */ public $post_purchase_body = ''; + /** + * @var string + */ + public $post_purchase_rest_method = 'POST'; + /** * @var array */ public static $casts = [ 'return_url' => 'string', 'post_purchase_url' => 'string', + 'post_purchase_rest_method' => 'string', 'post_purchase_headers' => 'array', 'post_purchase_body' => 'object', ]; diff --git a/app/Services/BillingSubscription/BillingSubscriptionService.php b/app/Services/BillingSubscription/BillingSubscriptionService.php index 0043e028e5ae..7b5013299fb6 100644 --- a/app/Services/BillingSubscription/BillingSubscriptionService.php +++ b/app/Services/BillingSubscription/BillingSubscriptionService.php @@ -13,17 +13,25 @@ namespace App\Services\BillingSubscription; use App\DataMapper\InvoiceItem; use App\Factory\InvoiceFactory; +use App\Jobs\Util\SystemLogger; use App\Models\BillingSubscription; use App\Models\ClientSubscription; use App\Models\PaymentHash; use App\Models\Product; +use App\Models\SystemLog; use App\Repositories\InvoiceRepository; +use App\Utils\Traits\MakesHash; +use GuzzleHttp\RequestOptions; class BillingSubscriptionService { + use MakesHash; + /** @var BillingSubscription */ private $billing_subscription; + private $client_subscription; + public function __construct(BillingSubscription $billing_subscription) { $this->billing_subscription = $billing_subscription; @@ -158,11 +166,38 @@ class BillingSubscriptionService // client_id $cs->save(); + + $this->client_subscription = $cs; + } public function triggerWebhook($payment_hash) { //hit the webhook to after a successful onboarding + //$client = xxxxxxx + //todo webhook + + $body = [ + 'billing_subscription' => $this->billing_subscription, + 'client_subscription' => $this->client_subscription, + // 'client' => $client->toArray(), + ]; + + + $client = new \GuzzleHttp\Client(['headers' => $this->billing_subscription->webhook_configuration->post_purchase_headers]); + + $response = $client->{$this->billing_subscription->webhook_configuration->post_purchase_rest_method}($this->billing_subscription->post_purchase_url,[ + RequestOptions::JSON => ['body' => $body] + ]); + + SystemLogger::dispatch( + $body, + SystemLog::CATEGORY_WEBHOOK, + SystemLog::EVENT_WEBHOOK_RESPONSE, + SystemLog::TYPE_WEBHOOK_RESPONSE, + //$client, + ); + } public function fireNotifications() diff --git a/database/factories/CompanyFactory.php b/database/factories/CompanyFactory.php index 4dd793b5a45c..2a4a9850c680 100644 --- a/database/factories/CompanyFactory.php +++ b/database/factories/CompanyFactory.php @@ -37,6 +37,7 @@ class CompanyFactory extends Factory 'db' => config('database.default'), 'settings' => CompanySettings::defaults(), 'is_large' => false, + 'default_password_timeout' => 30*60000, 'enabled_modules' => config('ninja.enabled_modules'), 'custom_fields' => (object) [ //'invoice1' => 'Custom Date|date', From 932185e6c2160429a2d9777afc088e18202f1b59 Mon Sep 17 00:00:00 2001 From: = Date: Sun, 21 Mar 2021 20:26:30 +1100 Subject: [PATCH 2/3] Working on client subscriptions --- .../BillingSubscriptionService.php | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/app/Services/BillingSubscription/BillingSubscriptionService.php b/app/Services/BillingSubscription/BillingSubscriptionService.php index 7b5013299fb6..0dc546fd955c 100644 --- a/app/Services/BillingSubscription/BillingSubscriptionService.php +++ b/app/Services/BillingSubscription/BillingSubscriptionService.php @@ -82,6 +82,10 @@ class BillingSubscriptionService } + /** + * Creates the required line items for the invoice + * for the billing subscription. + */ private function createLineItems($data): array { $line_items = []; @@ -116,11 +120,14 @@ class BillingSubscriptionService return $line_items; } + /** + * If a coupon is entered (and is valid) + * then we apply the coupon discount with a line item. + */ private function createPromoLine($data) { $product = $this->billing_subscription->product; - $discounted_amount = 0; $discount = 0; $amount = $data['quantity'] * $product->cost; @@ -150,21 +157,46 @@ class BillingSubscriptionService } - private function convertInvoiceToRecurring() + private function convertInvoiceToRecurring($payment_hash) { //The first invoice is a plain invoice - the second is fired on the recurring schedule. } - public function createClientSubscription($payment_hash, $recurring_invoice_id = null) + public function createClientSubscription($payment_hash) { - //create the client sub record + //create the client subscription record + + //are we in a trial phase? + //has money been paid? + //is this a recurring or one off subscription. - //?trial enabled? $cs = new ClientSubscription(); $cs->subscription_id = $this->billing_subscription->id; $cs->company_id = $this->billing_subscription->company_id; - // client_id + //if is_trial + //$cs->trial_started = time(); + //$cs->trial_duration = time() + duration period in seconds + + //trials will not have any monies paid. + + //if a payment has been made + //$cs->invoice_id = xx + + //if is_recurring + //create recurring invoice from invoice + $recurring_invoice = $this->convertInvoiceToRecurring($payment_hash); + $recurring_invoice->frequency_id = $this->billing_subscription->frequency_id; + $recurring_invoice->next_send_date = $recurring_invoice->nextDateByFrequency(now()->format('Y-m-d')); + //$cs->recurring_invoice_id = $recurring_invoice->id; + + //?set the recurring invoice as active - set the date here also based on the frequency? + + //$cs->quantity = xx + + // client_id + //$cs->client_id = xx + $cs->save(); $this->client_subscription = $cs; From 42ae8d53dd5875cbaf904e18d220ffaccf04e92a Mon Sep 17 00:00:00 2001 From: = Date: Sun, 21 Mar 2021 20:45:30 +1100 Subject: [PATCH 3/3] Working on client subscriptions --- app/Http/Controllers/ConnectedAccountController.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/Http/Controllers/ConnectedAccountController.php b/app/Http/Controllers/ConnectedAccountController.php index 03347318d6fc..8f6585533dec 100644 --- a/app/Http/Controllers/ConnectedAccountController.php +++ b/app/Http/Controllers/ConnectedAccountController.php @@ -104,7 +104,6 @@ class ConnectedAccountController extends BaseController } $connected_account = [ - 'password' => '', 'email' => $google->harvestEmail($user), 'oauth_user_id' => $google->harvestSubField($user), 'oauth_user_token' => $token, @@ -116,12 +115,9 @@ class ConnectedAccountController extends BaseController auth()->user()->update($connected_account); auth()->user()->email_verified_at = now(); auth()->user()->save(); - - //$ct = CompanyUser::whereUserId(auth()->user()->id); - //return $this->listResponse($ct); return $this->itemResponse(auth()->user()); - // return $this->listResponse(auth()->user()); + } return response()