From d3033c902d6a907f7ad146656e6072f213fd0ea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 16 Dec 2020 15:25:42 +0100 Subject: [PATCH] wip --- .../Controllers/PaymentWebhookController.php | 7 +++-- .../Payments/PaymentWebhookRequest.php | 29 +++++++++++++++++-- app/PaymentDrivers/Stripe/Alipay.php | 6 ++-- app/PaymentDrivers/StripePaymentDriver.php | 4 +-- routes/api.php | 2 ++ 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/PaymentWebhookController.php b/app/Http/Controllers/PaymentWebhookController.php index cdd517c2ece8..246a65b48a89 100644 --- a/app/Http/Controllers/PaymentWebhookController.php +++ b/app/Http/Controllers/PaymentWebhookController.php @@ -13,6 +13,7 @@ namespace App\Http\Controllers; use App\Http\Requests\Payments\PaymentWebhookRequest; +use Illuminate\Support\Arr; class PaymentWebhookController extends Controller { @@ -23,8 +24,10 @@ class PaymentWebhookController extends Controller public function __invoke(PaymentWebhookRequest $request, string $company_gateway_id, string $company_key) { + $payment = $request->getPayment(); + return $request->getCompanyGateway() - ->driver($request->getClient()) - ->processWebhookRequest($request, $request->getPayment()); + ->driver($payment->client) + ->processWebhookRequest($request, $payment); } } diff --git a/app/Http/Requests/Payments/PaymentWebhookRequest.php b/app/Http/Requests/Payments/PaymentWebhookRequest.php index 4286be1ec83e..11bb514bbc76 100644 --- a/app/Http/Requests/Payments/PaymentWebhookRequest.php +++ b/app/Http/Requests/Payments/PaymentWebhookRequest.php @@ -51,7 +51,7 @@ class PaymentWebhookRequest extends Request * Resolve payment hash. * * @param string $hash - * @return null|\App\Http\Requests\Payments\PaymentHash + * @return null|\App\Models\PaymentHash */ public function getPaymentHash(): ?PaymentHash { @@ -69,9 +69,32 @@ class PaymentWebhookRequest extends Request */ public function getPayment(): ?Payment { - $hash = $this->getPaymentHash(); + /** + * Some gateways, like Checkout, we can dynamically pass payment hash, + * which we will resolve here and get payment information from it. + */ + if ($this->getPaymentHash()) { + return $this->getPaymentHash()->payment; + } - return $hash->payment; + /** + * Some gateways, like Stripe, send us transcation reference via webhook, + * so we can resolve payment from there. + */ + if ($this->has('data') && $this->has('type')) { + $src = $this->data['object']['id']; + + info('Using src: ' . $src); + + $payment = \App\Models\Payment::where('transaction_reference', $src)->first(); + + info('payment fetched!'); + info($payment); + } + + info('before abort, 97'); + + abort(404); } /** diff --git a/app/PaymentDrivers/Stripe/Alipay.php b/app/PaymentDrivers/Stripe/Alipay.php index 0fd233675bd9..d3feaf73bcb9 100644 --- a/app/PaymentDrivers/Stripe/Alipay.php +++ b/app/PaymentDrivers/Stripe/Alipay.php @@ -60,13 +60,13 @@ class Alipay $this->stripe->payment_hash->save(); if ($request->redirect_status == 'succeeded') { - return $this->processSuccesfulRedirect(); + return $this->processSuccesfulRedirect($request->source); } return $this->processUnsuccesfulRedirect(); } - public function processSuccesfulRedirect() + public function processSuccesfulRedirect(string $source) { $this->stripe->init(); @@ -74,7 +74,7 @@ class Alipay 'payment_method' => $this->stripe->payment_hash->data->source, 'payment_type' => PaymentType::ALIPAY, 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision), - 'transaction_reference' => ctrans('texts.n/a'), + 'transaction_reference' => $source, ]; $payment = $this->stripe->createPayment($data, \App\Models\Payment::STATUS_PENDING); diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 1a4470d42433..4d91c65ee6e7 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -343,9 +343,9 @@ class StripePaymentDriver extends BaseDriver return $this->payment_method->processVerification($request, $payment_method); } - public function processWebhookRequest(PaymentWebhookRequest $request, Company $company, CompanyGateway $company_gateway, Payment $payment) + public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment) { - if ($request->type == 'source.chargable') { + if ($request->type == 'source.chargeable') { $payment->status_id = Payment::STATUS_COMPLETED; $payment->save(); } diff --git a/routes/api.php b/routes/api.php index 06aaafe67a6f..4b8fbaeeb597 100644 --- a/routes/api.php +++ b/routes/api.php @@ -188,3 +188,5 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::match(['get', 'post'], 'payment_webhook/{company_gateway_id}/{company_key}', 'PaymentWebhookController')->name('payment_webhook'); Route::fallback('BaseController@notFound'); + +// localhost:8080/payment_webhook/VolejRejNm/wrsef2tiyrwbcnrruwl24iqplayx0idmtjevmnyqniekawtwcgirgpzyceka4bd8