Merge pull request #8397 from turbo124/v5-develop

Change Pre Payments to be always available as recurring
This commit is contained in:
David Bomba 2023-03-23 07:48:55 +11:00 committed by GitHub
commit b42267d881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 243 additions and 82 deletions

View File

@ -471,12 +471,12 @@ class CompanySettings extends BaseSettings
public $client_initiated_payments_minimum = 0;
public $client_initiated_payments_recurring = false;
// public $client_initiated_payments_recurring = false;
public $sync_invoice_quote_columns = true;
public static $casts = [
'client_initiated_payments_recurring'=> 'bool',
// 'client_initiated_payments_recurring'=> 'bool',
'client_initiated_payments' => 'bool',
'client_initiated_payments_minimum' => 'float',
'sync_invoice_quote_columns' => 'bool',
@ -505,7 +505,6 @@ class CompanySettings extends BaseSettings
'purchase_order_design_id' => 'string',
'purchase_order_footer' => 'string',
'purchase_order_number_pattern' => 'string',
'purchase_order_number_counter' => 'int',
'page_numbering_alignment' => 'string',
'page_numbering' => 'bool',
'auto_archive_invoice_cancelled' => 'bool',
@ -537,7 +536,6 @@ class CompanySettings extends BaseSettings
'reminder_send_time' => 'int',
'email_sending_method' => 'string',
'gmail_sending_user_id' => 'string',
'currency_id' => 'string',
'counter_number_applied' => 'string',
'quote_number_applied' => 'string',
'email_subject_custom1' => 'string',

View File

@ -34,6 +34,8 @@ class InvoiceItemExport extends BaseExport
'amount' => 'amount',
'balance' => 'balance',
'client' => 'client_id',
'client_number' => 'client.number',
'client_id_number' => 'client.id_number',
'custom_surcharge1' => 'custom_surcharge1',
'custom_surcharge2' => 'custom_surcharge2',
'custom_surcharge3' => 'custom_surcharge3',
@ -198,6 +200,8 @@ class InvoiceItemExport extends BaseExport
// if(in_array('client_id', $this->input['report_keys']))
$entity['client'] = $invoice->client->present()->name();
$entity['client_id_number'] = $invoice->client->id_number;
$entity['client_number'] = $invoice->client->number;
// if(in_array('status_id', $this->input['report_keys']))
$entity['status'] = $invoice->stringStatus($invoice->status_id);

View File

@ -38,11 +38,16 @@ class PrePaymentController extends Controller
*/
public function index()
{
$client = auth()->guard('contact')->user()->client;
$minimum = $client->getSetting('client_initiated_payments_minimum');
$minimum_amount = $minimum == 0 ? "" : Number::formatMoney($minimum, $client);
$data = [
'title' => ctrans('texts.amount'). " " .auth()->guard('contact')->user()->client->currency()->code." (".auth()->guard('contact')->user()->client->currency()->symbol . ")",
'allows_recurring' => auth()->guard('contact')->user()->client->getSetting('client_initiated_payments_recurring'),
'title' => ctrans('texts.amount'). " " .$client->currency()->code." (".auth()->guard('contact')->user()->client->currency()->symbol . ")",
// 'allows_recurring' => auth()->guard('contact')->user()->client->getSetting('client_initiated_payments_recurring'),
'allows_recurring' => true,
'minimum_amount' => auth()->guard('contact')->user()->client->getSetting('client_initiated_payments_minimum'),
'minimum' => $minimum,
'minimum_amount' => $minimum_amount,
];
return $this->render('pre_payments.index', $data);

View File

@ -19,22 +19,28 @@ use App\Utils\Ninja;
class SubscriptionController extends Controller
{
/**
* This function is used to display the subscription page.
*
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
*/
public function index()
{
if (Ninja::isHosted()) {
$count = RecurringInvoice::query()
->where('client_id', auth()->guard('contact')->user()->client->id)
->where('company_id', auth()->guard('contact')->user()->client->company_id)
->where('status_id', RecurringInvoice::STATUS_ACTIVE)
->where('is_deleted', 0)
->whereNotNull('subscription_id')
->withTrashed()
->count();
// if (Ninja::isHosted()) {
// $count = RecurringInvoice::query()
// ->where('client_id', auth()->guard('contact')->user()->client->id)
// ->where('company_id', auth()->guard('contact')->user()->client->company_id)
// ->where('status_id', RecurringInvoice::STATUS_ACTIVE)
// ->where('is_deleted', 0)
// ->whereNotNull('subscription_id')
// ->withTrashed()
// ->count();
if ($count == 0) {
return redirect()->route('client.ninja_contact_login', ['contact_key' => auth()->guard('contact')->user()->contact_key, 'company_key' => auth()->guard('contact')->user()->company->company_key]);
}
}
// if ($count == 0) {
// return redirect()->route('client.ninja_contact_login', ['contact_key' => auth()->guard('contact')->user()->contact_key, 'company_key' => auth()->guard('contact')->user()->company->company_key]);
// }
// }
return render('subscriptions.index');
}
@ -44,7 +50,6 @@ class SubscriptionController extends Controller
*
* @param ShowRecurringInvoiceRequest $request
* @param RecurringInvoice $recurring_invoice
* @return Factory|View
*/
public function show(ShowRecurringInvoiceRequest $request, RecurringInvoice $recurring_invoice)
{

View File

@ -136,7 +136,7 @@ class EmailController extends BaseController
$mo->email_template_body = $request->input('template');
$mo->email_template_subject = str_replace("template", "subject", $request->input('template'));
if ($request->has('cc_email')) {
if ($request->has('cc_email') && $request->cc_email) {
$mo->cc[] = new Address($request->cc_email);
}

View File

@ -26,7 +26,17 @@ class StorePrePaymentRequest extends FormRequest
{
return [
'notes' => 'required|bail|',
'amount' => 'required|bail|',
'amount' => 'required|bail|gte:minimum_amount',
'minimum_amount' => '',
];
}
public function prepareForValidation()
{
$input = $this->all();
$this->replace($input);
}
}

View File

@ -43,7 +43,7 @@ class SendEmailRequest extends Request
'template' => 'bail|required',
'entity' => 'bail|required',
'entity_id' => 'bail|required',
'cc_email' => 'bail|sometimes|email',
'cc_email' => 'bail|sometimes|email|nullable',
];
}

View File

@ -138,9 +138,9 @@ class PortalComposer
$data[] = ['title' => ctrans('texts.subscriptions'), 'url' => 'client.subscriptions.index', 'icon' => 'calendar'];
}
// if (property_exists($this->settings, 'client_initiated_payments') && $this->settings->client_initiated_payments) {
if (auth()->guard('contact')->user()->client->getSetting('client_initiated_payments')) {
$data[] = ['title' => ctrans('texts.pre_payment'), 'url' => 'client.pre_payments.index', 'icon' => 'dollar-sign'];
// }
}
return $data;
}

