Razor Payment

  • Package Installation

    1.composer.json

    
                                "require": {
                                    "razorpay/razorpay": "^2.8"
                                }
                                

    2. run composer update

  • Coding

    1. add key and secret in .env

    
                                 RAZOR_KEY_ID=rzp_test_iOJPLwtl4LNoTD
                                RAZOR_KEY_SECRET=JK8q8JCGjYClBJ4DBrxUfy1F
                                

    2. add razor pay script in the view

    
                                <script src="https://checkout.razorpay.com/v1/checkout.js"></script>
    
    <script>
    
    
            var amount="{$documententries->balance_amount}";                
    		amount=amount*100;
    		
    		$("#paynow").click(function(){
    			$("#paynow_panel").hide();
    			
    			$.ajax({
                            url: '{route('paymentrequest')}',
                            type: 'get',
                            dataType: 'json',
                            data: {
    							'document_entry_id':"{$documententries->id}",
    							'amount' : "{$documententries->balance_amount}"
                            }, 
                            success: function (data) {
    									var payment_id=data.payment_id;
    									var options = {
    									"key": "",
    									"amount":amount, // 2000 paise = INR 20
    									"payment_id":payment_id,
    									"name": "Talent Attestation",
    									"description": "Payment",
    									"image": "https://talent.jaivamlife.com/web/public/Fe/images/logo.png",
    									"handler": function (response){
    
    											$("#message_panel").show();
    									
    											$.ajax({
    											url: '{route('paymentresponse')}',
    											type: 'get',
    											dataType: 'json',
    											data: {
    												razorpay_payment_id: response.razorpay_payment_id ,payment_id : payment_id,
    											}, 
    											success: function (msg) {
    												
    												Swal.fire({
    													title: 'success!',
    													text: 'Thank you for your payment.',
    													icon: 'success',
    													confirmButtonText: 'OK'
    												});
    
    
    											
    
    
    
    											}
    											});
    									
    									},
    									"prefill": {
    										"contact": '{$documententries->customer_mobile}',
    										"email":   '{$documententries->customer_email}',
    									},
    									"theme": {
    									"color": "#528FF0"
    									}
    									};
    									var rzp1 = new Razorpay(options);
    									rzp1.open();
    									e.preventDefault();
    									}
                            });
    		})
    		
    </script>
    
    

    3. insert payment request into payment_requests table and return id

    
                        $payment=PaymentRequest::create($input);
                        $arr = ['payment_id'=>$payment->id];  
                         echo json_encode($arr);
                        

    4. verify and capture the razor payment

    
                        use Razorpay\Api\Api;
                        ... .
                        ..... 
                        try {
                        
                            $api = new Api(env('RAZOR_KEY_ID'), env('RAZOR_KEY_SECRET'));
                            $payment_details = $api->payment->fetch($input['razorpay_payment_id'])->capture(array('amount'=>intval($paymentRequest->amount*100),'currency' => 'INR'));
                            
                        }
                        catch (exception $e) {
                            return response()->json([
                                'status' => 'error',
                                'message' =>'invalid payment.',
                                'code' => 'RAF'
                            ], 500);
                        }