1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/PaymentDrivers/Braintree/ACH.php

179 lines
6.0 KiB
PHP
Raw Normal View History

2021-08-26 15:37:56 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-08-26 15:37:56 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\Braintree;
2021-08-27 17:00:32 +02:00
use App\Exceptions\PaymentFailed;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
2021-08-26 15:37:56 +02:00
use App\Models\GatewayType;
2021-08-27 17:00:32 +02:00
use App\Models\Payment;
use App\Models\PaymentType;
use App\Models\SystemLog;
2021-08-26 15:37:56 +02:00
use App\PaymentDrivers\BraintreePaymentDriver;
use App\PaymentDrivers\Common\MethodInterface;
2021-08-27 17:00:32 +02:00
use App\Utils\Traits\MakesHash;
2021-10-26 16:21:26 +02:00
use Illuminate\Http\Request;
2021-08-26 15:37:56 +02:00
class ACH implements MethodInterface
{
2021-08-27 17:00:32 +02:00
use MakesHash;
2021-08-26 15:37:56 +02:00
protected BraintreePaymentDriver $braintree;
public function __construct(BraintreePaymentDriver $braintree)
{
$this->braintree = $braintree;
$this->braintree->init();
}
public function authorizeView(array $data)
{
$data['gateway'] = $this->braintree;
$data['client_token'] = $this->braintree->gateway->clientToken()->generate();
return render('gateways.braintree.ach.authorize', $data);
}
public function authorizeResponse(Request $request)
{
$request->validate([
'nonce' => ['required'],
'gateway_type_id' => ['required'],
]);
$customer = $this->braintree->findOrCreateCustomer();
$result = $this->braintree->gateway->paymentMethod()->create([
'customerId' => $customer->id,
'paymentMethodNonce' => $request->nonce,
'options' => [
'usBankAccountVerificationMethod' => \Braintree\Result\UsBankAccountVerification::NETWORK_CHECK,
],
]);
if ($result->success && $result->paymentMethod?->verified) {
2021-08-26 15:37:56 +02:00
$account = $result->paymentMethod;
try {
$payment_meta = new \stdClass;
$payment_meta->brand = (string) $account->bankName;
$payment_meta->last4 = (string) $account->last4;
2021-08-26 15:37:56 +02:00
$payment_meta->type = GatewayType::BANK_TRANSFER;
$payment_meta->state = 'authorized';
2021-08-26 15:37:56 +02:00
$data = [
'payment_meta' => $payment_meta,
'token' => $account->token,
'payment_method_id' => $request->gateway_type_id,
];
$this->braintree->storeGatewayToken($data, ['gateway_customer_reference' => $customer->id]);
2021-08-27 17:00:32 +02:00
return redirect()->route('client.payment_methods.index')->withMessage(ctrans('texts.payment_method_added'));
2021-08-26 15:37:56 +02:00
} catch (\Exception $e) {
2021-08-27 16:21:11 +02:00
return $this->braintree->processInternallyFailedPayment($this->braintree, $e);
2021-08-26 15:37:56 +02:00
}
}
return back()->withMessage(ctrans('texts.unable_to_verify_payment_method'));
2021-08-26 15:37:56 +02:00
}
2021-08-26 15:46:31 +02:00
public function paymentView(array $data)
{
$data['gateway'] = $this->braintree;
2021-08-27 17:00:32 +02:00
$data['currency'] = $this->braintree->client->getCurrencyCode();
$data['payment_method_id'] = GatewayType::BANK_TRANSFER;
$data['amount'] = $this->braintree->payment_hash->data->amount_with_fee;
2021-08-26 15:46:31 +02:00
return render('gateways.braintree.ach.pay', $data);
}
2021-08-27 17:00:32 +02:00
public function paymentResponse(PaymentResponseRequest $request)
{
$request->validate([
'source' => ['required'],
'payment_hash' => ['required'],
]);
$customer = $this->braintree->findOrCreateCustomer();
$token = ClientGatewayToken::query()
->where('client_id', auth()->guard('contact')->user()->client->id)
2021-08-27 17:00:32 +02:00
->where('id', $this->decodePrimaryKey($request->source))
->firstOrFail();
$result = $this->braintree->gateway->transaction()->sale([
'amount' => $this->braintree->payment_hash->data->amount_with_fee,
'paymentMethodToken' => $token->token,
'options' => [
'submitForSettlement' => true,
2021-08-27 17:00:32 +02:00
],
]);
if ($result->success) {
$this->braintree->logSuccessfulGatewayResponse(['response' => $request->server_response, 'data' => $this->braintree->payment_hash], SystemLog::TYPE_BRAINTREE);
return $this->processSuccessfulPayment($result);
}
return $this->processUnsuccessfulPayment($result);
}
private function processSuccessfulPayment($response)
{
$state = $this->braintree->payment_hash->data;
$data = [
'payment_type' => PaymentType::ACH,
'amount' => $this->braintree->payment_hash->data->amount_with_fee,
'transaction_reference' => $response->transaction->id,
'gateway_type_id' => GatewayType::BANK_TRANSFER,
];
$payment = $this->braintree->createPayment($data, Payment::STATUS_COMPLETED);
SystemLogger::dispatch(
['response' => $response, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_BRAINTREE,
$this->braintree->client,
$this->braintree->client->company,
);
return redirect()->route('client.payments.show', ['payment' => $this->braintree->encodePrimaryKey($payment->id)]);
}
private function processUnsuccessfulPayment($response)
{
2021-10-17 12:40:40 +02:00
$this->braintree->sendFailureMail($response->transaction->additionalProcessorResponse);
2021-08-27 17:00:32 +02:00
$message = [
'server_response' => $response,
'data' => $this->braintree->payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_BRAINTREE,
$this->braintree->client,
$this->braintree->client->company,
);
throw new PaymentFailed($response->transaction->additionalProcessorResponse, $response->transaction->processorResponseCode);
}
2021-08-26 15:37:56 +02:00
}