1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/PaymentDrivers/Stripe/Jobs/PaymentIntentPartiallyFundedWebhook.php

96 lines
3.2 KiB
PHP
Raw Normal View History

2023-02-23 06:52:45 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\Stripe\Jobs;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\CompanyGateway;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\PaymentDrivers\Stripe\Utilities;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class PaymentIntentPartiallyFundedWebhook implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Utilities;
public $tries = 1;
public $deleteWhenMissingModels = true;
public function __construct(public array $stripe_request, public string $company_key, public int $company_gateway_id)
{
}
public function handle()
{
MultiDB::findAndSetDbByCompanyKey($this->company_key);
2023-08-08 12:39:46 +02:00
$company = Company::query()->where('company_key', $this->company_key)->first();
2023-02-23 06:52:45 +01:00
foreach ($this->stripe_request as $transaction) {
$payment_intent = false;
if (array_key_exists('payment_intent', $transaction)) {
$payment_intent = $transaction['payment_intent'];
} else {
$payment_intent = $transaction['id'];
}
2023-03-18 08:24:56 +01:00
if (!$payment_intent) {
2023-02-23 06:52:45 +01:00
nlog("payment intent not found");
nlog($transaction);
return;
}
$payment = Payment::query()
->where('company_id', $company->id)
->where('transaction_reference', $payment_intent)
->first();
2023-03-18 08:24:56 +01:00
if (!$payment) {
2023-02-23 06:52:45 +01:00
nlog("paymentintent found but no payment");
}
2023-08-08 12:39:46 +02:00
$company_gateway = CompanyGateway::query()->find($this->company_gateway_id);
2023-02-23 06:52:45 +01:00
$stripe_driver = $company_gateway->driver()->init();
$hash = isset($transaction['metadata']['payment_hash']) ? $transaction['metadata']['payment_hash'] : false;
if (!$hash) {
nlog("no hash found");
return;
}
$payment_hash = PaymentHash::where('hash', $hash)->first();
if (!$payment_hash) {
nlog("no payment hash found");
return;
}
$stripe_driver->client = $payment_hash->fee_invoice->client;
$pi = \Stripe\PaymentIntent::retrieve($payment_intent, $stripe_driver->stripe_connect_auth);
$amount = $stripe_driver->convertFromStripeAmount($pi->amount, $stripe_driver->client->currency()->precision, $stripe_driver->client->currency()->precision);
$amount_received = $stripe_driver->convertFromStripeAmount($pi->amount_received, $stripe_driver->client->currency()->precision, $stripe_driver->client->currency()->precision);
//at this point we just send notification emails to the client and advise of over/under payments.
}
}
}