Support latest version of Stripe API

This commit is contained in:
David Bomba 2022-11-26 07:29:15 +11:00
parent ed97890b7e
commit 0210cededc
4 changed files with 103 additions and 92 deletions

View File

@ -48,7 +48,7 @@ class Kernel extends ConsoleKernel
$schedule->job(new VersionCheck)->daily(); $schedule->job(new VersionCheck)->daily();
/* Checks and cleans redundant files */ /* Checks and cleans redundant files */
$schedule->job(new DiskCleanup)->daily()->withoutOverlapping(); $schedule->job(new DiskCleanup)->dailyAt('02:10')->withoutOverlapping();
/* Send reminders */ /* Send reminders */
$schedule->job(new ReminderJob)->hourly()->withoutOverlapping(); $schedule->job(new ReminderJob)->hourly()->withoutOverlapping();
@ -63,7 +63,7 @@ class Kernel extends ConsoleKernel
$schedule->job(new UpdateExchangeRates)->dailyAt('23:30')->withoutOverlapping(); $schedule->job(new UpdateExchangeRates)->dailyAt('23:30')->withoutOverlapping();
/* Runs cleanup code for subscriptions */ /* Runs cleanup code for subscriptions */
$schedule->job(new SubscriptionCron)->daily()->withoutOverlapping(); $schedule->job(new SubscriptionCron)->dailyAt('00:01')->withoutOverlapping();
/* Sends recurring invoices*/ /* Sends recurring invoices*/
$schedule->job(new RecurringInvoicesCron)->hourly()->withoutOverlapping(); $schedule->job(new RecurringInvoicesCron)->hourly()->withoutOverlapping();
@ -72,22 +72,22 @@ class Kernel extends ConsoleKernel
$schedule->job(new RecurringExpensesCron)->dailyAt('00:10')->withoutOverlapping(); $schedule->job(new RecurringExpensesCron)->dailyAt('00:10')->withoutOverlapping();
/* Fires notifications for expired Quotes */ /* Fires notifications for expired Quotes */
$schedule->job(new QuoteCheckExpired)->dailyAt('05:00')->withoutOverlapping(); $schedule->job(new QuoteCheckExpired)->dailyAt('05:10')->withoutOverlapping();
/* Performs auto billing */ /* Performs auto billing */
$schedule->job(new AutoBillCron)->dailyAt('06:00')->withoutOverlapping(); $schedule->job(new AutoBillCron)->dailyAt('06:20')->withoutOverlapping();
/* Checks the status of the scheduler */ /* Checks the status of the scheduler */
$schedule->job(new SchedulerCheck)->daily()->withoutOverlapping(); $schedule->job(new SchedulerCheck)->dailyAt('01:10')->withoutOverlapping();
/* Checks for scheduled tasks */ /* Checks for scheduled tasks */
$schedule->job(new TaskScheduler())->daily()->withoutOverlapping(); $schedule->job(new TaskScheduler())->dailyAt('06:50')->withoutOverlapping();
/* Performs system maintenance such as pruning the backup table */ /* Performs system maintenance such as pruning the backup table */
$schedule->job(new SystemMaintenance)->weekly()->withoutOverlapping(); $schedule->job(new SystemMaintenance)->weekly()->withoutOverlapping();
/* Pulls in bank transactions from third party services */ /* Pulls in bank transactions from third party services */
$schedule->job(new BankTransactionSync)->dailyAt('04:00')->withoutOverlapping(); $schedule->job(new BankTransactionSync)->dailyAt('04:10')->withoutOverlapping();
if (Ninja::isSelfHost()) { if (Ninja::isSelfHost()) {

View File

@ -138,7 +138,7 @@ class CreditCard
'payment_method' => $this->stripe->payment_hash->data->server_response->payment_method, 'payment_method' => $this->stripe->payment_hash->data->server_response->payment_method,
'payment_type' => PaymentType::parseCardType(strtolower($stripe_method->card->brand)) ?: PaymentType::CREDIT_CARD_OTHER, 'payment_type' => PaymentType::parseCardType(strtolower($stripe_method->card->brand)) ?: PaymentType::CREDIT_CARD_OTHER,
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->server_response->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()), 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->server_response->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'transaction_reference' => optional($this->stripe->payment_hash->data->payment_intent->charges->data[0])->id, 'transaction_reference' => isset($this->stripe->payment_hash->data->payment_intent->latest_charge) ? $this->stripe->payment_hash->data->payment_intent->latest_charge : optional($this->stripe->payment_hash->data->payment_intent->charges->data[0])->id,
'gateway_type_id' => GatewayType::CREDIT_CARD, 'gateway_type_id' => GatewayType::CREDIT_CARD,
]; ];

