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;
|
|
|
|
|
|
|
|
|
2021-04-28 14:54:50 +02:00
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
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\SystemLog;
|
|
|
|
use App\PaymentDrivers\Braintree\CreditCard;
|
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 \Braintree\Gateway;
|
|
|
|
*/
|
2021-04-28 12:54:27 +02:00
|
|
|
public $gateway;
|
|
|
|
|
|
|
|
public static $methods = [
|
|
|
|
GatewayType::CREDIT_CARD => CreditCard::class,
|
|
|
|
GatewayType::PAYPAL,
|
|
|
|
];
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-04-28 14:54:50 +02:00
|
|
|
$this->gateway = new \Braintree\Gateway([
|
|
|
|
'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];
|
|
|
|
|
|
|
|
$this->payment_method = new $class($this);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function gatewayTypes(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
GatewayType::CREDIT_CARD,
|
|
|
|
GatewayType::PAYPAL,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
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([
|
|
|
|
'firstName' => $this->client->present()->name,
|
|
|
|
'email' => $this->client->present()->email,
|
|
|
|
'phone' => $this->client->present()->phone,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($result->success) {
|
|
|
|
return $result->customer;
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 12:54:27 +02:00
|
|
|
}
|