diff --git a/app/PaymentDrivers/Stripe/BACS.php b/app/PaymentDrivers/Stripe/BACS.php index 85dd8e86c4f2..3515d2d20f8b 100644 --- a/app/PaymentDrivers/Stripe/BACS.php +++ b/app/PaymentDrivers/Stripe/BACS.php @@ -169,7 +169,7 @@ class BACS $payment_meta = new \stdClass; $payment_meta->brand = (string) $method->bacs_debit->sort_code; $payment_meta->last4 = (string) $method->bacs_debit->last4; - $payment_meta->state = 'authorized'; + $payment_meta->state = 'unauthorized'; $payment_meta->type = GatewayType::BACS; $data = [ diff --git a/app/PaymentDrivers/Stripe/Jobs/PaymentIntentWebhook.php b/app/PaymentDrivers/Stripe/Jobs/PaymentIntentWebhook.php index e6971ae79a67..4876dadd3ad6 100644 --- a/app/PaymentDrivers/Stripe/Jobs/PaymentIntentWebhook.php +++ b/app/PaymentDrivers/Stripe/Jobs/PaymentIntentWebhook.php @@ -97,7 +97,7 @@ class PaymentIntentWebhook implements ShouldQueue if(isset($this->stripe_request['object']['charges']) && optional($this->stripe_request['object']['charges']['data'][0])['id']) $charge_id = $this->stripe_request['object']['charges']['data'][0]['id']; // API VERSION 2018 - elseif (isset($this->stripe_request['object']['latest_charge'])) + elseif (isset($this->stripe_request['object']['latest_charge'])) $charge_id = $this->stripe_request['object']['latest_charge']; // API VERSION 2022-11-15 @@ -126,7 +126,7 @@ class PaymentIntentWebhook implements ShouldQueue //return early if($payment && $payment->status_id == Payment::STATUS_COMPLETED){ - nlog(" payment found and status correct - returning "); + nlog(" payment found and status correct - returning "); return; } elseif($payment){ @@ -196,6 +196,9 @@ class PaymentIntentWebhook implements ShouldQueue $this->updateAchPayment($payment_hash, $client, $meta); } + elseif(isset($pi['payment_method_types']) && in_array('bacs_debit', $pi['payment_method_types'])){ + return; + } } @@ -216,7 +219,7 @@ class PaymentIntentWebhook implements ShouldQueue 'transaction_reference' => $meta['transaction_reference'], 'gateway_type_id' => GatewayType::BANK_TRANSFER, ]; - + $payment = $driver->createPayment($data, Payment::STATUS_COMPLETED); SystemLogger::dispatch( @@ -265,7 +268,7 @@ class PaymentIntentWebhook implements ShouldQueue } $driver->storeGatewayToken($data, $additional_data); - + } catch(\Exception $e){ nlog("failed to import payment methods"); @@ -291,7 +294,7 @@ class PaymentIntentWebhook implements ShouldQueue // 'transaction_reference' => $meta['transaction_reference'], // 'gateway_type_id' => GatewayType::CREDIT_CARD, // ]; - + // $payment = $driver->createPayment($data, Payment::STATUS_COMPLETED); // SystemLogger::dispatch( @@ -324,7 +327,7 @@ class PaymentIntentWebhook implements ShouldQueue 'transaction_reference' => $meta['transaction_reference'], 'gateway_type_id' => GatewayType::CREDIT_CARD, ]; - + $payment = $driver->createPayment($data, Payment::STATUS_COMPLETED); SystemLogger::dispatch( @@ -338,4 +341,4 @@ class PaymentIntentWebhook implements ShouldQueue } -} \ No newline at end of file +} diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index d60b26eb5dc0..d58948f00ddc 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -715,11 +715,53 @@ class StripePaymentDriver extends BaseDriver } } } elseif ($request->type === "payment_method.automatically_updated"){ + // Will notify customer on updated information return response()->json([], 200); } elseif ($request->type === "checkout.session.completed"){ - return response()->json([], 200); - } elseif ($request->type === "mandate.updated"){ + // Store payment token for Stripe BACS + try { + $setup_intent = $this->stripe->stripe->setupIntents->retrieve($request->data->setup_inent, []); + $customer = $this->stripe->findOrCreateCustomer(); + $this->stripe->attach($setup_intent->payment_method, $customer); + $payment_method = $this->stripe->getStripePaymentMethod($setup_intent->payment_method); + $payment_meta = new \stdClass; + $payment_meta->brand = (string) $payment_method->bacs_debit->sort_code; + $payment_meta->last4 = (string) $payment_method->bacs_debit->last4; + $payment_meta->state = 'unauthorized'; + $payment_meta->type = GatewayType::BACS; + $data = [ + 'payment_meta' => $payment_meta, + 'token' => $payment_method->id, + 'payment_method_id' => GatewayType::BACS, + ]; + $this->stripe->storeGatewayToken($data, ['gateway_customer_reference' => $customer->id]); + return response()->json([], 200); + } catch (\Exception $e) { + return $this->stripe->processInternallyFailedPayment($this->stripe, $e); + } + } elseif ($request->type === "mandate.updated"){ + // Check if payment method BACS is still valid + if ($request->data->status === "active"){ + // Check if payment method exists + $clientgateway = ClientGatewayToken::query() + ->where('token', $request->data->payment_method) + ->first(); + if ($clientgateway){ + $clientgateway->state = "authorized"; + $clientgateway->save(); + } + } + elseif ($request->data->status === "inactive"){ + // Deactivate payment method + $clientgateway = ClientGatewayToken::query() + ->where('token', $request->data->payment_method) + ->first(); + $clientgateway->delete(); + } + elseif ($request->data->status === "pending"){ + // Do nothing + } return response()->json([], 200); }