2022-04-18 04:47:08 +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 ;
2022-08-15 15:51:21 +02:00
use App\Jobs\Util\SystemLogger ;
2022-04-18 04:47:08 +02:00
use App\Models\GatewayType ;
2023-02-16 02:36:09 +01:00
use App\Models\Payment ;
use App\Models\SystemLog ;
2022-04-18 04:47:08 +02:00
use App\PaymentDrivers\Forte\ACH ;
use App\PaymentDrivers\Forte\CreditCard ;
2023-02-16 02:36:09 +01:00
use App\Utils\Traits\MakesHash ;
2022-04-18 04:47:08 +02:00
class FortePaymentDriver extends BaseDriver
{
use MakesHash ;
public $refundable = true ; //does this gateway support refunds?
public $token_billing = true ; //does this gateway support token billing?
public $can_authorise_credit_card = true ; //does this gateway support authorizations?
public $gateway ; //initialized gateway
public $payment_method ; //initialized payment method
public static $methods = [
GatewayType :: CREDIT_CARD => CreditCard :: class ,
GatewayType :: BANK_TRANSFER => ACH :: class ,
];
/**
* Returns the gateway types .
*/
public function gatewayTypes () : array
{
$types = [];
2023-02-16 02:36:09 +01:00
$types [] = GatewayType :: CREDIT_CARD ;
$types [] = GatewayType :: BANK_TRANSFER ;
2022-04-18 04:47:08 +02:00
return $types ;
}
2024-01-14 05:05:00 +01:00
public const SYSTEM_LOG_TYPE = SystemLog :: TYPE_FORTE ; //define a constant for your gateway ie TYPE_YOUR_CUSTOM_GATEWAY - set the const in the SystemLog model
2022-04-18 04:47:08 +02:00
public function setPaymentMethod ( $payment_method_id )
{
$class = self :: $methods [ $payment_method_id ];
$this -> payment_method = new $class ( $this );
return $this ;
}
public function authorizeView ( array $data )
{
2022-08-15 15:51:21 +02:00
return $this -> payment_method -> authorizeView ( $data );
2022-04-18 04:47:08 +02:00
}
public function authorizeResponse ( $request )
{
2022-08-15 15:51:21 +02:00
return $this -> payment_method -> authorizeResponse ( $request );
2022-04-18 04:47:08 +02:00
}
public function processPaymentView ( array $data )
{
2022-08-15 15:51:21 +02:00
return $this -> payment_method -> paymentView ( $data );
2022-04-18 04:47:08 +02:00
}
public function processPaymentResponse ( $request )
{
2022-08-15 15:51:21 +02:00
return $this -> payment_method -> paymentResponse ( $request );
2022-04-18 04:47:08 +02:00
}
2022-08-15 15:51:21 +02:00
public function refund ( Payment $payment , $amount , $return_client_response = false )
{
$forte_base_uri = " https://sandbox.forte.net/api/v3/ " ;
2023-02-16 02:36:09 +01:00
if ( $this -> company_gateway -> getConfigField ( 'testMode' ) == false ) {
$forte_base_uri = " https://api.forte.net/v3/ " ;
}
2022-08-15 15:51:21 +02:00
$forte_api_access_id = $this -> company_gateway -> getConfigField ( 'apiAccessId' );
$forte_secure_key = $this -> company_gateway -> getConfigField ( 'secureKey' );
$forte_auth_organization_id = $this -> company_gateway -> getConfigField ( 'authOrganizationId' );
$forte_organization_id = $this -> company_gateway -> getConfigField ( 'organizationId' );
$forte_location_id = $this -> company_gateway -> getConfigField ( 'locationId' );
try {
$curl = curl_init ();
2023-02-16 02:36:09 +01:00
curl_setopt_array ( $curl , [
2022-08-15 15:51:21 +02:00
CURLOPT_URL => $forte_base_uri . 'organizations/' . $forte_organization_id . '/locations/' . $forte_location_id . '/transactions' ,
CURLOPT_RETURNTRANSFER => true ,
CURLOPT_ENCODING => '' ,
CURLOPT_MAXREDIRS => 10 ,
CURLOPT_TIMEOUT => 0 ,
CURLOPT_FOLLOWLOCATION => true ,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 ,
CURLOPT_CUSTOMREQUEST => 'POST' ,
2024-01-14 05:05:00 +01:00
CURLOPT_POSTFIELDS => ' {
2022-08-15 15:51:21 +02:00
" action " : " reverse " ,
" authorization_amount " : '.$amount.' ,
" original_transaction_id " : " '. $payment->transaction_reference .' " ,
" authorization_code " : " 9ZQ754 "
} ' ,
2023-02-16 02:36:09 +01:00
CURLOPT_HTTPHEADER => [
2022-08-15 15:51:21 +02:00
'Content-Type: application/json' ,
'X-Forte-Auth-Organization-Id: ' . $forte_organization_id ,
'Authorization: Basic ' . base64_encode ( $forte_api_access_id . ':' . $forte_secure_key )
2023-02-16 02:36:09 +01:00
],
]);
2022-08-15 15:51:21 +02:00
$response = curl_exec ( $curl );
$httpcode = curl_getinfo ( $curl , CURLINFO_HTTP_CODE );
curl_close ( $curl );
2024-01-14 05:05:00 +01:00
$response = json_decode ( $response );
2022-08-15 15:51:21 +02:00
} catch ( \Throwable $th ) {
$message = [
'action' => 'error' ,
'data' => $th ,
];
SystemLogger :: dispatch (
$message ,
SystemLog :: CATEGORY_GATEWAY_RESPONSE ,
SystemLog :: EVENT_GATEWAY_FAILURE ,
SystemLog :: TYPE_FORTE ,
$this -> client ,
$this -> client -> company ,
);
}
$message = [
'action' => 'refund' ,
'server_message' => $response -> response -> response_desc ,
'server_response' => $response ,
'data' => $payment -> paymentables ,
];
2024-01-14 05:05:00 +01:00
if ( $httpcode > 299 ) {
2022-08-15 15:51:21 +02:00
SystemLogger :: dispatch (
$message ,
SystemLog :: CATEGORY_GATEWAY_RESPONSE ,
SystemLog :: EVENT_GATEWAY_FAILURE ,
SystemLog :: TYPE_FORTE ,
$this -> client ,
$this -> client -> company ,
);
2024-01-14 05:05:00 +01:00
2022-08-15 15:51:21 +02:00
return [
'transaction_reference' => $payment -> transaction_reference ,
'transaction_response' => $response ,
'success' => false ,
'description' => $payment -> paymentables ,
'code' => 422 ,
];
}
SystemLogger :: dispatch (
$message ,
SystemLog :: CATEGORY_GATEWAY_RESPONSE ,
SystemLog :: EVENT_GATEWAY_SUCCESS ,
SystemLog :: TYPE_FORTE ,
$this -> client ,
$this -> client -> company ,
);
return [
'transaction_reference' => $payment -> transaction_reference ,
'transaction_response' => $response ,
'success' => $response -> response -> response_code == 'A01' ? true : false ,
'description' => $payment -> paymentables ,
'code' => $httpcode ,
];
}
2022-04-18 04:47:08 +02:00
// public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
// {
2022-08-15 15:51:21 +02:00
// return $this->payment_method->yourTokenBillingImplmentation();
2022-04-18 04:47:08 +02:00
// }
}