Merge pull request #4513 from beganovich/v5-webhooks-refactor

(wip) (v5) Webhooks refactor
This commit is contained in:
Benjamin Beganović 2020-12-17 11:56:16 +01:00 committed by GitHub
commit d8405710e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 15 deletions

View File

@ -13,6 +13,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Http\Requests\Payments\PaymentWebhookRequest; use App\Http\Requests\Payments\PaymentWebhookRequest;
use Illuminate\Support\Arr;
class PaymentWebhookController extends Controller class PaymentWebhookController extends Controller
{ {
@ -21,10 +22,12 @@ class PaymentWebhookController extends Controller
$this->middleware('guest'); $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() return $request->getCompanyGateway()
->driver($request->getClient()) ->driver($payment->client)
->processWebhookRequest($request, $request->getPayment()); ->processWebhookRequest($request, $payment);
} }
} }

View File

@ -18,9 +18,12 @@ use App\Models\Company;
use App\Models\CompanyGateway; use App\Models\CompanyGateway;
use App\Models\Payment; use App\Models\Payment;
use App\Models\PaymentHash; use App\Models\PaymentHash;
use App\Utils\Traits\MakesHash;
class PaymentWebhookRequest extends Request class PaymentWebhookRequest extends Request
{ {
use MakesHash;
public function authorize() public function authorize()
{ {
return true; return true;
@ -41,20 +44,22 @@ class PaymentWebhookRequest extends Request
*/ */
public function getCompanyGateway(): ?CompanyGateway 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. * Resolve payment hash.
* *
* @param string $hash * @param string $hash
* @return null|\App\Http\Requests\Payments\PaymentHash * @return null|\App\Models\PaymentHash
*/ */
public function getPaymentHash(): ?PaymentHash public function getPaymentHash(): ?PaymentHash
{ {
if ($this->query('hash')) { if ($this->query('hash')) {
return PaymentHash::where('hash', $this->query('hash'))->firstOrFail(); return PaymentHash::where('hash', $this->query('hash'))->firstOrFail();
} }
return null;
} }
/** /**
@ -64,9 +69,32 @@ class PaymentWebhookRequest extends Request
*/ */
public function getPayment(): ?Payment 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);
} }
/** /**

View File

@ -129,7 +129,7 @@ class CreditCard
$payment->{'3ds'} = ['enabled' => true]; $payment->{'3ds'} = ['enabled' => true];
$payment->{'success_url'} = route('payment_webhook', [ $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, 'company_key' => $this->checkout->client->company->company_key,
'hash' => $this->checkout->payment_hash->hash, 'hash' => $this->checkout->payment_hash->hash,
]); ]);

View File

@ -60,13 +60,13 @@ class Alipay
$this->stripe->payment_hash->save(); $this->stripe->payment_hash->save();
if ($request->redirect_status == 'succeeded') { if ($request->redirect_status == 'succeeded') {
return $this->processSuccesfulRedirect(); return $this->processSuccesfulRedirect($request->source);
} }
return $this->processUnsuccesfulRedirect(); return $this->processUnsuccesfulRedirect();
} }
public function processSuccesfulRedirect() public function processSuccesfulRedirect(string $source)
{ {
$this->stripe->init(); $this->stripe->init();
@ -74,7 +74,7 @@ class Alipay
'payment_method' => $this->stripe->payment_hash->data->source, 'payment_method' => $this->stripe->payment_hash->data->source,
'payment_type' => PaymentType::ALIPAY, 'payment_type' => PaymentType::ALIPAY,
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision), '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); $payment = $this->stripe->createPayment($data, \App\Models\Payment::STATUS_PENDING);

View File

@ -343,9 +343,9 @@ class StripePaymentDriver extends BaseDriver
return $this->payment_method->processVerification($request, $payment_method); 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->status_id = Payment::STATUS_COMPLETED;
$payment->save(); $payment->save();
} }

View File

@ -11,6 +11,8 @@
| |
*/ */
use Illuminate\Support\Facades\Route;
Route::group(['middleware' => ['api_secret_check']], function () { Route::group(['middleware' => ['api_secret_check']], function () {
Route::post('api/v1/signup', 'AccountController@store')->name('signup.submit'); Route::post('api/v1/signup', 'AccountController@store')->name('signup.submit');
Route::post('api/v1/oauth_login', 'Auth\LoginController@oauthApiLogin'); 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('preimport', 'ImportController@preimport')->name('import.preimport');
Route::post('import', 'ImportController@import')->name('import.import'); Route::post('import', 'ImportController@import')->name('import.import');
/* /*
Route::resource('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit 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::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'); Route::fallback('BaseController@notFound');
// localhost:8080/payment_webhook/VolejRejNm/wrsef2tiyrwbcnrruwl24iqplayx0idmtjevmnyqniekawtwcgirgpzyceka4bd8