1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00

Updates for stripe connect - setting business name

This commit is contained in:
David Bomba 2023-09-04 16:47:03 +10:00
parent 26d221c2ed
commit 17ad4678fb
6 changed files with 28 additions and 239 deletions

View File

@ -111,8 +111,9 @@ class CompanyUserController extends BaseController
*/
public function update(UpdateCompanyUserRequest $request, User $user)
{
$company = auth()->user()->company();
/** @var \App\Models\User $auth_user */
$auth_user = auth()->user();
$company = $auth_user->company();
$company_user = CompanyUser::whereUserId($user->id)->whereCompanyId($company->id)->first();
@ -122,7 +123,7 @@ class CompanyUserController extends BaseController
return;
}
if (auth()->user()->isAdmin()) {
if ($auth_user->isAdmin()) {
$company_user->fill($request->input('company_user'));
} else {
$company_user->settings = $request->input('company_user')['settings'];
@ -136,8 +137,11 @@ class CompanyUserController extends BaseController
public function updatePreferences(UpdateCompanyUserPreferencesRequest $request, User $user)
{
/** @var \App\Models\User $auth_user */
$auth_user = auth()->user();
$company = $auth_user->company();
$company = auth()->user()->company();
$company = $auth_user->company();
$company_user = CompanyUser::whereUserId($user->id)->whereCompanyId($company->id)->first();

View File

@ -592,11 +592,8 @@ class CreditController extends BaseController
}
break;
case 'download':
// $file = $credit->pdf_file_path();
$file = $credit->service()->getCreditPdf($credit->invitations->first());
// return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);
return response()->streamDownload(function () use ($file) {
echo Storage::get($file);
}, basename($file), ['Content-Type' => 'application/pdf']);

View File

@ -74,7 +74,7 @@ class OneTimeTokenController extends BaseController
'user_id' => $user->id,
'company_key'=> $user->company()->company_key,
'context' => $request->input('context'),
'is_react' => $request->has('react') && $request->query('react') == 'true' ? true : false,
'is_react' => $request->request()->hasHeader('X-REACT') ? true : false,
];
Cache::put($hash, $data, 3600);

View File

@ -15,11 +15,9 @@ use App\DataMapper\FeesAndLimits;
use App\Factory\CompanyGatewayFactory;
use App\Http\Requests\StripeConnect\InitializeStripeConnectRequest;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\Company;
use App\Models\CompanyGateway;
use App\Models\GatewayType;
use App\PaymentDrivers\Stripe\Jobs\StripeWebhook;
use Stripe\Exception\ApiErrorException;
class StripeConnectController extends BaseController
@ -124,12 +122,27 @@ class StripeConnectController extends BaseController
$company_gateway->setConfig($payload);
$company_gateway->save();
try{
$stripe = $company_gateway->driver()->init();
$a = \Stripe\Account::retrieve($response->stripe_user_id, $stripe->stripe_connect_auth);
if($a->business_name ?? false) {
$company_gateway->label = substr("Stripe - {$a->business_name}", 0, 250);
$company_gateway->save();
}
}
catch(\Exception $e){
nlog("could not harvest stripe company name");
}
// nlog("Stripe Connect Redirect URI = {$redirect_uri}");
// StripeWebhook::dispatch($company->company_key, $company_gateway->id);
// if(isset($request->getTokenContent()['is_react']) && $request->getTokenContent()['is_react']) {
if(isset($request->getTokenContent()['is_react']) && $request->getTokenContent()['is_react']) {
$redirect_uri = 'https://app.invoicing.co/#/settings/online_payments';
// } else {
// $redirect_uri = 'https://invoicing.co/stripe/completed';
// }
} else {
$redirect_uri = 'https://invoicing.co/stripe/completed';
}
//response here
return view('auth.connect.completed', ['url' => $redirect_uri]);

View File

@ -1,89 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Utils\Traits;
use League\CommonMark\CommonMarkConverter;
use Parsedown;
/**
* Class PaymentEmailBuilder.
*/
trait PaymentEmailBuilder
{
/**
* Builds the correct template to send.
* @param null $reminder_template The template name ie reminder1
* @param null $contact
* @return array
*/
public function getEmailData($reminder_template = null, $contact = null) :array
{
//client
//$client = $this->client;
//Need to determine which email template we are producing
return $this->generateTemplateData($reminder_template, $contact);
}
private function generateTemplateData(string $reminder_template, $contact) :array
{
$data = [];
$client = $this->client;
$body_template = $client->getSetting('email_template_'.$reminder_template);
/* Use default translations if a custom message has not been set*/
if (iconv_strlen($body_template) == 0) {
$body_template = trans('texts.payment_message', ['amount'=>$this->present()->amount(), 'account'=>$this->company->present()->name()], null, $this->client->locale());
}
$subject_template = $client->getSetting('payment_subject');
if (iconv_strlen($subject_template) == 0) {
$subject_template = trans('texts.invoice_subject', ['number'=>$this->present()->invoice_number(), 'account'=>$this->company->present()->name()], null, $this->client->locale());
}
$data['body'] = $this->parseTemplate($body_template, false, $contact);
$data['subject'] = $this->parseTemplate($subject_template, true, $contact);
if ($client->getSetting('pdf_email_attachment') !== false) {
$data['files'][] = $this->pdf_file_path();
}
return $data;
}
private function parseTemplate(string $template_data, bool $is_markdown, $contact) :string
{
//$invoice_variables = $this->makeValues($contact);
//process variables
//$data = str_replace(array_keys($invoice_variables), array_values($invoice_variables), $template_data);
$data = strtr($template_data, $invoice_variables);
//process markdown
if ($is_markdown) {
//$data = Parsedown::instance()->line($data);
$converter = new CommonMarkConverter([
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
$data = $converter->convert($data);
}
return $data;
}
}

View File

@ -1,136 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Utils\Traits;
use App\Models\Quote;
use Illuminate\Support\Carbon;
use League\CommonMark\CommonMarkConverter;
use Parsedown;
/**
* Class QuoteEmailBuilder.
*/
trait QuoteEmailBuilder
{
/**
* Builds the correct template to send.
* @param null $reminder_template The template name ie reminder1
* @param null $contact
* @return array
*/
public function getEmailData($reminder_template = null, $contact = null) :array
{
if (! $reminder_template) {
$reminder_template = 'quote';
//$reminder_template = $this->calculateTemplate('quote');
}
//Need to determine which email template we are producing
return $this->generateTemplateData($reminder_template, $contact);
}
private function generateTemplateData(string $reminder_template, $contact) :array
{
$data = [];
$client = $this->client;
$body_template = $client->getSetting('email_template_'.$reminder_template);
/* Use default translations if a custom message has not been set*/
if (iconv_strlen($body_template) == 0) {
$body_template = trans('texts.quote_message', ['amount'=>$this->present()->amount(), 'account'=>$this->company->present()->name()], null, $this->client->locale());
}
$subject_template = $client->getSetting('email_subject_'.$reminder_template);
if (iconv_strlen($subject_template) == 0) {
if ($reminder_template == 'quote') {
$subject_template = trans('texts.quote_subject', ['number'=>$this->present()->invoice_number(), 'account'=>$this->company->present()->name()], null, $this->client->locale());
} else {
$subject_template = trans('texts.reminder_subject', ['number'=>$this->present()->invoice_number(), 'account'=>$this->company->present()->name()], null, $this->client->locale());
}
}
$data['body'] = $this->parseTemplate($body_template, true, $contact);
$data['subject'] = $this->parseTemplate($subject_template, false, $contact);
if ($client->getSetting('pdf_email_attachment') !== false) {
$data['files'][] = $this->pdf_file_path();
}
return $data;
}
private function parseTemplate(string $template_data, bool $is_markdown, $contact) :string
{
// $quote_variables = $this->makeValues($contact);
//process variables
// $data = str_replace(array_keys($quote_variables), array_values($quote_variables), $template_data);
$data = strtr($template_data, $quote_variables);
//process markdown
if ($is_markdown) {
//$data = Parsedown::instance()->line($data);
$converter = new CommonMarkConverter([
'html_input' => 'allow',
'allow_unsafe_links' => true,
]);
$data = $converter->convert($data);
}
return $data;
}
private function calculateTemplate() :string
{
//if invoice is currently a draft, or being marked as sent, this will be the initial email
$client = $this->client;
//if the invoice
if ($this->status_id == Quote::STATUS_DRAFT || Carbon::parse($this->due_date) > now()) {
return 'quote';
} elseif ($client->getSetting('enable_reminder1') !== false && $this->inReminderWindow($client->getSetting('schedule_reminder1'), $client->getSetting('num_days_reminder1'))) {
return 'template1';
} elseif ($client->getSetting('enable_reminder2') !== false && $this->inReminderWindow($client->getSetting('schedule_reminder2'), $client->getSetting('num_days_reminder2'))) {
return 'template2';
} elseif ($client->getSetting('enable_reminder3') !== false && $this->inReminderWindow($client->getSetting('schedule_reminder3'), $client->getSetting('num_days_reminder3'))) {
return 'template3';
} else {
return 'quote';
}
//also implement endless reminders here
}
private function inReminderWindow($schedule_reminder, $num_days_reminder)
{
switch ($schedule_reminder) {
case 'after_invoice_date':
return Carbon::parse($this->date)->addDays($num_days_reminder)->startOfDay()->eq(Carbon::now()->startOfDay());
break;
case 'before_due_date':
return Carbon::parse($this->due_date)->subDays($num_days_reminder)->startOfDay()->eq(Carbon::now()->startOfDay());
break;
case 'after_due_date':
return Carbon::parse($this->due_date)->addDays($num_days_reminder)->startOfDay()->eq(Carbon::now()->startOfDay());
break;
default:
// code...
break;
}
}
}