2020-06-16 02:21:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
|
2020-06-16 16:29:39 +02:00
|
|
|
use App\Events\Payment\PaymentWasCreated;
|
2020-06-16 15:31:08 +02:00
|
|
|
use App\Factory\PaymentFactory;
|
2020-06-16 16:29:39 +02:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
2020-06-16 02:31:05 +02:00
|
|
|
use App\Models\ClientGatewayToken;
|
2020-06-16 14:41:56 +02:00
|
|
|
use App\Models\GatewayType;
|
2020-06-16 15:31:08 +02:00
|
|
|
use App\Models\Payment;
|
2020-06-17 00:48:07 +02:00
|
|
|
use App\Models\PaymentType;
|
2020-06-16 16:29:39 +02:00
|
|
|
use App\Models\SystemLog;
|
2020-06-16 02:21:40 +02:00
|
|
|
use App\PaymentDrivers\AuthorizePaymentDriver;
|
2020-06-16 14:41:56 +02:00
|
|
|
use App\PaymentDrivers\Authorize\AuthorizeCreateCustomer;
|
2020-06-17 03:36:29 +02:00
|
|
|
use App\PaymentDrivers\Authorize\AuthorizePaymentMethod;
|
2020-06-16 14:41:56 +02:00
|
|
|
use App\PaymentDrivers\Authorize\ChargePaymentProfile;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-06-16 15:31:08 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2020-06-16 02:21:40 +02:00
|
|
|
|
|
|
|
/**
|
2020-06-16 06:01:10 +02:00
|
|
|
* Class AuthorizeCreditCard
|
|
|
|
* @package App\PaymentDrivers\Authorize
|
2020-06-16 02:21:40 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
class AuthorizeCreditCard
|
|
|
|
{
|
2020-06-16 14:41:56 +02:00
|
|
|
use MakesHash;
|
|
|
|
|
2020-06-16 02:21:40 +02:00
|
|
|
public $authorize;
|
|
|
|
|
|
|
|
public function __construct(AuthorizePaymentDriver $authorize)
|
|
|
|
{
|
|
|
|
$this->authorize = $authorize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processPaymentView($data)
|
|
|
|
{
|
2020-06-16 02:31:05 +02:00
|
|
|
$tokens = ClientGatewayToken::where('client_id', $this->authorize->client->id)
|
2020-06-16 05:49:45 +02:00
|
|
|
->where('company_gateway_id', $this->authorize->company_gateway->id)
|
2020-06-16 14:41:56 +02:00
|
|
|
->where('gateway_type_id', GatewayType::CREDIT_CARD)
|
2020-06-16 02:31:05 +02:00
|
|
|
->get();
|
|
|
|
|
|
|
|
$data['tokens'] = $tokens;
|
|
|
|
$data['gateway'] = $this->authorize->company_gateway;
|
2020-06-16 05:49:45 +02:00
|
|
|
$data['public_client_id'] = $this->authorize->init()->getPublicClientKey();
|
|
|
|
$data['api_login_id'] = $this->authorize->company_gateway->getConfigField('apiLoginId');
|
|
|
|
|
|
|
|
return render('gateways.authorize.credit_card_payment', $data);
|
2020-06-16 06:01:10 +02:00
|
|
|
|
2020-06-16 06:00:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 14:41:56 +02:00
|
|
|
public function processPaymentResponse($request)
|
|
|
|
{
|
|
|
|
if($request->token)
|
|
|
|
return $this->processTokenPayment($request);
|
|
|
|
|
|
|
|
$data = $request->all();
|
|
|
|
|
2020-06-17 03:36:29 +02:00
|
|
|
$authorise_create_customer = new AuthorizeCreateCustomer($this->authorize, $this->authorize->client);
|
2020-06-16 14:41:56 +02:00
|
|
|
|
2020-06-17 03:36:29 +02:00
|
|
|
$gateway_customer_reference = $authorise_create_customer->create($data);
|
2020-06-16 14:41:56 +02:00
|
|
|
|
|
|
|
info($gateway_customer_reference);
|
|
|
|
|
2020-06-17 03:36:29 +02:00
|
|
|
$authorise_payment_method = new AuthorizePaymentMethod($this->authorize);
|
|
|
|
|
2020-06-16 14:41:56 +02:00
|
|
|
$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();
|
2020-06-16 14:41:56 +02:00
|
|
|
|
2020-06-17 13:15:24 +02:00
|
|
|
info($request->input('store_card'));
|
|
|
|
|
|
|
|
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;
|
2020-06-16 14:41:56 +02:00
|
|
|
$client_gateway_token = $authorise_payment_method->createClientGatewayToken($payment_profile, $gateway_customer_reference);
|
2020-06-17 11:46:12 +02:00
|
|
|
}
|
2020-06-16 14:41:56 +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']);
|
2020-06-17 03:36:29 +02:00
|
|
|
|
|
|
|
return $this->handleResponse($data, $request);
|
2020-06-16 14:41:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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-16 15:31:08 +02:00
|
|
|
|
2020-06-17 00:48:07 +02:00
|
|
|
return $this->handleResponse($data, $request);
|
2020-06-16 15:31:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function handleResponse($data, $request)
|
|
|
|
{
|
|
|
|
//info(print_r( $response->getTransactionResponse()->getMessages(),1));
|
2020-06-16 14:41:56 +02:00
|
|
|
|
2020-06-16 15:31:08 +02:00
|
|
|
$response = $data['response'];
|
|
|
|
|
|
|
|
if($response != null && $response->getMessages()->getResultCode() == "Ok")
|
|
|
|
return $this->processSuccessfulResponse($data, $request);
|
|
|
|
|
|
|
|
return $this->processFailedResponse($data, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processSuccessfulResponse($data, $request)
|
|
|
|
{
|
2020-06-16 16:29:39 +02:00
|
|
|
$response = $data['response'];
|
2020-06-16 15:31:08 +02:00
|
|
|
//create a payment record and fire notifications and then return
|
|
|
|
|
|
|
|
$payment = PaymentFactory::create($this->authorize->client->company_id, $this->authorize->client->user_id);
|
2020-06-17 00:48:07 +02:00
|
|
|
$payment->client_id = $this->authorize->client->id;
|
2020-06-16 15:31:08 +02:00
|
|
|
$payment->company_gateway_id = $this->authorize->company_gateway->id;
|
2020-06-17 00:48:07 +02:00
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
|
|
|
$payment->type_id = PaymentType::CREDIT_CARD_OTHER;
|
2020-06-16 15:31:08 +02:00
|
|
|
$payment->currency_id = $this->authorize->client->getSetting('currency_id');
|
|
|
|
$payment->date = Carbon::now();
|
2020-06-16 16:29:39 +02:00
|
|
|
$payment->transaction_reference = $response->getTransactionResponse()->getTransId();
|
2020-06-17 13:15:24 +02:00
|
|
|
$payment->amount = $request->input('amount_with_fee');
|
2020-06-17 00:48:07 +02:00
|
|
|
$payment->currency_id = $this->authorize->client->getSetting('currency_id');
|
|
|
|
$payment->client->getNextPaymentNumber($this->authorize->client);
|
2020-06-16 16:29:39 +02:00
|
|
|
$payment->save();
|
2020-06-16 15:31:08 +02:00
|
|
|
|
2020-06-17 00:48:07 +02:00
|
|
|
|
2020-06-16 15:31:08 +02:00
|
|
|
$this->authorize->attachInvoices($payment, $request->hashed_ids);
|
|
|
|
|
2020-06-17 00:48:07 +02:00
|
|
|
$payment->service()->updateInvoicePayment();
|
2020-06-16 16:29:39 +02:00
|
|
|
|
|
|
|
event(new PaymentWasCreated($payment, $payment->company));
|
|
|
|
|
|
|
|
$logger_message = [
|
|
|
|
'server_response' => $response->getTransactionResponse()->getTransId(),
|
|
|
|
'data' => $this->formatGatewayResponse($data, $request)
|
|
|
|
];
|
|
|
|
|
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);
|
2020-06-16 16:29:39 +02:00
|
|
|
|
2020-06-16 15:31:08 +02:00
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
|
|
|
|
|
2020-06-16 14:41:56 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 16:29:39 +02:00
|
|
|
private function processFailedResponse($data, $request)
|
2020-06-17 13:15:24 +02:00
|
|
|
{ dd($data);
|
2020-06-17 00:48:07 +02:00
|
|
|
info(print_r($data,1));
|
2020-06-16 02:21:40 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 16:29:39 +02:00
|
|
|
private function formatGatewayResponse($data, $request)
|
|
|
|
{
|
|
|
|
$response = $data['response'];
|
|
|
|
|
|
|
|
return [
|
|
|
|
'transaction_reference' => $response->getTransactionResponse()->getTransId(),
|
|
|
|
'amount' => $request->input('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(),
|
2020-06-16 16:29:39 +02:00
|
|
|
'invoices' => $request->hashed_ids,
|
|
|
|
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-06-16 02:21:40 +02:00
|
|
|
}
|