Added webhooks and changed default BACS state to unauthorzied

This commit is contained in:
Lars Kusch 2023-01-16 19:19:14 +01:00
parent d5397a2954
commit f14131b494
3 changed files with 55 additions and 10 deletions

View File

@ -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 = [

View File

@ -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
}
}
}

View File

@ -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);
}