mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-03 23:37:33 -05:00 
			
		
		
		
	Merge pull request #6752 from turbo124/v5-develop
Fixes for ACH Auto Billing
This commit is contained in:
		
						commit
						dbd605f38a
					
				@ -500,7 +500,7 @@ class CompanyController extends BaseController
 | 
				
			|||||||
            $account->delete();
 | 
					            $account->delete();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if(Ninja::isHosted())
 | 
					            if(Ninja::isHosted())
 | 
				
			||||||
                \Modules\Admin\Jobs\Account\NinjaDeletedAccount::dispatch($account_key);
 | 
					                \Modules\Admin\Jobs\Account\NinjaDeletedAccount::dispatch($account_key, $request->all());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            LightLogs::create(new AccountDeleted())
 | 
					            LightLogs::create(new AccountDeleted())
 | 
				
			||||||
                     ->increment()
 | 
					                     ->increment()
 | 
				
			||||||
 | 
				
			|||||||
@ -30,6 +30,7 @@ class SubdomainController extends BaseController
 | 
				
			|||||||
        'invoiceninja',
 | 
					        'invoiceninja',
 | 
				
			||||||
        'cname',
 | 
					        'cname',
 | 
				
			||||||
        'sandbox',
 | 
					        'sandbox',
 | 
				
			||||||
 | 
					        'stage',
 | 
				
			||||||
    ];
 | 
					    ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function __construct()
 | 
					    public function __construct()
 | 
				
			||||||
 | 
				
			|||||||
