From f68465d6026519654d46a687619d6c66410e0499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Sat, 27 Jun 2020 17:39:28 +0200 Subject: [PATCH] Support Stripe webhook --- .../Controllers/PaymentWebhookController.php | 25 ++++++++++++++++++- .../Payments/PaymentWebhookRequest.php | 8 ++++++ app/PaymentDrivers/Stripe/ACH.php | 1 + app/PaymentDrivers/Stripe/Alipay.php | 1 + app/PaymentDrivers/Stripe/CreditCard.php | 1 + app/PaymentDrivers/Stripe/SOFORT.php | 2 ++ app/PaymentDrivers/StripePaymentDriver.php | 14 +++++++++++ 7 files changed, 51 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/PaymentWebhookController.php b/app/Http/Controllers/PaymentWebhookController.php index 9a687163be..caaea2c3f0 100644 --- a/app/Http/Controllers/PaymentWebhookController.php +++ b/app/Http/Controllers/PaymentWebhookController.php @@ -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 + } } } diff --git a/app/Http/Requests/Payments/PaymentWebhookRequest.php b/app/Http/Requests/Payments/PaymentWebhookRequest.php index a5e9f42528..324bbedfd8 100644 --- a/app/Http/Requests/Payments/PaymentWebhookRequest.php +++ b/app/Http/Requests/Payments/PaymentWebhookRequest.php @@ -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() diff --git a/app/PaymentDrivers/Stripe/ACH.php b/app/PaymentDrivers/Stripe/ACH.php index b69abe1a2f..ed2538eab6 100644 --- a/app/PaymentDrivers/Stripe/ACH.php +++ b/app/PaymentDrivers/Stripe/ACH.php @@ -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); diff --git a/app/PaymentDrivers/Stripe/Alipay.php b/app/PaymentDrivers/Stripe/Alipay.php index 9ed61a4cd2..784a9088d8 100644 --- a/app/PaymentDrivers/Stripe/Alipay.php +++ b/app/PaymentDrivers/Stripe/Alipay.php @@ -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; diff --git a/app/PaymentDrivers/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php index 670cc6c963..6b039f7d0e 100644 --- a/app/PaymentDrivers/Stripe/CreditCard.php +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -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); diff --git a/app/PaymentDrivers/Stripe/SOFORT.php b/app/PaymentDrivers/Stripe/SOFORT.php index 260134b8ce..fc47742bb4 100644 --- a/app/PaymentDrivers/Stripe/SOFORT.php +++ b/app/PaymentDrivers/Stripe/SOFORT.php @@ -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); diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 3e329e14ba..bbb82246fa 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -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 **********************************************************/ }