Skip to content

Commit

Permalink
Webhook Update
Browse files Browse the repository at this point in the history
Added webhook support for late paid orders, now after success full payment orders will be processed by webhook
  • Loading branch information
chandra-bhanu committed Sep 3, 2021
1 parent 7788c06 commit 38d0077
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 14 deletions.
117 changes: 107 additions & 10 deletions Neexpg/Razorpay/src/Http/Controllers/RazorpayController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use App\Exceptions\Handler;
use Webkul\Sales\Models\OrderPayment as OrderPayment;

use Neexpg\Razorpay\Models\RazorpayEvents;


class RazorpayController extends Controller
{

Expand Down Expand Up @@ -103,6 +106,12 @@ public function redirect()

$this->orderRepository->update(['status' => 'pending_payment'], $this->order->id);

//Make entry for webhook event IDS
//STORE IN PAYMENT GATEWAY
$paymentEvents = new RazorpayEvents();
$paymentEvents->razorpay_order_id =$pgorderId;
$paymentEvents->razorpay_invoice_status = 'pending_payment';
$paymentEvents->save();


return view('razorpay::drop-in-ui', compact('payment'));
Expand Down Expand Up @@ -130,7 +139,8 @@ public function redirect()

public function verifyPaymentHook(Request $request)
{
if(core()->getConfigData('sales.paymentmethods.razorpay.sandbox')):

if(core()->getConfigData('sales.paymentmethods.razorpay.sandbox')):
$merchentId = core()->getConfigData('sales.paymentmethods.razorpay.testclientid');
$privateKey = core()->getConfigData('sales.paymentmethods.razorpay.testclientsecret');
else:
Expand All @@ -147,9 +157,64 @@ public function verifyPaymentHook(Request $request)
$generatedSignature = hash_hmac('sha256',$request->getContent(), $privateKey);
if ($generatedSignature == $webhookSignature):

//verify agnaist payment

$payments= RazorpayEvents::where('razorpay_event_id',$eventId)->get();
if($payments->count()==0):

$paymentId= $webhookBody->payload->payment->entity->id;
$order_id= $webhookBody->payload->payment->entity->order_id;
$invoice_id= $webhookBody->payload->payment->entity->invoice_id;
$pgstatus= $webhookBody->payload->payment->entity->status;


if($invoice_id):
$pgData= RazorpayEvents::where('razorpay_invoice_id',$invoice_id)->first();
elseif($order_id):
$pgData= RazorpayEvents::where('razorpay_order_id',$order_id)->first();
else:
$pgData=FALSE;
endif;

if($pgData):

$pgId=$pgData->id;
if($pgstatus=='captured' || $pgstatus=='paid'):
//Updatee Payments
$rzp = RazorpayEvents::find($pgId);
$rzp->razorpay_event_id = $eventId;
$rzp->razorpay_invoice_id = $invoice_id;
$rzp->razorpay_payment_id = $paymentId;
$rzp->razorpay_invoice_status = 'paid';
$rzp->razorpay_signature = $webhookSignature;
$rzp->save();


//UPDATE ORDER core_order_id
$PGorder= OrderPayment::where('order_id',$pgData->core_order_id)->whereJsonContains('additional->status','Paid')->firstOrFail();
if($PGorder->count()<1):

$pgUpdateD = OrderPayment::where('order_id',$pgData->core_order_id)->firstOrFail();
$additional=array();
$additional['status']='Paid';
$additional['oid']=$order_id;
$additional['pgreference']=$paymentId;
$pgUpdateD->additional=$additional;
$pgUpdateD->save();

$this->orderRepository->update(['status' => 'processing'], $pgData->core_order_id);
$this->invoiceRepository->create($this->prepareInvoiceData($pgData->core_order_id));

endif;


endif;

endif;


endif;

endif;
return TRUE;
}

//PAYMENT CANCELLED
Expand All @@ -166,6 +231,9 @@ public function paymentFail(Request $request)
$pgUpdateD->additional=$additional;
$pgUpdateD->save();

$rzp = RazorpayEvents::where('razorpay_order_id', $this->order->id)->first();
$rzp->razorpay_invoice_status = 'canceled';
$rzp->save();


$this->orderRepository->update(['status' => 'canceled'], $this->order->id);
Expand Down Expand Up @@ -201,7 +269,9 @@ public function paymentSuccess(Request $request)
$pgUpdateD->additional=$additional;
$pgUpdateD->save();


$rzp = RazorpayEvents::where('razorpay_order_id', $this->order->id)->first();
$rzp->razorpay_invoice_status = 'error';
$rzp->save();

$this->orderRepository->update(['status' => 'pending_payment'], $this->order->id);
Cart::deActivateCart();
Expand Down Expand Up @@ -233,6 +303,20 @@ public function paymentSuccess(Request $request)
$this->orderRepository->update(['status' => 'processing'], $this->order->id);
$this->invoiceRepository->create($this->prepareInvoiceData());


$orderdata = $api->order->fetch($oid)->payments();
$link=$orderdata->items[0];

$rzp = RazorpayEvents::where('razorpay_order_id', $request['razorpay_order_id'])->first();
$rzp->core_order_id=$this->order->id;
$rzp->razorpay_payment_id = $link->id;
$rzp->razorpay_invoice_status = $link->status;
$rzp->razorpay_signature = $request['razorpay_signature'];
$rzp->save();




Cart::deActivateCart();
session()->flash('order', $this->order);
session()->flash('success', trans('Payment Successfull!'));
Expand All @@ -250,15 +334,28 @@ public function paymentSuccess(Request $request)
*
* @return array
*/
protected function prepareInvoiceData()
protected function prepareInvoiceData($oid=null)
{
$invoiceData = [
if($oid):
$invoiceData = [
"order_id" => $oid
];

$order = $this->orderRepository->findOrFail($oid);
foreach ($order->items as $item) {
$invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice;
}
else:
$invoiceData = [
"order_id" => $this->order->id
];
];
foreach ($this->order->items as $item) {
$invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice;
}
endif;


foreach ($this->order->items as $item) {
$invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice;
}


return $invoiceData;
}
Expand Down
8 changes: 4 additions & 4 deletions Neexpg/Razorpay/src/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Route::group(['middleware' => ['web']], function () {
Route::prefix('razorpay/payment')->group(function () {

Route::get('/direct', 'neexPg\Razorpay\Http\Controllers\RazorpayController@redirect')->name('razorpay.payment.redirect');
Route::get('/redirect', 'Neexpg\Razorpay\Http\Controllers\RazorpayController@redirect')->name('razorpay.payment.redirect');

Route::post('/success', 'neexPg\Razorpay\Http\Controllers\RazorpayController@paymentSuccess')->name('razorpay.payment.success');
Route::get('/fail', 'neexPg\Razorpay\Http\Controllers\RazorpayController@paymentFail')->name('razorpay.payment.fail');
Route::post('/success', 'Neexpg\Razorpay\Http\Controllers\RazorpayController@paymentSuccess')->name('razorpay.payment.success');
Route::get('/fail', 'Neexpg\Razorpay\Http\Controllers\RazorpayController@paymentFail')->name('razorpay.payment.fail');

Route::post('/razorpay-hook','neexPg\Razorpay\Http\Controllers\RazorpayController@verifyPaymentHook')->name('razorpay.payment.hook');
Route::post('/razorpay-hook','Neexpg\Razorpay\Http\Controllers\RazorpayController@verifyPaymentHook')->name('razorpay.payment.hook');
});
});
58 changes: 58 additions & 0 deletions Neexpg/Razorpay/src/Models/RazorpayEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
* CREATE TABLE `rzp_payments` (
`id` int(11) NOT NULL,
`razorpay_event_id` varchar(50) DEFAULT NULL,
`razorpay_invoice_id` varchar(40) DEFAULT NULL,
`razorpay_order_id` varchar(80) DEFAULT NULL,
`razorpay_payment_id` varchar(40) DEFAULT NULL,
`razorpay_invoice_status` varchar(40) DEFAULT 'issued',
`razorpay_invoice_receipt` varchar(40) DEFAULT NULL,
`razorpay_signature` longtext,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
*/
namespace Neexpg\Razorpay\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class RazorpayEvents extends Model
{
protected $table = 'rzp_payments';

use SoftDeletes;

protected $dates = [
'created_at',
'updated_at',
'deleted_at',
];

/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'core_order_id',
'razorpay_event_id',
'razorpay_invoice_id',
'razorpay_order_id',
'razorpay_payment_id',
'razorpay_invoice_status',
'razorpay_invoice_receipt',
'razorpay_signature',
'created_at',
'updated_at',
'deleted_at',
];


}

0 comments on commit 38d0077

Please sign in to comment.