I am trying to implement Paypal Smart Buttons in my project and on my local machine its working great. Now i tried to deploy it on heroku for some more tests and when I try to make a payment I get following error
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.
I am using a POST API route and in the Javascript I declared POST as the method and for some reason it always tries to use GET.
Checkout.blade.php (Javascript section):
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 (routes):
<?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']);
});
I tried a ton but i cant get my head around it.
CodePudding user response:
Ignore everything to do with this. It is not important.
If you are getting an error for another route that actually matters, you need to show what's actually happening with that other, non-logging route. Use your browser's Network tab.
Perhaps you should also link to your implementation somewhere, if you aren't able to diagnose this issue yourself.