From 0268cdbf9eaddbf27e3b0f9e05aac09999167e7c Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 29 Nov 2022 21:43:40 +1100 Subject: [PATCH] Refactor for Stripe payment intents + charges + new api --- app/Console/Commands/DemoMode.php | 29 +++------------------- app/PaymentDrivers/Stripe/Charge.php | 13 ++++++++-- app/PaymentDrivers/StripePaymentDriver.php | 24 ++++++++++++------ 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/app/Console/Commands/DemoMode.php b/app/Console/Commands/DemoMode.php index 1e01f40c16e2..d7c8bf94b0d5 100644 --- a/app/Console/Commands/DemoMode.php +++ b/app/Console/Commands/DemoMode.php @@ -41,6 +41,7 @@ use App\Models\Vendor; use App\Models\VendorContact; use App\Repositories\InvoiceRepository; use App\Utils\Ninja; +use App\Utils\Traits\AppSetup; use App\Utils\Traits\GeneratesCounter; use App\Utils\Traits\MakesHash; use Carbon\Carbon; @@ -53,7 +54,7 @@ use Illuminate\Support\Str; class DemoMode extends Command { - use MakesHash, GeneratesCounter; + use MakesHash, GeneratesCounter, AppSetup; protected $signature = 'ninja:demo-mode'; @@ -89,30 +90,8 @@ class DemoMode extends Command $this->info('Seeding'); Artisan::call('db:seed --force'); - foreach ($cached_tables as $name => $class) { - if (! Cache::has($name)) { - // check that the table exists in case the migration is pending - if (! Schema::hasTable((new $class())->getTable())) { - continue; - } - if ($name == 'payment_terms') { - $orderBy = 'num_days'; - } elseif ($name == 'fonts') { - $orderBy = 'sort_order'; - } elseif (in_array($name, ['currencies', 'industries', 'languages', 'countries', 'banks'])) { - $orderBy = 'name'; - } else { - $orderBy = 'id'; - } - $tableData = $class::orderBy($orderBy)->get(); - if ($tableData->count()) { - Cache::forever($name, $tableData); - } - } - } - - - + $this->buildCache(true); + $this->info('Seeding Random Data'); $this->createSmallAccount(); diff --git a/app/PaymentDrivers/Stripe/Charge.php b/app/PaymentDrivers/Stripe/Charge.php index 0c781c09ec4d..7dbd60c07f57 100644 --- a/app/PaymentDrivers/Stripe/Charge.php +++ b/app/PaymentDrivers/Stripe/Charge.php @@ -141,7 +141,16 @@ class Charge $payment_method_type = PaymentType::SEPA; $status = Payment::STATUS_PENDING; } else { - $payment_method_type = $response->charges->data[0]->payment_method_details->card->brand; + + 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; + } + elseif(isset($response->charges->data[0]->payment_method_details->card->brand)) + $payment_method_type = $response->charges->data[0]->payment_method_details->card->brand; + else + $payment_method_type = 'visa'; + $status = Payment::STATUS_COMPLETED; } @@ -153,7 +162,6 @@ class Charge 'gateway_type_id' => $cgt->gateway_type_id, 'payment_type' => $this->transformPaymentTypeToConstant($payment_method_type), 'transaction_reference' => isset($response->latest_charge) ? $response->latest_charge : $response->charges->data[0]->id, - // 'transaction_reference' => $response->charges->data[0]->id, 'amount' => $amount, ]; @@ -161,6 +169,7 @@ class Charge $payment->meta = $cgt->meta; $payment->save(); + $payment_hash->data = array_merge((array) $payment_hash->data, ['payment_intent' => $response, 'amount_with_fee' => $amount]); $payment_hash->payment_id = $payment->id; $payment_hash->save(); diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index ba180985cb06..b0d5751e539a 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -660,14 +660,22 @@ class StripePaymentDriver extends BaseDriver ], $this->stripe_connect_auth); if ($charge->captured) { - $payment = Payment::query() - ->where('transaction_reference', $transaction['payment_intent']) - ->where('company_id', $request->getCompany()->id) - ->where(function ($query) use ($transaction) { - $query->where('transaction_reference', $transaction['payment_intent']) - ->orWhere('transaction_reference', $transaction['id']); - }) - ->first(); + + $payment = false; + + if(isset($transaction['payment_intent'])) + { + $payment = Payment::query() + ->where('transaction_reference', $transaction['payment_intent']) + ->where('company_id', $request->getCompany()->id) + ->first(); + } + elseif(isset($transaction['id'])) { + $payment = Payment::query() + ->where('transaction_reference', $transaction['id']) + ->where('company_id', $request->getCompany()->id) + ->first(); + } if ($payment) { $payment->status_id = Payment::STATUS_COMPLETED;