View File

@ -60,118 +60,124 @@ class PaymentIntentWebhook implements ShouldQueue
$company = Company::where('company_key', $this->company_key)->first(); $company = Company::where('company_key', $this->company_key)->first();
foreach ($this->stripe_request as $transaction) { nlog($this->stripe_request);
if(array_key_exists('payment_intent', $transaction)) // foreach ($this->stripe_request as $transaction) {
{
$payment = Payment::query()
->where('company_id', $company->id)
->where(function ($query) use ($transaction) {
$query->where('transaction_reference', $transaction['payment_intent'])
->orWhere('transaction_reference', $transaction['id']);
})
->first();
}
else
{
$payment = Payment::query()
->where('company_id', $company->id)
->where('transaction_reference', $transaction['id'])
->first();
}
if ($payment) { // if(array_key_exists('payment_intent', $transaction))
$payment->status_id = Payment::STATUS_COMPLETED; // {
$payment->save(); // $payment = Payment::query()
// ->where('company_id', $company->id)
// ->where(function ($query) use ($transaction) {
// $query->where('transaction_reference', $transaction['payment_intent'])
// ->orWhere('transaction_reference', $transaction['id']);
// })
// ->first();
// }
// else
// {
// $payment = Payment::query()
// ->where('company_id', $company->id)
// ->where('transaction_reference', $transaction['id'])
// ->first();
// }
// if ($payment) {
// $payment->status_id = Payment::STATUS_COMPLETED;
// $payment->save();
$this->payment_completed = true; // $this->payment_completed = true;
} // }
} // }
if($this->payment_completed) // if($this->payment_completed)
return; // return;
if(isset($this->stripe_request['object']['charges']) && optional($this->stripe_request['object']['charges']['data'][0])['id']){ // if(isset($this->stripe_request['object']['charges']) && optional($this->stripe_request['object']['charges']['data'][0])['id']){
$company = Company::where('company_key', $this->company_key)->first(); // $company = Company::where('company_key', $this->company_key)->first();
$payment = Payment::query() // $payment = Payment::query()
->where('company_id', $company->id) // ->where('company_id', $company->id)
->where('transaction_reference', $this->stripe_request['object']['charges']['data'][0]['id']) // ->where('transaction_reference', $this->stripe_request['object']['charges']['data'][0]['id'])
->first(); // ->first();
//return early // //return early
if($payment && $payment->status_id == Payment::STATUS_COMPLETED){ // if($payment && $payment->status_id == Payment::STATUS_COMPLETED){
nlog(" payment found and status correct - returning "); // nlog(" payment found and status correct - returning ");
return; // return;
} // }
elseif($payment){ // elseif($payment){
$payment->status_id = Payment::STATUS_COMPLETED; // $payment->status_id = Payment::STATUS_COMPLETED;
$payment->save(); // $payment->save();
} // }
$hash = optional($this->stripe_request['object']['charges']['data'][0]['metadata'])['payment_hash']; // $hash = optional($this->stripe_request['object']['charges']['data'][0]['metadata'])['payment_hash'];
$payment_hash = PaymentHash::where('hash', $hash)->first(); // $payment_hash = PaymentHash::where('hash', $hash)->first();
if(!$payment_hash) // if(!$payment_hash)
return; // return;
nlog("payment intent"); // nlog("payment intent");
nlog($this->stripe_request); // nlog($this->stripe_request);
if(array_key_exists('allowed_source_types', $this->stripe_request['object']) && optional($this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash']) && in_array('card', $this->stripe_request['object']['allowed_source_types'])) // if(array_key_exists('allowed_source_types', $this->stripe_request['object']) && optional($this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash']) && in_array('card', $this->stripe_request['object']['allowed_source_types']))
{ // {
nlog("hash found"); // nlog("hash found");
$hash = $this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash']; // $hash = $this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash'];
$payment_hash = PaymentHash::where('hash', $hash)->first(); // $payment_hash = PaymentHash::where('hash', $hash)->first();
$invoice = Invoice::with('client')->find($payment_hash->fee_invoice_id); // $invoice = Invoice::with('client')->find($payment_hash->fee_invoice_id);
$client = $invoice->client; // $client = $invoice->client;
$this->updateCreditCardPayment($payment_hash, $client); // $this->updateCreditCardPayment($payment_hash, $client);
} // }
elseif(array_key_exists('payment_method_types', $this->stripe_request['object']) && optional($this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash']) && in_array('card', $this->stripe_request['object']['payment_method_types'])) // elseif(array_key_exists('payment_method_types', $this->stripe_request['object']) && optional($this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash']) && in_array('card', $this->stripe_request['object']['payment_method_types']))
{ // {
nlog("hash found"); // nlog("hash found");
$hash = $this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash']; // $hash = $this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash'];
$payment_hash = PaymentHash::where('hash', $hash)->first(); // $payment_hash = PaymentHash::where('hash', $hash)->first();
$invoice = Invoice::with('client')->find($payment_hash->fee_invoice_id); // $invoice = Invoice::with('client')->find($payment_hash->fee_invoice_id);
$client = $invoice->client; // $client = $invoice->client;
$this->updateCreditCardPayment($payment_hash, $client); // $this->updateCreditCardPayment($payment_hash, $client);
} // }
elseif(array_key_exists('payment_method_types', $this->stripe_request['object']) && optional($this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash']) && in_array('us_bank_account', $this->stripe_request['object']['payment_method_types'])) // elseif(array_key_exists('payment_method_types', $this->stripe_request['object']) && optional($this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash']) && in_array('us_bank_account', $this->stripe_request['object']['payment_method_types']))
{ // {
nlog("hash found"); // nlog("hash found");
$hash = $this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash']; // $hash = $this->stripe_request['object']['charges']['data'][0]['metadata']['payment_hash'];
$payment_hash = PaymentHash::where('hash', $hash)->first(); // $payment_hash = PaymentHash::where('hash', $hash)->first();
$invoice = Invoice::with('client')->find($payment_hash->fee_invoice_id); // $invoice = Invoice::with('client')->find($payment_hash->fee_invoice_id);
$client = $invoice->client; // $client = $invoice->client;
$this->updateAchPayment($payment_hash, $client); // $this->updateAchPayment($payment_hash, $client);
} // }
SystemLogger::dispatch( // SystemLogger::dispatch(
['response' => $this->stripe_request, 'data' => []], // ['response' => $this->stripe_request, 'data' => []],
SystemLog::CATEGORY_GATEWAY_RESPONSE, // SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS, // SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_STRIPE, // SystemLog::TYPE_STRIPE,
null, // null,
$company, // $company,
); // );
return; // return;
} // }
nlog("if");
nlog($this->stripe_request['object']['latest_charge']);
if(isset($this->stripe_request['object']['latest_charge'])) if(isset($this->stripe_request['object']['latest_charge']))
{ {
@ -179,6 +185,8 @@ class PaymentIntentWebhook implements ShouldQueue
$charge = reset($pi->charges->data); $charge = reset($pi->charges->data);
nlog($charge);
$company = Company::where('company_key', $this->company_key)->first(); $company = Company::where('company_key', $this->company_key)->first();
$payment = Payment::query() $payment = Payment::query()

View File

@ -116,11 +116,14 @@ class StripePaymentDriver extends BaseDriver
throw new StripeConnectFailure('Stripe Connect has not been configured'); throw new StripeConnectFailure('Stripe Connect has not been configured');
} }
} else { } else {
$this->stripe = new StripeClient( $this->stripe = new StripeClient(
$this->company_gateway->getConfigField('apiKey') $this->company_gateway->getConfigField('apiKey')
); );
Stripe::setApiKey($this->company_gateway->getConfigField('apiKey')); Stripe::setApiKey($this->company_gateway->getConfigField('apiKey'));
// Stripe::setApiVersion('2022-11-15');
} }
return $this; return $this;