1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/PaymentDrivers/Stripe/Charge.php

207 lines
7.7 KiB
PHP
Raw Normal View History

2020-07-14 14:50:16 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-07-14 14:50:16 +02:00
*
* @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)
2020-07-14 14:50:16 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-07-14 14:50:16 +02:00
*/
namespace App\PaymentDrivers\Stripe;
2020-07-15 07:05:02 +02:00
use App\Jobs\Util\SystemLogger;
2020-07-14 14:50:16 +02:00
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentHash;
2020-07-15 07:05:02 +02:00
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\StripePaymentDriver;
2020-10-14 12:45:26 +02:00
use App\Utils\Traits\MakesHash;
2020-10-28 11:10:49 +01:00
use Stripe\Exception\ApiErrorException;
use Stripe\Exception\AuthenticationException;
use Stripe\Exception\CardException;
use Stripe\Exception\InvalidRequestException;
use Stripe\Exception\RateLimitException;
2020-07-14 14:50:16 +02:00
class Charge
{
2020-10-14 12:45:26 +02:00
use MakesHash;
2020-10-28 11:10:49 +01:00
2020-07-14 14:50:16 +02:00
/** @var StripePaymentDriver */
public $stripe;
public function __construct(StripePaymentDriver $stripe)
{
$this->stripe = $stripe;
}
/**
* Create a charge against a payment method.
2020-10-28 11:10:49 +01:00
* @param ClientGatewayToken $cgt
* @param PaymentHash $payment_hash
2023-05-17 01:16:43 +02:00
* @return mixed success/failure
2020-10-28 11:10:49 +01:00
* @throws \Laracasts\Presenter\Exceptions\PresenterException
2020-07-14 14:50:16 +02:00
*/
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
2020-07-14 14:50:16 +02:00
{
if ($cgt->gateway_type_id == GatewayType::BANK_TRANSFER) {
return (new ACH($this->stripe))->tokenBilling($cgt, $payment_hash);
2024-01-14 05:05:00 +01:00
} elseif($cgt->gateway_type_id == GatewayType::ACSS) {
2023-12-18 05:25:16 +01:00
return (new ACSS($this->stripe))->tokenBilling($cgt, $payment_hash);
}
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$description = $this->stripe->getDescription(false);
2020-07-14 14:50:16 +02:00
2020-07-15 07:05:02 +02:00
$this->stripe->init();
2020-07-14 14:50:16 +02:00
2020-07-15 07:05:02 +02:00
$response = null;
try {
2021-04-28 03:27:44 +02:00
$data = [
'amount' => $this->stripe->convertToStripeAmount($amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'currency' => $this->stripe->client->getCurrencyCode(),
'payment_method' => $cgt->token,
'customer' => $cgt->gateway_customer_reference,
'confirm' => true,
'description' => $description,
'metadata' => [
'payment_hash' => $payment_hash->hash,
'gateway_type_id' => $cgt->gateway_type_id,
],
2021-04-28 03:27:44 +02:00
];
if ($cgt->gateway_type_id == GatewayType::SEPA) {
2022-05-08 03:22:59 +02:00
$data['payment_method_types'] = ['sepa_debit'];
}
2022-12-17 07:36:13 +01:00
if ($cgt->gateway_type_id == GatewayType::BACS) {
$data['payment_method_types'] = ['bacs_debit'];
}
2022-05-08 03:22:59 +02:00
/* Should improve token billing with client not present */
if (!auth()->guard('contact')->check()) {
$data['off_session'] = true;
}
2023-05-17 01:16:43 +02:00
$response = $this->stripe->createPaymentIntent($data);
SystemLogger::dispatch($response, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->stripe->client, $this->stripe->client->company);
2021-02-01 22:33:04 +01:00
} catch (\Exception $e) {
$data = [
2021-02-01 22:33:04 +01:00
'status' => '',
'error_type' => '',
'error_code' => '',
'param' => '',
'message' => '',
];
switch ($e) {
2023-12-18 05:37:03 +01:00
/** @var \Stripe\Exception\CardException $e */
case $e instanceof CardException:
2023-12-18 05:37:03 +01:00
$data['message'] = $e->getError()->message ?? $e->getMessage();
2023-02-16 02:36:09 +01:00
break;
case $e instanceof RateLimitException:
$data['message'] = 'Too many requests made to the API too quickly';
2023-02-16 02:36:09 +01:00
break;
case $e instanceof InvalidRequestException:
$data['message'] = 'Invalid parameters were supplied to Stripe\'s API';
2023-02-16 02:36:09 +01:00
break;
case $e instanceof AuthenticationException:
$data['message'] = 'Authentication with Stripe\'s API failed';
2023-02-16 02:36:09 +01:00
break;
case $e instanceof ApiErrorException:
$data['message'] = 'Network communication with Stripe failed';
2023-02-16 02:36:09 +01:00
break;
2021-02-01 22:33:04 +01:00
default:
$data['message'] = $e->getMessage();
2023-02-16 02:36:09 +01:00
break;
2021-02-01 22:33:04 +01:00
}
$this->stripe->processInternallyFailedPayment($this->stripe, $e);
2020-07-15 07:05:02 +02:00
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client, $this->stripe->client->company);
}
2020-07-15 07:05:02 +02:00
if (! $response) {
2020-07-15 07:05:02 +02:00
return false;
}
2020-07-15 07:05:02 +02:00
if ($cgt->gateway_type_id == GatewayType::SEPA) {
2022-05-09 00:19:35 +02:00
$payment_method_type = PaymentType::SEPA;
$status = Payment::STATUS_PENDING;
2023-03-18 08:24:56 +01:00
} elseif ($cgt->gateway_type_id == GatewayType::BACS) {
2022-12-17 12:43:08 +01:00
$payment_method_type = PaymentType::BACS;
2022-12-17 12:40:51 +01:00
$status = Payment::STATUS_PENDING;
2023-03-18 08:24:56 +01:00
} else {
if (isset($response->latest_charge)) {
$charge = \Stripe\Charge::retrieve($response->latest_charge, $this->stripe->stripe_connect_auth);
$payment_method_type = $charge->payment_method_details->card->brand;
2023-02-16 02:36:09 +01:00
} elseif (isset($response->charges->data[0]->payment_method_details->card->brand)) {
$payment_method_type = $response->charges->data[0]->payment_method_details->card->brand;
2023-02-16 02:36:09 +01:00
} else {
$payment_method_type = 'visa';
2023-02-16 02:36:09 +01:00
}
$status = Payment::STATUS_COMPLETED;
}
2022-12-17 07:36:13 +01:00
2023-03-18 08:24:56 +01:00
if (!in_array($response?->status, ['succeeded', 'processing'])) {
$this->stripe->processInternallyFailedPayment($this->stripe, new \Exception('Auto billing failed.', 400));
2022-11-09 12:36:05 +01:00
}
2020-07-15 07:05:02 +02:00
$data = [
'gateway_type_id' => $cgt->gateway_type_id,
2021-01-27 11:38:28 +01:00
'payment_type' => $this->transformPaymentTypeToConstant($payment_method_type),
2022-11-29 10:09:54 +01:00
'transaction_reference' => isset($response->latest_charge) ? $response->latest_charge : $response->charges->data[0]->id,
2021-01-27 11:38:28 +01:00
'amount' => $amount,
2020-07-15 07:05:02 +02:00
];
$payment = $this->stripe->createPayment($data, $status);
$payment->meta = $cgt->meta;
$payment->save();
2020-07-15 07:05:02 +02:00
$payment_hash->data = array_merge((array) $payment_hash->data, ['payment_intent' => $response, 'amount_with_fee' => $amount]);
2020-09-10 03:52:17 +02:00
$payment_hash->payment_id = $payment->id;
$payment_hash->save();
2020-10-28 11:10:49 +01:00
2020-07-15 07:05:02 +02:00
return $payment;
}
private function formatGatewayResponse($data, $vars)
{
$response = $data['response'];
return [
'transaction_reference' => $response->getTransactionResponse()->getTransId(),
'amount' => $vars['amount'],
'auth_code' => $response->getTransactionResponse()->getAuthCode(),
'code' => $response->getTransactionResponse()->getMessages()[0]->getCode(),
'description' => $response->getTransactionResponse()->getMessages()[0]->getDescription(),
'invoices' => $vars['hashed_ids'],
];
}
private function transformPaymentTypeToConstant($type)
{
switch ($type) {
case 'visa':
return PaymentType::VISA;
case 'mastercard':
return PaymentType::MASTERCARD;
2022-05-09 00:19:35 +02:00
case PaymentType::SEPA:
return PaymentType::SEPA;
2022-12-17 12:40:51 +01:00
case PaymentType::BACS:
return PaymentType::BACS;
2020-07-15 07:05:02 +02:00
default:
return PaymentType::CREDIT_CARD_OTHER;
}
2020-07-14 14:50:16 +02:00
}
}