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

Merge pull request #3851 from beganovich/v2-2606-payment-webhook

Stripe payment webhook
This commit is contained in:
David Bomba 2020-06-28 13:06:43 +10:00 committed by GitHub
commit a1abc4f69c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 120 additions and 7 deletions

View File

@ -0,0 +1,51 @@
<?php
/**
* 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
*/
namespace App\Http\Controllers;
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Models\Payment;
use Illuminate\Support\Arr;
class PaymentWebhookController extends Controller
{
public function __construct()
{
$this->middleware('guest');
}
public function __invoke(PaymentWebhookRequest $request, string $company_key, string $gateway_key)
{
$transaction_reference = $this->getTransactionReference($request->all());
$payment = Payment::where('transaction_reference', $transaction_reference)->first();
if (is_null($payment)) {
return response([], 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)
{
$flatten = Arr::dot($data);
if (isset($flatten['data.object.id'])) {
return $flatten['data.object.id']; // Request from Stripe
}
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace App\Http\Requests\Payments;
use App\Models\Company;
use App\Models\CompanyGateway;
use Illuminate\Foundation\Http\FormRequest;
class PaymentWebhookRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
//
];
}
public function company()
{
if (!$this->company_key) {
return false;
}
return Company::query()
->where('company_key', $this->company_key)
->firstOrFail();
}
public function companyGateway()
{
if (!$this->gateway_key || !$this->company_key) {
return false;
}
$company = $this->company();
return CompanyGateway::query()
->where('gateway_key', $this->gateway_key)
->where('company_id', $company->id)
->firstOrFail();
}
}

View File

@ -185,6 +185,7 @@ class ACH
'payment_method' => $state['charge_id'],
'payment_type' => $state['payment_type'],
'amount' => $state['charge']->amount,
'gateway_type_id' => GatewayType::BANK_TRANSFER,
];
$payment = $this->stripe->createPayment($data, Payment::STATUS_PENDING);

View File

@ -13,6 +13,7 @@
namespace App\PaymentDrivers\Stripe;
use App\Events\Payment\PaymentWasCreated;
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\GatewayType;

View File

@ -175,6 +175,7 @@ class CreditCard
'payment_method' => $state['charge_id'],
'payment_type' => $state['payment_type'],
'amount' => $state['server_response']->amount,
'gateway_type_id' => GatewayType::CREDIT_CARD,
];
$payment = $this->stripe->createPayment($data, $status = Payment::STATUS_COMPLETED);

View File

@ -14,6 +14,7 @@ namespace App\PaymentDrivers\Stripe;
use App\Events\Payment\PaymentWasCreated;
use App\Jobs\Util\SystemLogger;
use App\Models\Gateway;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentType;
@ -77,6 +78,7 @@ class SOFORT
'payment_method' => $state['charge_id'],
'payment_type' => $state['payment_type'],
'amount' => $state['amount'],
'gateway_type_id' => GatewayType::SOFORT,
];
$payment = $this->stripe->createPayment($data, Payment::STATUS_PENDING);

View File

@ -14,9 +14,12 @@ namespace App\PaymentDrivers;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\Company;
use App\Models\CompanyGateway;
use App\Models\GatewayType;
use App\Models\Invoice;
use App\Models\Payment;
@ -233,6 +236,7 @@ class StripePaymentDriver extends BasePaymentDriver
$payment->type_id = $data['payment_type'];
$payment->transaction_reference = $data['payment_method'];
$payment->client_contact_id = $client_contact_id;
$payment->gateway_type_id = GatewayType::ALIPAY;
$payment->save();
return $payment;
@ -352,5 +356,15 @@ class StripePaymentDriver extends BasePaymentDriver
return $this->payment_method->processVerification($payment_method);
}
public function processWebhookRequest(PaymentWebhookRequest $request, Company $company, CompanyGateway $company_gateway, Payment $payment)
{
if ($request->type == 'source.chargable') {
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->save();
}
return response([], 200);
}
/************************************** Omnipay API methods **********************************************************/
}

View File

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

View File

@ -1,7 +1,6 @@
<?php
/*
* Signup Routes
*/
use Illuminate\Support\Facades\Route;
Route::get('/', 'BaseController@flutterRoute')->middleware('guest');
Route::get('setup', 'SetupController@index')->middleware('guest');
@ -9,10 +8,6 @@ Route::post('setup/check_db', 'SetupController@checkDB')->middleware('guest');
Route::post('setup/check_mail', 'SetupController@checkMail')->middleware('guest');
Route::post('setup', 'SetupController@doSetup')->middleware('guest');
/*
* Password Reset Routes...
*/
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');