1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/PaymentDrivers/Square/CreditCard.php

243 lines
8.1 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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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;
2021-08-19 13:34:45 +02:00
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
2021-10-26 16:21:26 +02:00
use Illuminate\Http\Request;
2021-08-19 13:34:45 +02:00
use App\Models\ClientGatewayToken;
2021-08-14 10:11:45 +02:00
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentType;
2021-10-25 17:56:40 +02:00
use App\PaymentDrivers\Common\MethodInterface;
2021-08-14 10:11:45 +02:00
use App\PaymentDrivers\SquarePaymentDriver;
use App\Utils\Traits\MakesHash;
2021-10-25 17:56:40 +02:00
use Illuminate\Http\RedirectResponse;
2021-08-14 10:11:45 +02:00
use Illuminate\Support\Str;
2021-10-25 17:56:40 +02:00
use Illuminate\View\View;
2021-08-18 17:23:31 +02:00
use Square\Http\ApiResponse;
2021-08-14 10:11:45 +02:00
2021-10-25 17:56:40 +02:00
class CreditCard implements MethodInterface
2021-08-14 10:11:45 +02:00
{
use MakesHash;
2021-08-14 13:00:52 +02:00
public $square_driver;
2021-08-14 10:11:45 +02:00
2021-08-14 13:00:52 +02:00
public function __construct(SquarePaymentDriver $square_driver)
2021-08-14 10:11:45 +02:00
{
2021-08-14 13:00:52 +02:00
$this->square_driver = $square_driver;
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
2021-10-25 17:56:40 +02:00
* @return View
*/
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
* @return 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)
{
2021-08-14 13:00:52 +02:00
$data['gateway'] = $this->square_driver;
2021-10-03 05:50:01 +02:00
$data['amount'] = $this->square_driver->payment_hash->data->amount_with_fee;
$data['currencyCode'] = $this->square_driver->client->getCurrencyCode();
2021-10-03 06:04:34 +02:00
$data['square_contact'] = $this->buildClientObject();
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()
{
$client = new \stdClass;
2021-10-03 06:44:23 +02:00
$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();
$client->email = $this->square_driver->client->present()->email;
$client->phone = $this->square_driver->client->phone;
$client->city = $this->square_driver->client->city;
$client->region = $this->square_driver->client->state;
$client->country = $this->square_driver->client->country->iso_3166_2;
2021-10-03 06:08:38 +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()) {
$cgt = ClientGatewayToken::where('token', $request->token)->first();
$token = $cgt->token;
}
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);
$body = new \Square\Models\CreatePaymentRequest($token, $request->idempotencyKey, $amount_money);
2021-08-18 17:23:31 +02:00
$body->setAutocomplete(true);
$body->setLocationId($this->square_driver->company_gateway->getConfigField('locationId'));
$body->setReferenceId(Str::random(16));
2021-10-25 17:59:45 +02:00
if ($request->has('verificationToken') && $request->input('verificationToken')) {
2021-10-03 05:50:01 +02:00
$body->setVerificationToken($request->input('verificationToken'));
2021-10-25 17:59:45 +02:00
}
2021-10-03 05:50:01 +02:00
2021-08-19 13:34:45 +02:00
if ($request->shouldUseToken()) {
$body->setCustomerId($cgt->gateway_customer_reference);
}
2021-08-18 17:23:31 +02:00
/** @var ApiResponse */
$response = $this->square_driver->square->getPaymentsApi()->createPayment($body);
if ($response->isSuccess()) {
return $this->processSuccessfulPayment($response);
}
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
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
}
2021-08-15 13:14:18 +02:00
private function findOrCreateClient()
{
$email_address = new \Square\Models\CustomerTextFilter();
$email_address->setExact($this->square_driver->client->present()->email());
$filter = new \Square\Models\CustomerFilter();
$filter->setEmailAddress($email_address);
$query = new \Square\Models\CustomerQuery();
$query->setFilter($filter);
$body = new \Square\Models\SearchCustomersRequest();
$body->setQuery($query);
$api_response = $this->square_driver
->init()
->square
->getCustomersApi()
->searchCustomers($body);
$customers = false;
if ($api_response->isSuccess()) {
$customers = $api_response->getBody();
$customers = json_decode($customers);
2021-10-03 04:36:43 +02:00
2021-10-25 17:59:45 +02:00
if (count([$api_response->getBody(),1]) == 0) {
2021-10-03 04:36:43 +02:00
$customers = false;
2021-10-25 17:59:45 +02:00
}
2021-08-15 13:14:18 +02:00
} else {
$errors = $api_response->getErrors();
}
2021-10-03 10:20:59 +02:00
if (property_exists($customers, 'customers')) {
2021-08-15 13:14:18 +02:00
return $customers->customers[0]->id;
2021-08-18 17:07:15 +02:00
}
2021-08-15 13:14:18 +02:00
return $this->createClient();
}
private function createClient()
{
/* Step two - create the customer */
$billing_address = new \Square\Models\Address();
$billing_address->setAddressLine1($this->square_driver->client->address1);
$billing_address->setAddressLine2($this->square_driver->client->address2);
$billing_address->setLocality($this->square_driver->client->city);
$billing_address->setAdministrativeDistrictLevel1($this->square_driver->client->state);
$billing_address->setPostalCode($this->square_driver->client->postal_code);
$billing_address->setCountry($this->square_driver->client->country->iso_3166_2);
$body = new \Square\Models\CreateCustomerRequest();
$body->setGivenName($this->square_driver->client->present()->name());
$body->setFamilyName('');
$body->setEmailAddress($this->square_driver->client->present()->email());
$body->setAddress($billing_address);
$body->setPhoneNumber($this->square_driver->client->phone);
$body->setReferenceId($this->square_driver->client->number);
$body->setNote('Created by Invoice Ninja.');
2021-08-14 10:11:45 +02:00
2021-08-15 13:14:18 +02:00
$api_response = $this->square_driver
->init()
->square
->getCustomersApi()
->createCustomer($body);
2021-08-14 10:11:45 +02:00
2021-08-15 13:14:18 +02:00
if ($api_response->isSuccess()) {
$result = $api_response->getResult();
return $result->getCustomer()->getId();
} else {
$errors = $api_response->getErrors();
return $this->processUnsuccessfulPayment($errors);
}
}
2021-08-18 17:07:15 +02:00
}