1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #4527 from beganovich/v5-stripe-webhook-support

(v5) Stripe support for webhooks
This commit is contained in:
Benjamin Beganović 2020-12-21 08:14:00 +01:00 committed by GitHub
commit 6f32fd6c4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 12 deletions

View File

@ -16,11 +16,6 @@ use App\Http\Requests\Payments\PaymentWebhookRequest;
class PaymentWebhookController extends Controller class PaymentWebhookController extends Controller
{ {
public function __construct()
{
$this->middleware('guest');
}
public function __invoke(PaymentWebhookRequest $request, string $company_key, string $company_gateway_id) public function __invoke(PaymentWebhookRequest $request, string $company_key, string $company_gateway_id)
{ {
$payment = $request->getPayment(); $payment = $request->getPayment();

View File

@ -44,7 +44,7 @@ class PaymentWebhookRequest extends Request
*/ */
public function getCompanyGateway(): ?CompanyGateway public function getCompanyGateway(): ?CompanyGateway
{ {
return CompanyGateway::find($this->decodePrimaryKey($this->company_gateway_id))->firstOrFail(); return CompanyGateway::findOrFail($this->decodePrimaryKey($this->company_gateway_id));
} }
/** /**
@ -67,16 +67,30 @@ class PaymentWebhookRequest extends Request
* *
* @return null|\App\Models\Payment * @return null|\App\Models\Payment
*/ */
public function getPayment(): ?Payment public function getPayment()
{ {
/** // For testing purposes we'll slow down the webhook processing by 2 seconds
* Some gateways, like Checkout, we can dynamically pass payment hash, // to make sure webhook request doesn't came before our processing.
* which we will resolve here and get payment information from it. 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.
if ($this->getPaymentHash()) { if ($this->getPaymentHash()) {
return $this->getPaymentHash()->payment; return $this->getPaymentHash()->payment;
} }
// 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.
abort(404); abort(404);
} }

View File

@ -185,6 +185,8 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a
Route::post('support/messages/send', 'Support\Messages\SendingController'); Route::post('support/messages/send', 'Support\Messages\SendingController');
}); });
Route::match(['get', 'post'], 'payment_webhook/{company_key}/{company_gateway_id}', 'PaymentWebhookController')->name('payment_webhook'); Route::match(['get', 'post'], 'payment_webhook/{company_key}/{company_gateway_id}', 'PaymentWebhookController')
->middleware(['guest', 'api_db'])
->name('payment_webhook');
Route::fallback('BaseController@notFound'); Route::fallback('BaseController@notFound');