2020-06-09 13:17:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-06-09 13:17:26 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-06-09 13:17:26 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-06-09 13:17:26 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers;
|
|
|
|
|
2020-06-10 03:06:37 +02:00
|
|
|
use App\Models\ClientGatewayToken;
|
2020-06-10 10:05:30 +02:00
|
|
|
use App\Models\GatewayType;
|
2020-06-24 03:15:51 +02:00
|
|
|
use App\Models\Payment;
|
2020-09-04 00:01:17 +02:00
|
|
|
use App\Models\PaymentHash;
|
2020-11-16 13:36:29 +01:00
|
|
|
use App\Models\SystemLog;
|
2020-06-10 03:06:37 +02:00
|
|
|
use App\PaymentDrivers\Authorize\AuthorizeCreditCard;
|
2020-06-10 10:05:30 +02:00
|
|
|
use App\PaymentDrivers\Authorize\AuthorizePaymentMethod;
|
2020-06-24 07:20:33 +02:00
|
|
|
use App\PaymentDrivers\Authorize\RefundTransaction;
|
2020-06-09 13:17:26 +02:00
|
|
|
use net\authorize\api\constants\ANetEnvironment;
|
2020-06-09 13:53:23 +02:00
|
|
|
use net\authorize\api\contract\v1\GetMerchantDetailsRequest;
|
2020-06-09 13:17:26 +02:00
|
|
|
use net\authorize\api\contract\v1\MerchantAuthenticationType;
|
2020-06-09 13:53:23 +02:00
|
|
|
use net\authorize\api\controller\GetMerchantDetailsController;
|
2020-06-09 13:17:26 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class BaseDriver.
|
2020-06-09 13:17:26 +02:00
|
|
|
*/
|
|
|
|
class AuthorizePaymentDriver extends BaseDriver
|
|
|
|
{
|
2020-06-09 13:53:23 +02:00
|
|
|
public $merchant_authentication;
|
2020-06-09 13:17:26 +02:00
|
|
|
|
2020-07-15 07:05:02 +02:00
|
|
|
public $token_billing = true;
|
|
|
|
|
|
|
|
public $can_authorise_credit_card = true;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-06-16 02:21:40 +02:00
|
|
|
public static $methods = [
|
|
|
|
GatewayType::CREDIT_CARD => AuthorizeCreditCard::class,
|
|
|
|
];
|
|
|
|
|
2020-11-16 13:36:29 +01:00
|
|
|
const SYSTEM_LOG_TYPE = SystemLog::TYPE_AUTHORIZE;
|
|
|
|
|
2020-06-16 14:41:56 +02:00
|
|
|
public function setPaymentMethod($payment_method_id)
|
|
|
|
{
|
|
|
|
$class = self::$methods[$payment_method_id];
|
2020-06-16 02:21:40 +02:00
|
|
|
|
|
|
|
$this->payment_method = new $class($this);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-06-10 10:05:30 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns the gateway types.
|
2020-06-10 10:05:30 +02:00
|
|
|
*/
|
2021-06-29 12:33:12 +02:00
|
|
|
public function gatewayTypes(): array
|
2020-06-10 10:05:30 +02:00
|
|
|
{
|
2021-06-29 12:25:52 +02:00
|
|
|
$types = [];
|
|
|
|
|
|
|
|
$types[] = GatewayType::CREDIT_CARD;
|
2020-06-10 10:05:30 +02:00
|
|
|
|
|
|
|
return $types;
|
|
|
|
}
|
2020-06-10 10:11:53 +02:00
|
|
|
|
2021-01-13 13:34:21 +01:00
|
|
|
public function getClientRequiredFields(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['name' => 'client_name', 'label' => ctrans('texts.name'), 'type' => 'text', 'validation' => 'required|min:2'],
|
|
|
|
['name' => 'contact_email', 'label' => ctrans('texts.email'), 'type' => 'text', 'validation' => 'required|email:rfc'],
|
2021-01-19 14:36:07 +01:00
|
|
|
['name' => 'client_address_line_1', 'label' => ctrans('texts.address1'), 'type' => 'text', 'validation' => 'required'],
|
|
|
|
['name' => 'client_city', 'label' => ctrans('texts.city'), 'type' => 'text', 'validation' => 'required'],
|
|
|
|
['name' => 'client_state', 'label' => ctrans('texts.state'), 'type' => 'text', 'validation' => 'required'],
|
|
|
|
['name' => 'client_postal_code', 'label' => ctrans('texts.postal_code'), 'type' => 'text', 'validation' => 'required'],
|
|
|
|
['name' => 'client_country_id', 'label' => ctrans('texts.country'), 'type' => 'select', 'validation' => 'required'],
|
2021-01-13 13:34:21 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-11-26 00:42:59 +01:00
|
|
|
public function authorizeView($payment_method)
|
|
|
|
{
|
2020-11-26 03:30:36 +01:00
|
|
|
return (new AuthorizePaymentMethod($this))->authorizeView();
|
2020-11-26 00:42:59 +01:00
|
|
|
}
|
|
|
|
|
2020-11-26 11:00:55 +01:00
|
|
|
public function authorizeResponse($request)
|
2020-11-26 00:42:59 +01:00
|
|
|
{
|
2020-11-26 11:00:55 +01:00
|
|
|
return (new AuthorizePaymentMethod($this))->authorizeResponseView($request);
|
2020-11-26 00:42:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function processPaymentView($data)
|
|
|
|
{
|
|
|
|
return $this->payment_method->processPaymentView($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processPaymentResponse($request)
|
|
|
|
{
|
|
|
|
return $this->payment_method->processPaymentResponse($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function refund(Payment $payment, $refund_amount, $return_client_response = false)
|
|
|
|
{
|
|
|
|
return (new RefundTransaction($this))->refundTransaction($payment, $refund_amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
|
2021-01-28 15:42:11 +01:00
|
|
|
{
|
2020-11-26 00:42:59 +01:00
|
|
|
$this->setPaymentMethod($cgt->gateway_type_id);
|
|
|
|
|
|
|
|
return $this->payment_method->tokenBilling($cgt, $payment_hash);
|
|
|
|
}
|
|
|
|
|
2020-06-09 13:17:26 +02:00
|
|
|
public function init()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
error_reporting(E_ALL & ~E_DEPRECATED);
|
2020-06-09 13:17:26 +02:00
|
|
|
|
2020-06-09 13:53:23 +02:00
|
|
|
$this->merchant_authentication = new MerchantAuthenticationType();
|
|
|
|
$this->merchant_authentication->setName($this->company_gateway->getConfigField('apiLoginId'));
|
|
|
|
$this->merchant_authentication->setTransactionKey($this->company_gateway->getConfigField('transactionKey'));
|
2020-06-09 13:17:26 +02:00
|
|
|
|
2020-06-09 13:53:23 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2020-06-09 13:17:26 +02:00
|
|
|
|
2020-06-09 13:53:23 +02:00
|
|
|
public function getPublicClientKey()
|
|
|
|
{
|
|
|
|
$request = new GetMerchantDetailsRequest();
|
|
|
|
$request->setMerchantAuthentication($this->merchant_authentication);
|
|
|
|
|
|
|
|
$controller = new GetMerchantDetailsController($request);
|
|
|
|
$response = $controller->executeWithApiResponse($this->mode());
|
|
|
|
|
|
|
|
return $response->getPublicClientKey();
|
2020-06-09 13:17:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-10 10:11:53 +02:00
|
|
|
public function mode()
|
2020-06-09 13:17:26 +02:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->company_gateway->getConfigField('testMode')) {
|
2020-06-09 13:53:23 +02:00
|
|
|
return ANetEnvironment::SANDBOX;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-06-09 13:17:26 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
return $env = ANetEnvironment::PRODUCTION;
|
2020-06-09 13:53:23 +02:00
|
|
|
}
|
|
|
|
|
2020-06-10 10:11:53 +02:00
|
|
|
public function findClientGatewayRecord() :?ClientGatewayToken
|
2020-06-10 03:06:37 +02:00
|
|
|
{
|
|
|
|
return ClientGatewayToken::where('client_id', $this->client->id)
|
|
|
|
->where('company_gateway_id', $this->company_gateway->id)
|
|
|
|
->first();
|
|
|
|
}
|
2020-07-08 04:20:44 +02:00
|
|
|
|
2020-09-18 10:01:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Detach payment method from Authorize.net.
|
2020-10-28 11:10:49 +01:00
|
|
|
*
|
|
|
|
* @param ClientGatewayToken $token
|
|
|
|
* @return void
|
2020-09-18 10:01:19 +02:00
|
|
|
*/
|
|
|
|
public function detach(ClientGatewayToken $token)
|
|
|
|
{
|
2021-01-25 13:16:43 +01:00
|
|
|
return (new AuthorizePaymentMethod($this))->deletePaymentProfile($token->gateway_customer_reference, $token->token);
|
2020-09-18 10:01:19 +02:00
|
|
|
}
|
2020-06-09 13:17:26 +02:00
|
|
|
}
|