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

204 lines
6.4 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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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;
use App\Http\Requests\Payments\PaymentWebhookRequest;
2021-08-19 13:50:34 +02:00
use App\Jobs\Util\SystemLogger;
2021-08-14 10:11:45 +02:00
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
2021-08-19 13:50:34 +02:00
use App\Models\Invoice;
2021-08-14 10:11:45 +02:00
use App\Models\Payment;
use App\Models\PaymentHash;
2021-08-19 13:50:34 +02:00
use App\Models\PaymentType;
2021-08-14 10:11:45 +02:00
use App\Models\SystemLog;
use App\PaymentDrivers\Square\CreditCard;
use App\Utils\Traits\MakesHash;
2021-08-19 13:50:34 +02:00
use Square\Http\ApiResponse;
2021-08-14 10:11:45 +02:00
class SquarePaymentDriver extends BaseDriver
{
use MakesHash;
2021-08-21 14:22:22 +02:00
public $refundable = false; //does this gateway support refunds?
2021-08-14 10:11:45 +02:00
public $token_billing = true; //does this gateway support token billing?
public $can_authorise_credit_card = true; //does this gateway support authorizations?
2021-08-19 13:50:34 +02:00
public $square;
2021-08-14 10:11:45 +02:00
2021-08-14 11:32:16 +02:00
public $payment_method;
2021-08-14 10:11:45 +02:00
public static $methods = [
GatewayType::CREDIT_CARD => CreditCard::class, //maps GatewayType => Implementation class
];
2021-08-19 13:50:34 +02:00
const SYSTEM_LOG_TYPE = SystemLog::TYPE_SQUARE;
2021-08-14 10:11:45 +02:00
public function init()
{
2021-08-14 13:37:04 +02:00
$this->square = new \Square\SquareClient([
2021-08-14 11:32:16 +02:00
'accessToken' => $this->company_gateway->getConfigField('accessToken'),
2021-08-14 13:37:04 +02:00
'environment' => $this->company_gateway->getConfigField('testMode') ? \Square\Environment::SANDBOX : \Square\Environment::PRODUCTION,
2021-08-14 10:11:45 +02:00
]);
return $this; /* This is where you boot the gateway with your auth credentials*/
}
/* Returns an array of gateway types for the payment gateway */
public function gatewayTypes(): array
{
$types = [];
2021-08-19 13:50:34 +02:00
$types[] = GatewayType::CREDIT_CARD;
2021-08-14 10:11:45 +02:00
return $types;
}
/* Sets the payment method initialized */
public function setPaymentMethod($payment_method_id)
{
$class = self::$methods[$payment_method_id];
$this->payment_method = new $class($this);
2021-08-14 10:11:45 +02:00
return $this;
}
public function authorizeView(array $data)
{
return $this->payment_method->authorizeView($data); //this is your custom implementation from here
}
public function authorizeResponse($request)
{
return $this->payment_method->authorizeResponse($request); //this is your custom implementation from here
}
public function processPaymentView(array $data)
{
return $this->payment_method->paymentView($data); //this is your custom implementation from here
}
public function processPaymentResponse($request)
{
return $this->payment_method->paymentResponse($request); //this is your custom implementation from here
}
public function refund(Payment $payment, $amount, $return_client_response = false)
{
2021-08-19 14:05:11 +02:00
$this->init();
$amount_money = new \Square\Models\Money();
$amount_money->setAmount($this->convertAmount($amount));
$amount_money->setCurrency($this->square_driver->client->currency()->code);
$body = new \Square\Models\RefundPaymentRequest(\Illuminate\Support\Str::random(32), $amount_money, $payment->transaction_reference);
/** @var ApiResponse */
$response = $this->square->getRefundsApi()->refund($body);
2021-08-14 10:11:45 +02:00
}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
2021-08-19 13:50:34 +02:00
$this->init();
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$amount = $this->convertAmount($amount);
$invoice = Invoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->withTrashed()->first();
if ($invoice) {
$description = "Invoice {$invoice->number} for {$amount} for client {$this->client->present()->name()}";
} else {
$description = "Payment with no invoice for amount {$amount} for client {$this->client->present()->name()}";
}
$amount_money = new \Square\Models\Money();
$amount_money->setAmount($amount);
$amount_money->setCurrency($this->client->currency()->code);
$body = new \Square\Models\CreatePaymentRequest($cgt->token, \Illuminate\Support\Str::random(32), $amount_money);
2021-11-27 01:46:28 +01:00
$body->setCustomerId($cgt->gateway_customer_reference);
2021-08-19 13:50:34 +02:00
/** @var ApiResponse */
$response = $this->square->getPaymentsApi()->createPayment($body);
$body = json_decode($response->getBody());
if ($response->isSuccess()) {
$amount = array_sum(array_column($this->payment_hash->invoices(), 'amount')) + $this->payment_hash->fee_total;
$payment_record = [];
$payment_record['amount'] = $amount;
$payment_record['payment_type'] = PaymentType::CREDIT_CARD_OTHER;
$payment_record['gateway_type_id'] = GatewayType::CREDIT_CARD;
$payment_record['transaction_reference'] = $body->payment->id;
$payment = $this->createPayment($payment_record, Payment::STATUS_COMPLETED);
SystemLogger::dispatch(
['response' => $response, 'data' => $payment_record],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_CHECKOUT,
$this->client,
$this->client->company,
);
return $payment;
}
$this->unWindGatewayFees($payment_hash);
2021-10-17 12:40:40 +02:00
$this->sendFailureMail($body->errors[0]->detail);
2021-08-19 13:50:34 +02:00
$message = [
'server_response' => $response,
'data' => $payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_SQUARE,
$this->client,
$this->client->company,
);
return false;
2021-08-14 10:11:45 +02:00
}
public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null)
{
}
2021-08-16 03:11:08 +02:00
2021-08-21 14:22:22 +02:00
public function convertAmount($amount)
2021-08-19 13:50:34 +02:00
{
$precision = $this->client->currency()->precision;
if ($precision == 0) {
return $amount;
}
if ($precision == 1) {
return $amount * 10;
2021-08-19 13:50:34 +02:00
}
if ($precision == 2) {
return $amount * 100;
2021-08-19 13:50:34 +02:00
}
return $amount;
}
}