2020-06-09 14:40:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-06-09 14:40:55 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-06-09 14:40:55 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-06-09 14:40:55 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Stripe;
|
|
|
|
|
2020-11-01 18:47:48 +01:00
|
|
|
use App\Exceptions\PaymentFailed;
|
2021-06-23 00:15:03 +02:00
|
|
|
use App\Http\Requests\ClientPortal\PaymentMethod\VerifyPaymentMethodRequest;
|
2020-11-01 18:47:48 +01:00
|
|
|
use App\Http\Requests\Request;
|
2021-04-10 10:44:06 +02:00
|
|
|
use App\Jobs\Mail\NinjaMailerJob;
|
|
|
|
use App\Jobs\Mail\NinjaMailerObject;
|
2020-06-09 14:40:55 +02:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
2021-04-10 10:44:06 +02:00
|
|
|
use App\Mail\Gateways\ACHVerificationNotification;
|
2020-06-09 14:40:55 +02:00
|
|
|
use App\Models\ClientGatewayToken;
|
|
|
|
use App\Models\GatewayType;
|
2021-10-01 04:57:34 +02:00
|
|
|
use App\Models\Invoice;
|
2020-06-09 14:40:55 +02:00
|
|
|
use App\Models\Payment;
|
2021-10-01 04:57:34 +02:00
|
|
|
use App\Models\PaymentHash;
|
2020-06-09 14:40:55 +02:00
|
|
|
use App\Models\PaymentType;
|
|
|
|
use App\Models\SystemLog;
|
|
|
|
use App\PaymentDrivers\StripePaymentDriver;
|
2021-01-27 13:07:01 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-12-21 11:46:10 +01:00
|
|
|
use Exception;
|
|
|
|
use Stripe\Customer;
|
2022-05-19 07:48:31 +02:00
|
|
|
use Stripe\Exception\ApiErrorException;
|
|
|
|
use Stripe\Exception\AuthenticationException;
|
2020-11-02 16:29:02 +01:00
|
|
|
use Stripe\Exception\CardException;
|
2020-06-09 14:40:55 +02:00
|
|
|
use Stripe\Exception\InvalidRequestException;
|
2022-05-19 07:48:31 +02:00
|
|
|
use Stripe\Exception\RateLimitException;
|
|
|
|
use Stripe\PaymentIntent;
|
2022-11-29 09:55:20 +01:00
|
|
|
use App\Utils\Number;
|
2020-06-09 14:40:55 +02:00
|
|
|
|
|
|
|
class ACH
|
|
|
|
{
|
2021-01-27 13:07:01 +01:00
|
|
|
use MakesHash;
|
|
|
|
|
2020-06-09 14:40:55 +02:00
|
|
|
/** @var StripePaymentDriver */
|
|
|
|
public $stripe;
|
|
|
|
|
|
|
|
public function __construct(StripePaymentDriver $stripe)
|
|
|
|
{
|
|
|
|
$this->stripe = $stripe;
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:48:31 +02:00
|
|
|
/**
|
|
|
|
* Authorize a bank account - requires microdeposit verification
|
|
|
|
*/
|
2020-06-09 14:40:55 +02:00
|
|
|
public function authorizeView(array $data)
|
|
|
|
{
|
2021-01-21 15:53:37 +01:00
|
|
|
$data['gateway'] = $this->stripe;
|
|
|
|
|
2020-06-09 14:40:55 +02:00
|
|
|
return render('gateways.stripe.ach.authorize', array_merge($data));
|
|
|
|
}
|
|
|
|
|
2021-04-10 10:44:06 +02:00
|
|
|
public function authorizeResponse(Request $request)
|
2020-06-09 14:40:55 +02:00
|
|
|
{
|
|
|
|
$this->stripe->init();
|
|
|
|
|
2020-11-01 18:47:48 +01:00
|
|
|
$stripe_response = json_decode($request->input('gateway_response'));
|
|
|
|
|
|
|
|
$customer = $this->stripe->findOrCreateCustomer();
|
2020-06-09 14:40:55 +02:00
|
|
|
|
|
|
|
try {
|
2021-07-20 06:49:04 +02:00
|
|
|
$source = Customer::createSource($customer->id, ['source' => $stripe_response->token->id], $this->stripe->stripe_connect_auth);
|
2020-06-09 14:40:55 +02:00
|
|
|
} catch (InvalidRequestException $e) {
|
2020-11-01 18:47:48 +01:00
|
|
|
throw new PaymentFailed($e->getMessage(), $e->getCode());
|
2020-06-09 14:40:55 +02:00
|
|
|
}
|
|
|
|
|
2020-11-01 18:47:48 +01:00
|
|
|
$client_gateway_token = $this->storePaymentMethod($source, $request->input('method'), $customer);
|
2020-06-09 14:40:55 +02:00
|
|
|
|
2021-08-10 10:13:04 +02:00
|
|
|
$verification = route('client.payment_methods.verification', ['payment_method' => $client_gateway_token->hashed_id, 'method' => GatewayType::BANK_TRANSFER], false);
|
|
|
|
|
2021-04-10 10:44:06 +02:00
|
|
|
$mailer = new NinjaMailerObject();
|
2021-08-10 10:13:04 +02:00
|
|
|
|
|
|
|
$mailer->mailable = new ACHVerificationNotification(
|
2022-06-21 11:57:17 +02:00
|
|
|
auth()->guard('contact')->user()->client->company,
|
2022-02-16 03:24:10 +01:00
|
|
|
route('client.contact_login', ['contact_key' => auth()->guard('contact')->user()->contact_key, 'next' => $verification])
|
2021-08-10 10:13:04 +02:00
|
|
|
);
|
|
|
|
|
2022-02-16 03:24:10 +01:00
|
|
|
$mailer->company = auth()->guard('contact')->user()->client->company;
|
|
|
|
$mailer->settings = auth()->guard('contact')->user()->client->company->settings;
|
|
|
|
$mailer->to_user = auth()->guard('contact')->user();
|
2021-04-10 10:44:06 +02:00
|
|
|
|
2021-06-09 16:38:24 +02:00
|
|
|
NinjaMailerJob::dispatch($mailer);
|
2021-04-10 10:44:06 +02:00
|
|
|
|
2020-10-20 17:54:08 +02:00
|
|
|
return redirect()->route('client.payment_methods.verification', ['payment_method' => $client_gateway_token->hashed_id, 'method' => GatewayType::BANK_TRANSFER]);
|
2020-06-09 14:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function verificationView(ClientGatewayToken $token)
|
|
|
|
{
|
2021-06-23 14:52:54 +02:00
|
|
|
if (isset($token->meta->state) && $token->meta->state === 'authorized') {
|
|
|
|
return redirect()
|
|
|
|
->route('client.payment_methods.show', $token->hashed_id)
|
|
|
|
->with('message', __('texts.payment_method_verified'));
|
|
|
|
}
|
|
|
|
|
2021-01-21 15:53:37 +01:00
|
|
|
$data = [
|
|
|
|
'token' => $token,
|
|
|
|
'gateway' => $this->stripe,
|
|
|
|
];
|
|
|
|
|
|
|
|
return render('gateways.stripe.ach.verify', $data);
|
2020-06-09 14:40:55 +02:00
|
|
|
}
|
|
|
|
|
2021-08-05 13:45:59 +02:00
|
|
|
public function processVerification(Request $request, ClientGatewayToken $token)
|
2020-06-09 14:40:55 +02:00
|
|
|
{
|
2021-08-05 13:45:59 +02:00
|
|
|
$request->validate([
|
|
|
|
'transactions.*' => ['integer', 'min:1'],
|
|
|
|
]);
|
|
|
|
|
2021-06-23 14:52:54 +02:00
|
|
|
if (isset($token->meta->state) && $token->meta->state === 'authorized') {
|
|
|
|
return redirect()
|
|
|
|
->route('client.payment_methods.show', $token->hashed_id)
|
|
|
|
->with('message', __('texts.payment_method_verified'));
|
|
|
|
}
|
|
|
|
|
2020-06-09 14:40:55 +02:00
|
|
|
$this->stripe->init();
|
|
|
|
|
2021-08-04 03:31:06 +02:00
|
|
|
$bank_account = Customer::retrieveSource($request->customer, $request->source, [], $this->stripe->stripe_connect_auth);
|
2020-06-09 14:40:55 +02:00
|
|
|
|
2022-10-01 02:29:15 +02:00
|
|
|
/* Catch externally validated bank accounts and mark them as verified */
|
|
|
|
if(property_exists($bank_account, 'status') && $bank_account->status == 'verified'){
|
|
|
|
|
|
|
|
$meta = $token->meta;
|
|
|
|
$meta->state = 'authorized';
|
|
|
|
$token->meta = $meta;
|
|
|
|
$token->save();
|
|
|
|
|
|
|
|
return redirect()
|
|
|
|
->route('client.payment_methods.show', $token->hashed_id)
|
|
|
|
->with('message', __('texts.payment_method_verified'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-09 14:40:55 +02:00
|
|
|
try {
|
2020-12-21 11:46:10 +01:00
|
|
|
$bank_account->verify(['amounts' => request()->transactions]);
|
2020-06-09 14:40:55 +02:00
|
|
|
|
2021-06-23 14:52:54 +02:00
|
|
|
$meta = $token->meta;
|
|
|
|
$meta->state = 'authorized';
|
|
|
|
$token->meta = $meta;
|
2020-06-09 14:40:55 +02:00
|
|
|
$token->save();
|
|
|
|
|
|
|
|
return redirect()
|
2021-06-23 14:52:54 +02:00
|
|
|
->route('client.payment_methods.show', $token->hashed_id)
|
|
|
|
->with('message', __('texts.payment_method_verified'));
|
2020-10-28 11:10:49 +01:00
|
|
|
} catch (CardException $e) {
|
2020-06-09 14:40:55 +02:00
|
|
|
return back()->with('error', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-05-19 07:48:31 +02:00
|
|
|
/**
|
|
|
|
* Make a payment WITH instant verification.
|
|
|
|
*/
|
2020-06-09 14:40:55 +02:00
|
|
|
public function paymentView(array $data)
|
|
|
|
{
|
2020-11-01 19:21:31 +01:00
|
|
|
$data['gateway'] = $this->stripe;
|
|
|
|
$data['currency'] = $this->stripe->client->getCurrencyCode();
|
|
|
|
$data['payment_method_id'] = GatewayType::BANK_TRANSFER;
|
|
|
|
$data['customer'] = $this->stripe->findOrCreateCustomer();
|
2021-06-30 05:56:11 +02:00
|
|
|
$data['amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency());
|
2022-10-06 01:12:57 +02:00
|
|
|
$amount = $data['total']['amount_with_fee'];
|
|
|
|
|
|
|
|
$invoice = Invoice::whereIn('id', $this->transformKeys(array_column($this->stripe->payment_hash->invoices(), 'invoice_id')))
|
|
|
|
->withTrashed()
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($invoice) {
|
2022-11-30 07:20:58 +01:00
|
|
|
$description = ctrans('texts.stripe_paymenttext', ['invoicenumber' => $invoice->number, 'amount' => Number::formatMoney($amount, $this->stripe->client), 'client' => $this->stripe->client->present()->name()]);
|
2022-10-06 01:12:57 +02:00
|
|
|
} else {
|
2022-12-01 20:59:16 +01:00
|
|
|
$description = ctrans('texts.stripe_paymenttext_without_invoice', ['amount' => Number::formatMoney($amount, $this->stripe->client), 'client' => $this->stripe->client->present()->name()]);
|
2022-10-06 01:12:57 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 14:40:55 +02:00
|
|
|
|
2022-05-18 04:59:24 +02:00
|
|
|
$intent = false;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (count($data['tokens']) == 0) {
|
|
|
|
$intent =
|
2022-05-18 04:59:24 +02:00
|
|
|
$this->stripe->createPaymentIntent([
|
|
|
|
'amount' => $data['amount'],
|
|
|
|
'currency' => $data['currency'],
|
|
|
|
'setup_future_usage' => 'off_session',
|
|
|
|
'customer' => $data['customer']->id,
|
|
|
|
'payment_method_types' => ['us_bank_account'],
|
2022-10-06 01:12:57 +02:00
|
|
|
'description' => $description,
|
|
|
|
'metadata' => [
|
|
|
|
'payment_hash' => $this->stripe->payment_hash->hash,
|
|
|
|
'gateway_type_id' => GatewayType::BANK_TRANSFER,
|
|
|
|
],
|
2022-06-21 11:57:17 +02:00
|
|
|
]
|
2022-05-18 04:59:24 +02:00
|
|
|
);
|
|
|
|
}
|
2022-05-18 01:39:54 +02:00
|
|
|
|
2022-05-18 04:59:24 +02:00
|
|
|
$data['client_secret'] = $intent ? $intent->client_secret : false;
|
2022-05-18 01:39:54 +02:00
|
|
|
|
2022-05-18 04:59:24 +02:00
|
|
|
return render('gateways.stripe.ach.pay', $data);
|
2020-06-09 14:40:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 04:57:34 +02:00
|
|
|
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) {
|
2022-11-30 07:20:58 +01:00
|
|
|
$description = ctrans('texts.stripe_paymenttext', ['invoicenumber' => $invoice->number, 'amount' => Number::formatMoney($amount, $this->stripe->client), 'client' => $this->stripe->client->present()->name()]);
|
2021-10-01 04:57:34 +02:00
|
|
|
} else {
|
2022-12-01 20:59:16 +01:00
|
|
|
$description = ctrans('texts.stripe_paymenttext_without_invoice', ['amount' => Number::formatMoney($amount, $this->stripe->client), 'client' => $this->stripe->client->present()->name()]);
|
2021-10-01 04:57:34 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (substr($cgt->token, 0, 2) === 'pm') {
|
2022-05-19 07:48:31 +02:00
|
|
|
return $this->paymentIntentTokenBilling($amount, $invoice, $description, $cgt, false);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-19 07:48:31 +02:00
|
|
|
|
2021-10-01 04:57:34 +02:00
|
|
|
$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);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$payment_hash->data = array_merge((array) $payment_hash->data, $state);
|
2021-10-01 04:57:34 +02:00
|
|
|
$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) {
|
2021-10-17 13:21:56 +02:00
|
|
|
return redirect()->route('client.payment_methods.verification', ['payment_method' => $cgt->hashed_id, 'method' => GatewayType::BANK_TRANSFER]);
|
2021-10-01 04:57:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new PaymentFailed($e->getMessage(), $e->getCode());
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 19:21:31 +01:00
|
|
|
|
2022-05-19 07:48:31 +02:00
|
|
|
public function paymentIntentTokenBilling($amount, $invoice, $description, $cgt, $client_present = true)
|
|
|
|
{
|
|
|
|
$this->stripe->init();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$data = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => $this->stripe->convertToStripeAmount($amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
|
|
|
|
'currency' => $this->stripe->client->getCurrencyCode(),
|
|
|
|
'payment_method' => $cgt->token,
|
|
|
|
'customer' => $cgt->gateway_customer_reference,
|
|
|
|
'confirm' => true,
|
|
|
|
'description' => $description,
|
|
|
|
'metadata' => [
|
|
|
|
'payment_hash' => $this->stripe->payment_hash->hash,
|
|
|
|
'gateway_type_id' => $cgt->gateway_type_id,
|
2022-05-19 07:48:31 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($cgt->gateway_type_id == GatewayType::BANK_TRANSFER) {
|
2022-05-19 07:48:31 +02:00
|
|
|
$data['payment_method_types'] = ['us_bank_account'];
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-19 07:48:31 +02:00
|
|
|
|
|
|
|
$response = $this->stripe->createPaymentIntent($data, $this->stripe->stripe_connect_auth);
|
|
|
|
|
|
|
|
SystemLogger::dispatch($response, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->stripe->client, $this->stripe->client->company);
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$data = [
|
2022-05-19 07:48:31 +02:00
|
|
|
'status' => '',
|
|
|
|
'error_type' => '',
|
|
|
|
'error_code' => '',
|
|
|
|
'param' => '',
|
|
|
|
'message' => '',
|
|
|
|
];
|
|
|
|
|
|
|
|
switch ($e) {
|
2022-06-21 11:57:17 +02:00
|
|
|
case $e instanceof CardException:
|
2022-05-19 07:48:31 +02:00
|
|
|
$data['status'] = $e->getHttpStatus();
|
|
|
|
$data['error_type'] = $e->getError()->type;
|
|
|
|
$data['error_code'] = $e->getError()->code;
|
|
|
|
$data['param'] = $e->getError()->param;
|
|
|
|
$data['message'] = $e->getError()->message;
|
|
|
|
break;
|
2022-06-21 11:57:17 +02:00
|
|
|
case $e instanceof RateLimitException:
|
2022-05-19 07:48:31 +02:00
|
|
|
$data['message'] = 'Too many requests made to the API too quickly';
|
|
|
|
break;
|
2022-06-21 11:57:17 +02:00
|
|
|
case $e instanceof InvalidRequestException:
|
2022-05-19 07:48:31 +02:00
|
|
|
$data['message'] = 'Invalid parameters were supplied to Stripe\'s API';
|
|
|
|
break;
|
2022-06-21 11:57:17 +02:00
|
|
|
case $e instanceof AuthenticationException:
|
2022-05-19 07:48:31 +02:00
|
|
|
$data['message'] = 'Authentication with Stripe\'s API failed';
|
|
|
|
break;
|
2022-06-21 11:57:17 +02:00
|
|
|
case $e instanceof ApiErrorException:
|
2022-05-19 07:48:31 +02:00
|
|
|
$data['message'] = 'Network communication with Stripe failed';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$data['message'] = $e->getMessage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->stripe->processInternallyFailedPayment($this->stripe, $e);
|
|
|
|
|
|
|
|
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client, $this->stripe->client->company);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-19 07:48:31 +02:00
|
|
|
|
|
|
|
if (! $response) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$payment_method_type = PaymentType::ACH;
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'gateway_type_id' => $cgt->gateway_type_id,
|
|
|
|
'payment_type' => PaymentType::ACH,
|
2022-11-29 10:09:54 +01:00
|
|
|
'transaction_reference' => isset($response->latest_charge) ? $response->latest_charge : $response->charges->data[0]->id,
|
2022-05-19 07:48:31 +02:00
|
|
|
'amount' => $amount,
|
|
|
|
];
|
|
|
|
|
|
|
|
$payment = $this->stripe->createPayment($data, Payment::STATUS_PENDING);
|
|
|
|
$payment->meta = $cgt->meta;
|
|
|
|
$payment->save();
|
|
|
|
|
|
|
|
$this->stripe->payment_hash->payment_id = $payment->id;
|
|
|
|
$this->stripe->payment_hash->save();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($client_present) {
|
2022-05-19 07:48:31 +02:00
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $payment;
|
|
|
|
}
|
|
|
|
|
2022-05-18 04:59:24 +02:00
|
|
|
public function handlePaymentIntentResponse($request)
|
|
|
|
{
|
2022-05-19 07:48:31 +02:00
|
|
|
$response = json_decode($request->gateway_response);
|
|
|
|
$bank_account_response = json_decode($request->bank_account_response);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-05-19 07:48:31 +02:00
|
|
|
$method = $bank_account_response->payment_method->us_bank_account;
|
|
|
|
$method->id = $response->payment_method;
|
|
|
|
$method->state = 'authorized';
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->stripe->payment_hash = PaymentHash::where('hash', $request->input('payment_hash'))->first();
|
2022-05-19 07:48:31 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($response->id && $response->status === 'processing') {
|
2022-05-19 07:48:31 +02:00
|
|
|
$payment_intent = PaymentIntent::retrieve($response->id, $this->stripe->stripe_connect_auth);
|
|
|
|
|
|
|
|
$state = [
|
|
|
|
'gateway_type_id' => GatewayType::BANK_TRANSFER,
|
|
|
|
'amount' => $response->amount,
|
|
|
|
'currency' => $response->currency,
|
|
|
|
'customer' => $request->customer,
|
|
|
|
'source' => $response->payment_method,
|
2022-06-21 11:57:17 +02:00
|
|
|
'charge' => $response,
|
2022-05-19 07:48:31 +02:00
|
|
|
];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $state);
|
2022-05-19 07:48:31 +02:00
|
|
|
$this->stripe->payment_hash->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-05-19 07:48:31 +02:00
|
|
|
$customer = $this->stripe->getCustomer($request->customer);
|
|
|
|
|
|
|
|
$this->storePaymentMethod($method, GatewayType::BANK_TRANSFER, $customer);
|
|
|
|
|
|
|
|
return $this->processPendingPayment($state, true);
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($response->next_action) {
|
2022-05-19 07:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processPendingPaymentIntent($state, $client_present = true)
|
|
|
|
{
|
|
|
|
$this->stripe->init();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'payment_method' => $state['source'],
|
|
|
|
'payment_type' => PaymentType::ACH,
|
|
|
|
'amount' => $state['amount'],
|
|
|
|
'transaction_reference' => $state['charge'],
|
|
|
|
'gateway_type_id' => GatewayType::BANK_TRANSFER,
|
|
|
|
];
|
|
|
|
|
|
|
|
$payment = $this->stripe->createPayment($data, Payment::STATUS_PENDING);
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
['response' => $state, 'data' => $data],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_STRIPE,
|
|
|
|
$this->stripe->client,
|
|
|
|
$this->stripe->client->company,
|
|
|
|
);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $client_present) {
|
2022-05-19 07:48:31 +02:00
|
|
|
return $payment;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-19 07:48:31 +02:00
|
|
|
|
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]);
|
2022-05-18 04:59:24 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 14:40:55 +02:00
|
|
|
public function paymentResponse($request)
|
|
|
|
{
|
2020-11-01 19:21:31 +01:00
|
|
|
$this->stripe->init();
|
|
|
|
|
2022-05-18 04:59:24 +02:00
|
|
|
//it may be a payment intent here.
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($request->input('client_secret') != '') {
|
2022-05-19 07:48:31 +02:00
|
|
|
return $this->handlePaymentIntentResponse($request);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-18 04:59:24 +02:00
|
|
|
|
2021-01-27 13:07:01 +01:00
|
|
|
$source = ClientGatewayToken::query()
|
|
|
|
->where('id', $this->decodePrimaryKey($request->source))
|
2022-02-16 03:24:10 +01:00
|
|
|
->where('company_id', auth()->guard('contact')->user()->client->company->id)
|
2021-01-27 13:07:01 +01:00
|
|
|
->first();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $source) {
|
2021-01-27 13:07:01 +01:00
|
|
|
throw new PaymentFailed(ctrans('texts.payment_token_not_found'), 401);
|
|
|
|
}
|
|
|
|
|
2020-06-09 14:40:55 +02:00
|
|
|
$state = [
|
|
|
|
'payment_method' => $request->payment_method_id,
|
|
|
|
'gateway_type_id' => $request->company_gateway_id,
|
2021-06-30 05:56:11 +02:00
|
|
|
'amount' => $this->stripe->convertToStripeAmount($request->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
|
2020-06-09 14:40:55 +02:00
|
|
|
'currency' => $request->currency,
|
|
|
|
'customer' => $request->customer,
|
|
|
|
];
|
|
|
|
|
2020-11-01 19:21:31 +01:00
|
|
|
$state = array_merge($state, $request->all());
|
2021-01-27 13:07:01 +01:00
|
|
|
$state['source'] = $source->token;
|
2020-06-09 14:40:55 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $state);
|
2020-11-01 19:21:31 +01:00
|
|
|
$this->stripe->payment_hash->save();
|
2020-06-09 14:40:55 +02:00
|
|
|
|
2021-11-23 18:27:45 +01:00
|
|
|
$amount = array_sum(array_column($this->stripe->payment_hash->invoices(), 'amount')) + $this->stripe->payment_hash->fee_total;
|
|
|
|
$invoice = Invoice::whereIn('id', $this->transformKeys(array_column($this->stripe->payment_hash->invoices(), 'invoice_id')))
|
|
|
|
->withTrashed()
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($invoice) {
|
2022-11-30 07:20:58 +01:00
|
|
|
$description = ctrans('texts.stripe_paymenttext', ['invoicenumber' => $invoice->number, 'amount' => Number::formatMoney($amount, $this->stripe->client), 'client' => $this->stripe->client->present()->name()]);
|
2021-11-23 18:27:45 +01:00
|
|
|
} else {
|
2022-12-01 20:59:16 +01:00
|
|
|
$description = ctrans('texts.stripe_paymenttext_without_invoice', ['amount' => Number::formatMoney($amount, $this->stripe->client), 'client' => $this->stripe->client->present()->name()]);
|
2021-11-23 18:27:45 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (substr($source->token, 0, 2) === 'pm') {
|
2022-05-19 07:48:31 +02:00
|
|
|
return $this->paymentIntentTokenBilling($amount, $invoice, $description, $source);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-19 07:48:31 +02:00
|
|
|
|
2020-06-09 14:40:55 +02:00
|
|
|
try {
|
|
|
|
$state['charge'] = \Stripe\Charge::create([
|
|
|
|
'amount' => $state['amount'],
|
|
|
|
'currency' => $state['currency'],
|
|
|
|
'customer' => $state['customer'],
|
|
|
|
'source' => $state['source'],
|
2021-11-23 18:27:45 +01:00
|
|
|
'description' => $description,
|
2021-04-20 13:30:52 +02:00
|
|
|
], $this->stripe->stripe_connect_auth);
|
2020-06-09 14:40:55 +02:00
|
|
|
|
2020-11-01 19:21:31 +01:00
|
|
|
$state = array_merge($state, $request->all());
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $state);
|
2020-11-01 19:21:31 +01:00
|
|
|
$this->stripe->payment_hash->save();
|
|
|
|
|
2020-06-09 14:40:55 +02:00
|
|
|
if ($state['charge']->status === 'pending' && is_null($state['charge']->failure_message)) {
|
|
|
|
return $this->processPendingPayment($state);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->processUnsuccessfulPayment($state);
|
2020-10-28 11:10:49 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
if ($e instanceof CardException) {
|
2021-08-04 04:20:38 +02:00
|
|
|
return redirect()->route('client.payment_methods.verification', ['payment_method' => $source->hashed_id, 'method' => GatewayType::BANK_TRANSFER]);
|
2020-06-09 14:40:55 +02:00
|
|
|
}
|
2021-01-27 13:07:01 +01:00
|
|
|
|
|
|
|
throw new PaymentFailed($e->getMessage(), $e->getCode());
|
2020-06-09 14:40:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 04:57:34 +02:00
|
|
|
public function processPendingPayment($state, $client_present = true)
|
2020-06-09 14:40:55 +02:00
|
|
|
{
|
|
|
|
$this->stripe->init();
|
|
|
|
|
|
|
|
$data = [
|
2020-11-01 19:21:31 +01:00
|
|
|
'payment_method' => $state['source'],
|
|
|
|
'payment_type' => PaymentType::ACH,
|
2021-06-30 05:56:11 +02:00
|
|
|
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
|
2020-11-01 19:21:31 +01:00
|
|
|
'transaction_reference' => $state['charge']->id,
|
2021-01-27 11:38:28 +01:00
|
|
|
'gateway_type_id' => GatewayType::BANK_TRANSFER,
|
2020-06-09 14:40:55 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$payment = $this->stripe->createPayment($data, Payment::STATUS_PENDING);
|
|
|
|
|
2020-11-01 19:21:31 +01:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
['response' => $state['charge'], 'data' => $data],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_STRIPE,
|
2021-05-29 12:57:39 +02:00
|
|
|
$this->stripe->client,
|
|
|
|
$this->stripe->client->company,
|
2020-11-01 19:21:31 +01:00
|
|
|
);
|
2020-06-09 14:40:55 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $client_present) {
|
2021-10-01 04:57:34 +02:00
|
|
|
return $payment;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-10-01 04:57:34 +02:00
|
|
|
|
2020-06-09 14:40:55 +02:00
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processUnsuccessfulPayment($state)
|
|
|
|
{
|
2021-10-17 12:40:40 +02:00
|
|
|
$this->stripe->sendFailureMail($state['charge']);
|
2020-11-01 19:21:31 +01:00
|
|
|
|
2020-06-09 14:40:55 +02:00
|
|
|
$message = [
|
|
|
|
'server_response' => $state['charge'],
|
2020-11-01 19:21:31 +01:00
|
|
|
'data' => $this->stripe->payment_hash->data,
|
2020-06-09 14:40:55 +02:00
|
|
|
];
|
|
|
|
|
2020-11-01 19:21:31 +01:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
$message,
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_STRIPE,
|
|
|
|
$this->stripe->client
|
|
|
|
);
|
2020-06-09 14:40:55 +02:00
|
|
|
|
2020-11-01 19:21:31 +01:00
|
|
|
throw new PaymentFailed('Failed to process the payment.', 500);
|
2020-06-09 14:40:55 +02:00
|
|
|
}
|
2020-11-01 18:47:48 +01:00
|
|
|
|
|
|
|
private function storePaymentMethod($method, $payment_method_id, $customer)
|
|
|
|
{
|
2022-05-19 07:48:31 +02:00
|
|
|
$state = property_exists($method, 'state') ? $method->state : 'unauthorized';
|
|
|
|
|
2020-11-01 18:47:48 +01:00
|
|
|
try {
|
|
|
|
$payment_meta = new \stdClass;
|
2021-08-09 13:49:39 +02:00
|
|
|
$payment_meta->brand = (string) \sprintf('%s (%s)', $method->bank_name, ctrans('texts.ach'));
|
|
|
|
$payment_meta->last4 = (string) $method->last4;
|
2020-11-01 18:47:48 +01:00
|
|
|
$payment_meta->type = GatewayType::BANK_TRANSFER;
|
2022-05-19 07:48:31 +02:00
|
|
|
$payment_meta->state = $state;
|
2020-11-01 18:47:48 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'payment_meta' => $payment_meta,
|
|
|
|
'token' => $method->id,
|
|
|
|
'payment_method_id' => $payment_method_id,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->stripe->storeGatewayToken($data, ['gateway_customer_reference' => $customer->id]);
|
2020-12-21 11:46:10 +01:00
|
|
|
} catch (Exception $e) {
|
2020-11-01 18:47:48 +01:00
|
|
|
return $this->stripe->processInternallyFailedPayment($this->stripe, $e);
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 14:40:55 +02:00
|
|
|
}
|