1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 01:11:34 +02:00
invoiceninja/app/PaymentDrivers/PayTrace/CreditCard.php

239 lines
7.4 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
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\PayTrace;
use App\Exceptions\PaymentFailed;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
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_driver;
public function __construct(PaytracePaymentDriver $paytrace_driver)
{
$this->paytrace_driver = $paytrace_driver;
}
public function authorizeView($data)
{
$data['client_key'] = $this->paytrace_driver->getAuthToken();
2021-07-21 06:23:33 +02:00
$data['gateway'] = $this->paytrace_driver;
2021-07-20 13:26:24 +02:00
return render('gateways.paytrace.authorize', $data);
}
2021-07-21 09:04:44 +02:00
// +"success": true
// +"response_code": 160
// +"status_message": "The customer profile for PLS5U60OoLUfQXzcmtJYNefPA0gTthzT/11 was successfully created."
// +"customer_id": "PLS5U60OoLUfQXzcmtJYNefPA0gTthzT"
2021-07-20 13:26:24 +02:00
2021-07-21 09:04:44 +02:00
//if(!$response->success)
//handle failure
2021-07-20 13:26:24 +02:00
public function authorizeResponse($request)
{
$data = $request->all();
2021-07-21 06:23:33 +02:00
2021-07-21 09:04:44 +02:00
$response = $this->createCustomer($data);
return redirect()->route('client.payment_methods.index');
}
// "_token" => "Vl1xHflBYQt9YFSaNCPTJKlY5x3rwcFE9kvkw71I"
// "company_gateway_id" => "1"
// "HPF_Token" => "e484a92c-90ed-4468-ac4d-da66824c75de"
// "enc_key" => "zqz6HMHCXALWdX5hyBqrIbSwU7TBZ0FTjjLB3Cp0FQY="
// "amount" => "Amount"
// "q" => "/client/payment_methods"
// "method" => "1"
// ]
// "customer_id":"customer789",
// "hpf_token":"e369847e-3027-4174-9161-fa0d4e98d318",
// "enc_key":"lI785yOBMet4Rt9o4NLXEyV84WBU3tdStExcsfoaOoo=",
// "integrator_id":"xxxxxxxxxx",
// "billing_address":{
// "name":"Mark Smith",
// "street_address":"8320 E. West St.",
// "city":"Spokane",
// "state":"WA",
// "zip":"85284"
// }
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' => '959195xd1CuC',
2021-07-21 09:04:44 +02:00
'billing_address' => $this->buildBillingAddress(),
2021-07-21 06:23:33 +02:00
];
2021-07-21 09:04:44 +02:00
$response = $this->paytrace_driver->gatewayRequest('/v1/customer/pt_protect_create', $post_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_driver->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_driver->gatewayRequest('/v1/customer/export', [
'integrator_id' => '959195xd1CuC',
'customer_id' => $customer_id,
// 'include_bin' => true,
]);
return $profile->customers[0];
}
2021-07-21 09:04:44 +02:00
private function buildBillingAddress()
{
return [
'name' => $this->paytrace_driver->client->present()->name(),
'street_address' => $this->paytrace_driver->client->address1,
'city' => $this->paytrace_driver->client->city,
'state' => $this->paytrace_driver->client->state,
'zip' => $this->paytrace_driver->client->postal_code
];
}
2021-07-20 13:26:24 +02:00
public function paymentView($data)
{
2021-07-21 09:04:44 +02:00
2021-07-21 08:27:02 +02:00
$data['client_key'] = $this->paytrace_driver->getAuthToken();
$data['gateway'] = $this->paytrace_driver;
2021-07-20 13:26:24 +02:00
return render('gateways.paytrace.pay', $data);
}
public function paymentResponse(Request $request)
{
$response_array = $request->all();
2021-07-21 08:27:02 +02:00
if($request->token)
2021-07-21 09:04:44 +02:00
$this->processTokenPayment($request->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-07-21 08:27:02 +02:00
2021-07-21 09:04:44 +02:00
$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 = [
'hpf_token' => $data['HPF_Token'],
'enc_key' => $data['enc_key'],
'integrator_id' => '959195xd1CuC',
'billing_address' => $this->buildBillingAddress(),
];
$response = $this->paytrace_driver->gatewayRequest('/v1/transactions/sale/pt_protect', $data);
2021-07-20 13:26:24 +02:00
2021-07-21 09:04:44 +02:00
if($response->success)
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 = [
'customer_id' => $request->token,
'integrator_id' => '959195xd1CuC',
'amount' => $request->input('amount_with_fee'),
];
$response = $this->paytrace_driver->gatewayRequest('/v1/transactions/sale/by_customer', $data);
if($response->success){
$this->paytrace_driver->logSuccessfulGatewayResponse(['response' => $response, 'data' => $this->paytrace_driver->payment_hash], SystemLog::TYPE_PAYTRACE);
return $this->processSuccessfulPayment($response);
}
return $this->processUnsuccessfulPayment($response);
}
2021-07-21 09:04:44 +02:00
private function processSuccessfulPayment($response)
2021-07-20 13:26:24 +02:00
{
2021-07-21 09:04:44 +02:00
$amount = array_sum(array_column($this->paytrace_driver->payment_hash->invoices(), 'amount')) + $this->paytrace_driver->payment_hash->fee_total;
2021-07-20 13:26:24 +02:00
2021-07-21 09:04:44 +02:00
$response = $response_array;
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
2021-07-21 08:27:02 +02:00
$payment = $this->paytrace_driver->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;
$error_code = property_exists($response, 'approval_message') ? $response->approval_message : 'Undefined code';
$data = [
'response' => $response,
'error' => $error,
'error_code' => $error_code,
];
return $this->paytrace_driver->processUnsuccessfulTransaction($data);
2021-07-20 13:26:24 +02:00
}
}