简体   繁体   中英

Laravel 7 - Failed to load resource: the server responded with a status of 400 (Bad Request)

Can anyone help me with this please, any experience with Razorpay , I am trying to pass value from controller to javascript in the view, it is not populating data-order_id and I am getting the above error, which means I am getting null value

Razorpay Reference - Sample Code and Standard Checkout Script

Here is my view - welcome.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Razorpay</title>

    <style>
        .razorpay-payment-button {
            background: #6c5ce7;
            color: whitesmoke;
            font-size: 0.8rem;
            text-transform: uppercase;
            letter-spacing: 1;
            display: block;
            width: 15vw;
            height: 8vh;
            border: none;
            padding: 0.3rem 0.3rem;
            margin: 50px 50px;
        }
    </style>
</head>

<body>
    <!-- Razorpay Checkout Script -->
    <form action="/success" method="POST">

        <script src="https://checkout.razorpay.com/v1/checkout.js" 
        data-key="{{ env('RAZORPAY_KEY') }}" 
        data-amount="$order('amount')"
        data-currency="INR" 
        data-id="$order('id')"
        data-buttontext="Pay Now" 
        data-name="Test Corp" 
        data-description="Test Transaction"
        data-image="img/brand.png" 
        data-prefill.name="Test Example" 
        data-prefill.email="test@example.com" 
        data-prefill.contact="9999999999" 
        data-theme.color="#F37254"></script>

        <input type="hidden" custom="Hidden Element" name="hidden">
    </form>
</body>

</html>

My Controller - PaymentController.php

<?php

namespace App\Http\Controllers;

use Razorpay\Api\Api;

class PaymentController extends Controller
{
    public function gen_order_id()
    {
        $api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));

        $order  = $api->order->create([
            'receipt'         => 'rcptid' . '_' . mt_rand(),
            'amount'          => 50000,
            'currency'        => 'INR',
            'payment_capture' =>  '1',
        ]);
    }
}

My API Route - api.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('orders', 'PaymentController@gen_order_id');

this is the payload I get after executing dd($order)

Razorpay\Api\Order {#275
  #attributes: array:12 [
    "id" => "order_xxxxxxxxxxxxxx"
    "entity" => "order"
    "amount" => 50000
    "amount_paid" => 0
    "amount_due" => 50000
    "currency" => "INR"
    "receipt" => "rcptid_xxxxxxxxxxxx"
    "offer_id" => null
    "status" => "created"
    "attempts" => 0
    "notes" => Razorpay\Api\Order {#273
      #attributes: []
    }
    "created_at" => 1591257352
  ]
}

I'm not sure what you're trying to accomplish, but there's definitely an issue in the blade file:

data-amount="request()->route()->orders($amount)"
...
data-order_id="request()->route()->orders($order_id)"

Should be:

data-amount="{{ request()->route()->orders($amount) }}"
...
data-order_id="{{ request()->route()->orders($order_id) }}"

Hope this helps:)


To pass variables to your blade view from controller, use this method:

namespace App\Http\Controllers;

use Razorpay\Api\Api;

class PaymentController extends Controller
{
    ...

    return view('path.to.blade', [
        'order_id' => $order_id,
        'amount' => $amount,
    ]);
}

To access the passed var from your blade file, use the moustache notation enclosing your variable name:

data-amount="{{ $amount }}"
...
data-order_id="{{ $order_id }}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM