1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/PaymentDrivers/Braintree/CreditCard.php

195 lines
6.3 KiB
PHP
Raw Normal View History

2021-04-28 12:54:27 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers\Braintree;
2021-04-28 14:54:50 +02:00
use App\Exceptions\PaymentFailed;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Http\Requests\Request;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentType;
use App\Models\SystemLog;
2021-04-28 12:54:27 +02:00
use App\PaymentDrivers\BraintreePaymentDriver;
class CreditCard
{
/**
* @var BraintreePaymentDriver
*/
private $braintree;
public function __construct(BraintreePaymentDriver $braintree)
{
$this->braintree = $braintree;
2021-04-28 14:54:50 +02:00
$this->braintree->init();
2021-04-28 12:54:27 +02:00
}
2021-04-28 15:03:22 +02:00
public function authorizeView(array $data)
{
$data['gateway'] = $this->braintree;
return render('gateways.braintree.credit_card.authorize', $data);
}
public function authorizeResponse($data): \Illuminate\Http\RedirectResponse
2021-04-28 15:03:22 +02:00
{
return back();
2021-04-28 15:03:22 +02:00
}
2021-04-28 12:54:27 +02:00
/**
* Credit card payment page.
*
* @param array $data
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function paymentView(array $data)
{
$data['gateway'] = $this->braintree;
2021-04-28 14:54:50 +02:00
$data['client_token'] = $this->braintree->gateway->clientToken()->generate();
2021-04-28 12:54:27 +02:00
return render('gateways.braintree.credit_card.pay', $data);
}
2021-04-28 14:54:50 +02:00
2021-04-28 15:03:22 +02:00
/**
* Process the credit card payments.
*
* @param PaymentResponseRequest $request
* @return \Illuminate\Http\RedirectResponse|void
* @throws PaymentFailed
*/
2021-04-28 14:54:50 +02:00
public function paymentResponse(PaymentResponseRequest $request)
{
$state = [
'server_response' => json_decode($request->gateway_response),
'payment_hash' => $request->payment_hash,
];
$state = array_merge($state, $request->all());
$state['store_card'] = boolval($state['store_card']);
$this->braintree->payment_hash->data = array_merge((array)$this->braintree->payment_hash->data, $state);
$this->braintree->payment_hash->save();
2021-04-29 15:05:45 +02:00
$customer = $this->braintree->findOrCreateCustomer();
$payment_method = $this->braintree->gateway->paymentMethod()->create([
'customerId' => $customer->id,
'paymentMethodNonce' => $state['token'],
'options' => [
'verifyCard' => true,
],
]);
2021-04-28 14:54:50 +02:00
$result = $this->braintree->gateway->transaction()->sale([
'amount' => $this->braintree->payment_hash->data->amount_with_fee,
2021-04-29 15:05:45 +02:00
'paymentMethodToken' => $payment_method->paymentMethod->token,
2021-04-28 14:54:50 +02:00
'deviceData' => $state['client-data'],
'options' => [
'submitForSettlement' => true
],
]);
if ($result->success) {
$this->braintree->logSuccessfulGatewayResponse(['response' => $request->server_response, 'data' => $this->braintree->payment_hash], SystemLog::TYPE_BRAINTREE);
if ($request->store_card) {
2021-04-29 15:05:45 +02:00
$this->storePaymentMethod($payment_method, $customer->id);
}
2021-04-28 14:54:50 +02:00
return $this->processSuccessfulPayment($result);
}
return $this->processUnsuccessfulPayment($result);
}
private function processSuccessfulPayment($response)
{
$state = $this->braintree->payment_hash->data;
$data = [
'payment_type' => PaymentType::parseCardType(strtolower($state->server_response->details->cardType)),
'amount' => $this->braintree->payment_hash->data->amount_with_fee,
2021-04-28 14:54:50 +02:00
'transaction_reference' => $response->transaction->id,
'gateway_type_id' => GatewayType::CREDIT_CARD,
];
$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
);
return redirect()->route('client.payments.show', ['payment' => $this->braintree->encodePrimaryKey($payment->id)]);
}
/**
* @throws PaymentFailed
*/
private function processUnsuccessfulPayment($response)
{
PaymentFailureMailer::dispatch($this->braintree->client, $response->transaction->additionalProcessorResponse, $this->braintree->client->company, 10);
PaymentFailureMailer::dispatch(
$this->braintree->client,
$response,
$this->braintree->client->company,
$this->braintree->payment_hash->data->amount_with_fee,
2021-04-28 14:54:50 +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
);
throw new PaymentFailed($response->transaction->additionalProcessorResponse, $response->transaction->processorResponseCode);
}
2021-04-29 15:05:45 +02:00
private function storePaymentMethod($method, $customer_reference)
{
try {
$payment_meta = new \stdClass;
2021-04-29 15:05:45 +02:00
$payment_meta->exp_month = (string)$method->paymentMethod->expirationMonth;
$payment_meta->exp_year = (string)$method->paymentMethod->expirationYear;
$payment_meta->brand = (string)$method->paymentMethod->cardType;
$payment_meta->last4 = (string)$method->paymentMethod->last4;
$payment_meta->type = GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
2021-04-29 15:05:45 +02:00
'token' => $method->paymentMethod->token,
'payment_method_id' => $this->braintree->payment_hash->data->payment_method_id,
];
2021-04-29 15:05:45 +02:00
$this->braintree->storeGatewayToken($data, ['gateway_customer_reference' => $customer_reference]);
} catch (\Exception $e) {
return $this->braintree->processInternallyFailedPayment($this->braintree, $e);
}
}
2021-04-28 12:54:27 +02:00
}