This commit is contained in:
Benjamin Beganović 2020-12-16 15:25:42 +01:00
parent 9662d9af92
commit d3033c902d
5 changed files with 38 additions and 10 deletions

View File

@ -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);
}
}

View File

@ -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);
}
/**

View File

@ -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);

View File

@ -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();
}

View File

@ -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