使用Laravel创建和支付Razorpay订单流程步骤

2023-06-01 00:00:00 订单 步骤 流程

工作流程:

按下立即购买按钮后,所有数据都将提交到数据库,然后在 Razorpay 表中创建订单并重定向到使用 Razorpay 支付页面。

单击使用 Razorpay 付款按钮时,使用 try catch 验证 Razorpay 付款,并且订单表付款状态从未付款更改为已付款和 razorpay_payment_id,通过检查 razorpay_order_id 和会话订单 ID 在 Razorpay 表中更新 razorpay_signature删除。

最后,它使用具有订单 ID 的数据变量前往 Success 视图。


注意:

当这个 "callback_url": "route('frontend.payment_success')" 被命中时,
razorpay 发送 razorpay_payment_id、razorpay_signature、razorpay_order_id 并产生 419 错误,
导致 CSRF 令牌不匹配,因为路由是 POST 路由,
为此需要在 VerifyCsrfToken 中间件中添加特定路由,
以便免除 csrf 令牌不匹配。
public static function order_payment($request, $data)
{
$payment_arr=[];
        try{
if($request->type_of_payment == 'online')
{
$api = new Api(config()->get('razorpay_key_id'), config()->get('razorpay_key_secret'));
//Create Order and save in Database
//Delete Shopping cart products
//Razorpay Payment            
                        $orderData = [
                            'receipt'         => 'rcpt_'.$order_id,
                            'amount'          => $data['tot_amount'] * 100, // 39900 rupees in paise
                            'currency'        => config()->get('currency_code'),
                            'partial_payment' => false,
                        ];
                        $razorpayOrder = $api->order->create($orderData);
}
//Create data in Razorpay table
 RazorpayPayment::create([
                            'razorpay_order_id' => $razorpayOrder->id,
                            'amount' => $razorpayOrder->amount,
                            'receipt_id' => $razorpayOrder->receipt, 
                            'status' => $razorpayOrder->status,
                            'created_ts' => $razorpayOrder->created_at,
                        ]);
//Put order id in session
 session()->put(['order_id'=>$razorpayOrder->id, 'order_mode'=>'razorpay']);
if(isset($razorpayOrder->id))
            {
                return view('frontend.razorpay-payment', $data);
            }
}

Razorpay 支付 Blade:

@extends('layouts.frontendLayout')
@push('head_script')
@endpush
@section('content')
<button id="rzp-button1" class="btn btn-primary mb-3 mt-3">Pay with Razorpay</button>
@endsection
@push('footer_script')
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var options = {
    "key": "{{config()->get('razorpay_key_id')}}", // Enter the Key ID generated from the Dashboard
    "amount": "{{$payment_arr['razorpayOrder']->amount}}", // Amount is in currency subunits. Default currency is INR. Hence, 50000 refers to 50000 paise
    "currency": "{{config()->get('currency_code')}}",
    "name": "{{config()->get('setting.siteName')}}",
    "description": "Test Transaction for purchasing {{$payment_arr['order_id']}}",
    "image": "{{asset('customResource/apy-frontend/images/oxgo.jpg')}}",
    "order_id": "{{$payment_arr['razorpayOrder']->id}}", //This is a sample Order ID. Pass the `id` obtained in the response of Step 1
    "callback_url": "{{route('frontend.payment_success')}}",
    "prefill": {
        "name": "{{session()->get('user.name')}}",
        "email": "{{session()->get('user.email')}}",
        "contact": "1234567890"
    },
    "notes": {
        "address": "Some address"
    },
    "theme": {
        "color": "#3399cc"
    }
};
var rzp1 = new Razorpay(options);
document.getElementById('rzp-button1').onclick = function(e){
    rzp1.open();
    e.preventDefault();
}
</script>
@endpush

前往支付验证控制器

public function payment_success(Request $request)
{
$data['order_id'] = session()->get('order_id');
if($request->filled('razorpay_payment_id'))
       $api = new Api(config()->get('razorpay_key_id'), config()->get('razorpay_key_secret'));
            try{
                $attributes = array(
                    'razorpay_order_id' =>  $data['order_id'],
                    'razorpay_payment_id' => $request->razorpay_payment_id,
                    'razorpay_signature' => $request->razorpay_signature,
                );
                $api->utility->verifyPaymentSignature($attributes);
                $rzr_record = RazorpayPayment::where('razorpay_order_id',  $data['order_id'])->first();
                Order::where('order_unique_id', substr($rzr_record->receipt_id,5))->update([
                    'payment_status' => 'paid',
                ]);
                $rzr_record->update([
                    'razorpay_payment_id' => $request->razorpay_payment_id,
                    'razorpay_order_id' => session()->get('order_id'),
                    'razorpay_signature' => $request->razorpay_signature,
                ]);
                session()->forget('order_id');
            }
            catch(SignatureVerificationError $e)
            {
                $success = false;
                $error = 'Razorpay Error : '. $e->getMessage();
                $data['error'] =  $error;
                return view('frontend.order-success',$data); //If error
            }
 return view('frontend.order-success', $data); //If success
}

路线:

Route::any('/checkout', [HomepageController::class, 'checkout'])->middleware('customer','roleif:customer')->name('checkout');//used any for flash message
        Route::post('/checkout-cart', [HomepageController::class, 'checkout_post'])->name('checkout_post');
        Route::post('/payment-success', [HomepageController::class, 'payment_success'])->name('payment_success'); //Hitting when the payment is done and also verified

验证CsrfToken:

protected $except = [
        //
        '/payment-success',
    ];


转:

https://dev.to/debo2696/razorpay-order-creation-and-payment-using-laravel-1oi1

相关文章