1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/PaymentDrivers/PayTrace/CreditCard.php

241 lines
7.7 KiB
PHP
Raw Normal View History

2021-07-20 13:26:24 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2021-07-20 13:26:24 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\PayTrace;
use App\Exceptions\PaymentFailed;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
2021-07-21 09:44:35 +02:00
use App\Models\Invoice;
2021-07-20 13:26:24 +02:00
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\PayFastPaymentDriver;
use App\PaymentDrivers\PaytracePaymentDriver;
2021-07-21 08:27:02 +02:00
use App\Utils\Traits\MakesHash;
2021-07-20 13:26:24 +02:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
class CreditCard
{
2021-07-21 08:27:02 +02:00
use MakesHash;
2021-07-20 13:26:24 +02:00
public $paytrace;
2021-07-20 13:26:24 +02:00
public function __construct(PaytracePaymentDriver $paytrace)
2021-07-20 13:26:24 +02:00
{
$this->paytrace = $paytrace;
2021-07-20 13:26:24 +02:00
}
public function authorizeView($data)
{
$data['client_key'] = $this->paytrace->getAuthToken();
$data['gateway'] = $this->paytrace;
2021-07-20 13:26:24 +02:00
return render('gateways.paytrace.authorize', $data);
}
public function authorizeResponse($request)
{
2021-07-20 13:26:24 +02:00
$data = $request->all();
$response = $this->createCustomer($data);
2021-07-21 09:04:44 +02:00
return redirect()->route('client.payment_methods.index');
}
2021-07-21 09:04:44 +02:00
private function createCustomer($data)
{
2021-07-21 06:23:33 +02:00
$post_data = [
'customer_id' => Str::random(32),
'hpf_token' => $data['HPF_Token'],
'enc_key' => $data['enc_key'],
'integrator_id' => $this->paytrace->company_gateway->getConfigField('integratorId'),
2021-07-21 09:04:44 +02:00
'billing_address' => $this->buildBillingAddress(),
2021-07-21 06:23:33 +02:00
];
$response = $this->paytrace->gatewayRequest('/v1/customer/pt_protect_create', $post_data);
2021-07-21 07:34:20 +02:00
if (! $response->success) {
2022-04-20 00:51:27 +02:00
$error = 'Error creating customer in gateway';
$error_code = isset($response->response_code) ? $response->response_code : 'PT_ERR';
if (isset($response->errors)) {
foreach ($response->errors as $err) {
2022-04-20 00:51:27 +02:00
$error = end($err);
}
}
$data = [
'response' => $response,
'error' => $error,
'error_code' => $error_code,
];
return $this->paytrace->processUnsuccessfulTransaction($data);
}
2021-07-21 07:34:20 +02:00
$cgt = [];
$cgt['token'] = $response->customer_id;
$cgt['payment_method_id'] = GatewayType::CREDIT_CARD;
$profile = $this->getCustomerProfile($response->customer_id);
$payment_meta = new \stdClass;
$payment_meta->exp_month = $profile->credit_card->expiration_month;
$payment_meta->exp_year = $profile->credit_card->expiration_year;
$payment_meta->brand = 'CC';
$payment_meta->last4 = $profile->credit_card->masked_number;
$payment_meta->type = GatewayType::CREDIT_CARD;
$cgt['payment_meta'] = $payment_meta;
$token = $this->paytrace->storeGatewayToken($cgt, []);
2021-07-21 06:23:33 +02:00
2021-07-21 09:04:44 +02:00
return $response;
}
2021-07-21 07:34:20 +02:00
private function getCustomerProfile($customer_id)
{
$profile = $this->paytrace->gatewayRequest('/v1/customer/export', [
'integrator_id' => $this->paytrace->company_gateway->getConfigField('integratorId'),
2021-07-21 07:34:20 +02:00
'customer_id' => $customer_id,
]);
return $profile->customers[0];
}
2021-07-21 09:04:44 +02:00
private function buildBillingAddress()
{
2022-08-02 11:38:11 +02:00
$data = [
'name' => $this->paytrace->client->present()->name(),
'street_address' => $this->paytrace->client->address1,
'city' => $this->paytrace->client->city,
'state' => $this->paytrace->client->state,
'zip' => $this->paytrace->client->postal_code,
];
2022-08-02 11:38:11 +02:00
2022-08-12 01:23:52 +02:00
return $data;
2021-07-21 09:04:44 +02:00
}
2021-07-20 13:26:24 +02:00
public function paymentView($data)
{
$data['client_key'] = $this->paytrace->getAuthToken();
$data['gateway'] = $this->paytrace;
2021-07-20 13:26:24 +02:00
return render('gateways.paytrace.pay', $data);
}
public function paymentResponse(Request $request)
{
$response_array = $request->all();
if ($request->token) {
2021-08-09 06:33:08 +02:00
$token = ClientGatewayToken::find($this->decodePrimaryKey($request->token));
2021-08-09 06:33:08 +02:00
return $this->processTokenPayment($token->token, $request);
}
2021-07-21 08:27:02 +02:00
if ($request->has('store_card') && $request->input('store_card') === true) {
2021-07-21 09:04:44 +02:00
$response = $this->createCustomer($request->all());
2021-08-09 06:33:08 +02:00
return $this->processTokenPayment($response->customer_id, $request);
2021-07-21 08:27:02 +02:00
}
2021-07-20 13:26:24 +02:00
2021-07-21 09:04:44 +02:00
//process a regular charge here:
$data = [
2021-07-21 09:44:35 +02:00
'hpf_token' => $response_array['HPF_Token'],
'enc_key' => $response_array['enc_key'],
'integrator_id' => $this->paytrace->company_gateway->getConfigField('integratorId'),
2021-07-21 09:04:44 +02:00
'billing_address' => $this->buildBillingAddress(),
2021-07-21 09:44:35 +02:00
'amount' => $request->input('amount_with_fee'),
'invoice_id' => $this->harvestInvoiceId(),
];
2021-07-21 09:04:44 +02:00
$response = $this->paytrace->gatewayRequest('/v1/transactions/sale/pt_protect', $data);
2021-07-20 13:26:24 +02:00
if ($response->success) {
2021-07-21 09:04:44 +02:00
return $this->processSuccessfulPayment($response);
}
2021-07-20 13:26:24 +02:00
2021-07-21 09:04:44 +02:00
return $this->processUnsuccessfulPayment($response);
2021-07-20 13:26:24 +02:00
}
2021-07-21 09:04:44 +02:00
public function processTokenPayment($token, $request)
2021-07-21 08:27:02 +02:00
{
$data = [
2021-08-09 06:33:08 +02:00
'customer_id' => $token,
'integrator_id' => $this->paytrace->company_gateway->getConfigField('integratorId'),
2021-07-21 08:27:02 +02:00
'amount' => $request->input('amount_with_fee'),
];
$response = $this->paytrace->gatewayRequest('/v1/transactions/sale/by_customer', $data);
2021-07-21 08:27:02 +02:00
if ($response->success) {
$this->paytrace->logSuccessfulGatewayResponse(['response' => $response, 'data' => $this->paytrace->payment_hash], SystemLog::TYPE_PAYTRACE);
2021-07-21 08:27:02 +02:00
return $this->processSuccessfulPayment($response);
}
return $this->processUnsuccessfulPayment($response);
}
2021-07-21 09:44:35 +02:00
private function harvestInvoiceId()
{
$_invoice = collect($this->paytrace->payment_hash->data->invoices)->first();
2021-07-21 09:44:35 +02:00
$invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($_invoice->invoice_id));
if ($invoice) {
return ctrans('texts.invoice_number').'# '.$invoice->number;
}
2021-07-21 09:44:35 +02:00
return ctrans('texts.invoice_number').'####';
2021-07-21 09:44:35 +02:00
}
2021-07-21 08:27:02 +02:00
2021-07-21 09:04:44 +02:00
private function processSuccessfulPayment($response)
2021-07-20 13:26:24 +02:00
{
$amount = array_sum(array_column($this->paytrace->payment_hash->invoices(), 'amount')) + $this->paytrace->payment_hash->fee_total;
2021-07-20 13:26:24 +02:00
2021-07-21 09:04:44 +02:00
$payment_record = [];
$payment_record['amount'] = $amount;
$payment_record['payment_type'] = PaymentType::CREDIT_CARD_OTHER;
$payment_record['gateway_type_id'] = GatewayType::CREDIT_CARD;
$payment_record['transaction_reference'] = $response->transaction_id;
2021-07-20 13:26:24 +02:00
$payment = $this->paytrace->createPayment($payment_record, Payment::STATUS_COMPLETED);
2021-07-20 13:26:24 +02:00
2021-07-21 09:04:44 +02:00
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
2021-07-20 13:26:24 +02:00
}
2021-07-21 09:04:44 +02:00
private function processUnsuccessfulPayment($response)
2021-07-20 13:26:24 +02:00
{
2021-07-21 09:04:44 +02:00
$error = $response->status_message;
2021-07-21 09:44:35 +02:00
if (property_exists($response, 'approval_message') && $response->approval_message) {
2021-07-21 09:44:35 +02:00
$error .= " - {$response->approval_message}";
}
2021-07-21 09:44:35 +02:00
2021-07-21 09:04:44 +02:00
$error_code = property_exists($response, 'approval_message') ? $response->approval_message : 'Undefined code';
$data = [
'response' => $response,
'error' => $error,
'error_code' => $error_code,
];
return $this->paytrace->processUnsuccessfulTransaction($data);
2021-07-20 13:26:24 +02:00
}
}