1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php

260 lines
8.4 KiB
PHP
Raw Normal View History

2020-06-16 02:21:40 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-06-16 02:21:40 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers\Authorize;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Jobs\Util\SystemLogger;
2020-06-16 02:31:05 +02:00
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Payment;
2020-09-02 11:36:32 +02:00
use App\Models\PaymentHash;
2020-06-17 00:48:07 +02:00
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\Authorize\AuthorizeCreateCustomer;
use App\PaymentDrivers\Authorize\AuthorizePaymentMethod;
use App\PaymentDrivers\Authorize\ChargePaymentProfile;
use App\PaymentDrivers\AuthorizePaymentDriver;
2020-07-08 14:02:16 +02:00
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Carbon;
2020-06-16 02:21:40 +02:00
/**
* Class AuthorizeCreditCard.
2020-06-16 02:21:40 +02:00
*/
class AuthorizeCreditCard
{
use MakesHash;
2020-06-16 02:21:40 +02:00
public $authorize;
public function __construct(AuthorizePaymentDriver $authorize)
{
$this->authorize = $authorize;
}
public function processPaymentView($data)
{
$tokens = ClientGatewayToken::where('client_id', $this->authorize->client->id)
->where('company_gateway_id', $this->authorize->company_gateway->id)
->where('gateway_type_id', GatewayType::CREDIT_CARD)
->get();
$data['tokens'] = $tokens;
$data['gateway'] = $this->authorize->company_gateway;
$data['public_client_id'] = $this->authorize->init()->getPublicClientKey();
$data['api_login_id'] = $this->authorize->company_gateway->getConfigField('apiLoginId');
2020-06-16 06:01:10 +02:00
return render('gateways.authorize.credit_card_payment', $data);
}
public function processPaymentResponse($request)
{
if ($request->token) {
return $this->processTokenPayment($request);
}
$data = $request->all();
$authorise_create_customer = new AuthorizeCreateCustomer($this->authorize, $this->authorize->client);
$gateway_customer_reference = $authorise_create_customer->create($data);
$authorise_payment_method = new AuthorizePaymentMethod($this->authorize);
$payment_profile = $authorise_payment_method->addPaymentMethodToClient($gateway_customer_reference, $data);
2020-06-17 13:15:24 +02:00
$payment_profile_id = $payment_profile->getPaymentProfile()->getCustomerPaymentProfileId();
if ($request->has('store_card') && $request->input('store_card') === 'true') {
2020-06-17 11:46:12 +02:00
$authorise_payment_method->payment_method = GatewayType::CREDIT_CARD;
$client_gateway_token = $authorise_payment_method->createClientGatewayToken($payment_profile, $gateway_customer_reference);
2020-06-17 11:46:12 +02:00
}
2020-06-17 13:15:24 +02:00
$data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($gateway_customer_reference, $payment_profile_id, $data['amount_with_fee']);
return $this->handleResponse($data, $request);
}
private function processTokenPayment($request)
{
$client_gateway_token = ClientGatewayToken::find($this->decodePrimaryKey($request->token));
2020-06-17 13:15:24 +02:00
$data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($client_gateway_token->gateway_customer_reference, $client_gateway_token->token, $request->input('amount_with_fee'));
2020-06-17 00:48:07 +02:00
return $this->handleResponse($data, $request);
}
private function tokenBilling($cgt, $payment_hash)
{
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($cgt->gateway_customer_reference, $cgt->token, $amount);
2020-07-13 06:46:16 +02:00
2020-11-26 05:43:53 +01:00
/*Refactor and push to BaseDriver*/
if ($data['response'] != null && $data['response']->getMessages()->getResultCode() == 'Ok') {
2020-11-26 05:43:53 +01:00
$payment_record = [];
$payment_record['amount'] = $amount;
$payment_record['payment_type'] = PaymentType::CREDIT_CARD_OTHER;;
$payment_record['transaction_reference'] = $data['response']->getTransactionResponse()->getTransId();
$this->authorize->createPayment($payment_record);
// $payment = $this->createPaymentRecord($data, $amount);
// $payment->meta = $cgt->meta;
// $payment->save();
2020-09-10 03:52:17 +02:00
2020-11-26 05:43:53 +01:00
// $payment_hash->payment_id = $payment->id;
// $payment_hash->save();
2020-11-26 05:43:53 +01:00
// $this->authorize->attachInvoices($payment, $payment_hash);
// $payment->service()->updateInvoicePayment($payment_hash);
2020-07-13 06:46:16 +02:00
2020-11-26 05:43:53 +01:00
// event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
2020-07-13 06:56:07 +02:00
$vars = [
'hashed_ids' => $invoice->hashed_id,
'amount' => $amount,
2020-07-13 06:56:07 +02:00
];
$logger_message = [
'server_response' => $response->getTransactionResponse()->getTransId(),
'data' => $this->formatGatewayResponse($data, $vars),
2020-07-13 06:56:07 +02:00
];
SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_AUTHORIZE, $this->authorize->client);
return true;
} else {
2020-07-14 14:50:16 +02:00
return false;
2020-07-13 06:46:16 +02:00
}
2020-07-08 08:54:16 +02:00
}
2020-11-26 05:43:53 +01:00
2020-07-08 08:54:16 +02:00
private function handleResponse($data, $request)
{
$response = $data['response'];
if ($response != null && $response->getMessages()->getResultCode() == 'Ok') {
2020-11-25 11:30:00 +01:00
$this->authorize->confirmGatewayFee($request);
return $this->processSuccessfulResponse($data, $request);
}
return $this->processFailedResponse($data, $request);
}
private function storePayment($payment_hash, $data)
{
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$payment = $this->createPaymentRecord($data, $amount);
2020-09-10 03:52:17 +02:00
$payment_hash->payment_id = $payment->id;
$payment_hash->save();
$this->authorize->attachInvoices($payment, $payment_hash);
$payment->service()->updateInvoicePayment($payment_hash);
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
return $payment;
}
2020-07-13 06:46:16 +02:00
private function createPaymentRecord($data, $amount) :?Payment
{
$response = $data['response'];
//create a payment record
2020-08-17 00:58:52 +02:00
$payment = $this->authorize->createPayment($data['response']);
2020-06-27 04:35:11 +02:00
$payment->gateway_type_id = GatewayType::CREDIT_CARD;
2020-06-17 00:48:07 +02:00
$payment->type_id = PaymentType::CREDIT_CARD_OTHER;
$payment->transaction_reference = $response->getTransactionResponse()->getTransId();
$payment->amount = $amount;
$payment->save();
2020-07-13 06:46:16 +02:00
return $payment;
}
private function processSuccessfulResponse($data, $request)
{
$payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->input('payment_hash')])->firstOrFail();
$payment = $this->storePayment($payment_hash, $data);
2020-07-13 06:56:07 +02:00
$vars = [
2020-09-03 13:22:49 +02:00
'invoices' => $payment_hash->invoices(),
'amount' => array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total,
2020-07-13 06:56:07 +02:00
];
$logger_message = [
2020-08-13 04:33:40 +02:00
'server_response' => $data['response']->getTransactionResponse()->getTransId(),
'data' => $this->formatGatewayResponse($data, $vars),
];
2020-06-17 00:48:07 +02:00
SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_AUTHORIZE, $this->authorize->client);
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
}
private function processFailedResponse($data, $request)
{
2020-08-13 04:30:45 +02:00
//dd($data);
2020-10-22 12:14:14 +02:00
// info(print_r($data, 1));
2020-06-16 02:21:40 +02:00
}
2020-07-13 06:56:07 +02:00
private function formatGatewayResponse($data, $vars)
{
$response = $data['response'];
return [
'transaction_reference' => $response->getTransactionResponse()->getTransId(),
2020-07-13 06:56:07 +02:00
'amount' => $vars['amount'],
'auth_code' => $response->getTransactionResponse()->getAuthCode(),
2020-06-17 00:48:07 +02:00
'code' => $response->getTransactionResponse()->getMessages()[0]->getCode(),
'description' => $response->getTransactionResponse()->getMessages()[0]->getDescription(),
'invoices' => $vars['invoices'],
];
}
}