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

192 lines
6.8 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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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\Events\Payment\PaymentWasCreated;
use App\Jobs\Util\SystemLogger;
2020-07-14 14:50:16 +02:00
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
2020-07-15 07:05:02 +02:00
use App\Models\Invoice;
use App\Models\PaymentHash;
2020-07-15 07:05:02 +02:00
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\Stripe\ACH;
use App\PaymentDrivers\StripePaymentDriver;
2020-07-15 07:05:02 +02:00
use App\Utils\Ninja;
2020-10-14 12:45:26 +02:00
use App\Utils\Traits\MakesHash;
2020-10-28 11:10:49 +01:00
use Stripe\Exception\ApiConnectionException;
use Stripe\Exception\ApiErrorException;
use Stripe\Exception\AuthenticationException;
use Stripe\Exception\CardException;
use Stripe\Exception\InvalidRequestException;
use Stripe\Exception\RateLimitException;
use Stripe\StripeClient;
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
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
*/
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);
}
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$invoice = Invoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->withTrashed()->first();
if ($invoice) {
2020-07-14 14:50:16 +02:00
$description = "Invoice {$invoice->number} for {$amount} for client {$this->stripe->client->present()->name()}";
} else {
2020-07-14 14:50:16 +02:00
$description = "Payment with no invoice for amount {$amount} for client {$this->stripe->client->present()->name()}";
}
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-05-08 03:22:59 +02:00
2021-05-11 23:58:18 +02:00
$response = $this->stripe->createPaymentIntent($data, $this->stripe->stripe_connect_auth);
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) {
case $e instanceof CardException:
$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;
2021-02-01 22:33:04 +01:00
break;
case $e instanceof RateLimitException:
$data['message'] = 'Too many requests made to the API too quickly';
2021-02-01 22:33:04 +01:00
break;
case $e instanceof InvalidRequestException:
$data['message'] = 'Invalid parameters were supplied to Stripe\'s API';
2021-02-01 22:33:04 +01:00
break;
case $e instanceof AuthenticationException:
$data['message'] = 'Authentication with Stripe\'s API failed';
2021-02-01 22:33:04 +01:00
break;
case $e instanceof ApiErrorException:
$data['message'] = 'Network communication with Stripe failed';
2021-02-01 22:33:04 +01:00
break;
default:
$data['message'] = $e->getMessage();
2021-02-01 22:33:04 +01:00
break;
}
$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;
} else {
2022-05-09 00:19:35 +02:00
$payment_method_type = $response->charges->data[0]->payment_method_details->card->brand;
}
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),
2020-07-15 07:05:02 +02:00
'transaction_reference' => $response->charges->data[0]->id,
2021-01-27 11:38:28 +01:00
'amount' => $amount,
2020-07-15 07:05:02 +02:00
];
2021-01-27 11:38:28 +01:00
$payment = $this->stripe->createPayment($data);
$payment->meta = $cgt->meta;
$payment->save();
2020-07-15 07:05:02 +02:00
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;
2020-07-15 07:05:02 +02:00
default:
return PaymentType::CREDIT_CARD_OTHER;
break;
}
2020-07-14 14:50:16 +02:00
}
}