1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/PaymentDrivers/Stripe/ACSS.php

403 lines
14 KiB
PHP
Raw Normal View History

2021-10-12 16:46:42 +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-10-12 16:46:42 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\Stripe;
2023-12-18 05:25:16 +01:00
use App\Models\Payment;
use App\Models\SystemLog;
use App\Models\GatewayType;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use Illuminate\Support\Str;
2021-10-13 15:53:32 +02:00
use App\Http\Requests\Request;
2023-12-18 05:25:16 +01:00
use App\Jobs\Util\SystemLogger;
use App\Utils\Traits\MakesHash;
use App\Exceptions\PaymentFailed;
use App\Models\ClientGatewayToken;
use Illuminate\Support\Facades\Cache;
2021-10-12 16:46:42 +02:00
use App\Jobs\Mail\PaymentFailureMailer;
use App\PaymentDrivers\StripePaymentDriver;
2023-12-18 05:25:16 +01:00
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use Stripe\PaymentIntent;
2021-10-12 16:46:42 +02:00
class ACSS
{
2023-12-18 05:25:16 +01:00
use MakesHash;
2021-10-12 16:46:42 +02:00
/** @var StripePaymentDriver */
public StripePaymentDriver $stripe;
public function __construct(StripePaymentDriver $stripe)
{
$this->stripe = $stripe;
2021-10-13 15:53:32 +02:00
$this->stripe->init();
2021-10-12 16:46:42 +02:00
}
2024-01-14 05:05:00 +01:00
2023-12-18 05:25:16 +01:00
/**
* Generate mandate for future ACSS billing
*
* @param mixed $data
* @return void
*/
2021-10-12 16:46:42 +02:00
public function authorizeView($data)
{
2021-10-13 15:53:32 +02:00
$data['gateway'] = $this->stripe;
2023-12-18 05:25:16 +01:00
$data['company_gateway'] = $this->stripe->company_gateway;
$data['customer'] = $this->stripe->findOrCreateCustomer()->id;
$data['country'] = $this->stripe->client->country->iso_3166_2;
$data['post_auth_response'] = false;
$intent = \Stripe\SetupIntent::create([
'usage' => 'off_session',
'payment_method_types' => ['acss_debit'],
'customer' => $data['customer'],
'payment_method_options' => [
'acss_debit' => [
'currency' => 'cad',
'mandate_options' => [
'payment_schedule' => 'combined',
'interval_description' => 'On any invoice due date',
'transaction_type' => 'personal',
],
'verification_method' => 'instant',
],
],
], $this->stripe->stripe_connect_auth);
$data['pi_client_secret'] = $intent->client_secret;
2021-10-13 15:53:32 +02:00
return render('gateways.stripe.acss.authorize', array_merge($data));
}
2024-01-14 05:05:00 +01:00
2023-12-18 05:25:16 +01:00
/**
* Authorizes the mandate for future billing
*
* @param Request $request
2023-12-18 05:25:16 +01:00
* @return void
*/
2021-10-13 15:53:32 +02:00
public function authorizeResponse(Request $request)
{
2023-12-18 05:25:16 +01:00
$setup_intent = json_decode($request->input('gateway_response'));
if (isset($setup_intent->type)) {
2024-01-14 05:05:00 +01:00
2023-12-18 05:25:16 +01:00
$error = "There was a problem setting up this payment method for future use";
if(in_array($setup_intent->type, ["validation_error", "invalid_request_error"])) {
$error = "Please provide complete payment details.";
}
SystemLogger::dispatch(
['response' => (array)$setup_intent, 'data' => $request->all()],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_STRIPE,
$this->stripe->client,
$this->stripe->client->company,
);
throw new PaymentFailed($error, 400);
}
2021-10-13 15:53:32 +02:00
2023-12-18 05:25:16 +01:00
$stripe_setup_intent = $this->stripe->getSetupIntentId($setup_intent->id); //needed to harvest the Mandate
2021-10-13 15:53:32 +02:00
2023-12-18 05:25:16 +01:00
$client_gateway_token = $this->storePaymentMethod($setup_intent->payment_method, $stripe_setup_intent->mandate, $setup_intent->status == 'succeeded' ? 'authorized' : 'unauthorized');
2021-10-13 15:53:32 +02:00
2023-12-18 05:25:16 +01:00
if($request->has('post_auth_response') && boolval($request->post_auth_response)) {
/** @var array $data */
$data = Cache::pull($request->post_auth_response);
2021-10-13 15:53:32 +02:00
2024-01-14 05:05:00 +01:00
if(!$data) {
2023-12-18 05:25:16 +01:00
throw new PaymentFailed("There was a problem storing this payment method", 500);
2024-01-14 05:05:00 +01:00
}
2021-10-13 15:53:32 +02:00
2023-12-18 05:25:16 +01:00
$hash = PaymentHash::with('fee_invoice')->where('hash', $data['payment_hash'])->first();
$data['tokens'] = [$client_gateway_token];
2021-10-13 15:53:32 +02:00
2024-01-14 05:05:00 +01:00
$this->stripe->setPaymentHash($hash);
2023-12-18 05:25:16 +01:00
$this->stripe->setClient($hash->fee_invoice->client);
$this->stripe->setPaymentMethod(GatewayType::ACSS);
2021-10-13 15:53:32 +02:00
2023-12-18 05:25:16 +01:00
return $this->continuePayment($data);
}
2021-10-13 15:53:32 +02:00
2023-12-18 05:25:16 +01:00
return redirect()->route('client.payment_methods.show', $client_gateway_token->hashed_id);
2021-10-13 15:53:32 +02:00
}
2024-01-14 05:05:00 +01:00
/**
* Generates a token Payment Intent
*
* @param ClientGatewayToken $token
2023-12-18 05:37:03 +01:00
* @return PaymentIntent
*/
private function tokenIntent(ClientGatewayToken $token): PaymentIntent
2021-10-13 15:53:32 +02:00
{
2023-12-18 05:25:16 +01:00
$intent = \Stripe\PaymentIntent::create([
'amount' => $this->stripe->convertToStripeAmount($this->stripe->payment_hash->amount_with_fee(), $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'currency' => $this->stripe->client->currency()->code,
'payment_method_types' => ['acss_debit'],
'customer' => $this->stripe->findOrCreateCustomer(),
'description' => $this->stripe->getDescription(false),
'metadata' => [
'payment_hash' => $this->stripe->payment_hash->hash,
'gateway_type_id' => GatewayType::ACSS,
],
2024-01-14 05:05:00 +01:00
'payment_method' => $token->token,
'mandate' => $token->meta?->mandate,
'confirm' => true,
2023-12-18 05:25:16 +01:00
], $this->stripe->stripe_connect_auth);
2021-10-13 15:53:32 +02:00
2023-12-18 05:25:16 +01:00
return $intent;
2021-10-13 15:53:32 +02:00
}
2024-01-14 05:05:00 +01:00
/**
* Payment view for ACSS
2024-01-14 05:05:00 +01:00
*
* Determines if any payment tokens are available and if not, generates a mandate
*
* @param array $data
2021-10-13 15:53:32 +02:00
*/
2023-12-18 05:25:16 +01:00
public function paymentView(array $data)
2021-10-13 15:53:32 +02:00
{
2024-01-14 05:05:00 +01:00
2023-12-18 05:25:16 +01:00
if(count($data['tokens']) == 0) {
$hash = Str::random(32);
Cache::put($hash, $data, 3600);
2024-01-14 05:05:00 +01:00
$data['post_auth_response'] = $hash;
2021-10-13 15:53:32 +02:00
2023-12-18 05:25:16 +01:00
return $this->generateMandate($data);
2021-10-13 15:53:32 +02:00
}
2023-12-18 05:25:16 +01:00
return $this->continuePayment($data);
2021-10-12 16:46:42 +02:00
}
2024-01-14 05:05:00 +01:00
/**
* Generate a payment Mandate for ACSS
*
* @param array $data
2021-10-12 16:46:42 +02:00
*/
2023-12-18 05:25:16 +01:00
private function generateMandate(array $data)
2021-10-12 16:46:42 +02:00
{
2021-10-12 16:46:42 +02:00
$data['gateway'] = $this->stripe;
2023-12-18 05:25:16 +01:00
$data['company_gateway'] = $this->stripe->company_gateway;
2021-10-12 16:46:42 +02:00
$data['customer'] = $this->stripe->findOrCreateCustomer()->id;
$data['country'] = $this->stripe->client->country->iso_3166_2;
2024-01-14 05:05:00 +01:00
2023-12-18 05:25:16 +01:00
$intent = \Stripe\SetupIntent::create([
'usage' => 'off_session',
2021-10-12 16:46:42 +02:00
'payment_method_types' => ['acss_debit'],
2023-12-18 05:25:16 +01:00
'customer' => $data['customer'],
2021-10-12 16:46:42 +02:00
'payment_method_options' => [
'acss_debit' => [
2023-12-18 05:25:16 +01:00
'currency' => 'cad',
'mandate_options' => [
'payment_schedule' => 'combined',
'interval_description' => 'On any invoice due date',
'transaction_type' => 'personal',
],
'verification_method' => 'instant',
],
],
2022-01-22 05:22:59 +01:00
], $this->stripe->stripe_connect_auth);
2021-10-12 16:46:42 +02:00
$data['pi_client_secret'] = $intent->client_secret;
2023-12-18 05:25:16 +01:00
return render('gateways.stripe.acss.authorize', array_merge($data));
}
2024-01-14 05:05:00 +01:00
/**
* Continues the payment flow after a Mandate has been successfully generated
*
* @param array $data
*/
2023-12-18 05:25:16 +01:00
private function continuePayment(array $data)
{
2024-01-14 05:05:00 +01:00
$this->stripe->init();
2023-12-18 05:25:16 +01:00
$data['gateway'] = $this->stripe;
$data['return_url'] = $this->buildReturnUrl();
$data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency());
$data['client'] = $this->stripe->client;
$data['customer'] = $this->stripe->findOrCreateCustomer()->id;
$data['country'] = $this->stripe->client->country->iso_3166_2;
2021-10-12 16:46:42 +02:00
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, ['stripe_amount' => $data['stripe_amount']]);
$this->stripe->payment_hash->save();
return render('gateways.stripe.acss.pay', $data);
}
2024-01-14 05:05:00 +01:00
/**
* ?redundant
*
* @return string
*/
2021-10-12 16:46:42 +02:00
private function buildReturnUrl(): string
{
return route('client.payments.response', [
'company_gateway_id' => $this->stripe->company_gateway->id,
'payment_hash' => $this->stripe->payment_hash->hash,
'payment_method_id' => GatewayType::ACSS,
]);
}
2024-01-14 05:05:00 +01:00
/**
* PaymentResponseRequest
*
2023-12-18 05:37:03 +01:00
* @param PaymentResponseRequest $request
*/
2021-10-12 16:46:42 +02:00
public function paymentResponse(PaymentResponseRequest $request)
{
2023-12-18 05:25:16 +01:00
2021-10-12 16:46:42 +02:00
$gateway_response = json_decode($request->gateway_response);
2023-12-18 05:25:16 +01:00
$cgt = ClientGatewayToken::find($this->decodePrimaryKey($request->token));
2023-12-18 05:37:03 +01:00
/** @var \Stripe\PaymentIntent $intent */
2023-12-18 05:25:16 +01:00
$intent = $this->tokenIntent($cgt);
2021-10-12 16:46:42 +02:00
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $request->all());
$this->stripe->payment_hash->save();
2023-12-18 05:25:16 +01:00
if ($intent->status && $intent->status == 'processing') {
return $this->processSuccessfulPayment($intent->id);
2021-10-12 16:46:42 +02:00
}
2021-10-12 16:46:42 +02:00
return $this->processUnsuccessfulPayment();
}
2024-01-14 05:05:00 +01:00
/**
* Performs token billing using a ACSS payment method
*
* @param ClientGatewayToken $cgt
* @param PaymentHash $payment_hash
*/
2023-12-18 05:25:16 +01:00
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
$this->stripe->init();
$this->stripe->setPaymentHash($payment_hash);
$this->stripe->setClient($cgt->client);
$stripe_amount = $this->stripe->convertToStripeAmount($payment_hash->amount_with_fee(), $this->stripe->client->currency()->precision, $this->stripe->client->currency());
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, ['stripe_amount' => $stripe_amount]);
$this->stripe->payment_hash->save();
2023-12-18 05:37:03 +01:00
/** @var \Stripe\PaymentIntent $intent */
2023-12-18 05:25:16 +01:00
$intent = $this->tokenIntent($cgt);
if ($intent->status && $intent->status == 'processing') {
$this->processSuccessfulPayment($intent->id);
2024-01-14 05:05:00 +01:00
} else {
2023-12-18 05:25:16 +01:00
$e = new \Exception("There was a problem processing this payment method", 500);
$this->stripe->processInternallyFailedPayment($this->stripe, $e);
}
}
2024-01-14 05:05:00 +01:00
/**
* Creates a payment for the transaction
*
* @param string $payment_intent
*/
2023-12-18 05:37:03 +01:00
public function processSuccessfulPayment(string $payment_intent)
2021-10-12 16:46:42 +02:00
{
$data = [
'payment_method' => $payment_intent,
'payment_type' => PaymentType::ACSS,
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'transaction_reference' => $payment_intent,
'gateway_type_id' => GatewayType::ACSS,
];
2021-10-15 15:38:01 +02:00
$payment = $this->stripe->createPayment($data, Payment::STATUS_PENDING);
2021-10-12 16:46:42 +02:00
SystemLogger::dispatch(
['response' => $this->stripe->payment_hash->data, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_STRIPE,
$this->stripe->client,
$this->stripe->client->company,
);
2021-10-15 15:38:01 +02:00
return redirect()->route('client.payments.show', $payment->hashed_id);
2021-10-12 16:46:42 +02:00
}
public function processUnsuccessfulPayment()
{
$server_response = $this->stripe->payment_hash->data;
PaymentFailureMailer::dispatch(
$this->stripe->client,
$server_response,
$this->stripe->client->company,
$this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency())
);
$message = [
'server_response' => $server_response,
'data' => $this->stripe->payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_STRIPE,
$this->stripe->client,
$this->stripe->client->company,
);
throw new PaymentFailed('Failed to process the payment.', 500);
}
2024-01-14 05:05:00 +01:00
/**
* Stores the payment token
*
* @param string $payment_method
* @param string $mandate
* @param string $status
* @return ClientGatewayToken
*/
2023-12-18 05:25:16 +01:00
private function storePaymentMethod(string $payment_method, string $mandate, string $status = 'authorized'): ?ClientGatewayToken
2021-10-12 16:46:42 +02:00
{
try {
2023-12-18 05:25:16 +01:00
$method = $this->stripe->getStripePaymentMethod($payment_method);
2021-10-12 16:46:42 +02:00
2024-01-14 05:05:00 +01:00
$payment_meta = new \stdClass();
2022-03-09 01:31:59 +01:00
$payment_meta->brand = (string) $method->acss_debit->bank_name;
$payment_meta->last4 = (string) $method->acss_debit->last4;
2023-12-18 05:25:16 +01:00
$payment_meta->state = $status;
2021-10-12 16:46:42 +02:00
$payment_meta->type = GatewayType::ACSS;
2023-12-18 05:25:16 +01:00
$payment_meta->mandate = $mandate;
2021-10-12 16:46:42 +02:00
$data = [
'payment_meta' => $payment_meta,
2023-12-18 05:25:16 +01:00
'token' => $payment_method,
2021-10-12 16:46:42 +02:00
'payment_method_id' => GatewayType::ACSS,
];
2023-12-18 05:25:16 +01:00
return $this->stripe->storeGatewayToken($data, ['gateway_customer_reference' => $method->customer]);
2021-10-12 16:46:42 +02:00
} catch (\Exception $e) {
return $this->stripe->processInternallyFailedPayment($this->stripe, $e);
}
}
}