2020-07-14 14:50:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* 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;
|
2021-10-01 04:57:34 +02:00
|
|
|
use App\Models\GatewayType;
|
2022-09-14 02:51:41 +02:00
|
|
|
use App\Models\Payment;
|
2020-09-04 00:01:17 +02:00
|
|
|
use App\Models\PaymentHash;
|
2020-07-15 07:05:02 +02:00
|
|
|
use App\Models\PaymentType;
|
|
|
|
use App\Models\SystemLog;
|
2022-06-21 11:57:17 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Create a charge against a payment method.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param ClientGatewayToken $cgt
|
|
|
|
* @param PaymentHash $payment_hash
|
2020-07-14 14:50:16 +02:00
|
|
|
* @return bool success/failure
|
2020-10-28 11:10:49 +01:00
|
|
|
* @throws \Laracasts\Presenter\Exceptions\PresenterException
|
2020-07-14 14:50:16 +02:00
|
|
|
*/
|
2020-09-04 00:01:17 +02:00
|
|
|
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
|
2020-07-14 14:50:16 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($cgt->gateway_type_id == GatewayType::BANK_TRANSFER) {
|
2021-10-01 04:57:34 +02:00
|
|
|
return (new ACH($this->stripe))->tokenBilling($cgt, $payment_hash);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-10-01 04:57:34 +02:00
|
|
|
|
2020-09-04 00:01:17 +02:00
|
|
|
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
|
|
|
|
|
2023-02-23 01:14:14 +01:00
|
|
|
$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 = [
|
2022-06-21 11:57:17 +02:00
|
|
|
'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,
|
2022-01-24 04:24:47 +01:00
|
|
|
],
|
2021-04-28 03:27:44 +02:00
|
|
|
];
|
2021-07-21 10:18:14 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($cgt->gateway_type_id == GatewayType::SEPA) {
|
2022-05-08 03:22:59 +02:00
|
|
|
$data['payment_method_types'] = ['sepa_debit'];
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
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
|
|
|
|
2022-12-05 03:19:36 +01:00
|
|
|
/* Should improve token billing with client not present */
|
|
|
|
if (!auth()->guard('contact')->check()) {
|
|
|
|
$data['off_session'] = true;
|
|
|
|
}
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$response = $this->stripe->createPaymentIntent($data, array_merge($this->stripe->stripe_connect_auth, ['idempotency_key' => uniqid("st", true)]));
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-19 03:12:23 +02:00
|
|
|
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) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$data = [
|
2021-02-01 22:33:04 +01:00
|
|
|
'status' => '',
|
|
|
|
'error_type' => '',
|
|
|
|
'error_code' => '',
|
|
|
|
'param' => '',
|
|
|
|
'message' => '',
|
|
|
|
];
|
|
|
|
|
|
|
|
switch ($e) {
|
2022-06-21 11:57:17 +02:00
|
|
|
case $e instanceof CardException:
|
2021-02-02 02:04:52 +01:00
|
|
|
$data['status'] = $e->getHttpStatus();
|
|
|
|
$data['error_type'] = $e->getError()->type;
|
|
|
|
$data['error_code'] = $e->getError()->code;
|
|
|
|
$data['param'] = $e->getError()->param;
|
|
|
|
$data['message'] = $e->getError()->message;
|
2023-02-16 02:36:09 +01:00
|
|
|
break;
|
2022-06-21 11:57:17 +02:00
|
|
|
case $e instanceof RateLimitException:
|
2021-02-02 02:04:52 +01:00
|
|
|
$data['message'] = 'Too many requests made to the API too quickly';
|
2023-02-16 02:36:09 +01:00
|
|
|
break;
|
2022-06-21 11:57:17 +02:00
|
|
|
case $e instanceof InvalidRequestException:
|
2021-02-02 02:04:52 +01:00
|
|
|
$data['message'] = 'Invalid parameters were supplied to Stripe\'s API';
|
2023-02-16 02:36:09 +01:00
|
|
|
break;
|
2022-06-21 11:57:17 +02:00
|
|
|
case $e instanceof AuthenticationException:
|
2021-02-02 02:04:52 +01:00
|
|
|
$data['message'] = 'Authentication with Stripe\'s API failed';
|
2023-02-16 02:36:09 +01:00
|
|
|
break;
|
2022-06-21 11:57:17 +02:00
|
|
|
case $e instanceof ApiErrorException:
|
2021-02-02 02:04:52 +01:00
|
|
|
$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:
|
2021-02-02 02:04:52 +01:00
|
|
|
$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
|
|
|
|
2021-05-19 03:12:23 +02:00
|
|
|
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->stripe->client, $this->stripe->client->company);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2020-07-15 07:05:02 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $response) {
|
2020-07-15 07:05:02 +02:00
|
|
|
return false;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-07-15 07:05:02 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($cgt->gateway_type_id == GatewayType::SEPA) {
|
2022-05-09 00:19:35 +02:00
|
|
|
$payment_method_type = PaymentType::SEPA;
|
2022-09-14 02:51:41 +02:00
|
|
|
$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)) {
|
2022-11-29 11:43:40 +01:00
|
|
|
$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)) {
|
2022-11-29 11:43:40 +01:00
|
|
|
$payment_method_type = $response->charges->data[0]->payment_method_details->card->brand;
|
2023-02-16 02:36:09 +01:00
|
|
|
} else {
|
2022-11-29 11:43:40 +01:00
|
|
|
$payment_method_type = 'visa';
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-11-29 11:43:40 +01:00
|
|
|
|
2022-09-14 02:51:41 +02:00
|
|
|
$status = Payment::STATUS_COMPLETED;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-12-17 07:36:13 +01:00
|
|
|
|
2023-03-11 08:30:23 +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
|
|
|
];
|
|
|
|
|
2022-09-14 02:51:41 +02:00
|
|
|
$payment = $this->stripe->createPayment($data, $status);
|
2020-09-04 00:01:17 +02:00
|
|
|
$payment->meta = $cgt->meta;
|
|
|
|
$payment->save();
|
2020-07-15 07:05:02 +02:00
|
|
|
|
2022-11-29 11:43:40 +01: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;
|
|
|
|
break;
|
|
|
|
case 'mastercard':
|
|
|
|
return PaymentType::MASTERCARD;
|
|
|
|
break;
|
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;
|
|
|
|
break;
|
|
|
|
}
|
2020-07-14 14:50:16 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|