1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/PaymentDrivers/CBAPowerBoard/CreditCard.php

492 lines
17 KiB
PHP
Raw Normal View History

2024-09-06 06:26:13 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\CBAPowerBoard;
use App\Models\Payment;
use App\Models\SystemLog;
2024-09-08 11:06:31 +02:00
use App\Models\GatewayType;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Jobs\Util\SystemLogger;
use App\Exceptions\PaymentFailed;
2024-09-09 10:50:07 +02:00
use Illuminate\Http\Client\RequestException;
2024-09-06 06:26:13 +02:00
use App\PaymentDrivers\CBAPowerBoardPaymentDriver;
2024-09-08 11:06:31 +02:00
use App\PaymentDrivers\CBAPowerBoard\Models\Charge;
2024-09-06 06:26:13 +02:00
use App\PaymentDrivers\Common\LivewireMethodInterface;
2024-09-08 11:06:31 +02:00
use App\PaymentDrivers\CBAPowerBoard\Models\PaymentSource;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
2024-09-10 00:40:40 +02:00
use App\PaymentDrivers\CBAPowerBoard\Models\Gateway;
2024-09-06 06:26:13 +02:00
class CreditCard implements LivewireMethodInterface
{
2024-09-10 00:40:40 +02:00
private Gateway $cba_gateway;
2024-09-06 06:26:13 +02:00
public function __construct(public CBAPowerBoardPaymentDriver $powerboard)
{
2024-09-10 00:40:40 +02:00
$this->cba_gateway = $this->powerboard->settings()->getPaymentGatewayConfiguration(GatewayType::CREDIT_CARD);
2024-09-06 06:26:13 +02:00
}
public function authorizeView(array $data)
{
2024-09-09 11:56:47 +02:00
$data['payment_method_id'] = GatewayType::CREDIT_CARD;
$data['threeds'] = $this->powerboard->company_gateway->getConfigField('threeds');
2024-09-09 11:56:47 +02:00
return render('gateways.powerboard.credit_card.authorize', $this->paymentData($data));
2024-09-06 06:26:13 +02:00
}
public function authorizeResponse($request)
{
2024-09-09 11:56:47 +02:00
if($request->browser_details)
{
$payment_source = $this->storePaymentSource($request);
nlog($payment_source);
$browser_details = json_decode($request->browser_details, true);
$payload = [
2024-09-10 00:40:40 +02:00
"capture" => false,
2024-09-09 11:56:47 +02:00
"amount" => 1,
"currency" => $this->powerboard->client->currency()->code,
"description" => "Card authorization",
"customer" => [
"payment_source" => [
"vault_token" => $payment_source->vault_token,
2024-09-10 00:40:40 +02:00
"gateway_id" => $this->powerboard->settings()->getGatewayId(GatewayType::CREDIT_CARD),
2024-09-09 11:56:47 +02:00
],
],
"_3ds" => [
"browser_details" => $browser_details,
],
];
nlog($payload);
$r = $this->powerboard->gatewayRequest('/v1/charges/3ds', (\App\Enum\HttpVerb::POST)->value, $payload, []);
if ($r->failed()) {
$error_payload = $this->getErrorFromResponse($r);
return response()->json(['message' => $error_payload[0]], 400);
// return $this->processUnsuccessfulPayment($r);
2024-09-09 11:56:47 +02:00
}
$charge = $r->json();
nlog($charge['resource']['data']);
2024-09-10 00:40:40 +02:00
return response()->json($charge['resource']['data'], 200);
2024-09-09 11:56:47 +02:00
}
elseif($request->charge) {
$charge_request = json_decode($request->charge, true);
nlog("we have the charge request");
nlog($charge_request);
$payload = [
'_3ds' => [
'id' => array_key_exists('charge_3ds_id', $charge_request) ? $charge_request['charge_3ds_id'] : $charge_request['_3ds']['id'],
2024-09-09 11:56:47 +02:00
],
"capture" => false,
2024-09-10 00:40:40 +02:00
"authorization" => true,
2024-09-09 11:56:47 +02:00
"amount"=> 1,
"currency"=> $this->powerboard->client->currency()->code,
"store_cvv"=> true,
];
nlog($payload);
$r = $this->powerboard->gatewayRequest("/v1/charges", (\App\Enum\HttpVerb::POST)->value, $payload, []);
2024-09-10 00:40:40 +02:00
if($r->failed()){
$error_payload = $this->getErrorFromResponse($r);
throw new PaymentFailed($error_payload[0], $error_payload[1]);
}
2024-09-09 11:56:47 +02:00
$charge = (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(Charge::class, $r->object()->resource->data) ?? $r->throw();
nlog($charge);
if ($charge->status == 'complete') {
2024-09-10 00:40:40 +02:00
2024-09-09 11:56:47 +02:00
$this->powerboard->logSuccessfulGatewayResponse(['response' => $charge, 'data' => $this->powerboard->payment_hash], SystemLog::TYPE_POWERBOARD);
$vt = $charge->customer->payment_source->vault_token;
2024-09-10 00:40:40 +02:00
$data = [
"payment_source" => [
"vault_token" => $vt,
],
];
$customer = $this->powerboard->customer()->findOrCreateCustomer($data);
$cgt = $this->powerboard->customer()->storePaymentMethod($charge->customer->payment_source, $charge->customer);
2024-09-09 11:56:47 +02:00
2024-09-10 00:40:40 +02:00
return redirect()->route('client.payment_methods.show', ['payment_method' => $cgt->hashed_id]);
2024-09-09 11:56:47 +02:00
}
}
2024-09-10 01:49:17 +02:00
elseif($request->charge_no3d){
nlog($request->all());
$payment_source = $this->storePaymentSource($request);
nlog($payment_source);
$data = [
"payment_source" => [
"vault_token" => $payment_source->vault_token,
],
];
$customer = $this->powerboard->customer()->findOrCreateCustomer($data);
$cgt = $this->powerboard->customer()->storePaymentMethod($payment_source, $customer);
$cgt->gateway_customer_reference = $this->powerboard->settings()->getGatewayId(GatewayType::CREDIT_CARD);
$cgt->save();
2024-09-09 11:56:47 +02:00
2024-09-10 01:49:17 +02:00
return redirect()->route('client.payment_methods.show', ['payment_method' => $cgt->hashed_id]);
}
2024-09-06 11:15:08 +02:00
2024-09-06 15:14:21 +02:00
return redirect()->route('client.payment_methods.index');
}
private function getCustomer(): array
{
2024-09-08 11:06:31 +02:00
$data = [
2024-09-06 14:55:56 +02:00
'first_name' => $this->powerboard->client->present()->first_name(),
'last_name' => $this->powerboard->client->present()->first_name(),
'email' => $this->powerboard->client->present()->email(),
2024-09-08 11:06:31 +02:00
// 'phone' => $this->powerboard->client->present()->phone(),
// 'type' => 'card',
2024-09-06 14:55:56 +02:00
'address_line1' => $this->powerboard->client->address1 ?? '',
'address_line2' => $this->powerboard->client->address2 ?? '',
'address_state' => $this->powerboard->client->state ?? '',
'address_country' => $this->powerboard->client->country->iso_3166_3 ?? '',
'address_city' => $this->powerboard->client->city ?? '',
'address_postcode' => $this->powerboard->client->postal_code ?? '',
2024-09-06 11:15:08 +02:00
];
2024-09-08 11:06:31 +02:00
return \App\Helpers\Sanitizer::removeBlanks($data);
2024-09-06 15:14:21 +02:00
}
2024-09-08 11:06:31 +02:00
private function storePaymentSource($request)
2024-09-06 15:14:21 +02:00
{
$this->powerboard->init();
2024-09-06 11:15:08 +02:00
2024-09-06 15:14:21 +02:00
$payment_source = $request->gateway_response;
2024-09-07 01:34:53 +02:00
$payload = array_merge($this->getCustomer(), [
2024-09-06 15:14:21 +02:00
'token' => $payment_source,
2024-09-09 07:52:11 +02:00
"vault_type" => "permanent",
2024-09-06 15:14:21 +02:00
'store_ccv' => true,
2024-09-07 01:34:53 +02:00
]);
2024-09-10 01:49:17 +02:00
nlog($payload);
2024-09-07 01:34:53 +02:00
$r = $this->powerboard->gatewayRequest('/v1/vault/payment_sources', (\App\Enum\HttpVerb::POST)->value, $payload, []);
2024-09-06 14:55:56 +02:00
2024-09-06 15:14:21 +02:00
if($r->failed())
return $this->powerboard->processInternallyFailedPayment($this->powerboard, $r->throw());
2024-09-06 14:55:56 +02:00
2024-09-08 11:06:31 +02:00
nlog($r->object());
2024-09-06 06:26:13 +02:00
2024-09-08 11:06:31 +02:00
$source = (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(PaymentSource ::class, $r->object()->resource->data);
2024-09-08 11:06:31 +02:00
return $source;
2024-09-06 06:26:13 +02:00
}
2024-09-06 14:55:56 +02:00
2024-09-06 06:26:13 +02:00
public function paymentData(array $data): array
{
$this->powerboard->init();
2024-09-10 00:40:40 +02:00
if($this->cba_gateway->verification_status != "completed")
throw new PaymentFailed("This payment method is not configured as yet. Reference Powerboard portal for further information", 400);
2024-09-06 07:15:33 +02:00
$merge = [
2024-09-06 10:31:00 +02:00
'public_key' => $this->powerboard->company_gateway->getConfigField('publicKey'),
'widget_endpoint' => $this->powerboard->widget_endpoint,
'gateway' => $this->powerboard,
'environment' => $this->powerboard->environment,
2024-09-10 00:40:40 +02:00
'gateway_id' => $this->cba_gateway->_id,
2024-09-06 07:15:33 +02:00
];
2024-09-06 06:26:13 +02:00
2024-09-06 07:15:33 +02:00
return array_merge($data, $merge);
2024-09-06 06:26:13 +02:00
}
public function paymentView(array $data)
{
$data = $this->paymentData($data);
2024-09-06 10:31:00 +02:00
return render('gateways.powerboard.credit_card.pay', $data);
2024-09-06 06:26:13 +02:00
}
public function livewirePaymentView(array $data): string
{
return 'gateways.powerboard.credit_card.pay_livewire';
}
2024-09-06 15:14:21 +02:00
public function tokenBilling($request, $cgt, $client_present = false)
{
2024-09-09 10:50:07 +02:00
$payload = [
"amount" => $this->powerboard->payment_hash->data->amount_with_fee,
"currency" => $this->powerboard->client->currency()->code,
"customer" => [
"payment_source" => [
"vault_token" => $cgt->token,
"gateway_id" => $cgt->gateway_customer_reference
]
]
];
$r = $this->powerboard->gatewayRequest('/v1/charges', (\App\Enum\HttpVerb::POST)->value, $payload, []);
nlog($r->body());
2024-09-10 01:49:17 +02:00
if($r->failed()){
$error_payload = $this->getErrorFromResponse($r);
throw new PaymentFailed($error_payload[0], $error_payload[1]);
}
2024-09-09 10:50:07 +02:00
$charge = (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(Charge::class, $r->object()->resource->data) ?? $r->throw();
nlog($charge);
$this->powerboard->logSuccessfulGatewayResponse(['response' => $charge, 'data' => $this->powerboard->payment_hash], SystemLog::TYPE_POWERBOARD);
return $this->processSuccessfulPayment($charge);
2024-09-06 15:14:21 +02:00
}
2024-09-08 11:06:31 +02:00
private function get3dsToken(PaymentSource $source, $request)
2024-09-06 06:26:13 +02:00
{
2024-09-06 11:15:08 +02:00
2024-09-08 11:06:31 +02:00
$payment_hash = PaymentHash::query()->where('hash', $request->payment_hash)->first();
$browser_details = json_decode($request->browser_details,true);
2024-09-08 11:06:31 +02:00
$payload = [
"amount" => $payment_hash->data->amount_with_fee,
"currency" => $this->powerboard->client->currency()->code,
"description" => $this->powerboard->getDescription(),
"customer" => [
"payment_source" => [
"vault_token" => $source->vault_token,
2024-09-10 00:40:40 +02:00
"gateway_id" => $this->powerboard->settings()->getGatewayId(GatewayType::CREDIT_CARD),
2024-09-08 11:06:31 +02:00
],
],
"_3ds" => [
"browser_details" => $browser_details,
],
];
2024-09-08 11:06:31 +02:00
nlog($payload);
2024-09-08 11:06:31 +02:00
$r = $this->powerboard->gatewayRequest('/v1/charges/3ds', (\App\Enum\HttpVerb::POST)->value, $payload, []);
2024-09-09 08:28:56 +02:00
if ($r->failed()) {
$error_payload = $this->getErrorFromResponse($r);
return response()->json(['message' => $error_payload[0]], 400);
2024-09-09 08:28:56 +02:00
}
2024-09-08 11:06:31 +02:00
$charge = $r->json();
2024-09-10 08:36:20 +02:00
2024-09-08 11:06:31 +02:00
return response()->json($charge['resource']['data'], 200);
}
public function paymentResponse(PaymentResponseRequest $request)
{
nlog($request->all());
2024-09-09 08:28:56 +02:00
2024-09-09 07:52:11 +02:00
$this->powerboard->payment_hash->data = array_merge((array) $this->powerboard->payment_hash->data, ['response' => $request->all()]);
$this->powerboard->payment_hash->save();
2024-09-08 11:06:31 +02:00
$payload = [];
/** Token Payment */
if($request->input('token', false))
{
$cgt = $this->powerboard
->client
->gateway_tokens()
->where('company_gateway_id', $this->powerboard->company_gateway->id)
->where('token', $request->token)
->first();
2024-09-09 10:50:07 +02:00
return $this->tokenBilling($request, $cgt, true);
2024-09-06 15:14:21 +02:00
}
2024-09-08 11:06:31 +02:00
elseif($request->browser_details)
{
$payment_source = $this->storePaymentSource($request);
2024-09-06 15:14:21 +02:00
2024-09-09 07:52:11 +02:00
nlog($payment_source);
2024-09-08 11:06:31 +02:00
return $this->get3dsToken($payment_source, $request);
}
elseif($request->charge) {
2024-09-08 11:52:39 +02:00
$charge_request = json_decode($request->charge, true);
2024-09-10 08:36:20 +02:00
2024-09-08 11:52:39 +02:00
nlog($charge_request);
2024-09-08 11:06:31 +02:00
$payload = [
2024-09-08 11:52:39 +02:00
'_3ds' => [
'id' => array_key_exists('charge_3ds_id', $charge_request) ? $charge_request['charge_3ds_id'] : $charge_request['_3ds']['id'],
2024-09-08 11:06:31 +02:00
],
2024-09-10 08:36:20 +02:00
"amount"=> $this->powerboard->payment_hash->data->amount_with_fee, //@phpstan-ignore-line
2024-09-08 11:06:31 +02:00
"currency"=> $this->powerboard->client->currency()->code,
"store_cvv"=> true,
];
2024-09-06 15:14:21 +02:00
2024-09-08 11:06:31 +02:00
$r = $this->powerboard->gatewayRequest("/v1/charges", (\App\Enum\HttpVerb::POST)->value, $payload, []);
2024-09-08 11:06:31 +02:00
if($r->failed())
2024-09-09 08:28:56 +02:00
return $this->processUnsuccessfulPayment($r);
2024-09-06 15:14:21 +02:00
2024-09-08 11:06:31 +02:00
$charge = (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(Charge::class, $r->object()->resource->data) ?? $r->throw();
2024-09-06 06:26:13 +02:00
2024-09-09 07:52:11 +02:00
nlog($charge);
2024-09-08 11:06:31 +02:00
if ($charge->status == 'complete') {
$this->powerboard->logSuccessfulGatewayResponse(['response' => $charge, 'data' => $this->powerboard->payment_hash], SystemLog::TYPE_POWERBOARD);
2024-09-09 07:52:11 +02:00
$vt = $charge->customer->payment_source->vault_token;
2024-09-06 06:26:13 +02:00
2024-09-09 07:52:11 +02:00
if($request->store_card){
$data = [
"payment_source" => [
"vault_token" => $vt,
],
];
2024-09-09 10:19:29 +02:00
2024-09-09 07:52:11 +02:00
$customer = $this->powerboard->customer()->findOrCreateCustomer($data);
2024-09-09 10:19:29 +02:00
$cgt = $this->powerboard->customer()->storePaymentMethod($charge->customer->payment_source, $charge->customer);
2024-09-09 07:52:11 +02:00
}
2024-09-06 06:26:13 +02:00
2024-09-09 07:52:11 +02:00
return $this->processSuccessfulPayment($charge);
}
2024-09-10 05:51:43 +02:00
elseif($charge->error){
2024-09-10 08:36:20 +02:00
$this->powerboard->logUnsuccessfulGatewayResponse($charge, SystemLog::TYPE_POWERBOARD);
2024-09-10 05:51:43 +02:00
throw new PaymentFailed($charge->error->message, $charge->status);
}
2024-09-06 06:26:13 +02:00
2024-09-09 07:52:11 +02:00
}
2024-09-06 06:26:13 +02:00
session()->flash('message', ctrans('texts.payment_token_not_found'));
return redirect()->back();
2024-09-08 11:06:31 +02:00
}
2024-09-06 06:26:13 +02:00
2024-09-08 11:06:31 +02:00
public function processSuccessfulPayment(Charge $charge)
{
2024-09-06 06:26:13 +02:00
2024-09-08 11:06:31 +02:00
$data = [
'payment_type' => PaymentType::CREDIT_CARD_OTHER,
'amount' => $this->powerboard->payment_hash->data->amount_with_fee,
'transaction_reference' => $charge->_id,
'gateway_type_id' => GatewayType::CREDIT_CARD,
];
2024-09-06 06:26:13 +02:00
2024-09-08 11:06:31 +02:00
$payment = $this->powerboard->createPayment($data, Payment::STATUS_COMPLETED);
2024-09-06 06:26:13 +02:00
2024-09-08 11:06:31 +02:00
SystemLogger::dispatch(
2024-09-08 11:52:39 +02:00
['response' => $charge, 'data' => $data],
2024-09-08 11:06:31 +02:00
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_POWERBOARD,
$this->powerboard->client,
$this->powerboard->client->company,
);
2024-09-06 06:26:13 +02:00
2024-09-08 11:06:31 +02:00
if ($payment->invoices()->whereHas('subscription')->exists()) {
$subscription = $payment->invoices()->first()->subscription;
2024-09-06 06:26:13 +02:00
2024-09-08 11:06:31 +02:00
if ($subscription && array_key_exists('return_url', $subscription->webhook_configuration) && strlen($subscription->webhook_configuration['return_url']) >= 1) {
return redirect($subscription->webhook_configuration['return_url']);
}
}
2024-09-06 06:26:13 +02:00
2024-09-08 11:06:31 +02:00
return redirect()->route('client.payments.show', ['payment' => $payment->hashed_id]);
2024-09-06 06:26:13 +02:00
}
2024-09-10 00:40:40 +02:00
private function getErrorFromResponse($response)
2024-09-06 06:26:13 +02:00
{
2024-09-10 00:40:40 +02:00
try {
2024-09-09 08:28:56 +02:00
$response->throw();
2024-09-10 00:40:40 +02:00
} catch (RequestException $exception) {
2024-09-10 08:36:20 +02:00
2024-09-09 08:28:56 +02:00
$error_object = $exception->response->object();
2024-09-10 08:36:20 +02:00
$this->powerboard->logUnsuccessfulGatewayResponse($error_object, SystemLog::TYPE_POWERBOARD);
2024-09-10 00:40:40 +02:00
$error_message = "Unknown error";
match($error_object->error->code) {
"UnfulfilledCondition" => $error_message = $error_object->error->details->messages[0] ?? $error_object->error->message ?? "Unknown error",
2024-09-10 01:49:17 +02:00
"GatewayError" => $error_message = $error_object->error->message,
2024-09-10 00:40:40 +02:00
"UnfulfilledCondition" => $error_message = $error_object->error->message,
"transaction_declined" => $error_message = $error_object->error->details[0]->status_code_description,
2024-09-10 01:49:17 +02:00
default => $error_message = $error_object->error->message ?? "Unknown error",
2024-09-10 00:40:40 +02:00
};
return [$error_message, $exception->getCode()];
2024-09-09 08:28:56 +02:00
}
2024-09-10 00:40:40 +02:00
}
public function processUnsuccessfulPayment($response)
{
$error = $this->getErrorFromResponse($response);
$this->powerboard->sendFailureMail($error[0]);
2024-09-10 00:40:40 +02:00
// $message = [
// 'server_response' => $server_response,
// 'data' => $this->stripe->payment_hash->data,
// ];
SystemLogger::dispatch(
$error[0],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_POWERBOARD,
$this->powerboard->client,
$this->powerboard->client->company,
);
if (request()->wantsJson()) {
return response()->json($error[0], 200);
}
2024-09-10 00:40:40 +02:00
throw new PaymentFailed('Failed to process the payment.', $error[1]);
2024-09-06 06:26:13 +02:00
}
}