1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/PaymentDrivers/Square/CreditCard.php

260 lines
8.7 KiB
PHP
Raw Normal View History

2021-08-14 10:11:45 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2021-08-14 10:11:45 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\Square;
2023-10-26 04:57:44 +02:00
use App\Exceptions\PaymentFailed;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
2023-08-19 04:23:37 +02:00
use App\Models\Invoice;
2021-08-14 10:11:45 +02:00
use App\Models\Payment;
use App\Models\PaymentType;
2023-10-26 04:57:44 +02:00
use App\Models\SystemLog;
use App\PaymentDrivers\Common\LivewireMethodInterface;
2023-10-26 04:57:44 +02:00
use App\PaymentDrivers\Common\MethodInterface;
use App\PaymentDrivers\SquarePaymentDriver;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\RedirectResponse;
2023-10-26 04:57:44 +02:00
use Illuminate\Http\Request;
use Illuminate\View\View;
use Square\Http\ApiResponse;
2021-08-14 10:11:45 +02:00
class CreditCard implements MethodInterface, LivewireMethodInterface
2021-08-14 10:11:45 +02:00
{
use MakesHash;
2023-08-19 08:32:24 +02:00
public function __construct(public SquarePaymentDriver $square_driver)
2021-08-14 10:11:45 +02:00
{
2021-08-14 13:37:04 +02:00
$this->square_driver->init();
2021-08-14 10:11:45 +02:00
}
2021-10-25 17:56:40 +02:00
/**
* Authorization page for credit card.
2021-10-25 17:59:45 +02:00
*
* @param array $data
2023-10-26 04:57:44 +02:00
* @return \Illuminate\View\View
2021-10-25 17:56:40 +02:00
*/
public function authorizeView($data): View
2021-08-14 10:11:45 +02:00
{
2021-08-14 13:00:52 +02:00
$data['gateway'] = $this->square_driver;
2021-08-14 10:11:45 +02:00
return render('gateways.square.credit_card.authorize', $data);
}
2021-10-25 17:56:40 +02:00
/**
* Handle authorization for credit card.
2021-10-25 17:59:45 +02:00
*
* @param Request $request
2024-06-16 00:30:25 +02:00
* @return \Illuminate\Http\RedirectResponse
2021-10-25 17:56:40 +02:00
*/
public function authorizeResponse($request): RedirectResponse
2021-08-14 10:11:45 +02:00
{
2021-08-15 12:27:52 +02:00
return redirect()->route('client.payment_methods.index');
2021-08-14 10:11:45 +02:00
}
public function paymentView($data)
{
$data = $this->paymentData($data);
2021-08-16 03:11:08 +02:00
return render('gateways.square.credit_card.pay', $data);
2021-08-14 10:11:45 +02:00
}
2021-10-03 05:50:01 +02:00
private function buildClientObject()
{
2024-01-14 05:05:00 +01:00
$client = new \stdClass();
2021-10-03 05:50:01 +02:00
2022-07-27 03:28:30 +02:00
$country = $this->square_driver->client->country ? $this->square_driver->client->country->iso_3166_2 : $this->square_driver->client->company->country()->iso_3166_2;
$client->addressLines = [$this->square_driver->client->address1 ?: '', $this->square_driver->client->address2 ?: ''];
2021-10-03 05:50:01 +02:00
$client->givenName = $this->square_driver->client->present()->first_name();
$client->familyName = $this->square_driver->client->present()->last_name();
2022-09-05 05:12:47 +02:00
$client->email = $this->square_driver->client->present()->email();
2021-10-03 05:50:01 +02:00
$client->phone = $this->square_driver->client->phone;
$client->city = $this->square_driver->client->city;
$client->region = $this->square_driver->client->state;
2022-07-27 03:28:30 +02:00
$client->country = $country;
2021-10-03 05:50:01 +02:00
return (array) $client;
2021-10-03 05:50:01 +02:00
}
2021-08-19 13:34:45 +02:00
public function paymentResponse(PaymentResponseRequest $request)
2021-08-14 10:11:45 +02:00
{
2021-08-19 13:34:45 +02:00
$token = $request->sourceId;
2021-08-19 13:50:34 +02:00
$amount = $this->square_driver->convertAmount(
$this->square_driver->payment_hash->data->amount_with_fee
);
2021-08-19 13:34:45 +02:00
if ($request->shouldUseToken()) {
2023-08-19 08:32:24 +02:00
$cgt = ClientGatewayToken::query()->where('token', $request->token)->first();
2021-08-19 13:34:45 +02:00
$token = $cgt->token;
}
2023-08-19 04:23:37 +02:00
$invoice = Invoice::query()->whereIn('id', $this->transformKeys(array_column($this->square_driver->payment_hash->invoices(), 'invoice_id')))->withTrashed()->first();
if ($invoice) {
$description = "Invoice {$invoice->number} for {$amount} for client {$this->square_driver->client->present()->name()}";
} else {
$description = "Payment with no invoice for amount {$amount} for client {$this->square_driver->client->present()->name()}";
}
2021-08-18 17:23:31 +02:00
$amount_money = new \Square\Models\Money();
2021-08-19 13:50:34 +02:00
$amount_money->setAmount($amount);
2021-08-18 17:23:31 +02:00
$amount_money->setCurrency($this->square_driver->client->currency()->code);
2023-08-20 11:17:21 +02:00
$body = new \Square\Models\CreatePaymentRequest($token, $request->idempotencyKey);
2023-08-19 02:39:24 +02:00
$body->setAmountMoney($amount_money);
2021-08-18 17:23:31 +02:00
$body->setAutocomplete(true);
$body->setLocationId($this->square_driver->company_gateway->getConfigField('locationId'));
2023-08-19 04:23:37 +02:00
$body->setReferenceId($this->square_driver->payment_hash->hash);
$body->setNote($description);
2024-01-14 05:05:00 +01:00
2021-08-19 13:34:45 +02:00
if ($request->shouldUseToken()) {
$body->setCustomerId($cgt->gateway_customer_reference);
2023-10-26 04:57:44 +02:00
} elseif ($request->has('verificationToken') && $request->input('verificationToken')) {
$body->setVerificationToken($request->input('verificationToken'));
2021-08-19 13:34:45 +02:00
}
2021-08-18 17:23:31 +02:00
$response = $this->square_driver->square->getPaymentsApi()->createPayment($body);
if ($response->isSuccess()) {
$body = json_decode($response->getBody());
2023-10-26 04:57:44 +02:00
if($request->store_card) {
$this->createCard($body->payment->id);
}
2021-08-18 17:23:31 +02:00
return $this->processSuccessfulPayment($response);
}
2023-09-27 01:03:49 +02:00
if(is_array($response)) {
nlog("square");
nlog($response);
}
2021-08-18 17:23:31 +02:00
return $this->processUnsuccessfulPayment($response);
2021-08-14 10:11:45 +02:00
}
2021-08-18 17:23:31 +02:00
private function processSuccessfulPayment(ApiResponse $response)
2021-08-14 10:11:45 +02:00
{
2021-08-18 17:23:31 +02:00
$body = json_decode($response->getBody());
2021-08-14 13:00:52 +02:00
$amount = array_sum(array_column($this->square_driver->payment_hash->invoices(), 'amount')) + $this->square_driver->payment_hash->fee_total;
2021-08-14 10:11:45 +02:00
$payment_record = [];
$payment_record['amount'] = $amount;
$payment_record['payment_type'] = PaymentType::CREDIT_CARD_OTHER;
$payment_record['gateway_type_id'] = GatewayType::CREDIT_CARD;
2021-08-18 17:23:31 +02:00
$payment_record['transaction_reference'] = $body->payment->id;
2021-08-14 10:11:45 +02:00
2021-08-14 13:00:52 +02:00
$payment = $this->square_driver->createPayment($payment_record, Payment::STATUS_COMPLETED);
2021-08-14 10:11:45 +02:00
$message = [
'server_response' => $body,
'data' => $this->square_driver->payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_SQUARE,
$this->square_driver->client,
$this->square_driver->client->company,
);
2021-08-14 10:11:45 +02:00
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
}
2021-08-18 17:23:31 +02:00
private function processUnsuccessfulPayment(ApiResponse $response)
2021-08-14 10:11:45 +02:00
{
2021-08-19 13:34:45 +02:00
$body = \json_decode($response->getBody());
2021-08-14 10:11:45 +02:00
$data = [
'response' => $response,
2021-08-19 13:34:45 +02:00
'error' => $body->errors[0]->detail,
2021-08-15 12:39:05 +02:00
'error_code' => '',
2021-08-14 10:11:45 +02:00
];
2021-08-14 13:00:52 +02:00
return $this->square_driver->processUnsuccessfulTransaction($data);
2021-08-14 10:11:45 +02:00
}
private function createCard($source_id)
{
2024-01-14 05:05:00 +01:00
$square_card = new \Square\Models\Card();
2024-03-19 00:38:54 +01:00
$square_card->setCustomerId($this->square_driver->findOrCreateClient());
$body = new \Square\Models\CreateCardRequest(uniqid("st", true), $source_id, $square_card);
2024-01-14 05:05:00 +01:00
$api_response = $this->square_driver
->init()
->square
->getCardsApi()
->createCard($body);
$body = json_decode($api_response->getBody());
if ($api_response->isSuccess()) {
try {
2024-01-14 05:05:00 +01:00
$payment_meta = new \stdClass();
$payment_meta->exp_month = (string) $body->card->exp_month;
$payment_meta->exp_year = (string) $body->card->exp_year;
$payment_meta->brand = (string) $body->card->card_brand;
$payment_meta->last4 = (string) $body->card->last_4;
$payment_meta->type = GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
'token' => $body->card->id,
'payment_method_id' => GatewayType::CREDIT_CARD,
];
$this->square_driver->storeGatewayToken($data, ['gateway_customer_reference' => $body->card->customer_id]);
} catch (\Exception $e) {
return $this->square_driver->processInternallyFailedPayment($this->square_driver, $e);
}
2023-10-26 04:57:44 +02:00
} else {
throw new PaymentFailed($body->errors[0]->detail, 500);
}
return false;
}
/**
* @inheritDoc
*/
public function livewirePaymentView(array $data): string
{
return 'gateways.square.credit_card.pay_livewire';
}
/**
* @inheritDoc
*/
public function paymentData(array $data): array
{
$data['gateway'] = $this->square_driver;
$data['amount'] = $this->square_driver->payment_hash->data->amount_with_fee;
$data['currencyCode'] = $this->square_driver->client->getCurrencyCode();
$data['square_contact'] = $this->buildClientObject();
2024-06-14 09:09:44 +02:00
return $data;
}
2021-08-18 17:07:15 +02:00
}