Buen día a todos,
Estoy haciendo una integración con PAYPAL V2, el flujo de trabajo ya me funciona todo. solo tengo un gran problema, cuando doy click al boton para que me redireccione a PAYPAl me rechaza y me muestra lo siguente, El flujo de trabajo esta bien, solo no logro que desde la pagina origen me abra la ventana de paypal.
Si abro la URL directamente con botón derecho en otra ventana, me direcciona sin problema y el flujo continua hasta terminar la compra, ya lo probé ONLINE y me arroja lo mismo.
Mi FRONT ES :
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Ensures optimal rendering on mobile devices. -->
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <!-- Optimal Internet Explorer compatibility -->
<?php include ('layouts/utilities.php'); ?>
</head>
<body>
<script
src="https://www.paypal.com/sdk/js?client-id=AUvmZW-GI99S8UBfjXqA5BoZbvkuqVi3_T6NlJdPU9LMTCfAmsHexg1oXjHlKTs5fgMxr1iwLtjpGRfz">
</script>
<script>
paypal.Buttons({
createOrder: function() {
return fetch('/mipagina/assets/paypal/CreateOrder.php', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
"intent": "CAPTURE",
"purchase_units": [{
"reference_id": "1",
"custom_id": "121",
"amount": {
"currency_code": "MXN",
"value": "100.00"
}
}]
})
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.id; // Use the key sent by your server's response, ex. 'id' or 'token'
});
},
onApprove: function(data) {
return fetch('/mipagina/assets/paypal/CreateOrder.php', {
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
}).then(function(res) {
return res.json();
}).then(function(details) {
alert('Transaction funds captured from ' + details.payer_given_name);
})
}
}).render('#paypal-button-container');
//This function displays Smart Payment Buttons on your web page.
</script>
</body>
Mi SW donde inicia el flujo de Paypal (siguiendo la documentación de ellos) es
introducir el código aquí
header("Origin: http://localhost:8888/miPagina/");
header('crossDomain: true');
require __DIR__ . '/vendor/autoload.php';
//1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
include 'PayPalClient.php';
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
class CreateOrder
{
// 2. Set up your server to receive a call from the client
/**
*This is the sample function to create an order. It uses the
*JSON body returned by buildRequestBody() to create an order.
*/
public static function createOrders($debug = false)
{
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if ($contentType === "application/json") {
//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));
$items = json_decode($content, true);
//If json_decode failed, the JSON is invalid.
if (!is_array($items)) {
return "algo salio mal";
} else if (!isset($items['purchase_units']) && !isset($items['intent'])) {
return "algo salio mal";;
}
}
$items = ($items['purchase_units']);
$request = new OrdersCreateRequest();
$clientStatus = new PayPalClient();
$request->prefer('return=representation');
$request->body = self::buildRequestBody($items);
/* echo json_encode($request);
die(); */
// 3. Call PayPal to set up a transaction
$client = PayPalClient::client();
try {
// Call API with your client and get a response for your call
$response = $client->execute($request);
// If call returns body in response, you can get the deserialized version from the result attribute of the response
// print_r($response);
session_start();
$_SESSION['paypal_order_id'] = strval($response->result->id);
foreach ($response->result->links as $link) {
if ($link->rel == "approve") {
header('Location:' . $link->href);
}
}
} catch (HttpException $ex) {
echo $ex->statusCode;
print_r($ex->getMessage());
}
if ($debug) {
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->status}\n";
print "Order ID: {$response->result->id}\n";
print "Intent: {$response->result->intent}\n";
print "Links:\n";
foreach ($response->result->links as $link) {
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// To print the whole response body, uncomment the following line
// echo json_encode($response->result, JSON_PRETTY_PRINT);
}
// 4. Return a successful response to the client.
//return $response;
}
/**
* Setting up the JSON request body for creating the order with minimum request body. The intent in the
* request body should be "AUTHORIZE" for authorize intent flow.
*
*/
private static function buildRequestBody($items)
{
return array(
'intent' => 'CAPTURE',
'application_context' => array(
'brand_name' => 'MI MARCA',
'locale' => 'en-MX',
'user_action' => 'PAY_NOW',
'return_url' => 'http://localhost:8888/mipagina/assets/paypal/return.php',
'cancel_url' => 'http://localhost:8888/mipagina/c.php',
),
'purchase_units' => $items,
);
}
}
/**
*This is the driver function that invokes the createOrder function to create
*a sample order.
*/
if (!count(debug_backtrace())) {
CreateOrder::createOrders(true);
}
header("Origin: http://localhost:8888/miPagina/");
el script cambia a127.0.0.1
, por lo tanto paypal no puede procesar, ya lo haz intentado con un dominioo en linea?