diff --git a/app/Http/Controllers/PaymentWebhookController.php b/app/Http/Controllers/PaymentWebhookController.php index e1884e7af149..b32afa1d4c11 100644 --- a/app/Http/Controllers/PaymentWebhookController.php +++ b/app/Http/Controllers/PaymentWebhookController.php @@ -13,8 +13,6 @@ namespace App\Http\Controllers; use App\Http\Requests\Payments\PaymentWebhookRequest; -use App\Models\Payment; -use Illuminate\Support\Arr; class PaymentWebhookController extends Controller { @@ -23,36 +21,10 @@ class PaymentWebhookController extends Controller $this->middleware('guest'); } - public function __invoke(PaymentWebhookRequest $request, string $company_key = null, string $gateway_key = null) + public function __invoke(PaymentWebhookRequest $request, string $gateway_key, string $company_key) { - $transaction_reference = $this->getTransactionReference($request->all(), $request); - - $payment = Payment::where('transaction_reference', $transaction_reference)->first(); - - if (is_null($payment)) { - return response([ - 'message' => 'Sorry, we couldn\'t find requested payment.', - 'status_code' => 404, - ], 404); /* Record event, throw an exception.. */ - } - - return $request - ->companyGateway() - ->driver($payment->client) - ->setPaymentMethod($payment->gateway_type_id) - ->processWebhookRequest($request->all(), $request->company(), $request->companyGateway(), $payment); - } - - public function getTransactionReference(array $data, PaymentWebhookRequest $request) - { - $flatten = Arr::dot($data); - - if (isset($flatten['data.object.id'])) { - return $flatten['data.object.id']; // stripe.com - } - - if ($request->has('cko-session-id')) { - // checkout.com - } + return $request->getCompanyGateway() + ->driver($request->getClient()) + ->processWebhookRequest($request, $request->getPayment()); } } diff --git a/app/Http/Requests/Payments/PaymentWebhookRequest.php b/app/Http/Requests/Payments/PaymentWebhookRequest.php index 7dc483272a23..81b44bcfd1b0 100644 --- a/app/Http/Requests/Payments/PaymentWebhookRequest.php +++ b/app/Http/Requests/Payments/PaymentWebhookRequest.php @@ -1,4 +1,5 @@ company_key) { - return false; - } - - return Company::query() - ->where('company_key', $this->company_key) - ->firstOrFail(); + return CompanyGateway::where('gateway_key', $this->gateway_key)->firstOrFail(); } - public function companyGateway() + /** + * Resolve payment hash. + * + * @param string $hash + * @return null|\App\Http\Requests\Payments\PaymentHash + */ + public function getPaymentHash(): ?PaymentHash { - if (! $this->gateway_key || ! $this->company_key) { - return false; + if ($this->query('hash')) { + return PaymentHash::where('hash', $this->query('hash'))->firstOrFail(); } + } - $company = $this->company(); + /** + * Resolve possible payment in the request. + * + * @return null|\App\Models\Payment + */ + public function getPayment(): ?Payment + { + $hash = $this->getPaymentHash(); - return CompanyGateway::query() - ->where('gateway_key', $this->gateway_key) - ->where('company_id', $company->id) - ->firstOrFail(); + return $hash->payment; + } + + /** + * Resolve client from payment hash. + * + * @return null|\App\Models\Client + */ + public function getClient(): ?Client + { + $hash = $this->getPaymentHash(); + + return Client::find($hash->data->client_id)->firstOrFail(); + } + + /** + * Resolve company from company_key parameter. + * + * @return null|\App\Models\Company + */ + public function getCompany(): ?Company + { + return Company::where('company_key', $this->company_key)->firstOrFail(); } }