@ -407,7 +407,7 @@ class Account extends BaseModel
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        catch(\Exception $e){
 | 
					        catch(\Exception $e){
 | 
				
			||||||
            \Sentry\captureMessage("I encountered an error with email quotas - defaulting to SEND");
 | 
					            \Sentry\captureMessage("I encountered an error with email quotas for account {$this->key} - defaulting to SEND");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
 | 
				
			|||||||
@ -22,7 +22,9 @@ use App\Jobs\Util\SystemLogger;
 | 
				
			|||||||
use App\Mail\Gateways\ACHVerificationNotification;
 | 
					use App\Mail\Gateways\ACHVerificationNotification;
 | 
				
			||||||
use App\Models\ClientGatewayToken;
 | 
					use App\Models\ClientGatewayToken;
 | 
				
			||||||
use App\Models\GatewayType;
 | 
					use App\Models\GatewayType;
 | 
				
			||||||
 | 
					use App\Models\Invoice;
 | 
				
			||||||
use App\Models\Payment;
 | 
					use App\Models\Payment;
 | 
				
			||||||
 | 
					use App\Models\PaymentHash;
 | 
				
			||||||
use App\Models\PaymentType;
 | 
					use App\Models\PaymentType;
 | 
				
			||||||
use App\Models\SystemLog;
 | 
					use App\Models\SystemLog;
 | 
				
			||||||
use App\PaymentDrivers\StripePaymentDriver;
 | 
					use App\PaymentDrivers\StripePaymentDriver;
 | 
				
			||||||
@ -145,6 +147,62 @@ class ACH
 | 
				
			|||||||
        return render('gateways.stripe.ach.pay', $data);
 | 
					        return render('gateways.stripe.ach.pay', $data);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
 | 
				
			||||||
 | 
					        $invoice = Invoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))
 | 
				
			||||||
 | 
					                          ->withTrashed()
 | 
				
			||||||
 | 
					                          ->first();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if ($invoice) {
 | 
				
			||||||
 | 
					            $description = "Invoice {$invoice->number} for {$amount} for client {$this->stripe->client->present()->name()}";
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            $description = "Payment with no invoice for amount {$amount} for client {$this->stripe->client->present()->name()}";
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->stripe->init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $response = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $state = [
 | 
				
			||||||
 | 
					                'gateway_type_id' => GatewayType::BANK_TRANSFER,
 | 
				
			||||||
 | 
					                'amount' => $this->stripe->convertToStripeAmount($amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
 | 
				
			||||||
 | 
					                'currency' => $this->stripe->client->getCurrencyCode(),
 | 
				
			||||||
 | 
					                'customer' => $cgt->gateway_customer_reference,
 | 
				
			||||||
 | 
					                'source' => $cgt->token,
 | 
				
			||||||
 | 
					            ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $state['charge'] = \Stripe\Charge::create([
 | 
				
			||||||
 | 
					                'amount' => $state['amount'],
 | 
				
			||||||
 | 
					                'currency' => $state['currency'],
 | 
				
			||||||
 | 
					                'customer' => $state['customer'],
 | 
				
			||||||
 | 
					                'source' => $state['source'],
 | 
				
			||||||
 | 
					                'description' => $description,
 | 
				
			||||||
 | 
					            ], $this->stripe->stripe_connect_auth);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $payment_hash->data = array_merge((array)$payment_hash->data, $state);
 | 
				
			||||||
 | 
					            $payment_hash->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if ($state['charge']->status === 'pending' && is_null($state['charge']->failure_message)) {
 | 
				
			||||||
 | 
					                return $this->processPendingPayment($state, false);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return $this->processUnsuccessfulPayment($state);
 | 
				
			||||||
 | 
					        } catch (Exception $e) {
 | 
				
			||||||
 | 
					            if ($e instanceof CardException) {
 | 
				
			||||||
 | 
					                return redirect()->route('client.payment_methods.verification', ['payment_method' => $source->hashed_id, 'method' => GatewayType::BANK_TRANSFER]);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            throw new PaymentFailed($e->getMessage(), $e->getCode());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function paymentResponse($request)
 | 
					    public function paymentResponse($request)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@ -201,7 +259,7 @@ class ACH
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function processPendingPayment($state)
 | 
					    public function processPendingPayment($state, $client_present = true)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $this->stripe->init();
 | 
					        $this->stripe->init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -224,6 +282,9 @@ class ACH
 | 
				
			|||||||
            $this->stripe->client->company,
 | 
					            $this->stripe->client->company,
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if(!$client_present)
 | 
				
			||||||
 | 
					            return $payment;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]);
 | 
					        return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -15,11 +15,13 @@ namespace App\PaymentDrivers\Stripe;
 | 
				
			|||||||
use App\Events\Payment\PaymentWasCreated;
 | 
					use App\Events\Payment\PaymentWasCreated;
 | 
				
			||||||
use App\Jobs\Util\SystemLogger;
 | 
					use App\Jobs\Util\SystemLogger;
 | 
				
			||||||
use App\Models\ClientGatewayToken;
 | 
					use App\Models\ClientGatewayToken;
 | 
				
			||||||
 | 
					use App\Models\GatewayType;
 | 
				
			||||||
use App\Models\Invoice;
 | 
					use App\Models\Invoice;
 | 
				
			||||||
use App\Models\PaymentHash;
 | 
					use App\Models\PaymentHash;
 | 
				
			||||||
use App\Models\PaymentType;
 | 
					use App\Models\PaymentType;
 | 
				
			||||||
use App\Models\SystemLog;
 | 
					use App\Models\SystemLog;
 | 
				
			||||||
use App\PaymentDrivers\StripePaymentDriver;
 | 
					use App\PaymentDrivers\StripePaymentDriver;
 | 
				
			||||||
 | 
					use App\PaymentDrivers\Stripe\ACH;
 | 
				
			||||||
use App\Utils\Ninja;
 | 
					use App\Utils\Ninja;
 | 
				
			||||||
use App\Utils\Traits\MakesHash;
 | 
					use App\Utils\Traits\MakesHash;
 | 
				
			||||||
use Stripe\Exception\ApiConnectionException;
 | 
					use Stripe\Exception\ApiConnectionException;
 | 
				
			||||||
@ -51,6 +53,9 @@ class Charge
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
 | 
					    public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        if($cgt->gateway_type_id == GatewayType::BANK_TRANSFER)
 | 
				
			||||||
 | 
					            return (new ACH($this->stripe))->tokenBilling($cgt, $payment_hash);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        nlog(" DB = ".$this->stripe->client->company->db);
 | 
					        nlog(" DB = ".$this->stripe->client->company->db);
 | 
				
			||||||
        $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
 | 
					        $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
 | 
				
			||||||
        $invoice = Invoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->withTrashed()->first();
 | 
					        $invoice = Invoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->withTrashed()->first();
 | 
				
			||||||
 | 
				
			|||||||
@ -314,7 +314,9 @@ class StripePaymentDriver extends BaseDriver
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $this->init();
 | 
					        $this->init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $client_gateway_token = ClientGatewayToken::whereClientId($this->client->id)->whereCompanyGatewayId($this->company_gateway->id)->first();
 | 
					        $client_gateway_token = ClientGatewayToken::whereClientId($this->client->id)
 | 
				
			||||||
 | 
					                                                  ->whereCompanyGatewayId($this->company_gateway->id)
 | 
				
			||||||
 | 
					                                                  ->first();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //Search by customer reference
 | 
					        //Search by customer reference
 | 
				
			||||||
        if ($client_gateway_token && $client_gateway_token->gateway_customer_reference) {
 | 
					        if ($client_gateway_token && $client_gateway_token->gateway_customer_reference) {
 | 
				
			||||||
 | 
				
			|||||||
@ -45,6 +45,10 @@ class TriggeredActions extends AbstractService
 | 
				
			|||||||
            $this->quote = $this->quote->service()->markSent()->save();
 | 
					            $this->quote = $this->quote->service()->markSent()->save();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if ($this->request->has('convert') && $this->request->input('convert') == 'true') {
 | 
				
			||||||
 | 
					            $this->quote = $this->quote->service()->convert()->save();
 | 
				
			||||||
 | 
					        }        
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        return $this->quote;
 | 
					        return $this->quote;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user