1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00

Support Stripe webhook

This commit is contained in:
Benjamin Beganović 2020-06-27 17:39:28 +02:00
parent 1e4e482801
commit f68465d602
7 changed files with 51 additions and 1 deletions

View File

@ -13,6 +13,8 @@
namespace App\Http\Controllers;
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Models\Payment;
use Illuminate\Support\Arr;
class PaymentWebhookController extends Controller
{
@ -23,6 +25,27 @@ class PaymentWebhookController extends Controller
public function __invoke(PaymentWebhookRequest $request, string $company_key, string $gateway_key)
{
return response([], 200);
$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

@ -22,6 +22,10 @@ class PaymentWebhookRequest extends FormRequest
public function company()
{
if (!$this->company_key) {
return false;
}
return Company::query()
->where('company_key', $this->company_key)
->firstOrFail();
@ -29,6 +33,10 @@ class PaymentWebhookRequest extends FormRequest
public function companyGateway()
{
if (!$this->gateway_key || !$this->company_key) {
return false;
}
$company = $this->company();
return CompanyGateway::query()

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 **********************************************************/
}