diff --git a/app/Http/Controllers/PaymentWebhookController.php b/app/Http/Controllers/PaymentWebhookController.php index b32afa1d4c11..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 { @@ -21,10 +22,12 @@ class PaymentWebhookController extends Controller $this->middleware('guest'); } - public function __invoke(PaymentWebhookRequest $request, string $gateway_key, string $company_key) + 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 f4704559aac8..11bb514bbc76 100644 --- a/app/Http/Requests/Payments/PaymentWebhookRequest.php +++ b/app/Http/Requests/Payments/PaymentWebhookRequest.php @@ -18,9 +18,12 @@ use App\Models\Company; use App\Models\CompanyGateway; use App\Models\Payment; use App\Models\PaymentHash; +use App\Utils\Traits\MakesHash; class PaymentWebhookRequest extends Request { + use MakesHash; + public function authorize() { return true; @@ -41,20 +44,22 @@ class PaymentWebhookRequest extends Request */ public function getCompanyGateway(): ?CompanyGateway { - return CompanyGateway::where('gateway_key', $this->gateway_key)->firstOrFail(); + return CompanyGateway::find($this->decodePrimaryKey($this->company_gateway_id))->firstOrFail(); } /** * Resolve payment hash. * * @param string $hash - * @return null|\App\Http\Requests\Payments\PaymentHash + * @return null|\App\Models\PaymentHash */ public function getPaymentHash(): ?PaymentHash { if ($this->query('hash')) { return PaymentHash::where('hash', $this->query('hash'))->firstOrFail(); } + + return null; } /** @@ -64,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/CheckoutCom/CreditCard.php b/app/PaymentDrivers/CheckoutCom/CreditCard.php index f5b51eb52b09..5b7a75369744 100644 --- a/app/PaymentDrivers/CheckoutCom/CreditCard.php +++ b/app/PaymentDrivers/CheckoutCom/CreditCard.php @@ -129,7 +129,7 @@ class CreditCard $payment->{'3ds'} = ['enabled' => true]; $payment->{'success_url'} = route('payment_webhook', [ - 'gateway_key' => $this->checkout->company_gateway->gateway_key, + 'company_gateway_id' => $this->checkout->company_gateway->hashed_id, 'company_key' => $this->checkout->client->company->company_key, 'hash' => $this->checkout->payment_hash->hash, ]); 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 609f364eab58..4b8fbaeeb597 100644 --- a/routes/api.php +++ b/routes/api.php @@ -11,6 +11,8 @@ | */ +use Illuminate\Support\Facades\Route; + Route::group(['middleware' => ['api_secret_check']], function () { Route::post('api/v1/signup', 'AccountController@store')->name('signup.submit'); Route::post('api/v1/oauth_login', 'Auth\LoginController@oauthApiLogin'); @@ -170,7 +172,7 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::post('preimport', 'ImportController@preimport')->name('import.preimport'); Route::post('import', 'ImportController@import')->name('import.import'); - + /* Route::resource('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit @@ -183,6 +185,8 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::post('support/messages/send', 'Support\Messages\SendingController'); }); -Route::match(['get', 'post'], 'payment_webhook/{gateway_key}/{company_key}', 'PaymentWebhookController')->name('payment_webhook'); +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