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

263 lines
8.2 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)
*
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;
2021-04-28 14:54:50 +02:00
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
2021-04-29 15:05:45 +02:00
use App\Models\ClientGatewayToken;
2021-04-28 12:54:27 +02:00
use App\Models\GatewayType;
use App\Models\Invoice;
2021-05-05 17:24:31 +02:00
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\PaymentType;
2021-04-28 12:54:27 +02:00
use App\Models\SystemLog;
2021-08-26 15:38:28 +02:00
use App\PaymentDrivers\Braintree\ACH;
2021-04-28 12:54:27 +02:00
use App\PaymentDrivers\Braintree\CreditCard;
2021-05-03 14:10:46 +02:00
use App\PaymentDrivers\Braintree\PayPal;
use Braintree\Gateway;
use Exception;
2021-04-28 14:54:50 +02:00
use Illuminate\Http\Request;
2021-04-28 12:54:27 +02:00
class BraintreePaymentDriver extends BaseDriver
{
public $refundable = true;
public $token_billing = true;
public $can_authorise_credit_card = true;
2021-04-28 14:54:50 +02:00
/**
* @var Gateway;
2021-04-28 14:54:50 +02:00
*/
2021-04-28 12:54:27 +02:00
public $gateway;
public static $methods = [
GatewayType::CREDIT_CARD => CreditCard::class,
2021-05-04 14:56:30 +02:00
GatewayType::PAYPAL => PayPal::class,
2021-08-26 15:38:28 +02:00
GatewayType::BANK_TRANSFER => ACH::class,
2021-04-28 12:54:27 +02:00
];
const SYSTEM_LOG_TYPE = SystemLog::TYPE_BRAINTREE;
2021-04-28 14:54:50 +02:00
public function init(): void
2021-04-28 12:54:27 +02:00
{
$this->gateway = new Gateway([
2021-04-28 14:54:50 +02:00
'environment' => $this->company_gateway->getConfigField('testMode') ? 'sandbox' : 'production',
'merchantId' => $this->company_gateway->getConfigField('merchantId'),
'publicKey' => $this->company_gateway->getConfigField('publicKey'),
'privateKey' => $this->company_gateway->getConfigField('privateKey'),
]);
2021-04-28 12:54:27 +02:00
}
public function setPaymentMethod($payment_method_id)
{
$class = self::$methods[$payment_method_id];
2021-05-03 15:48:53 +02:00
$this->payment_method = new $class($this);
2021-04-28 12:54:27 +02:00
return $this;
}
public function gatewayTypes(): array
{
$types = [
2021-04-28 12:54:27 +02:00
GatewayType::PAYPAL,
2021-08-26 15:38:28 +02:00
GatewayType::CREDIT_CARD,
GatewayType::BANK_TRANSFER,
2021-04-28 12:54:27 +02:00
];
2021-08-26 15:38:28 +02:00
return $types;
2021-04-28 12:54:27 +02:00
}
2021-04-28 15:03:22 +02:00
public function authorizeView($data)
{
return $this->payment_method->authorizeView($data);
}
public function authorizeResponse($data)
{
return $this->payment_method->authorizeResponse($data);
}
2021-04-28 12:54:27 +02:00
public function processPaymentView(array $data)
{
return $this->payment_method->paymentView($data);
}
2021-04-28 14:54:50 +02:00
public function processPaymentResponse($request)
{
return $this->payment_method->paymentResponse($request);
}
2021-04-29 15:05:45 +02:00
public function findOrCreateCustomer()
{
$existing = ClientGatewayToken::query()
->where('company_gateway_id', $this->company_gateway->id)
->where('client_id', $this->client->id)
->first();
if ($existing) {
return $this->gateway->customer()->find($existing->gateway_customer_reference);
}
$result = $this->gateway->customer()->create([
2021-04-29 15:05:45 +02:00
'firstName' => $this->client->present()->name,
'email' => $this->client->present()->email,
'phone' => $this->client->present()->phone,
]);
if ($result->success) {
$address = $this->gateway->address()->create([
'customerId' => $result->customer->id,
'firstName' => $this->client->present()->name,
'streetAddress' => $this->client->address1,
'postalCode' => $this->client->postal_code,
'countryCodeAlpha2' => $this->client->country ? $this->client->country->iso_3166_2 : '',
]);
2021-04-29 15:05:45 +02:00
return $result->customer;
}
}
2021-05-05 17:24:31 +02:00
public function refund(Payment $payment, $amount, $return_client_response = false)
{
$this->init();
2021-08-25 06:36:30 +02:00
try{
2021-08-26 15:38:28 +02:00
2021-05-05 17:24:31 +02:00
$response = $this->gateway->transaction()->refund($payment->transaction_reference, $amount);
2021-08-26 15:38:28 +02:00
2021-08-25 06:36:30 +02:00
} catch (Exception $e) {
$data = [
'transaction_reference' => null,
'transaction_response' => json_encode($e->getMessage()),
'success' => false,
'description' => $e->getMessage(),
'code' => $e->getCode(),
];
2021-08-26 15:38:28 +02:00
2021-08-25 06:36:30 +02:00
SystemLogger::dispatch(['server_response' => null, 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_BRAINTREE, $this->client, $this->client->company);
return $data;
}
2021-08-26 15:38:28 +02:00
2021-08-25 06:36:30 +02:00
if($response->success)
{
2021-05-06 16:38:54 +02:00
2021-08-25 06:36:30 +02:00
$data = [
2021-05-06 16:38:54 +02:00
'transaction_reference' => $response->id,
'transaction_response' => json_encode($response),
'success' => (bool)$response->success,
2021-05-06 16:38:54 +02:00
'description' => $response->status,
'code' => 0,
];
2021-08-25 06:36:30 +02:00
SystemLogger::dispatch(['server_response' => $response, 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_BRAINTREE, $this->client, $this->client->company);
return $data;
}
else{
$error = $response->errors->deepAll()[0];
$data = [
2021-05-06 16:38:54 +02:00
'transaction_reference' => null,
2021-08-25 06:36:30 +02:00
'transaction_response' => $response->errors->deepAll(),
2021-05-06 16:38:54 +02:00
'success' => false,
2021-08-25 06:36:30 +02:00
'description' => $error->message,
'code' => $error->code,
2021-05-06 16:38:54 +02:00
];
2021-08-25 06:36:30 +02:00
SystemLogger::dispatch(['server_response' => $response, 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_BRAINTREE, $this->client, $this->client->company);
return $data;
2021-05-05 17:24:31 +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) {
$description = "Invoice {$invoice->number} for {$amount} for client {$this->client->present()->name()}";
} else {
$description = "Payment with no invoice for amount {$amount} for client {$this->client->present()->name()}";
}
$this->init();
$result = $this->gateway->transaction()->sale([
'amount' => $amount,
'paymentMethodToken' => $cgt->token,
'deviceData' => '',
'options' => [
'submitForSettlement' => true
],
]);
if ($result->success) {
$this->confirmGatewayFee();
$data = [
'payment_type' => PaymentType::parseCardType(strtolower($result->transaction->creditCard['cardType'])),
'amount' => $amount,
'transaction_reference' => $result->transaction->id,
'gateway_type_id' => GatewayType::CREDIT_CARD,
];
$payment = $this->createPayment($data, Payment::STATUS_COMPLETED);
SystemLogger::dispatch(
['response' => $result, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_BRAINTREE,
2021-08-30 12:43:00 +02:00
$this->client,
$this->client->company,
);
return $payment;
}
2021-05-06 16:38:54 +02:00
if (!$result->success) {
$this->unWindGatewayFees($payment_hash);
PaymentFailureMailer::dispatch($this->client, $result->transaction->additionalProcessorResponse, $this->client->company, $this->payment_hash->data->amount_with_fee);
$message = [
'server_response' => $result,
'data' => $this->payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_BRAINTREE,
2021-08-30 12:43:00 +02:00
$this->client,
$this->client->company
);
return false;
}
}
2021-04-28 12:54:27 +02:00
}