View File

@ -65,8 +65,8 @@ class PaymentFailedMailer implements ShouldQueue
*/
public function handle()
{
if (!is_string($this->error)) {
$this->error = "Payment failed, no reason given.";
if (!is_string($this->error) || strlen($this->error) <=1) {
$this->error = "";
}
//Set DB

View File

@ -37,6 +37,8 @@ class CleanStaleInvoiceOrder implements ShouldQueue
*/
public function handle(InvoiceRepository $repo) : void
{
nlog("Cleaning Stale Invoices:");
if (! config('ninja.db.multi_db_enabled')) {
Invoice::query()
->withTrashed()

View File

@ -124,6 +124,10 @@ class ClientPaymentFailureObject
'company' => $this->company,
];
if (strlen($this->error > 1)) {
$data['content'] .= "\n\n".$this->error;
}
return $data;
}
}

View File

@ -120,6 +120,10 @@ class PaymentFailureObject
'additional_info' => $this->error,
];
if (strlen($this->error > 1)) {
$data['content'] .= "\n\n".$this->error;
}
return $data;
}

View File

@ -30,6 +30,7 @@ use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;
//@deprecated
class ACH implements MethodInterface
{
use MakesHash;
@ -172,10 +173,13 @@ class ACH implements MethodInterface
$description = "Amount {$request->amount} from client {$this->go_cardless->client->present()->name()}";
}
$amount = $this->go_cardless->convertToGoCardlessAmount($this->go_cardless->payment_hash?->amount_with_fee(), $this->go_cardless->client->currency()->precision);
try {
$payment = $this->go_cardless->gateway->payments()->create([
'params' => [
'amount' => $request->amount,
// 'amount' => $request->amount,
'amount' => $amount,
'currency' => $request->currency,
'description' => $description,
'metadata' => [

View File

@ -50,36 +50,96 @@ class DirectDebit implements MethodInterface
*/
public function authorizeView(array $data)
{
$session_token = \Illuminate\Support\Str::uuid()->toString();
return $this->billingRequestFlows($data);
// $session_token = \Illuminate\Support\Str::uuid()->toString();
try {
$redirect = $this->go_cardless->gateway->redirectFlows()->create([
'params' => [
'session_token' => $session_token,
'success_redirect_url' => route('client.payment_methods.confirm', [
'method' => GatewayType::DIRECT_DEBIT,
'session_token' => $session_token,
]),
'prefilled_customer' => [
'given_name' => auth()->guard('contact')->user()->first_name ?: '',
'family_name' => auth()->guard('contact')->user()->last_name ?: '',
'email' => auth()->guard('contact')->user()->email ?: '',
'address_line1' => auth()->guard('contact')->user()->client->address1 ?: '',
'city' => auth()->guard('contact')->user()->client->city ?: '',
'postal_code' => auth()->guard('contact')->user()->client->postal_code ?: '',
'country_code' => auth()->guard('contact')->user()->client->country->iso_3166_2,
],
],
// try {
// $redirect = $this->go_cardless->gateway->redirectFlows()->create([
// 'params' => [
// 'session_token' => $session_token,
// 'success_redirect_url' => route('client.payment_methods.confirm', [
// 'method' => GatewayType::DIRECT_DEBIT,
// 'session_token' => $session_token,
// ]),
// 'prefilled_customer' => [
// 'given_name' => auth()->guard('contact')->user()->first_name ?: '',
// 'family_name' => auth()->guard('contact')->user()->last_name ?: '',
// 'email' => auth()->guard('contact')->user()->email ?: '',
// 'address_line1' => auth()->guard('contact')->user()->client->address1 ?: '',
// 'city' => auth()->guard('contact')->user()->client->city ?: '',
// 'postal_code' => auth()->guard('contact')->user()->client->postal_code ?: '',
// 'country_code' => auth()->guard('contact')->user()->client->country->iso_3166_2,
// ],
// ],
// ]);
// return redirect(
// $redirect->redirect_url
// );
// } catch (\Exception $exception) {
// return $this->processUnsuccessfulAuthorization($exception);
// }
}
/**
* Response
* {
* "billing_request_flows": {
* "authorisation_url": "https://pay.gocardless.com/flow/static/billing_request?id=<br_id>",
* "lock_customer_details": false,
* "lock_bank_account": false,
* "auto_fulfil": true,
* "created_at": "2021-03-30T16:23:10.679Z",
* "expires_at": "2021-04-06T16:23:10.679Z",
* "redirect_uri": "https://my-company.com/completed",
* "links": {
* "billing_request": "BRQ123"
* }
* }
* }
*
*
*/
public function billingRequestFlows(array $data)
{
$session_token = \Illuminate\Support\Str::uuid()->toString();
$exit_uri = route('client.payment_methods.index');
$response = $this->go_cardless->gateway->billingRequests()->create([
"params" => [
"mandate_request" => [
"currency" => auth()->guard('contact')->user()->client->currency()->code
]
]
]);
return redirect(
$redirect->redirect_url
);
try {
$brf = $this->go_cardless->gateway->billingRequestFlows()->create([
"params" => [
"redirect_uri" => route('client.payment_methods.confirm', [
'method' => GatewayType::DIRECT_DEBIT,
'session_token' => $session_token,
'billing_request' => $response->id,
]),
"exit_uri" => $exit_uri,
"links" => [
"billing_request" => $response->id
],
"show_redirect_buttons" => true,
"show_success_redirect_button" => true,
]
]);
return redirect($brf->authorisation_url);
} catch (\Exception $exception) {
nlog($exception->getMessage());
return $this->processUnsuccessfulAuthorization($exception);
}
}
/**
* Handle unsuccessful authorization.
*
@ -109,37 +169,68 @@ class DirectDebit implements MethodInterface
*/
public function authorizeResponse(Request $request)
{
try {
$redirect_flow = $this->go_cardless->gateway->redirectFlows()->complete(
$request->redirect_flow_id,
['params' => [
'session_token' => $request->session_token,
]],
);
try{
$billing_request = $this->go_cardless->gateway->billingRequests()->get($request->billing_request);
$payment_meta = new \stdClass;
$payment_meta->brand = ctrans('texts.payment_type_direct_debit');
$payment_meta->type = GatewayType::DIRECT_DEBIT;
$payment_meta->state = 'authorized';
$payment_meta->brand = $billing_request->resources->customer_bank_account->bank_name;
$payment_meta->type = $this->resolveScheme($billing_request->mandate_request->scheme);
$payment_meta->state = 'pending';
$payment_meta->last4 = $billing_request->resources->customer_bank_account->account_number_ending;
$data = [
'payment_meta' => $payment_meta,
'token' => $redirect_flow->links->mandate,
'payment_method_id' => $this->resolveScheme($redirect_flow->scheme),
'token' => $billing_request->mandate_request->links->mandate,
'payment_method_id' => $this->resolveScheme($billing_request->mandate_request->scheme),
];
$payment_method = $this->go_cardless->storeGatewayToken($data, ['gateway_customer_reference' => $redirect_flow->links->customer]);
$payment_method = $this->go_cardless->storeGatewayToken($data, ['gateway_customer_reference' => $billing_request->resources->customer->id]);
$mandate = $this->go_cardless->gateway->mandates()->get($billing_request->mandate_request->links->mandate);
nlog($mandate);
return redirect()->route('client.payment_methods.show', $payment_method->hashed_id);
} catch (\Exception $exception) {
}
catch (\Exception $exception) {
return $this->processUnsuccessfulAuthorization($exception);
}
// try {
// $redirect_flow = $this->go_cardless->gateway->redirectFlows()->complete(
// $request->redirect_flow_id,
// ['params' => [
// 'session_token' => $request->session_token,
// ]],
// );
// $payment_meta = new \stdClass;
// $payment_meta->brand = ctrans('texts.payment_type_direct_debit');
// $payment_meta->type = GatewayType::DIRECT_DEBIT;
// $payment_meta->state = 'authorized';
// $data = [
// 'payment_meta' => $payment_meta,
// 'token' => $redirect_flow->links->mandate,
// 'payment_method_id' => $this->resolveScheme($redirect_flow->scheme),
// ];
// $payment_method = $this->go_cardless->storeGatewayToken($data, ['gateway_customer_reference' => $redirect_flow->links->customer]);
// return redirect()->route('client.payment_methods.show', $payment_method->hashed_id);
// } catch (\Exception $exception) {
// return $this->processUnsuccessfulAuthorization($exception);
// }
}
private function resolveScheme(string $scheme): int
{
match ($scheme) {
'sepa_core' => $type = GatewayType::SEPA,
'ach' => $type = GatewayType::BANK_TRANSFER,
default => $type = GatewayType::DIRECT_DEBIT,
};
@ -176,10 +267,13 @@ class DirectDebit implements MethodInterface
$description = "Amount {$request->amount} from client {$this->go_cardless->client->present()->name()}";
}
$amount = $this->go_cardless->convertToGoCardlessAmount($this->go_cardless->payment_hash?->amount_with_fee(), $this->go_cardless->client->currency()->precision);
try {
$payment = $this->go_cardless->gateway->payments()->create([
'params' => [
'amount' => $request->amount,
// 'amount' => $request->amount,
'amount' => $amount,
'currency' => $request->currency,
'description' => $description,
'metadata' => [

View File

@ -172,10 +172,12 @@ class SEPA implements MethodInterface
$description = "Amount {$request->amount} from client {$this->go_cardless->client->present()->name()}";
}
$amount = $this->go_cardless->convertToGoCardlessAmount($this->go_cardless->payment_hash?->amount_with_fee(), $this->go_cardless->client->currency()->precision);
try {
$payment = $this->go_cardless->gateway->payments()->create([
'params' => [
'amount' => $request->amount,
'amount' => $amount,
'currency' => $request->currency,
'description' => $description,
'metadata' => [

View File

@ -14,6 +14,7 @@ namespace App\PaymentDrivers;
use App\Factory\ClientContactFactory;
use App\Factory\ClientFactory;
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Jobs\Mail\PaymentFailedMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\Client;
use App\Models\ClientGatewayToken;
@ -46,7 +47,7 @@ class GoCardlessPaymentDriver extends BaseDriver
private bool $completed = true;
public static $methods = [
GatewayType::BANK_TRANSFER => \App\PaymentDrivers\GoCardless\ACH::class,
GatewayType::BANK_TRANSFER => \App\PaymentDrivers\GoCardless\DirectDebit::class,
GatewayType::DIRECT_DEBIT => \App\PaymentDrivers\GoCardless\DirectDebit::class,
GatewayType::SEPA => \App\PaymentDrivers\GoCardless\SEPA::class,
GatewayType::INSTANT_BANK_PAY => \App\PaymentDrivers\GoCardless\InstantBankPay::class,
@ -79,7 +80,7 @@ class GoCardlessPaymentDriver extends BaseDriver
$this->client
&& isset($this->client->country)
// && in_array($this->client->country->iso_3166_3, ['GBR'])
&& in_array($this->client->currency()->code, ['EUR', 'GBP','DKK','SEK','AUD','NZD','USD'])
&& in_array($this->client->currency()->code, ['EUR', 'GBP','DKK','SEK','AUD','NZD'])
) {
$types[] = GatewayType::DIRECT_DEBIT;
}
@ -242,7 +243,6 @@ class GoCardlessPaymentDriver extends BaseDriver
$this->init();
nlog('GoCardless Event');
nlog($request->all());
if (! $request->has('events')) {
nlog('No GoCardless events to process in response?');
@ -278,9 +278,26 @@ class GoCardlessPaymentDriver extends BaseDriver
->first();
if ($payment) {
if ($payment->status_id == Payment::STATUS_PENDING) {
$payment->service()->deletePayment();
}
$payment->status_id = Payment::STATUS_FAILED;
$payment->save();
nlog('GoCardless completed');
$payment_hash = PaymentHash::where('payment_id', $payment->id)->first();
$error = '';
if (isset($event['details']['description'])) {
$error = $event['details']['description'];
}
PaymentFailedMailer::dispatch(
$payment_hash,
$payment->client->company,
$payment->client,
$error
);
}
}

View File

@ -21,17 +21,29 @@
@endisset
@isset($url)
<!-- <a href="{{ $url }}" class="button" target="_blank">{{ ctrans($button) }}</a> -->
<table border="0" cellspacing="0" cellpadding="0" align="center">
<tr style="border: 0 !important; ">
<td class="new_button" style="padding: 12px 18px 12px 18px; border-radius:5px;" align="center">
<a href="{{ $url }}") }}" target="_blank" style="border: 0 !important;font-size: 18px; font-family: Helvetica, Arial, sans-serif; color: #ffffff; text-decoration: none; display: inline-block;">
{{ ctrans($button) }}
<div>
<!--[if (gte mso 9)|(IE)]>
<table align="center" cellspacing="0" cellpadding="0" style="width: 600px;">
<tr>
<td align="center" valign="top">
<![endif]-->
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" >
<tbody><tr>
<td align="center" class="new_button" style="border-radius: 2px; background-color: '.$this->settings->primary_color.'">
<a href="{{ $url }}" target="_blank" class="new_button" style="text-decoration: none; border: 1px solid '.$this->settings->primary_color.'; display: inline-block; border-radius: 2px; padding-top: 15px; padding-bottom: 15px; padding-left: 25px; padding-right: 25px; font-size: 20px; color: #fff">
<singleline label="cta button">{{ ctrans($button) }}</singleline>
</a>
</td>
</tr>
</tbody>
</table>
<!--[if (gte mso 9)|(IE)]>
</td>
</tr>
</table>
<![endif]-->
</div>
@endisset

View File

@ -22,7 +22,7 @@
<label class="mr-4">
<input type="radio" data-token="{{ $token->token }}" name="payment-type"
class="form-radio cursor-pointer toggle-payment-with-token" />
<span class="ml-1 cursor-pointer">{{ ctrans('texts.payment_type_direct_debit') }}
<span class="ml-1 cursor-pointer">{{ App\Models\GatewayType::getAlias($token->gateway_type_id) }}
(#{{ $token->token }})</span>
</label>
@endforeach

View File

@ -34,16 +34,16 @@
@endif
@endcomponent
<input type="hidden" name="minimum_amount" value="{{ $minimum }}">
@component('portal.ninja2020.components.general.card-element', ['title' => $title])
<input
type="text"
class="input mt-0 mr-4 relative"
name="amount"
placeholder=""
min="{{ $minimum_amount }}"/>
placeholder=""/>
@if($minimum_amount > 0)
@if($minimum > 0)
<p>{{ ctrans('texts.minimum_required_payment', ['amount' => $minimum_amount])}}</p>
@endif
@ -62,9 +62,9 @@
<div x-cloak x-show="show">
@component('portal.ninja2020.components.general.card-element', ['title' => ctrans('texts.number_of_payments')])
<select name="remaining_cycles" class="form-select input w-full bg-white">
<option value="-1" selected>{{ ctrans('texts.pre_payment_indefinitely')}}</option>
<option value="-1">{{ ctrans('texts.pre_payment_indefinitely')}}</option>
@for($i = 1; $i < 60; $i++)
<option value={{$i}}>{{$i}}</option>
<option value={{$i}} @if($i == 1) selected @endif>{{$i}}</option>
@endfor
</select>
<span class="py-2">