refactor paymentwebhook suite with new methods

This commit is contained in:
Benjamin Beganović 2020-12-07 14:49:30 +01:00
parent 2609e0027a
commit c548bc2e0d
2 changed files with 57 additions and 49 deletions

View File

@ -13,8 +13,6 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Http\Requests\Payments\PaymentWebhookRequest; use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Models\Payment;
use Illuminate\Support\Arr;
class PaymentWebhookController extends Controller class PaymentWebhookController extends Controller
{ {
@ -23,36 +21,10 @@ class PaymentWebhookController extends Controller
$this->middleware('guest'); $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); return $request->getCompanyGateway()
->driver($request->getClient())
$payment = Payment::where('transaction_reference', $transaction_reference)->first(); ->processWebhookRequest($request, $request->getPayment());
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
}
} }
} }

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Invoice Ninja (https://invoiceninja.com). * Invoice Ninja (https://invoiceninja.com).
* *
@ -9,12 +10,14 @@
* @license https://opensource.org/licenses/AAL * @license https://opensource.org/licenses/AAL
*/ */
namespace App\Http\Requests\Payments; namespace App\Http\Requests\Payments;
use App\Http\Requests\Request; use App\Http\Requests\Request;
use App\Models\Client;
use App\Models\Company; use App\Models\Company;
use App\Models\CompanyGateway; use App\Models\CompanyGateway;
use App\Models\Payment;
use App\Models\PaymentHash;
class PaymentWebhookRequest extends Request class PaymentWebhookRequest extends Request
{ {
@ -30,28 +33,61 @@ class PaymentWebhookRequest extends Request
]; ];
} }
public function company() /**
* Resolve company gateway.
*
* @param mixed $id
* @return null|\App\Models\CompanyGateway
*/
public function getCompanyGateway(): ?CompanyGateway
{ {
if (! $this->company_key) { return CompanyGateway::where('gateway_key', $this->gateway_key)->firstOrFail();
return false;
}
return Company::query()
->where('company_key', $this->company_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) { if ($this->query('hash')) {
return false; 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() return $hash->payment;
->where('gateway_key', $this->gateway_key) }
->where('company_id', $company->id)
->firstOrFail(); /**
* 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();
} }
} }