1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/PaymentDrivers/Braintree/CreditCard.php

244 lines
8.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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2021-04-28 12:54:27 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2021-04-28 12:54:27 +02:00
*/
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\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
if ($this->braintree->company_gateway->getConfigField('merchantAccountId')) {
/** https://developer.paypal.com/braintree/docs/reference/request/client-token/generate#merchant_account_id */
$data['client_token'] = $this->braintree->gateway->clientToken()->generate([
'merchantAccountId' => $this->braintree->company_gateway->getConfigField('merchantAccountId'),
]);
}
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);
2021-04-28 14:54:50 +02:00
$this->braintree->payment_hash->save();
2021-04-29 15:05:45 +02:00
$customer = $this->braintree->findOrCreateCustomer();
2021-05-01 17:53:39 +02:00
$token = $this->getPaymentToken($request->all(), $customer->id);
$data = [
'amount' => $this->braintree->payment_hash->data->amount_with_fee,
2021-05-01 17:53:39 +02:00
'paymentMethodToken' => $token,
2021-04-28 14:54:50 +02:00
'deviceData' => $state['client-data'],
'options' => [
'submitForSettlement' => true,
2021-04-28 14:54:50 +02:00
],
];
if ($this->braintree->company_gateway->getConfigField('merchantAccountId')) {
/** https://developer.paypal.com/braintree/docs/reference/request/transaction/sale/php#full-example */
$data['merchantAccountId'] = $this->braintree->company_gateway->getConfigField('merchantAccountId');
}
try {
$result = $this->braintree->gateway->transaction()->sale($data);
} catch (\Exception $e) {
if ($e instanceof \Braintree\Exception\Authorization) {
2021-10-17 12:40:40 +02:00
$this->braintree->sendFailureMail(ctrans('texts.generic_gateway_error'));
throw new PaymentFailed(ctrans('texts.generic_gateway_error'), $e->getCode());
}
2021-10-17 12:40:40 +02:00
$this->braintree->sendFailureMail($e->getMessage());
2021-10-17 04:59:43 +02:00
throw new PaymentFailed($e->getMessage(), $e->getCode());
}
2021-04-28 14:54:50 +02:00
if ($result->success) {
$this->braintree->logSuccessfulGatewayResponse(['response' => $request->server_response, 'data' => $this->braintree->payment_hash], SystemLog::TYPE_BRAINTREE);
2021-04-29 16:43:59 +02:00
if ($request->store_card && is_null($request->token)) {
2021-05-01 17:53:39 +02:00
$payment_method = $this->braintree->gateway->paymentMethod()->find($token);
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);
}
2021-11-24 23:42:19 +01:00
$error = $result ?: 'Undefined gateway error';
return $this->processUnsuccessfulPayment($error);
2021-04-28 14:54:50 +02:00
}
2021-04-29 16:43:59 +02:00
private function getPaymentToken(array $data, $customerId): ?string
{
if (array_key_exists('token', $data) && ! is_null($data['token'])) {
2021-04-29 16:43:59 +02:00
return $data['token'];
}
$gateway_response = \json_decode($data['gateway_response']);
2021-04-29 16:43:59 +02:00
$data = [
2021-09-10 13:17:33 +02:00
'customerId' => $customerId,
'paymentMethodNonce' => $gateway_response->nonce,
'options' => [
'verifyCard' => true,
],
];
2021-09-21 23:35:57 +02:00
if ($this->braintree->company_gateway->getConfigField('merchantAccountId')) {
/** https://developer.paypal.com/braintree/docs/reference/request/transaction/sale/php#full-example */
2021-09-24 13:31:23 +02:00
$data['options']['verificationMerchantAccountId'] = $this->braintree->company_gateway->getConfigField('merchantAccountId');
2021-09-21 23:35:57 +02:00
}
$response = $this->braintree->gateway->paymentMethod()->create($data);
2021-09-10 13:17:33 +02:00
if ($response->success) {
return $response->paymentMethod->token;
}
2021-09-10 13:17:33 +02:00
2021-10-17 12:40:40 +02:00
$this->braintree->sendFailureMail($response->message);
2021-09-10 13:17:33 +02:00
throw new PaymentFailed($response->message);
2021-04-29 16:43:59 +02:00
}
2021-04-28 14:54:50 +02:00
private function processSuccessfulPayment($response)
{
$state = $this->braintree->payment_hash->data;
$data = [
2021-04-29 16:43:59 +02:00
'payment_type' => PaymentType::parseCardType(strtolower($response->transaction->creditCard['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,
$this->braintree->client->company,
2021-04-28 14:54:50 +02:00
);
return redirect()->route('client.payments.show', ['payment' => $this->braintree->encodePrimaryKey($payment->id)]);
}
/**
* @throws PaymentFailed
*/
private function processUnsuccessfulPayment($response)
{
2021-10-17 12:40:40 +02:00
$this->braintree->sendFailureMail($response->transaction->additionalProcessorResponse);
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,
$this->braintree->client->company,
2021-04-28 14:54:50 +02:00
);
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;
$payment_meta->exp_month = (string) $method->expirationMonth;
$payment_meta->exp_year = (string) $method->expirationYear;
$payment_meta->brand = (string) $method->cardType;
$payment_meta->last4 = (string) $method->last4;
$payment_meta->type = GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
2021-05-01 17:53:39 +02:00
'token' => $method->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
}