Estoy intentando pasar un JSON con el contenido de un carrito de compras. (para esto estoy usando PayPal sandbox) y estoy pasando el carrito mediante query parameters (el carrito es el de gloudemans), el problema es al momento de usar el json_encode() en el controlador para hacer las inserciones en la tabla correspondiente. No se realiza la inserción de PurchaseUnit pero si elimino el ciclo e inserto datos estáticos en los campos de " 'id_product' y 'quantity'" inserta correctamente.
Estaría demasiado agradecido si alguien me puede ayudar.
Este es el controlador:
<?php
namespace App\Http\Controllers\Payments;
use App\Http\Controllers\AuthController;
use GuzzleHttp\Client;
use App\Http\Controllers\Controller;
use App\Models\PurchaseUnit;
use App\Models\SaleInvoice;
use App\Models\State;
use App\Models\User;
use Carbon\Carbon;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PayPalCardController extends Controller
{
private $client;
private $clientId;
private $secret;
public function __construct()
{
$this->client = new Client([
'base_uri' => 'https://api-m.sandbox.paypal.com'
]);
$this->clientId = env('PAYPAL_CLIENT_ID');
$this->secret = env('PAYPAL_SECRET');
}
private function getAccessToken()
{
$response = $this->client->request('POST', '/v1/oauth2/token', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'body' => 'grant_type=client_credentials',
'auth' => [
$this->clientId, $this->secret, 'basic'
]
]);
$data = json_decode($response->getBody(), true);
return $data['access_token'];
}
public function process(Request $request, $orderId)
{
$accessToken = $this->getAccessToken();
$response = $this->client->request('GET', '/v2/checkout/orders/'. $orderId, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "Bearer $accessToken"
]
]);
$user = AuthController::get();
$data = json_decode($response->getBody());
if ($data['status'] === 'APPROVED' && $user !== null)
{
$subtotal = $request->input('subtotal');
$taxes = $request->input('taxes');
$total = $request->input('total');
$cartContent = json_decode($request->input('cart_content'));
return
[
'success' => $this->createInvoicePayment($user, $subtotal, $taxes, $total, $cartContent),
'url' => route('cart.clear.after.purchase'),
];
}
return
[
'success' => false
];
}
private function createInvoicePayment(User $user, float $subtotal, float $taxes, float $total, mixed $cartContent): bool
{
try
{
DB::transaction(function() use($user, $subtotal, $taxes, $total, $cartContent) {
$sale = SaleInvoice::create([
'date_sale' => Carbon::now(),
'id_client' => $user->id,
'subtotal' => $subtotal,
'taxes' => $taxes,
'total' => $total,
'id_state' => State::where('name', 'Por entregar')->first()->id
]);
foreach ($cartContent as $product)
{
PurchaseUnit::create([
'id_invoice' => $sale->id,
'id_product' => $product->id,
'quantity' => $product->qty,
]);
}
});
return true;
}
catch(Exception)
{
return false;
}
}
}
Este es el script de JS junto con la vista:
{{-- Declaracion e importacion componente principal --}}
@extends('layouts.landing')
{{-- Declaracion complemtento etiqueta litle del Header --}}
@section('title','Carrito de Compras')
{{-- Declaracion clases css adicionales al contenedor body --}}
@section('body_class','flex flex-col min-h-screen')
{{-- Declaracion clases css adicionales al contenedor main --}}
@section('main_class','text-justify mx-2')
{{-- Declaracion contenido principal de la pagina web --}}
@section('content')
@include('components.custom-alert')
<div id="loader-overlay">
<div class="loader"></div>
</div>
<div>
@if ($cartContent)
<h1 class="text-center text-xl font-semibold py-2.5">Carrito de compras</h1>
<section class="grid grid-cols-3 gap-2">
<div class="card col-span-3 md:col-span-2">
<div class="card-body">
<table class="table table-striped">
<thead>
<th class="font-medium">Imágen</th>
<th class="font-medium">Nombre</th>
<th class="font-medium">Cantidad</th>
<th class="font-medium">Precio Unitario</th>
<th class="font-medium">Precio Total</th>
<th></th>
</thead>
<tbody>
@foreach ($cartContent as $cartProduct)
<tr class="align-middle">
<td>
<img src="{{ asset('storage/products/'. strtoupper($cartProduct->options->slug)."/images/". $cartProduct->options->image) }}" alt="{{ $cartProduct->name }}" width="50">
</td>
<td>
<a class="product-name" href="{{route('product.show',$cartProduct->options->slug)}}">{{$cartProduct->name}}</a>
</td>
<td>{{($cartProduct->qty)}}</td>
<td>${{number_format($cartProduct->price, 0, ',', '.')}}</td>
<td>${{number_format($cartProduct->qty * $cartProduct->price, 0, ',', '.')}}</td>
<td>
<form action="{{route("cart.remove")}}" method="post">
@csrf
<input type="hidden" name="rowId" value="{{$cartProduct->rowId}}">
<div class="button-tooltip" data-tooltip="Eliminar producto">
<button type="submit" class="btn bg-red-600 hover:bg-red-700 text-white w-full">
<i class="fa-solid fa-trash"></i>
</button>
</div>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
<div class="card col-span-3 md:col-span-1 py-2.5 px-3 flex flex-col gap-y-6">
<h2 class="text-center font-medium border-b pb-1.5">Resumen</h2>
<div class="summary">
<ul class="flex flex-col gap-y-0.5">
<div class="flex gap-0.5">
<li class="font-medium">Subtotal:</li>
<li>${{number_format(Cart::subtotal(), 0, ',', '.')}}</li> </li>
</div>
<div class="flex gap-0.5">
<li class="font-medium">Impuestos:</li>
<li>${{number_format(Cart::tax(), 0, ',', '.')}}</li> </li>
</div>
<div class="flex gap-0.5">
<li class="font-medium">Total:</li>
<li>${{number_format(Cart::total(), 0, ',', '.')}}</li> </li>
</div>
</ul>
</div>
<div class="flex flex-col gap-y-1.5">
<div class="w-full" id="sectionCard">
<div id="paypal-button-container"></div>
</div>
<a role="button" href="{{route("index", "#categories")}}" class="text-indigo-600 border-2 text-center border-indigo-600 px-3 py-1 rounded-xl hover:bg-indigo-600 hover:text-white transition-colors font-medium w-full">
Seguir comprando
</a>
<a role="button" href="{{route('cart.clear')}}"class="bg-red-600 text-white px-3 py-1 rounded-xl hover:bg-red-700 hover:text-white transition-colors font-medium w-full text-center">
Vaciar el carrito
</a>
</div>
</div>
</section>
@else
<section class="flex flex-col gap-y-3 text-center justify-center items-center">
<img src="{{asset("storage/others/cart-empty.png")}}" style="aspect-ratio: 1/1; object-fit: cover; width:25%;" alt="Shopping Cart Empty">
<div class="flex flex-col gap-0.5">
<h1 class="text-xl font-medium">El carrito de compras está vacío</h1>
<p>¡Parece que aún no has agregado ningún producto al carrito de compras!</p>
</div>
<a role="button" href="{{route("index", "#categories")}}" class="text-indigo-600 border-2 text-center border-indigo-600 px-3 py-1 rounded-xl hover:bg-indigo-600 hover:text-white transition-colors font-medium">
Seguir comprando
</a>
</section>
@endif
</div>
@endsection
{{-- Sobreescritura/Eliminacion del la declaracion del footer del layout de donde se extiende --}}
@section('footer','')
{{-- Importacion de scripts --}}
@section('scripts')
@routes
@vite([
'resources/js/navbar.js',
'resources/js/search.js',
'resources/js/fade-alert.js',
])
<script src="https://www.paypal.com/sdk/js?client-id={{ env('PAYPAL_CLIENT_ID') }}&components=buttons,funding-eligibility"></script>
<script>
const loaderOverlay = document.getElementById('loader-overlay');
const paypalButtonContainer = document.getElementById('paypal-button-container');
if (paypalButtonContainer) { // Verificar si el contenedor del botón de PayPal existe
paypal
.Buttons({
fundingSource: paypal.FUNDING.CARD,
createOrder: function (data, actions) {
return actions.order.create({
application_context: {
shipping_preference: "NO_SHIPPING",
},
payer: {
email_address: "{{ $user->email }}",
name: {
given_name: "{{ $user->names }}",
surname: "{{ $user->surnames }}",
},
phone: {
phone_number: {
national_number: "{{ $user->phone_number }}",
country_code: "57",
},
},
address: {
country_code: "CO",
},
},
purchase_units: [
{
amount: {
currency_code: "USD",
value: "{{ intval(Cart::total() / 3893) }}",
},
},
],
});
},
onApprove: function (data, actions) {
loaderOverlay.style.display = "block";
return fetch("/paypal/process/" + data.orderID + '?subtotal=' + {{ Cart::subtotal() }} + '&taxes=' + {{ Cart::tax() }} + '&total=' + {{ Cart::total() }} + '&cart_content=' + {!! json_encode($cartContent) !!} )
.then((res) => res.json())
.then(function (response) {
if (!response.success) {
const msg =
"Lo sentimos, tu transacción no pudo ser procesada, intenta más tarde.";
alert(msg);
} else {
// Vaciar el carrito y redireccionar al index
fetch("/cart/clear")
.then(() => {
location.href = response.url;
})
.catch((error) => {
console.error(
"Error al vaciar el carrito:",
error
);
// En caso de error, redireccionar al index de todos modos
location.href = response.url;
});
}
})
.finally(() => {
loaderOverlay.style.display = "none"; // Ocultar la pantalla de carga una vez que la solicitud haya finalizado
});
},
onError: function (err) {
alert(err);
},
})
.render("#paypal-button-container");
}