-1

I am working on Razorpay integration and after succesful payment when I try to save the details to the database I get an error. I am sending the details through AJAX and in controller when I try to use foreach and look in console I always get the error invalid argument supplied for foreach statement.

this is the script after successful transaction

<script>

    function demoSuccessHandler(transaction) {
        // You can write success code here. If you want to store some data in database.
        $("#paymentDetail").removeAttr('style');
        $('#paymentID').text(transaction.razorpay_payment_id);
        var paymentDate = new Date();
        $('#paymentDate').text(
                padStart(paymentDate.getDate()) + '.' + padStart(paymentDate.getMonth() + 1) + '.' + paymentDate.getFullYear() + ' ' + padStart(paymentDate.getHours()) + ':' + padStart(paymentDate.getMinutes())
                );

        $.ajax({
            method: 'post',
            url: "{!!route('dopayment')!!}",
            dataType:"json",
            data: {
                "_token": "{{ csrf_token() }}",
                "product": "{{ json_encode($product)}}",
                "Company": "{{ $company}}",
                "Address": "{{$address}}",
                "Contact": "{{$contact}}",
                "razorpay_payment_id": transaction.razorpay_payment_id
            },
            complete: function (r) {
                console.log('complete');
                console.log(r);
            }
        })
    }
</script>
<script>
    var options = {
        key: "{{ env('RAZORPAY_KEY') }}",
        amount: '{{ $subtotal}}',
        name: 'AMCOR RAZORPAY',
        description: 'AMCOR INTERNATIONAL',
        image: 'https://i.imgur.com/n5tjHFD.png',
        handler: demoSuccessHandler
    }
</script>

the controller function is

 public function dopayment(Request $request) {

            $input = $request->all();

            $product = $request->product;


            foreach ($product as $single) {
                # code...

                print_r($single->name);
            }

            print_r($product);
            exit;
        }
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • You are sending JSON in string form for that parameter, so you will need to _decode_ it again, before you can work with it … – 04FS Feb 05 '20 at 11:47
  • please print $input = $request->all(); print_r($input); and put output – Paras Raiyani Feb 05 '20 at 12:24
  • Array\n(\n [_token] => b4ta6V2znRmJwTRg8eBgkZnqtyKfF6Gbqtnt2nKB\n [product] => ["{"id":"9","name":"R 007-4ft","price":40,"quantity":"202","attributes":{"image":"glassfilms\\/December2019\\/uiwv4WDsYWjmtVxcSuES.jpg","crm":"Pro181"},"conditions":[]}","{"id":"7","name":"Frosted 007-4ft","price":40,"quantity":967,"attributes":{"image":"glassfilms\\/December2019\\/ZJgWUNaYrPnvsoRfuagv.jpg","crm":"PRO105"},"conditions":[]}"]\n [Company] => Company:\n [Address] => Company: landmark:\n [Contact] => \n [razorpay_payment_id] => pay_ED3rIld3uOhRfE\n)\n – Gaurav Singh Feb 05 '20 at 12:30
  • I think the formatting of product sent to the controller is invalid as i checked through json validator the result is Invalid JSON format. Please help me to change the format as when i remove the json_encode from $products i get htmlspecialchars() expects parameter 1 to be string, array given – Gaurav Singh Feb 05 '20 at 13:14
  • Can you var_dump $product? – Lajos Arpad Feb 05 '20 at 14:00
  • var_dump is null – Gaurav Singh Feb 07 '20 at 08:49

3 Answers3

0

You may decode product string before loop through it

//...
$product  = json_decode($request->product, true);
//...
Foued MOUSSI
  • 4,643
  • 3
  • 19
  • 39
0

Please Json Decode before foreach loop

 public function dopayment(Request $request) {

            $input = $request->all();

            $product = json_decode($request->product);


            foreach ($product as $single) {
                # code...

                print_r($single->name);
            }

           print_r($product);
            exit;
        }
Paras Raiyani
  • 748
  • 5
  • 10
-1

The error occurs because $product returns a single value instead of an array; a check will work in this case.

if (is_array($product ) || is_object($product ))
{
    foreach($product as $arrayItems){
      // Perform x.
    }
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95