尝试使用 POST 方法访问我的路由,但服务器尝试使用 GET
Trying to access my route with the POST method, but the server tries with GET
我正在尝试在我的项目中实现 Paypal Smart Buttons,在我的本地机器上运行良好。现在我尝试将它部署在 heroku 上进行更多测试,当我尝试付款时出现以下错误
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.sandbox.paypal.com/xoplatform/logger/api/logger. (Reason: CORS request did not succeed). Status code: (null).
&&
The GET method is not supported for this route. Supported methods: POST.
我正在使用 POST API 路由,在 Javascript 中我将 POST 声明为方法,出于某种原因它总是尝试使用 GET。
Checkout.blade.php(Javascript 部分):
createOrder: function(data, actions) {
return fetch('api/paypal/order/create/', {
method: 'post',
body: JSON.stringify({
"content" : {!! json_encode($cartContent, JSON_HEX_TAG) !!},
"value" : "{{ Cart::subtotal() }}",
"company" : document.getElementById('company').value,
"firstName" : document.getElementById('firstName').value,
"lastName" : document.getElementById('lastName').value,
"email" : document.getElementById('email').value,
"street" : document.getElementById('street').value,
"housenumber" : document.getElementById('housenumber').value,
"city" : document.getElementById('city').value,
"postcode" : document.getElementById('postcode').value,
})
}).then(function(res) {
return res.json();
}).then(function(orderData) {
return orderData.id;
});
},
PayPalController.php
<?php
namespace App\Http\Controllers;
use Cart;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Transaction;
use Illuminate\Http\Request;
use Srmklive\PayPal\Service\Paypal;
class PayPalController extends Controller
{
public function create(Request $request)
{
$data = json_decode($request->getContent(), true);
// Init PayPal
$provider = \PayPal::setProvider();
$provider->setApiCredentials(config('paypal'));
$token = $provider->getAccessToken();
$provider->setAccessToken($token);
$price = $data['value'];
$description = 'Einkauf bei Isipack24.de';
$order = $provider->createOrder([
"intent" => "CAPTURE",
"payer" => [
"name" => [
"given_name" => $data['firstName'],
"surname" => $data['lastName']
],
"address" => [
"address_line_1" => $data['street'] . " " . $data['housenumber'],
"admin_area_2" => $data['city'],
"admin_area_1" => 'NRW',
"postal_code" => $data['postcode'],
"country_code" => "DE"
],
"email_address" => $data['email']
],
"purchase_units" => [
[
"amount" => [
"currency_code" => "EUR",
"value" => $price
],
"description" => $description
]
]
]);
$newOrder = Order::create([
'Firma' => $data['company'],
'Vorname' => $data['firstName'],
'Nachname' => $data['lastName'],
'Email' => $data['email'],
'Straße' => $data['street'],
'Hausnummer' => $data['housenumber'],
'Postleitzahl' => $data['postcode'],
'Ort' => $data['city'],
]);
foreach($data['content'] as $item){
OrderItem::create([
'product_id' => $item['id'],
'order_id' => $newOrder->id,
'price' => $item['price'],
'quantity' => $item['qty'],
'total' => $item['price']*$item['qty'],
]);
}
Transaction::create([
'order_id' => $newOrder->id,
'status' => $order['status'],
'reference_number' => $order['id']
]);
return response()->json($order);
}
public function capture(Request $request)
{
$data = json_decode($request->getContent(), true);
$orderID = $data['orderID'];
// Init PayPal
$provider = \PayPal::setProvider();
$provider->setApiCredentials(config('paypal'));
$token = $provider->getAccessToken();
$provider->setAccessToken($token);
$result = $provider->capturePaymentOrder($orderID);
// Update Database
if ($result['status'] == "COMPLETED") {
Transaction::where('reference_number', $result['id'])
->update(['status' => 'COMPLETED', 'updated_at' => \Carbon\Carbon::now()]);
}
return response()->json($result);
}
}
api.php(路线):
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PayPalController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::prefix('paypal')->group(function () {
Route::post('/order/create', [PayPalController::class, 'create']);
Route::post('/order/capture', [PayPalController::class, 'capture']);
});
我尝试了很多,但我无法理解它。
忽略与此相关的一切。不重要。
如果您在另一条真正重要的路线上遇到错误,则需要显示另一条 non-logging 路线的实际情况。使用浏览器的网络选项卡。
如果您自己无法诊断此问题,也许您还应该 link 在某处实施。
我正在尝试在我的项目中实现 Paypal Smart Buttons,在我的本地机器上运行良好。现在我尝试将它部署在 heroku 上进行更多测试,当我尝试付款时出现以下错误
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.sandbox.paypal.com/xoplatform/logger/api/logger. (Reason: CORS request did not succeed). Status code: (null).
&&
The GET method is not supported for this route. Supported methods: POST.
我正在使用 POST API 路由,在 Javascript 中我将 POST 声明为方法,出于某种原因它总是尝试使用 GET。
Checkout.blade.php(Javascript 部分):
createOrder: function(data, actions) {
return fetch('api/paypal/order/create/', {
method: 'post',
body: JSON.stringify({
"content" : {!! json_encode($cartContent, JSON_HEX_TAG) !!},
"value" : "{{ Cart::subtotal() }}",
"company" : document.getElementById('company').value,
"firstName" : document.getElementById('firstName').value,
"lastName" : document.getElementById('lastName').value,
"email" : document.getElementById('email').value,
"street" : document.getElementById('street').value,
"housenumber" : document.getElementById('housenumber').value,
"city" : document.getElementById('city').value,
"postcode" : document.getElementById('postcode').value,
})
}).then(function(res) {
return res.json();
}).then(function(orderData) {
return orderData.id;
});
},
PayPalController.php
<?php
namespace App\Http\Controllers;
use Cart;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Transaction;
use Illuminate\Http\Request;
use Srmklive\PayPal\Service\Paypal;
class PayPalController extends Controller
{
public function create(Request $request)
{
$data = json_decode($request->getContent(), true);
// Init PayPal
$provider = \PayPal::setProvider();
$provider->setApiCredentials(config('paypal'));
$token = $provider->getAccessToken();
$provider->setAccessToken($token);
$price = $data['value'];
$description = 'Einkauf bei Isipack24.de';
$order = $provider->createOrder([
"intent" => "CAPTURE",
"payer" => [
"name" => [
"given_name" => $data['firstName'],
"surname" => $data['lastName']
],
"address" => [
"address_line_1" => $data['street'] . " " . $data['housenumber'],
"admin_area_2" => $data['city'],
"admin_area_1" => 'NRW',
"postal_code" => $data['postcode'],
"country_code" => "DE"
],
"email_address" => $data['email']
],
"purchase_units" => [
[
"amount" => [
"currency_code" => "EUR",
"value" => $price
],
"description" => $description
]
]
]);
$newOrder = Order::create([
'Firma' => $data['company'],
'Vorname' => $data['firstName'],
'Nachname' => $data['lastName'],
'Email' => $data['email'],
'Straße' => $data['street'],
'Hausnummer' => $data['housenumber'],
'Postleitzahl' => $data['postcode'],
'Ort' => $data['city'],
]);
foreach($data['content'] as $item){
OrderItem::create([
'product_id' => $item['id'],
'order_id' => $newOrder->id,
'price' => $item['price'],
'quantity' => $item['qty'],
'total' => $item['price']*$item['qty'],
]);
}
Transaction::create([
'order_id' => $newOrder->id,
'status' => $order['status'],
'reference_number' => $order['id']
]);
return response()->json($order);
}
public function capture(Request $request)
{
$data = json_decode($request->getContent(), true);
$orderID = $data['orderID'];
// Init PayPal
$provider = \PayPal::setProvider();
$provider->setApiCredentials(config('paypal'));
$token = $provider->getAccessToken();
$provider->setAccessToken($token);
$result = $provider->capturePaymentOrder($orderID);
// Update Database
if ($result['status'] == "COMPLETED") {
Transaction::where('reference_number', $result['id'])
->update(['status' => 'COMPLETED', 'updated_at' => \Carbon\Carbon::now()]);
}
return response()->json($result);
}
}
api.php(路线):
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PayPalController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::prefix('paypal')->group(function () {
Route::post('/order/create', [PayPalController::class, 'create']);
Route::post('/order/capture', [PayPalController::class, 'capture']);
});
我尝试了很多,但我无法理解它。
忽略与此相关的一切。不重要。
如果您在另一条真正重要的路线上遇到错误,则需要显示另一条 non-logging 路线的实际情况。使用浏览器的网络选项卡。
如果您自己无法诊断此问题,也许您还应该 link 在某处实施。