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

526 lines
17 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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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;
2024-03-18 02:58:42 +01:00
use Exception;
use App\Models\Client;
use Braintree\Gateway;
use App\Models\Invoice;
2021-05-05 17:24:31 +02:00
use App\Models\Payment;
2024-03-18 02:58:42 +01:00
use App\Models\SystemLog;
use App\Models\GatewayType;
use App\Models\PaymentHash;
use App\Models\PaymentType;
2024-03-18 02:58:42 +01:00
use App\Models\ClientContact;
use App\Factory\ClientFactory;
2024-03-18 03:50:12 +01:00
use Illuminate\Support\Carbon;
2024-03-18 02:58:42 +01:00
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Factory\ClientContactFactory;
2021-08-26 15:38:28 +02:00
use App\PaymentDrivers\Braintree\ACH;
2024-03-18 02:58:42 +01:00
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Database\QueryException;
2021-05-03 14:10:46 +02:00
use App\PaymentDrivers\Braintree\PayPal;
2022-01-09 06:30:39 +01:00
use Illuminate\Support\Facades\Validator;
2024-03-18 02:58:42 +01:00
use App\PaymentDrivers\Braintree\CreditCard;
2021-04-28 12:54:27 +02:00
class BraintreePaymentDriver extends BaseDriver
{
2024-03-18 02:58:42 +01:00
use GeneratesCounter;
2021-04-28 12:54:27 +02:00
public $refundable = true;
public $token_billing = true;
public $can_authorise_credit_card = true;
2024-03-18 02:58:42 +01:00
private bool $completed = true;
2021-04-28 14:54:50 +02:00
/**
* @var Gateway;
2021-04-28 14:54:50 +02:00
*/
2021-10-25 17:44:55 +02:00
public Gateway $gateway;
2021-04-28 12:54:27 +02:00
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
];
2024-01-14 05:05:00 +01:00
public const SYSTEM_LOG_TYPE = SystemLog::TYPE_BRAINTREE;
2021-04-28 12:54:27 +02:00
2023-03-14 07:56:04 +01:00
public function init(): self
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'),
]);
2023-03-14 07:56:04 +01:00
return $this;
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);
}
2023-03-14 07:56:04 +01:00
$customer = $this->searchByEmail();
if ($customer) {
return $customer;
}
$result = $this->gateway->customer()->create([
2022-09-05 05:01:13 +02:00
'firstName' => $this->client->present()->name(),
'email' => $this->client->present()->email(),
'phone' => $this->client->present()->phone(),
2021-04-29 15:05:45 +02:00
]);
if ($result->success) {
2021-10-25 17:45:25 +02:00
$address = $this->gateway->address()->create([
'customerId' => $result->customer->id,
2022-09-05 05:01:13 +02:00
'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;
}
2023-02-16 02:36:09 +01:00
//12-08-2022 catch when the customer is not created.
$data = [
'transaction_reference' => null,
'transaction_response' => $result,
'success' => false,
'description' => 'Could not create customer',
'code' => 500,
];
2023-02-16 02:36:09 +01:00
SystemLogger::dispatch(['server_response' => $result, 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_BRAINTREE, $this->client, $this->client->company);
2021-04-29 15:05:45 +02:00
}
2023-03-14 07:56:04 +01:00
private function searchByEmail()
{
$result = $this->gateway->customer()->search([
\Braintree\CustomerSearch::email()->is($this->client->present()->email()),
]);
if ($result->maximumCount() > 0) {
return $result->firstItem();
}
}
2021-05-05 17:24:31 +02:00
public function refund(Payment $payment, $amount, $return_client_response = false)
{
$this->init();
2021-10-25 17:45:25 +02:00
try {
2021-05-05 17:24:31 +02:00
$response = $this->gateway->transaction()->refund($payment->transaction_reference, $amount);
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-10-25 17:45:25 +02:00
if ($response->success) {
2021-08-25 06:36:30 +02:00
$data = [
2021-10-25 17:44:34 +02:00
'transaction_reference' => $payment->transaction_reference,
2021-05-06 16:38:54 +02:00
'transaction_response' => json_encode($response),
2021-10-25 17:44:34 +02:00
'success' => (bool) $response->success,
'description' => ctrans('texts.plan_refunded'),
2021-05-06 16:38:54 +02:00
'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;
2021-10-25 17:45:25 +02:00
} else {
2021-08-25 06:36:30 +02:00
$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;
2023-08-06 09:03:12 +02:00
$invoice = Invoice::query()->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;
}
if (! $result->success) {
$this->unWindGatewayFees($payment_hash);
2021-10-17 12:40:40 +02:00
$this->sendFailureMail($result->transaction->additionalProcessorResponse);
$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;
}
}
2022-01-09 06:30:39 +01:00
2024-03-25 02:26:59 +01:00
/**
* Required fields for client to fill, to proceed with gateway actions.
*
* @return array[]
*/
public function getClientRequiredFields(): array
{
$fields = [];
$fields[] = ['name' => 'contact_first_name', 'label' => ctrans('texts.first_name'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'contact_last_name', 'label' => ctrans('texts.last_name'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'contact_email', 'label' => ctrans('texts.email'), 'type' => 'text', 'validation' => 'required,email:rfc'];
$fields[] = ['name' => 'client_address_line_1', 'label' => ctrans('texts.address1'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_city', 'label' => ctrans('texts.city'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_state', 'label' => ctrans('texts.state'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_country_id', 'label' => ctrans('texts.country'), 'type' => 'text', 'validation' => 'required'];
return $fields;
}
2022-01-09 06:30:39 +01:00
public function processWebhookRequest($request)
{
$validator = Validator::make($request->all(), [
'bt_signature' => ['required'],
'bt_payload' => ['required'],
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 422);
}
$this->init();
$webhookNotification = $this->gateway->webhookNotification()->parse(
2023-02-16 02:36:09 +01:00
$request->input('bt_signature'),
$request->input('bt_payload')
2022-01-09 06:30:39 +01:00
);
nlog('braintree webhook');
2022-01-09 07:01:41 +01:00
2024-03-15 04:21:42 +01:00
return response()->json([], 200);
}
2024-03-15 04:21:42 +01:00
public function auth(): bool
{
try {
$ct =$this->init()->gateway->clientToken()->generate();
2024-03-18 02:58:42 +01:00
2024-03-15 04:21:42 +01:00
return true;
}
catch(\Exception $e) {
2022-01-09 06:30:39 +01:00
2024-03-15 04:21:42 +01:00
}
return false;
2022-01-09 06:30:39 +01:00
}
2024-03-18 02:58:42 +01:00
private function find(string $customer_id = '') {
try {
return $this->init()->gateway->customer()->find($customer_id);
}
catch(\Exception $e){
return false;
}
return false;
}
private function findTokens(string $gateway_customer_reference)
{
return ClientGatewayToken::where('company_id', $this->company_gateway->company_id)
->where('gateway_customer_reference', $gateway_customer_reference)
->exists();
}
2024-03-18 03:50:12 +01:00
private function getToken(string $token, string $gateway_customer_reference)
{
return ClientGatewayToken::where('company_id', $this->company_gateway->company_id)
->where('gateway_customer_reference', $gateway_customer_reference)
->where('token', $token)
->first();
}
private function findClient(string $email) {
2024-03-18 02:58:42 +01:00
return ClientContact::where('company_id', $this->company_gateway->company_id)
->where('email', $email)
->first()->client ?? false;
}
private function addClientCards(Client $client, array $cards)
{
$this->client = $client;
foreach($cards as $card) {
2024-03-18 03:50:12 +01:00
if($this->getToken($card->token, $card->customerId) || Carbon::createFromDate($card->expirationYear, $card->expirationMonth, '1')->lt(now()))
continue;
2024-03-18 02:58:42 +01:00
$payment_meta = new \stdClass();
$payment_meta->exp_month = (string) $card->expirationMonth;
$payment_meta->exp_year = (string) $card->expirationYear;
$payment_meta->brand = (string) $card->cardType;
$payment_meta->last4 = (string) $card->last4;
$payment_meta->type = GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
'token' => $card->token,
'payment_method_id' => GatewayType::CREDIT_CARD,
];
$this->storeGatewayToken($data, ['gateway_customer_reference' => $card->customerId]);
nlog("adding card to customer payment profile");
}
}
public function createNinjaClient(mixed $customer): Client
{
$client = ClientFactory::create($this->company_gateway->company_id, $this->company_gateway->user_id);
$b_business_address = count($customer->addresses) >= 1 ? $customer->addresses[0] : false;
$b_shipping_address = count($customer->addresses) > 1 ? $customer->addresses[1] : false;
$import_client_data = [];
if($b_business_address) {
$braintree_address =
[
'address1' => $b_business_address->extendedAddress ?? '',
'address2' => $b_business_address->streetAddress ?? '',
'city' => $b_business_address->locality ?? '',
'postal_code' => $b_business_address->postalCode ?? '',
'state' => $b_business_address->region ?? '',
'country_id' => $b_business_address->countryCodeNumeric ?? '840',
];
$import_client_data = array_merge($import_client_data, $braintree_address);
}
if($b_shipping_address) {
$braintree_shipping_address =
[
'shipping_address1' => $b_shipping_address->extendedAddress ?? '',
'shipping_address2' => $b_shipping_address->streetAddress ?? '',
'shipping_city' => $b_shipping_address->locality ?? '',
'shipping_postal_code' => $b_shipping_address->postalCode ?? '',
'shipping_state' => $b_shipping_address->region ?? '',
'shipping_country_id' => $b_shipping_address->countryCodeNumeric ?? '840',
];
$import_client_data = array_merge($import_client_data, $braintree_shipping_address);
}
$client->fill($import_client_data);
$client->phone = $customer->phone ?? '';
$client->name = $customer->company ?? $customer->firstName;
$settings = $client->settings;
$settings->currency_id = (string) $this->company_gateway->company->settings->currency_id;
$client->settings = $settings;
$client->save();
$contact = ClientContactFactory::create($this->company_gateway->company_id, $this->company_gateway->user_id);
$contact->first_name = $customer->firstName ?? '';
$contact->last_name = $customer->lastName ?? '';
$contact->email = $customer->email ?? '';
$contact->phone = $customer->phone ?? '';
$contact->client_id = $client->id;
$contact->saveQuietly();
if (! isset($client->number) || empty($client->number)) {
$x = 1;
do {
try {
$client->number = $this->getNextClientNumber($client);
$client->saveQuietly();
$this->completed = false;
} catch (QueryException $e) {
$x++;
if ($x > 10) {
$this->completed = false;
}
}
} while ($this->completed);
} else {
$client->saveQuietly();
}
return $client;
}
public function importCustomers()
{
$customers = $this->init()->gateway->customer()->all();
foreach($customers as $c){
$customer = $this->find($c->id);
2024-03-18 03:50:12 +01:00
// nlog(count($customer->creditCards). " Exists for {$c->id}");
2024-03-18 02:58:42 +01:00
if(!$customer)
continue;
2024-03-18 03:50:12 +01:00
$client = $this->findClient($customer->email);
if(!$this->findTokens($c->id) && !$client) {
2024-03-18 02:58:42 +01:00
//customer is not referenced in the system - create client
$client = $this->createNinjaClient($customer);
2024-03-18 03:50:12 +01:00
// nlog("Creating new Client");
2024-03-18 02:58:42 +01:00
}
$this->addClientCards($client, $customer->creditCards);
2024-03-18 03:50:12 +01:00
// nlog("Adding Braintree Client: {$c->id} => {$client->id}");
2024-03-18 02:58:42 +01:00
}
}
2021-04-28 12:54:27 +02:00
}