2020-06-27 15:53:12 +02:00
|
|
|
<?php
|
2020-12-07 14:49:30 +01:00
|
|
|
|
2020-10-22 08:46:02 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
2020-06-27 15:53:12 +02:00
|
|
|
namespace App\Http\Requests\Payments;
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
use App\Http\Requests\Request;
|
2020-12-07 14:49:30 +01:00
|
|
|
use App\Models\Client;
|
2020-06-27 15:53:12 +02:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\CompanyGateway;
|
2020-12-07 14:49:30 +01:00
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\PaymentHash;
|
2020-12-16 13:47:10 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-06-27 15:53:12 +02:00
|
|
|
|
2020-10-22 08:46:02 +02:00
|
|
|
class PaymentWebhookRequest extends Request
|
2020-06-27 15:53:12 +02:00
|
|
|
{
|
2020-12-16 13:47:10 +01:00
|
|
|
use MakesHash;
|
|
|
|
|
2020-06-27 15:53:12 +02:00
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
//
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-12-07 14:49:30 +01:00
|
|
|
/**
|
|
|
|
* Resolve company gateway.
|
2020-12-07 14:56:23 +01:00
|
|
|
*
|
|
|
|
* @param mixed $id
|
|
|
|
* @return null|\App\Models\CompanyGateway
|
2020-12-07 14:49:30 +01:00
|
|
|
*/
|
|
|
|
public function getCompanyGateway(): ?CompanyGateway
|
|
|
|
{
|
2020-12-19 13:11:42 +01:00
|
|
|
return CompanyGateway::findOrFail($this->decodePrimaryKey($this->company_gateway_id));
|
2020-12-07 14:49:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve payment hash.
|
2020-12-07 14:56:23 +01:00
|
|
|
*
|
|
|
|
* @param string $hash
|
2020-12-16 15:25:42 +01:00
|
|
|
* @return null|\App\Models\PaymentHash
|
2020-12-07 14:49:30 +01:00
|
|
|
*/
|
|
|
|
public function getPaymentHash(): ?PaymentHash
|
2020-06-27 15:53:12 +02:00
|
|
|
{
|
2020-12-07 14:49:30 +01:00
|
|
|
if ($this->query('hash')) {
|
|
|
|
return PaymentHash::where('hash', $this->query('hash'))->firstOrFail();
|
2020-06-27 17:39:28 +02:00
|
|
|
}
|
2020-12-16 13:47:10 +01:00
|
|
|
|
|
|
|
return null;
|
2020-12-07 14:49:30 +01:00
|
|
|
}
|
2020-06-27 17:39:28 +02:00
|
|
|
|
2020-12-07 14:49:30 +01:00
|
|
|
/**
|
|
|
|
* Resolve possible payment in the request.
|
2020-12-07 14:56:23 +01:00
|
|
|
*
|
|
|
|
* @return null|\App\Models\Payment
|
2020-12-07 14:49:30 +01:00
|
|
|
*/
|
2020-12-19 13:11:42 +01:00
|
|
|
public function getPayment()
|
2020-12-07 14:49:30 +01:00
|
|
|
{
|
2020-12-19 13:11:42 +01:00
|
|
|
// For testing purposes we'll slow down the webhook processing by 2 seconds
|
|
|
|
// to make sure webhook request doesn't came before our processing.
|
|
|
|
if (app()->environment() !== 'production') {
|
|
|
|
sleep(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some gateways, like Checkout, we can dynamically pass payment hash,
|
|
|
|
// which we will resolve here and get payment information from it.
|
2020-12-16 15:25:42 +01:00
|
|
|
if ($this->getPaymentHash()) {
|
|
|
|
return $this->getPaymentHash()->payment;
|
|
|
|
}
|
|
|
|
|
2020-12-19 13:11:42 +01:00
|
|
|
// While for some gateways, we need to extract the payment source/reference from the webhook request.
|
|
|
|
// Gateways like this: Stripe
|
|
|
|
if ($this->has('api_version') && $this->has('type') && $this->has('data')) {
|
|
|
|
$src = $this->data['object']['id'];
|
|
|
|
|
|
|
|
return Payment::where('transaction_reference', $src)->firstOrFail();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If none of previously done logics is correct, we'll just display
|
|
|
|
// not found page.
|
2020-12-16 15:25:42 +01:00
|
|
|
abort(404);
|
2020-06-27 15:53:12 +02:00
|
|
|
}
|
|
|
|
|
2020-12-07 14:49:30 +01:00
|
|
|
/**
|
|
|
|
* Resolve client from payment hash.
|
2020-12-07 14:56:23 +01:00
|
|
|
*
|
|
|
|
* @return null|\App\Models\Client
|
2020-12-07 14:49:30 +01:00
|
|
|
*/
|
|
|
|
public function getClient(): ?Client
|
2020-06-27 15:53:12 +02:00
|
|
|
{
|
2020-12-07 14:49:30 +01:00
|
|
|
$hash = $this->getPaymentHash();
|
2020-06-27 17:39:28 +02:00
|
|
|
|
2020-12-07 14:49:30 +01:00
|
|
|
return Client::find($hash->data->client_id)->firstOrFail();
|
|
|
|
}
|
2020-06-27 15:53:12 +02:00
|
|
|
|
2020-12-07 14:49:30 +01:00
|
|
|
/**
|
|
|
|
* Resolve company from company_key parameter.
|
2020-12-07 14:56:23 +01:00
|
|
|
*
|
|
|
|
* @return null|\App\Models\Company
|
2020-12-07 14:49:30 +01:00
|
|
|
*/
|
|
|
|
public function getCompany(): ?Company
|
|
|
|
{
|
|
|
|
return Company::where('company_key', $this->company_key)->firstOrFail();
|
2020-06-27 15:53:12 +02:00
|
|
|
}
|
|
|
|
}
|