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://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Forte;
|
|
|
|
|
2024-08-14 05:32:26 +02:00
|
|
|
use App\Exceptions\PaymentFailed;
|
2023-02-16 02:36:09 +01:00
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
|
|
|
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;
|
2022-04-19 14:17:28 +02:00
|
|
|
use App\Models\PaymentHash;
|
2022-04-19 07:57:40 +02:00
|
|
|
use App\Models\PaymentType;
|
2023-02-16 02:36:09 +01:00
|
|
|
use App\Models\SystemLog;
|
|
|
|
use App\PaymentDrivers\FortePaymentDriver;
|
2022-04-18 04:47:08 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
|
|
|
class CreditCard
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-04-18 04:47:08 +02:00
|
|
|
public $forte;
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
private $forte_base_uri = "";
|
|
|
|
private $forte_api_access_id = "";
|
|
|
|
private $forte_secure_key = "";
|
|
|
|
private $forte_auth_organization_id = "";
|
|
|
|
private $forte_organization_id = "";
|
|
|
|
private $forte_location_id = "";
|
|
|
|
|
2022-04-18 04:47:08 +02:00
|
|
|
public function __construct(FortePaymentDriver $forte)
|
|
|
|
{
|
|
|
|
$this->forte = $forte;
|
2022-04-18 14:23:45 +02:00
|
|
|
|
2022-04-21 08:32:01 +02:00
|
|
|
$this->forte_base_uri = "https://sandbox.forte.net/api/v3/";
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->forte->company_gateway->getConfigField('testMode') == false) {
|
|
|
|
$this->forte_base_uri = "https://api.forte.net/v3/";
|
|
|
|
}
|
2022-04-18 14:23:45 +02:00
|
|
|
$this->forte_api_access_id = $this->forte->company_gateway->getConfigField('apiAccessId');
|
|
|
|
$this->forte_secure_key = $this->forte->company_gateway->getConfigField('secureKey');
|
|
|
|
$this->forte_auth_organization_id = $this->forte->company_gateway->getConfigField('authOrganizationId');
|
|
|
|
$this->forte_organization_id = $this->forte->company_gateway->getConfigField('organizationId');
|
|
|
|
$this->forte_location_id = $this->forte->company_gateway->getConfigField('locationId');
|
2022-04-18 04:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeView(array $data)
|
|
|
|
{
|
2022-10-06 23:20:07 +02:00
|
|
|
$data['gateway'] = $this->forte;
|
2022-04-18 04:47:08 +02:00
|
|
|
return render('gateways.forte.credit_card.authorize', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeResponse($request)
|
|
|
|
{
|
2024-08-14 05:32:26 +02:00
|
|
|
$cst = $this->forte->findOrCreateCustomer();
|
2022-04-18 04:47:08 +02:00
|
|
|
|
|
|
|
$data = [
|
2024-08-14 05:32:26 +02:00
|
|
|
"label" => $request->card_holders_name." " .$request->card_type,
|
|
|
|
"notes" => $request->card_holders_name." " .$request->card_type,
|
|
|
|
"card" => [
|
|
|
|
"one_time_token" => $request->one_time_token,
|
|
|
|
"name_on_card" => $request->card_holders_name
|
|
|
|
],
|
2022-04-18 04:47:08 +02:00
|
|
|
];
|
|
|
|
|
2024-08-14 05:32:26 +02:00
|
|
|
$response = $this->forte->stubRequest()
|
|
|
|
->post("{$this->forte->baseUri()}/organizations/{$this->forte->getOrganisationId()}/locations/{$this->forte->getLocationId()}/customers/{$cst}/paymethods", $data);
|
|
|
|
|
|
|
|
if($response->successful()){
|
|
|
|
|
|
|
|
$token = $response->object();
|
|
|
|
|
|
|
|
$payment_meta = new \stdClass();
|
|
|
|
$payment_meta->exp_month = (string) $request->expire_month;
|
|
|
|
$payment_meta->exp_year = (string) $request->expire_year;
|
|
|
|
$payment_meta->brand = (string) $request->card_brand;
|
|
|
|
$payment_meta->last4 = (string) $request->last_4;
|
|
|
|
$payment_meta->type = GatewayType::CREDIT_CARD;
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'payment_meta' => $payment_meta,
|
|
|
|
'token' => $token->paymethod_token,
|
|
|
|
'payment_method_id' => $request->payment_method_id,
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->forte->storeGatewayToken($data, ['gateway_customer_reference' => $cst]);
|
|
|
|
|
|
|
|
return redirect()->route('client.payment_methods.index')->withSuccess('Payment Method added.');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$error = $response->object();
|
|
|
|
$message = [
|
|
|
|
'server_message' => $error->response->response_desc,
|
|
|
|
'server_response' => $response->json(),
|
|
|
|
'data' => $data,
|
|
|
|
];
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
$message,
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_FORTE,
|
2024-08-27 03:49:01 +02:00
|
|
|
$this->forte->client,
|
|
|
|
$this->forte->client->company,
|
2024-08-14 05:32:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
throw new \App\Exceptions\PaymentFailed("Unable to store payment method: {$error->response->response_desc}", 400);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createPaymentToken($request)
|
|
|
|
{
|
|
|
|
$cst = $this->forte->findOrCreateCustomer();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
"label" => $this->forte->client->present()->name(),
|
|
|
|
"notes" => $this->forte->client->present()->name(),
|
|
|
|
"card" => [
|
|
|
|
"one_time_token" => $request->payment_token,
|
|
|
|
"name_on_card" => $this->forte->client->present()->first_name(). " ". $this->forte->client->present()->last_name()
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->forte->stubRequest()
|
|
|
|
->post("{$this->forte->baseUri()}/organizations/{$this->forte->getOrganisationId()}/locations/{$this->forte->getLocationId()}/customers/{$cst}/paymethods", $data);
|
|
|
|
|
|
|
|
if($response->successful()){
|
|
|
|
|
|
|
|
$token = $response->object();
|
|
|
|
|
|
|
|
$payment_meta = new \stdClass();
|
|
|
|
$payment_meta->exp_month = (string) $request->expire_month;
|
|
|
|
$payment_meta->exp_year = (string) $request->expire_year;
|
|
|
|
$payment_meta->brand = (string) $request->card_brand;
|
|
|
|
$payment_meta->last4 = (string) $request->last_4;
|
|
|
|
$payment_meta->type = GatewayType::CREDIT_CARD;
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'payment_meta' => $payment_meta,
|
|
|
|
'token' => $token->paymethod_token,
|
|
|
|
'payment_method_id' => $request->payment_method_id,
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->forte->storeGatewayToken($data, ['gateway_customer_reference' => $cst]);
|
|
|
|
}
|
2022-04-18 04:47:08 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paymentView(array $data)
|
|
|
|
{
|
|
|
|
$this->forte->payment_hash->data = array_merge((array) $this->forte->payment_hash->data, $data);
|
|
|
|
$this->forte->payment_hash->save();
|
|
|
|
|
2022-10-06 23:20:07 +02:00
|
|
|
$data['gateway'] = $this->forte;
|
2022-04-18 04:47:08 +02:00
|
|
|
return render('gateways.forte.credit_card.pay', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paymentResponse(PaymentResponseRequest $request)
|
|
|
|
{
|
2024-08-14 05:32:26 +02:00
|
|
|
|
2022-11-10 07:28:00 +01:00
|
|
|
$payment_hash = PaymentHash::where('hash', $request->input('payment_hash'))->firstOrFail();
|
2024-08-14 05:32:26 +02:00
|
|
|
|
|
|
|
if(strlen($request->token ?? '') > 3){
|
|
|
|
|
|
|
|
|
|
|
|
$cgt = \App\Models\ClientGatewayToken::find($this->decodePrimaryKey($request->token));
|
|
|
|
|
|
|
|
$payment = $this->forte->tokenBilling($cgt, $payment_hash);
|
|
|
|
|
|
|
|
return redirect()->route('client.payments.show', ['payment' => $payment->hashed_id]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-20 12:59:19 +02:00
|
|
|
$amount_with_fee = $payment_hash->data->total->amount_with_fee;
|
2022-06-23 08:33:57 +02:00
|
|
|
$invoice_totals = $payment_hash->data->total->invoice_totals;
|
2022-11-18 02:32:40 +01:00
|
|
|
$fee_total = null;
|
2022-06-24 08:06:44 +02:00
|
|
|
|
2022-11-10 07:28:00 +01:00
|
|
|
$fees_and_limits = $this->forte->company_gateway->getFeesAndLimits(GatewayType::CREDIT_CARD);
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (property_exists($fees_and_limits, 'fee_percent') && $fees_and_limits->fee_percent > 0) {
|
2022-11-18 02:32:40 +01:00
|
|
|
$fee_total = 0;
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
for ($i = ($invoice_totals * 100) ; $i < ($amount_with_fee * 100); $i++) {
|
|
|
|
$calculated_fee = (3 * $i) / 100;
|
|
|
|
$calculated_amount_with_fee = round(($i + $calculated_fee) / 100, 2);
|
2022-11-10 07:28:00 +01:00
|
|
|
if ($calculated_amount_with_fee == $amount_with_fee) {
|
2023-02-16 02:36:09 +01:00
|
|
|
$fee_total = round($calculated_fee / 100, 2);
|
2022-11-10 07:28:00 +01:00
|
|
|
$amount_with_fee = $calculated_amount_with_fee;
|
|
|
|
break;
|
|
|
|
}
|
2022-06-23 08:33:57 +02:00
|
|
|
}
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2022-04-18 04:47:08 +02:00
|
|
|
try {
|
|
|
|
$curl = curl_init();
|
2022-04-25 07:55:10 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
curl_setopt_array($curl, [
|
2022-04-25 07:55:10 +02:00
|
|
|
CURLOPT_URL => $this->forte_base_uri.'organizations/'.$this->forte_organization_id.'/locations/'.$this->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-04-25 07:55:10 +02:00
|
|
|
"action":"sale",
|
2022-06-20 12:59:19 +02:00
|
|
|
"authorization_amount":'.$amount_with_fee.',
|
|
|
|
"service_fee_amount":'.$fee_total.',
|
2022-04-25 07:55:10 +02:00
|
|
|
"billing_address":{
|
2024-08-14 05:32:26 +02:00
|
|
|
"first_name":"'.$this->forte->client->present()->first_name().'",
|
|
|
|
"last_name":"'.$this->forte->client->present()->last_name().'"
|
2022-04-25 07:55:10 +02:00
|
|
|
},
|
|
|
|
"card":{
|
|
|
|
"one_time_token":"'.$request->payment_token.'"
|
|
|
|
}
|
|
|
|
}',
|
2023-02-16 02:36:09 +01:00
|
|
|
CURLOPT_HTTPHEADER => [
|
2022-04-25 07:55:10 +02:00
|
|
|
'Content-Type: application/json',
|
|
|
|
'X-Forte-Auth-Organization-Id: '.$this->forte_organization_id,
|
|
|
|
'Authorization: Basic '.base64_encode($this->forte_api_access_id.':'.$this->forte_secure_key)
|
2023-02-16 02:36:09 +01:00
|
|
|
],
|
|
|
|
]);
|
2022-04-18 04:47:08 +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);
|
2024-08-14 05:32:26 +02:00
|
|
|
|
2022-04-18 04:47:08 +02:00
|
|
|
} catch (\Throwable $th) {
|
|
|
|
throw $th;
|
|
|
|
}
|
2022-08-04 16:02:15 +02:00
|
|
|
|
|
|
|
$message = [
|
|
|
|
'server_message' => $response->response->response_desc,
|
|
|
|
'server_response' => $response,
|
|
|
|
'data' => $payment_hash->data,
|
|
|
|
];
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
if ($httpcode > 299) {
|
2022-08-04 16:02:15 +02:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
$message,
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_FORTE,
|
|
|
|
$this->forte->client,
|
|
|
|
$this->forte->client->company,
|
|
|
|
);
|
2022-04-18 04:47:08 +02:00
|
|
|
$error = Validator::make([], []);
|
|
|
|
$error->getMessageBag()->add('gateway_error', $response->response->response_desc);
|
|
|
|
return redirect('client/invoices')->withErrors($error);
|
|
|
|
}
|
|
|
|
|
2022-08-04 16:02:15 +02:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
$message,
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_FORTE,
|
|
|
|
$this->forte->client,
|
|
|
|
$this->forte->client->company,
|
|
|
|
);
|
|
|
|
|
2022-04-19 14:17:28 +02:00
|
|
|
$data = [
|
|
|
|
'payment_method' => $request->payment_method_id,
|
|
|
|
'payment_type' => PaymentType::parseCardType(strtolower($request->card_brand)) ?: PaymentType::CREDIT_CARD_OTHER,
|
|
|
|
'amount' => $payment_hash->data->amount_with_fee,
|
|
|
|
'transaction_reference' => $response->transaction_id,
|
|
|
|
'gateway_type_id' => GatewayType::CREDIT_CARD,
|
|
|
|
];
|
2024-01-14 05:05:00 +01:00
|
|
|
$payment = $this->forte->createPayment($data, Payment::STATUS_COMPLETED);
|
2024-08-14 05:32:26 +02:00
|
|
|
|
|
|
|
if($request->store_card) {
|
|
|
|
$this->createPaymentToken($request);
|
|
|
|
}
|
|
|
|
|
2024-07-24 04:27:09 +02:00
|
|
|
return redirect()->route('client.payments.show', ['payment' => $payment->hashed_id]);
|
|
|
|
|
2022-04-18 04:47:08 +02:00
|
|
|
}
|
|
|
|
}
|